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/>.
24 #Unix and windows do not have the same import
26 # import subprocess, datetime, os, time, signal
28 # TerminateProcess = ctypes.windll.kernel32.TerminateProcess
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'),
40 '_in_timeout': StringProp(
44 'exit_status': StringProp(
48 'long_output': StringProp(
52 #'ref_type' : {'required': False, 'default':''},
53 't_to_go': StringProp(
55 'check_time': StringProp(
57 'execution_time': StringProp(
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'
72 self
.status
= 'scheduled'
73 if id == None: #id != None is for copy call only
76 self
._in
_timeout
= False
77 self
.timeout
= timeout
79 self
.command
= command
82 self
.t_to_go
= time
.time()
84 self
.execution_time
= 0
90 #return a copy of the check but just what is important for execution
91 #So we remove the ref and all
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')
109 elts_line1
= elts
[0].split('|')
110 #First line before | is output
111 self
.output
= elts_line1
[0]
113 if len(elts_line1
) > 1:
114 self
.perf_data
= elts_line1
[1]
115 #The others lines are long_output
117 self
.long_output
= '\n'.join(elts
[1:])
120 def is_launchable(self
, t
):
121 return t
>= self
.t_to_go
125 return "Check %d status:%s command:%s" % (self
.id, self
.status
, self
.command
)
133 #Call by picle for dataify the coment
134 #because we DO NOT WANT REF in this pickleisation!
135 def __getstate__(self
):
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
)
146 # Inversed funtion of getstate
147 def __setstate__(self
, state
):
149 self
.id = state
['id']
150 for prop
in cls
.properties
:
152 setattr(self
, prop
, state
[prop
])