Fix : bad host group member id in NDO.
[shinken.git] / shinken / eventhandler.py
blobe16c87d842cd6a3ea47c6863f25c30424e3c18f6
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=''),
61 'sched_id' : IntegerProp(default=0),
62 'timeout' : IntegerProp(default=10),
63 'check_time' : IntegerProp(default=0),
64 'command' : StringProp(default=''),
67 #id = 0 #Is common to Actions
68 def __init__(self, command, id=None, timeout=10, env={}):
69 self.is_a = 'eventhandler'
70 self.type = ''
71 self.status = 'scheduled'
72 if id == None: #id != None is for copy call only
73 self.id = Action.id
74 Action.id += 1
75 self._in_timeout = False
76 self.timeout = timeout
77 self.exit_status = 3
78 self.command = command
79 self.output = ''
80 self.long_output = ''
81 self.t_to_go = time.time()
82 self.check_time = 0
83 self.execution_time = 0
84 self.perf_data = ''
85 self.env = {}
89 #return a copy of the check but just what is important for execution
90 #So we remove the ref and all
91 def copy_shell(self):
92 #We create a dummy check with nothing in it, jsut defaults values
93 return self.copy_shell__( EventHandler('', id=self.id) )
96 def get_return_from(self, e):
97 self.exit_status = e.exit_status
98 self.output = e.output
99 self.long_output = e.long_output
100 self.check_time = e.check_time
101 self.execution_time = e.execution_time
102 self.perf_data = e.perf_data
105 def get_outputs(self, out, max_plugins_output_length):
106 elts = out.split('\n')
107 #For perf data
108 elts_line1 = elts[0].split('|')
109 #First line before | is output
110 self.output = elts_line1[0]
111 #After | is perfdata
112 if len(elts_line1) > 1:
113 self.perf_data = elts_line1[1]
114 #The others lines are long_output
115 if len(elts) > 1:
116 self.long_output = '\n'.join(elts[1:])
119 def is_launchable(self, t):
120 return t > self.t_to_go
123 def set_status(self, status):
124 self.status = status
127 def get_status(self):
128 return self.status
131 def get_output(self):
132 return self.output
135 def __str__(self):
136 return "Check %d status:%s command:%s" % (self.id, self.status, self.command)
139 def get_id(self):
140 return self.id
144 #Call by picle for dataify the coment
145 #because we DO NOT WANT REF in this pickleisation!
146 def __getstate__(self):
147 cls = self.__class__
148 # id is not in *_properties
149 res = {'id' : self.id}
150 for prop in cls.properties:
151 if hasattr(self, prop):
152 res[prop] = getattr(self, prop)
154 return res
157 # Inversed funtion of getstate
158 def __setstate__(self, state):
159 cls = self.__class__
160 self.id = state['id']
161 for prop in cls.properties:
162 if prop in state:
163 setattr(self, prop, state[prop])