Add : child_dependencies/ parent_dependencies in livestatus module.
[shinken.git] / shinken / notification.py
blob1ce077ef3adb35b0dd4fed59b3bdd3ee78b0e54d
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/>.
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 brok import Brok
34 from shinken.property import UnusedProp, BoolProp, IntegerProp, FloatProp, CharProp, StringProp, ListProp
36 class Notification(Action):
37 #id = 0 #Is in fact in the Action class to be common with Checks and
38 #events handlers
40 properties={
41 'type' : StringProp(default=''),
42 'notification_type': IntegerProp(
43 default=0,
44 fill_brok=['full_status']),
45 'start_time': StringProp(
46 default=0,
47 fill_brok=['full_status']),
48 'end_time': StringProp(
49 default=0,
50 fill_brok=['full_status']),
51 'contact_name': StringProp(
52 default='',
53 fill_brok=['full_status']),
54 'host_name': StringProp(
55 default='',
56 fill_brok=['full_status']),
57 'service_description': StringProp(
58 default='',
59 fill_brok=['full_status']),
60 'reason_type': StringProp(
61 default=0,
62 fill_brok=['full_status']),
63 'state': StringProp(
64 default=0,
65 fill_brok=['full_status']),
66 'output': StringProp(
67 default='',
68 fill_brok=['full_status']),
69 'ack_author': StringProp(
70 default='',
71 fill_brok=['full_status']),
72 'ack_data': StringProp(
73 default='',
74 fill_brok=['full_status']),
75 'escalated': BoolProp(
76 default=False,
77 fill_brok=['full_status']),
78 'contacts_notified': StringProp(
79 default=0,
80 fill_brok=['full_status']),
81 'env': StringProp(
82 default={}),
83 'exit_status' : IntegerProp(default=3),
84 'command_call' : StringProp(default=None),
85 'contact' : StringProp(default=None),
86 '_in_timeout' : BoolProp(default=False),
87 'notif_nb' : IntegerProp(default=0),
88 'status' : StringProp(default='scheduled'),
89 't_to_go' : IntegerProp(default=0),
90 'is_a' : StringProp(default=''),
91 'command' : StringProp(default=''),
92 'host_name' : StringProp(default=''),
97 macros = {
98 'NOTIFICATIONTYPE' : 'type',
99 'NOTIFICATIONRECIPIENTS' : 'recipients',
100 'NOTIFICATIONISESCALATED' : 'escaladed',
101 'NOTIFICATIONAUTHOR' : 'author',
102 'NOTIFICATIONAUTHORNAME' : 'author_name',
103 'NOTIFICATIONAUTHORALIAS' : 'author_alias',
104 'NOTIFICATIONCOMMENT' : 'comment',
105 'HOSTNOTIFICATIONNUMBER' : 'notif_nb',
106 'HOSTNOTIFICATIONID' : 'id',
107 'SERVICENOTIFICATIONNUMBER' : 'notif_nb',
108 'SERVICENOTIFICATIONID' : 'id'
112 def __init__(self, type , status, command, command_call, ref, contact, t_to_go, \
113 contact_name='', host_name='', service_description='',
114 reason_type=1, state=0, ack_author='', ack_data='', \
115 escalated=False, contacts_notified=0, \
116 start_time=0, end_time=0, notification_type=0, id=None, \
117 notif_nb=1, timeout=10, env={}):
118 self.is_a = 'notification'
119 self.type = type
120 if id == None: #id != None is for copy call only
121 self.id = Action.id
122 Action.id += 1
125 self._in_timeout = False
126 self.timeout = timeout
127 self.status = status
128 self.exit_status = 3
129 self.command = command
130 self.command_call = command_call
131 self.output = None
132 self.ref = ref
133 self.env = env
134 #self.ref_type = ref_type
135 self.t_to_go = t_to_go
136 self.notif_nb = notif_nb
137 self.contact = contact
139 #For brok part
140 self.contact_name = contact_name
141 self.host_name = host_name
142 self.service_description = service_description
143 self.reason_type = reason_type
144 self.state = state
145 self.ack_author = ack_author
146 self.ack_data = ack_data
147 self.escalated = escalated
148 self.contacts_notified = contacts_notified
149 self.start_time = start_time
150 self.end_time = end_time
151 self.notification_type = notification_type
153 #DBG
154 self.creation_time = time.time()
157 #return a copy of the check but just what is important for execution
158 #So we remove the ref and all
159 def copy_shell(self):
160 #We create a dummy check with nothing in it, jsut defaults values
161 new_n = Notification('', '', '', '', '', '', '', id=self.id)
162 only_copy_prop = ['id', 'status', 'command', 't_to_go', 'timeout', 'env']
163 for prop in only_copy_prop:
164 val = getattr(self, prop)
165 setattr(new_n, prop, val)
166 return new_n
170 # def execute(self):
171 # print "Notification %s" % self.command
172 # child = spawn ('/bin/sh -c "%s"' % self.command)
173 # self.status = 'launched'
175 # try:
176 # child.expect_exact(EOF, timeout=5)
177 # self.output = child.before
178 # child.terminate(force=True)
179 # self.exit_status = child.exitstatus
180 # self.status = 'done'
181 # except TIMEOUT:
182 # print "On le kill"
183 # self.status = 'timeout'
184 # child.terminate(force=True)
187 def is_launchable(self, t):
188 return t > self.t_to_go
191 def set_status(self, status):
192 self.status = status
195 def get_status(self):
196 return self.status
199 def get_output(self):
200 return self.output
203 def is_administrative(self):
204 if self.type in ('PROBLEM', 'RECOVERY'):
205 return False
206 else:
207 return True
210 def __str__(self):
211 return "Notification %d status:%s command:%s ref:%s t_to_go:%s" % (self.id, self.status, self.command, self.ref, time.asctime(time.localtime(self.t_to_go)))
212 #return ''#str(self.__dict__)
215 def get_id(self):
216 return self.id
219 def get_return_from(self, n):
220 self.exit_status = n.exit_status
221 #self.output = c.output
222 #self.check_time = c.check_time
223 #self.execution_time = c.execution_time
226 #Fill data with info of item by looking at brok_type
227 #in props of properties or running_propterties
228 def fill_data_brok_from(self, data, brok_type):
229 cls = self.__class__
230 #Now config properties
231 for prop in cls.properties:
232 if hasattr(prop, 'fill_brok'):
233 if brok_type in cls.properties[prop]['fill_brok']:
234 data[prop] = getattr(self, prop)
237 #Get a brok with initial status
238 def get_initial_status_brok(self):
239 data = {'id' : self.id}
241 self.fill_data_brok_from(data, 'full_status')
242 b = Brok('notification_raise', data)
243 return b
246 #Call by picle for dataify the coment
247 #because we DO NOT WANT REF in this pickleisation!
248 def __getstate__(self):
249 # print "Asking a getstate for a notification on", self.ref.get_dbg_name()
250 cls = self.__class__
251 #id is not in *_properties
252 res = [self.id]
253 for prop in cls.properties:
254 res.append(getattr(self, prop))
255 #We reverse because we want to recreate
256 #By check at properties in the same order
257 res.reverse()
258 return res
261 #Inversed funtion of getstate
262 def __setstate__(self, state):
263 cls = self.__class__
264 self.id = state.pop()
265 for prop in cls.properties:
266 val = state.pop()
267 setattr(self, prop, val)