Fix : get back LiveStatus as default.
[shinken.git] / shinken / eventhandler.py
blobfc84a3f81f14c1196aa4c0f951541f2a805de227
1 #!/usr/bin/env python
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
5 # Gregory Starck, g.starck@gmail.com
6 # Hartmut Goebel, h.goebel@goebel-consult.de
8 #This file is part of Shinken.
10 #Shinken is free software: you can redistribute it and/or modify
11 #it under the terms of the GNU Affero General Public License as published by
12 #the Free Software Foundation, either version 3 of the License, or
13 #(at your option) any later version.
15 #Shinken is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #GNU Affero General Public License for more details.
20 #You should have received a copy of the GNU Affero General Public License
21 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
23 import time
25 from action import Action
26 from shinken.property import IntegerProp, StringProp
28 class EventHandler(Action):
29 properties = {
30 'is_a': StringProp (default='eventhandler'),
31 'type': StringProp (default=''),
32 '_in_timeout': StringProp (default=False),
33 'status': StringProp (default=''),
34 'exit_status': StringProp (default=3),
35 'output': StringProp (default=''),
36 'long_output': StringProp (default=''),
37 't_to_go': StringProp (default=0),
38 'check_time': StringProp (default=0),
39 'execution_time': StringProp (default=0),
40 'env': StringProp (default={}),
41 'perf_data': StringProp (default=''),
42 'sched_id': IntegerProp(default=0),
43 'timeout': IntegerProp(default=10),
44 'check_time': IntegerProp(default=0),
45 'command': StringProp (default=''),
46 'module_type': StringProp (default=''),
49 #id = 0 #Is common to Actions
50 def __init__(self, command, id=None, timeout=10, env={}, module_type='fork'):
51 self.is_a = 'eventhandler'
52 self.type = ''
53 self.status = 'scheduled'
54 if id is None: #id != None is for copy call only
55 self.id = Action.id
56 Action.id += 1
57 self._in_timeout = False
58 self.timeout = timeout
59 self.exit_status = 3
60 self.command = command
61 self.output = ''
62 self.long_output = ''
63 self.t_to_go = time.time()
64 self.check_time = 0
65 self.execution_time = 0
66 self.perf_data = ''
67 self.env = {}
68 self.module_type = module_type
71 #return a copy of the check but just what is important for execution
72 #So we remove the ref and all
73 def copy_shell(self):
74 #We create a dummy check with nothing in it, jsut defaults values
75 return self.copy_shell__( EventHandler('', id=self.id) )
78 def get_return_from(self, e):
79 self.exit_status = e.exit_status
80 self.output = e.output
81 self.long_output = e.long_output
82 self.check_time = e.check_time
83 self.execution_time = e.execution_time
84 self.perf_data = e.perf_data
87 def get_outputs(self, out, max_plugins_output_length):
88 elts = out.split('\n')
89 #For perf data
90 elts_line1 = elts[0].split('|')
91 #First line before | is output
92 self.output = elts_line1[0]
93 #After | is perfdata
94 if len(elts_line1) > 1:
95 self.perf_data = elts_line1[1]
96 #The others lines are long_output
97 if len(elts) > 1:
98 self.long_output = '\n'.join(elts[1:])
101 def is_launchable(self, t):
102 return t >= self.t_to_go
105 def __str__(self):
106 return "Check %d status:%s command:%s" % (self.id, self.status, self.command)
109 def get_id(self):
110 return self.id
114 #Call by picle for dataify the coment
115 #because we DO NOT WANT REF in this pickleisation!
116 def __getstate__(self):
117 cls = self.__class__
118 # id is not in *_properties
119 res = {'id' : self.id}
120 for prop in cls.properties:
121 if hasattr(self, prop):
122 res[prop] = getattr(self, prop)
124 return res
127 # Inversed funtion of getstate
128 def __setstate__(self, state):
129 cls = self.__class__
130 self.id = state['id']
131 for prop in cls.properties:
132 if prop in state:
133 setattr(self, prop, state[prop])