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/>.
23 #Unix and windows do not have the same import
25 # import subprocess, datetime, os, time, signal
27 # TerminateProcess = ctypes.windll.kernel32.TerminateProcess
29 # from pexpect import *
31 from action
import Action
32 from shinken
.property import UnusedProp
, BoolProp
, IntegerProp
, FloatProp
, CharProp
, StringProp
, ListProp
34 class EventHandler(Action
):
35 properties
={'is_a': StringProp(
36 default
='eventhandler'),
39 '_in_timeout': StringProp(
43 'exit_status': StringProp(
47 'long_output': StringProp(
51 #'ref_type' : {'required': False, 'default':''},
52 't_to_go': StringProp(
54 'check_time': StringProp(
56 'execution_time': StringProp(
60 'perf_data' : StringProp(default
=''),
63 #id = 0 #Is common to Actions
64 def __init__(self
, command
, id=None, timeout
=10, env
={}):
65 self
.is_a
= 'eventhandler'
67 self
.status
= 'scheduled'
68 if id == None: #id != None is for copy call only
71 self
._in
_timeout
= False
72 self
.timeout
= timeout
74 self
.command
= command
77 self
.t_to_go
= time
.time()
79 self
.execution_time
= 0
85 #return a copy of the check but just what is important for execution
86 #So we remove the ref and all
88 #We create a dummy check with nothing in it, jsut defaults values
89 new_n
= EventHandler('', id=self
.id)
90 only_copy_prop
= ['id', 'status', 'command', 't_to_go', 'timeout', 'env']
91 for prop
in only_copy_prop
:
92 val
= getattr(self
, prop
)
93 setattr(new_n
, prop
, val
)
97 def get_return_from(self
, e
):
98 self
.exit_status
= e
.exit_status
99 self
.output
= e
.output
100 self
.long_output
= e
.long_output
101 self
.check_time
= e
.check_time
102 self
.execution_time
= e
.execution_time
103 self
.perf_data
= e
.perf_data
106 def get_outputs(self
, out
, max_plugins_output_length
):
107 elts
= out
.split('\n')
109 elts_line1
= elts
[0].split('|')
110 #First line before | is output
111 self
.output
= elts_line1
[0]
113 if len(elts_line1
) > 1:
114 self
.perf_data
= elts_line1
[1]
115 #The others lines are long_output
117 self
.long_output
= '\n'.join(elts
[1:])
121 # print "Launching EVENT HANDLER command", self.command
122 # child = spawn ('/bin/sh -c "%s"' % self.command)
123 # self.status = 'launched'
124 # self.check_time = time.time()
127 # child.expect_exact(EOF, timeout=5)
128 # self.get_outputs(child.before)
129 # child.terminate(force=True)
130 # self.exit_status = child.exitstatus
131 # self.status = 'done'
134 # self.status = 'timeout'
135 # child.terminate(force=True)
136 # self.execution_time = time.time() - self.check_time
139 def is_launchable(self
, t
):
140 return t
> self
.t_to_go
143 def set_status(self
, status
):
147 def get_status(self
):
151 def get_output(self
):
156 return "Check %d status:%s command:%s" % (self
.id, self
.status
, self
.command
)
163 #Call by picle for dataify the coment
164 #because we DO NOT WANT REF in this pickleisation!
165 def __getstate__(self
):
166 print "Asking a getstate for a even handler on", self
.ref
.get_dbg_name()
168 #id is not in *_properties
170 for prop
in cls
.properties
:
171 res
.append(getattr(self
, prop
))
172 #We reverse because we want to recreate
173 #By check at properties in the same order
178 #Inversed funtion of getstate
179 def __setstate__(self
, state
):
181 self
.id = state
.pop()
182 for prop
in cls
.properties
:
184 setattr(self
, prop
, val
)