2 # Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
6 # This file is part of Shinken.
8 # Shinken is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # Shinken is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with Shinken. If not, see <http://www.gnu.org/licenses/>.
22 from shinken
.action
import Action
26 __slots__
= ('id', 'is_a', 'type', '_in_timeout', 'status', 'exit_status',\
27 '_command', 'output', 'long_output', 'ref', 'ref_type', \
28 't_to_go', 'depend_on', 'depend_on_me', 'check_time', \
29 'execution_time', 'env')
31 properties
={'is_a' : {'required': False, 'default':'check'},
32 'type' : {'required': False, 'default': ''},
33 '_in_timeout' : {'required': False, 'default': False},
34 'status' : {'required': False, 'default':''},
35 'exit_status' : {'required': False, 'default':3},
36 'state' : {'required': False, 'default':0},
37 'output' : {'required': False, 'default':''},
38 'long_output' : {'required': False, 'default':''},
39 'ref' : {'required': False, 'default': -1},
40 #'ref_type' : {'required': False, 'default':''},
41 't_to_go' : {'required': False, 'default': 0},
42 'depend_on' : {'required': False, 'default': []},
43 'dep_check' : {'required': False, 'default': []},
44 'check_time' : {'required': False, 'default': 0},
45 'execution_time' : {'required': False, 'default': 0},
46 'perf_data' : {'required': False, 'default':''},
47 'poller_tag' : {'required': False, 'default': None},
48 'env' : {'required' : False, 'default' : {}},
49 'internal' : {'required': False, 'default':False},
52 #id = 0 #Is common to Actions
53 def __init__(self
, status
, command
, ref
, t_to_go
, dep_check
=None, id=None, timeout
=10, poller_tag
=None, env
={}):
56 if id == None: #id != None is for copy call only
59 self
._in
_timeout
= False
60 self
.timeout
= timeout
63 self
.command
= command
67 #self.ref_type = ref_type
68 self
.t_to_go
= t_to_go
71 self
.depend_on_me
= []
73 self
.depend_on_me
= [dep_check
]
75 self
.execution_time
= 0
77 self
.poller_tag
= poller_tag
79 # If it's a business rule, manage it as a special check
80 if ref
and ref
.got_business_rule
or command
.startswith('_internal'):
86 #return a copy of the check but just what is important for execution
87 #So we remove the ref and all
89 #We create a dummy check with nothing in it, jsut defaults values
90 new_c
= Check('', '', '', '', '', id=self
.id)
91 only_copy_prop
= ['id', 'status', 'command', 't_to_go', 'timeout', 'env']
92 for prop
in only_copy_prop
:
93 val
= getattr(self
, prop
)
94 setattr(new_c
, prop
, val
)
98 def get_return_from(self
, c
):
99 self
.exit_status
= c
.exit_status
100 self
.output
= c
.output
101 self
.long_output
= c
.long_output
102 self
.check_time
= c
.check_time
103 self
.execution_time
= c
.execution_time
104 self
.perf_data
= c
.perf_data
107 # def get_outputs(self, out):
108 # elts = out.split('\n')
110 # elts_line1 = elts[0].split('|')
111 # #First line before | is output
112 # self.output = elts_line1[0]
113 # #After | is perfdata
114 # if len(elts_line1) > 1:
115 # self.perf_data = elts_line1[1]
116 # #The others lines are long_output
118 # self.long_output = '\n'.join(elts[1:])
122 # if os.name == 'nt':
123 # self.execute_windows()
125 # self.execute_unix()
128 # def execute_windows(self):
129 # """call shell-command and either return its output or kill it
130 # if it doesn't normally exit within timeout seconds and return None"""
132 # self.status = 'launched'
133 # self.check_time = time.time()
134 # start = datetime.datetime.now()
136 # process = subprocess.Popen(self.command.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
137 # except WindowsError:
139 # self.status = 'timeout'
140 # self.execution_time = time.time() - self.check_time
142 # while process.poll() is None:
144 # now = datetime.datetime.now()
145 # if (now - start).seconds> timeout:
146 # TerminateProcess(int(process._handle), -1)
148 # self.status = 'timeout'
149 # self.execution_time = time.time() - self.check_time
151 # self.get_outputs(process.stdout.read())
152 # self.exit_status = process.returncode
153 # print "Output:", self.output, self.long_output, "exit status", self.exit_status
154 # self.status = 'done'
155 # self.execution_time = time.time() - self.check_time
158 # def execute_unix(self):
159 # #print "Launching command", self.command
160 # child = spawn ('/bin/sh -c "%s"' % self.command)
161 # self.status = 'launched'
162 # self.check_time = time.time()
165 # child.expect_exact(EOF, timeout=5)
166 # self.get_outputs(child.before)
167 # child.terminate(force=True)
168 # self.exit_status = child.exitstatus
169 # self.status = 'done'
172 # self.status = 'timeout'
173 # child.terminate(force=True)
174 # self.execution_time = time.time() - self.check_time
177 def is_launchable(self
, t
):
178 return t
> self
.t_to_go
181 def set_status(self
, status
):
185 def get_status(self
):
189 def get_output(self
):
194 return "Check %d status:%s command:%s ref:%s" % (self
.id, self
.status
, self
.command
, self
.ref
)