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/>.
23 #Unix and windows do not have the same import
25 # import subprocess, datetime, os, time, signal
27 # TerminateProcess = ctypes.windll.kernel32.TerminateProcess
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'),
39 '_in_timeout': StringProp(
43 'exit_status': StringProp(
47 'long_output': StringProp(
51 #'ref_type' : {'required': False, 'default':''},
52 't_to_go': StringProp(
54 'check_time': StringProp(
56 'execution_time': StringProp(
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'
71 self
.status
= 'scheduled'
72 if id == None: #id != None is for copy call only
75 self
._in
_timeout
= False
76 self
.timeout
= timeout
78 self
.command
= command
81 self
.t_to_go
= time
.time()
83 self
.execution_time
= 0
89 #return a copy of the check but just what is important for execution
90 #So we remove the ref and all
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')
108 elts_line1
= elts
[0].split('|')
109 #First line before | is output
110 self
.output
= elts_line1
[0]
112 if len(elts_line1
) > 1:
113 self
.perf_data
= elts_line1
[1]
114 #The others lines are long_output
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
):
127 def get_status(self
):
131 def get_output(self
):
136 return "Check %d status:%s command:%s" % (self
.id, self
.status
, self
.command
)
144 #Call by picle for dataify the coment
145 #because we DO NOT WANT REF in this pickleisation!
146 def __getstate__(self
):
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
)
157 # Inversed funtion of getstate
158 def __setstate__(self
, state
):
160 self
.id = state
['id']
161 for prop
in cls
.properties
:
163 setattr(self
, prop
, state
[prop
])