Add : child_dependencies/ parent_dependencies in livestatus module.
[shinken.git] / shinken / eventhandler.py
blobc66dba113563306b0d2f7d61661dd6aaadc072cc
1 #!/usr/bin/env python
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/>.
21 import time
23 #Unix and windows do not have the same import
24 #if os.name == 'nt':
25 # import subprocess, datetime, os, time, signal
26 # import ctypes
27 # TerminateProcess = ctypes.windll.kernel32.TerminateProcess
28 #else:
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'),
37 'type': StringProp(
38 default=''),
39 '_in_timeout': StringProp(
40 default=False),
41 'status': StringProp(
42 default=''),
43 'exit_status': StringProp(
44 default=3),
45 'output': StringProp(
46 default=''),
47 'long_output': StringProp(
48 default=''),
49 # 'ref': StringProp(
50 # default=-1),
51 #'ref_type' : {'required': False, 'default':''},
52 't_to_go': StringProp(
53 default=0),
54 'check_time': StringProp(
55 default=0),
56 'execution_time': StringProp(
57 default=0),
58 'env': StringProp(
59 default={}),
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'
66 self.type = ''
67 self.status = 'scheduled'
68 if id == None: #id != None is for copy call only
69 self.id = Action.id
70 Action.id += 1
71 self._in_timeout = False
72 self.timeout = timeout
73 self.exit_status = 3
74 self.command = command
75 self.output = ''
76 self.long_output = ''
77 self.t_to_go = time.time()
78 self.check_time = 0
79 self.execution_time = 0
80 self.perf_data = ''
81 self.env = {}
85 #return a copy of the check but just what is important for execution
86 #So we remove the ref and all
87 def copy_shell(self):
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)
94 return new_n
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')
108 #For perf data
109 elts_line1 = elts[0].split('|')
110 #First line before | is output
111 self.output = elts_line1[0]
112 #After | is perfdata
113 if len(elts_line1) > 1:
114 self.perf_data = elts_line1[1]
115 #The others lines are long_output
116 if len(elts) > 1:
117 self.long_output = '\n'.join(elts[1:])
120 # def execute(self):
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()
126 # try:
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'
132 # except TIMEOUT:
133 # print "On le kill"
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):
144 self.status = status
147 def get_status(self):
148 return self.status
151 def get_output(self):
152 return self.output
155 def __str__(self):
156 return "Check %d status:%s command:%s" % (self.id, self.status, self.command)
159 def get_id(self):
160 return self.id
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()
167 cls = self.__class__
168 #id is not in *_properties
169 res = [self.id]
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
174 res.reverse()
175 return res
178 #Inversed funtion of getstate
179 def __setstate__(self, state):
180 cls = self.__class__
181 self.id = state.pop()
182 for prop in cls.properties:
183 val = state.pop()
184 setattr(self, prop, val)