*Change the jenkins/hudson test scripts
[shinken.git] / shinken / eventhandler.py
blob2b49e51f0fbaba79bbff1115c4ff4c6834d47d73
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
7 #This file is part of Shinken.
9 #Shinken is free software: you can redistribute it and/or modify
10 #it under the terms of the GNU Affero General Public License as published by
11 #the Free Software Foundation, either version 3 of the License, or
12 #(at your option) any later version.
14 #Shinken is distributed in the hope that it will be useful,
15 #but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 #GNU Affero General Public License for more details.
19 #You should have received a copy of the GNU Affero General Public License
20 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
22 import time
24 #Unix and windows do not have the same import
25 #if os.name == 'nt':
26 # import subprocess, datetime, os, time, signal
27 # import ctypes
28 # TerminateProcess = ctypes.windll.kernel32.TerminateProcess
29 #else:
30 # from pexpect import *
32 from action import Action
33 from shinken.property import UnusedProp, BoolProp, IntegerProp, FloatProp, CharProp, StringProp, ListProp
35 class EventHandler(Action):
36 properties={'is_a': StringProp(
37 default='eventhandler'),
38 'type': StringProp(
39 default=''),
40 '_in_timeout': StringProp(
41 default=False),
42 'status': StringProp(
43 default=''),
44 'exit_status': StringProp(
45 default=3),
46 'output': StringProp(
47 default=''),
48 'long_output': StringProp(
49 default=''),
50 # 'ref': StringProp(
51 # default=-1),
52 #'ref_type' : {'required': False, 'default':''},
53 't_to_go': StringProp(
54 default=0),
55 'check_time': StringProp(
56 default=0),
57 'execution_time': StringProp(
58 default=0),
59 'env': StringProp(
60 default={}),
61 'perf_data' : StringProp(default=''),
62 'sched_id' : IntegerProp(default=0),
63 'timeout' : IntegerProp(default=10),
64 'check_time' : IntegerProp(default=0),
65 'command' : StringProp(default=''),
68 #id = 0 #Is common to Actions
69 def __init__(self, command, id=None, timeout=10, env={}):
70 self.is_a = 'eventhandler'
71 self.type = ''
72 self.status = 'scheduled'
73 if id == None: #id != None is for copy call only
74 self.id = Action.id
75 Action.id += 1
76 self._in_timeout = False
77 self.timeout = timeout
78 self.exit_status = 3
79 self.command = command
80 self.output = ''
81 self.long_output = ''
82 self.t_to_go = time.time()
83 self.check_time = 0
84 self.execution_time = 0
85 self.perf_data = ''
86 self.env = {}
90 #return a copy of the check but just what is important for execution
91 #So we remove the ref and all
92 def copy_shell(self):
93 #We create a dummy check with nothing in it, jsut defaults values
94 return self.copy_shell__( EventHandler('', id=self.id) )
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 is_launchable(self, t):
121 return t >= self.t_to_go
124 def __str__(self):
125 return "Check %d status:%s command:%s" % (self.id, self.status, self.command)
128 def get_id(self):
129 return self.id
133 #Call by picle for dataify the coment
134 #because we DO NOT WANT REF in this pickleisation!
135 def __getstate__(self):
136 cls = self.__class__
137 # id is not in *_properties
138 res = {'id' : self.id}
139 for prop in cls.properties:
140 if hasattr(self, prop):
141 res[prop] = getattr(self, prop)
143 return res
146 # Inversed funtion of getstate
147 def __setstate__(self, state):
148 cls = self.__class__
149 self.id = state['id']
150 for prop in cls.properties:
151 if prop in state:
152 setattr(self, prop, state[prop])