Merge branch 'master' of ssh://lausser,shinken@shinken.git.sourceforge.net/gitroot...
[shinken.git] / shinken / eventhandler.py
blobc9fbd175335b34934a9e5c82a831c893c61d9b5f
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=''),
47 'worker': StringProp (default='none'),
48 'reactionner_tag': StringProp (default='None'),
51 #id = 0 #Is common to Actions
52 def __init__(self, command, id=None, timeout=10, env={}, \
53 module_type='fork', reactionner_tag='None'):
54 self.is_a = 'eventhandler'
55 self.type = ''
56 self.status = 'scheduled'
57 if id is None: #id != None is for copy call only
58 self.id = Action.id
59 Action.id += 1
60 self._in_timeout = False
61 self.timeout = timeout
62 self.exit_status = 3
63 self.command = command
64 self.output = ''
65 self.long_output = ''
66 self.t_to_go = time.time()
67 self.check_time = 0
68 self.execution_time = 0
69 self.perf_data = ''
70 self.env = {}
71 self.module_type = module_type
72 self.worker = 'none'
73 self.reactionner_tag = reactionner_tag
76 #return a copy of the check but just what is important for execution
77 #So we remove the ref and all
78 def copy_shell(self):
79 #We create a dummy check with nothing in it, jsut defaults values
80 return self.copy_shell__( EventHandler('', id=self.id) )
83 def get_return_from(self, e):
84 self.exit_status = e.exit_status
85 self.output = e.output
86 self.long_output = e.long_output
87 self.check_time = e.check_time
88 self.execution_time = e.execution_time
89 self.perf_data = e.perf_data
92 def get_outputs(self, out, max_plugins_output_length):
93 elts = out.split('\n')
94 #For perf data
95 elts_line1 = elts[0].split('|')
96 #First line before | is output
97 self.output = elts_line1[0]
98 #After | is perfdata
99 if len(elts_line1) > 1:
100 self.perf_data = elts_line1[1]
101 #The others lines are long_output
102 if len(elts) > 1:
103 self.long_output = '\n'.join(elts[1:])
106 def is_launchable(self, t):
107 return t >= self.t_to_go
110 def __str__(self):
111 return "Check %d status:%s command:%s" % (self.id, self.status, self.command)
114 def get_id(self):
115 return self.id
119 #Call by picle for dataify the coment
120 #because we DO NOT WANT REF in this pickleisation!
121 def __getstate__(self):
122 cls = self.__class__
123 # id is not in *_properties
124 res = {'id' : self.id}
125 for prop in cls.properties:
126 if hasattr(self, prop):
127 res[prop] = getattr(self, prop)
129 return res
132 # Inversed funtion of getstate
133 def __setstate__(self, state):
134 cls = self.__class__
135 self.id = state['id']
136 for prop in cls.properties:
137 if prop in state:
138 setattr(self, prop, state[prop])