*Code cleanup in livestatus.
[shinken.git] / shinken / notification.py
blobc0f4cc8b8b69be51ffa73046fe3e487726bb0d07
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
35 class Notification(Action):
36 #id = 0 #Is in fact in the Action class to be common with Checks and
37 #events handlers
39 properties={
40 'notification_type' : {'required' : False, 'default' : 0, 'fill_brok' : ['full_status']},
41 'start_time' : {'required' : False, 'default' : 0, 'fill_brok' : ['full_status']},
42 'end_time' : {'required' : False, 'default' : 0, 'fill_brok' : ['full_status']},
43 'contact_name' : {'required' : False, 'default' : '', 'fill_brok' : ['full_status']},
44 'host_name' : {'required' : False, 'default' : '', 'fill_brok' : ['full_status']},
45 'service_description' : {'required' : False, 'default' : '', 'fill_brok' : ['full_status']},
46 'reason_type' : {'required' : False, 'default' : 0, 'fill_brok' : ['full_status']},
47 'state' : {'required' : False, 'default' : 0, 'fill_brok' : ['full_status']},
48 'output' : {'required' : False, 'default' : '', 'fill_brok' : ['full_status']},
49 'ack_author' : {'required' : False, 'default' : '', 'fill_brok' : ['full_status']},
50 'ack_data' : {'required' : False, 'default' : '', 'fill_brok' : ['full_status']},
51 'escalated' : {'required' : False, 'default' : False, 'fill_brok' : ['full_status']},
52 'contacts_notified' : {'required': False, 'default':0, 'fill_brok' : ['full_status']},
53 'env' : {'required' : False, 'default' : {}},
56 macros = {
57 'NOTIFICATIONTYPE' : 'type',
58 'NOTIFICATIONRECIPIENTS' : 'recipients',
59 'NOTIFICATIONISESCALATED' : 'escaladed',
60 'NOTIFICATIONAUTHOR' : 'author',
61 'NOTIFICATIONAUTHORNAME' : 'author_name',
62 'NOTIFICATIONAUTHORALIAS' : 'author_alias',
63 'NOTIFICATIONCOMMENT' : 'comment',
64 'HOSTNOTIFICATIONNUMBER' : 'notif_nb',
65 'HOSTNOTIFICATIONID' : 'id',
66 'SERVICENOTIFICATIONNUMBER' : 'notif_nb',
67 'SERVICENOTIFICATIONID' : 'id'
71 def __init__(self, type , status, command, command_call, ref, contact, t_to_go, \
72 contact_name='', host_name='', service_description='',
73 reason_type=1, state=0, ack_author='', ack_data='', \
74 escalated=False, contacts_notified=0, \
75 start_time=0, end_time=0, notification_type=0, id=None, \
76 notif_nb=1, timeout=10, env={}):
77 self.is_a = 'notification'
78 self.type = type
79 if id == None: #id != None is for copy call only
80 self.id = Action.id
81 Action.id += 1
84 self._in_timeout = False
85 self.timeout = timeout
86 self.status = status
87 self.exit_status = 3
88 self.command = command
89 self.command_call = command_call
90 self.output = None
91 self.ref = ref
92 self.env = env
93 #self.ref_type = ref_type
94 self.t_to_go = t_to_go
95 self.notif_nb = notif_nb
96 self.contact = contact
98 #For brok part
99 self.contact_name = contact_name
100 self.host_name = host_name
101 self.service_description = service_description
102 self.reason_type = reason_type
103 self.state = state
104 self.ack_author = ack_author
105 self.ack_data = ack_data
106 self.escalated = escalated
107 self.contacts_notified = contacts_notified
108 self.start_time = start_time
109 self.end_time = end_time
110 self.notification_type = notification_type
113 #return a copy of the check but just what is important for execution
114 #So we remove the ref and all
115 def copy_shell(self):
116 #We create a dummy check with nothing in it, jsut defaults values
117 new_n = Notification('', '', '', '', '', '', '', id=self.id)
118 only_copy_prop = ['id', 'status', 'command', 't_to_go', 'timeout', 'env']
119 for prop in only_copy_prop:
120 val = getattr(self, prop)
121 setattr(new_n, prop, val)
122 return new_n
126 # def execute(self):
127 # print "Notification %s" % self.command
128 # child = spawn ('/bin/sh -c "%s"' % self.command)
129 # self.status = 'launched'
131 # try:
132 # child.expect_exact(EOF, timeout=5)
133 # self.output = child.before
134 # child.terminate(force=True)
135 # self.exit_status = child.exitstatus
136 # self.status = 'done'
137 # except TIMEOUT:
138 # print "On le kill"
139 # self.status = 'timeout'
140 # child.terminate(force=True)
143 def get_return_from(self, c):
144 self.exit_status = c.exit_status
145 self.output = c.output
146 #self.long_output = c.long_output
147 #self.check_time = c.check_time
148 #self.execution_time = c.execution_time
149 #self.perf_data = c.perf_data
153 def is_launchable(self, t):
154 return t > self.t_to_go
157 def set_status(self, status):
158 self.status = status
161 def get_status(self):
162 return self.status
165 def get_output(self):
166 return self.output
169 def is_administrative(self):
170 if self.type == 'PROBLEM' or self.type == 'RECOVERY':
171 return False
172 else:
173 return True
176 def __str__(self):
177 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)))
178 #return ''#str(self.__dict__)
181 def get_id(self):
182 return self.id
185 def get_return_from(self, n):
186 self.exit_status = n.exit_status
187 #self.output = c.output
188 #self.check_time = c.check_time
189 #self.execution_time = c.execution_time
192 #Fill data with info of item by looking at brok_type
193 #in props of properties or running_propterties
194 def fill_data_brok_from(self, data, brok_type):
195 cls = self.__class__
196 #Now config properties
197 for prop in cls.properties:
198 if hasattr(prop, 'fill_brok'):
199 if brok_type in cls.properties[prop]['fill_brok']:
200 data[prop] = getattr(self, prop)
203 #Get a brok with initial status
204 def get_initial_status_brok(self):
205 data = {'id' : self.id}
207 self.fill_data_brok_from(data, 'full_status')
208 b = Brok('notification_raise', data)
209 return b