Merge branch 'master' of ssh://lausser,shinken@shinken.git.sourceforge.net/gitroot...
[shinken.git] / shinken / notification.py
blob6b4be55ab5ac1dd2e779def96b03dd8bda9ff760
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
6 # Hartmut Goebel, h.goebel@goebel-consult.de
8 #This file is part of Shinken.
10 #Shinken is free software: you can redistribute it and/or modify
11 #it under the terms of the GNU Affero General Public License as published by
12 #the Free Software Foundation, either version 3 of the License, or
13 #(at your option) any later version.
15 #Shinken is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #GNU Affero General Public License for more details.
20 #You should have received a copy of the GNU Affero General Public License
21 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
24 import time
26 from shinken.action import Action
27 from shinken.brok import Brok
28 from shinken.property import BoolProp, IntegerProp, StringProp
30 class Notification(Action):
31 #id = 0 #Is in fact in the Action class to be common with Checks and
32 #events handlers
34 properties = {
35 'type' : StringProp (default=''),
36 'notification_type': IntegerProp(default=0, fill_brok=['full_status']),
37 'start_time': StringProp (default=0, fill_brok=['full_status']),
38 'end_time': StringProp (default=0, fill_brok=['full_status']),
39 'contact_name': StringProp (default='', fill_brok=['full_status']),
40 'host_name': StringProp (default='', fill_brok=['full_status']),
41 'service_description': StringProp (default='', fill_brok=['full_status']),
42 'reason_type': StringProp (default=0, fill_brok=['full_status']),
43 'state': StringProp (default=0, fill_brok=['full_status']),
44 'output': StringProp (default='', fill_brok=['full_status']),
45 'ack_author': StringProp (default='', fill_brok=['full_status']),
46 'ack_data': StringProp (default='', fill_brok=['full_status']),
47 'escalated': BoolProp (default=False, fill_brok=['full_status']),
48 'contacts_notified': StringProp (default=0, fill_brok=['full_status']),
49 'env': StringProp (default={}),
50 'exit_status': IntegerProp(default=3),
51 'command_call': StringProp (default=None),
52 'contact': StringProp (default=None),
53 '_in_timeout': BoolProp (default=False),
54 'notif_nb': IntegerProp(default=0),
55 'status': StringProp (default='scheduled'),
56 't_to_go': IntegerProp(default=0),
57 'is_a': StringProp (default=''),
58 'command': StringProp (default=''),
59 'host_name': StringProp (default=''),
60 'sched_id': IntegerProp(default=0),
61 'timeout': IntegerProp(default=10),
62 'check_time': IntegerProp(default=0),
63 'module_type': StringProp (default='', fill_brok=['full_status']),
64 'worker': StringProp (default='none'),
65 'reactionner_tag': StringProp (default='None'),
68 macros = {
69 'NOTIFICATIONTYPE': 'type',
70 'NOTIFICATIONRECIPIENTS': 'recipients',
71 'NOTIFICATIONISESCALATED': 'escaladed',
72 'NOTIFICATIONAUTHOR': 'author',
73 'NOTIFICATIONAUTHORNAME': 'author_name',
74 'NOTIFICATIONAUTHORALIAS': 'author_alias',
75 'NOTIFICATIONCOMMENT': 'comment',
76 'HOSTNOTIFICATIONNUMBER': 'notif_nb',
77 'HOSTNOTIFICATIONID': 'id',
78 'SERVICENOTIFICATIONNUMBER': 'notif_nb',
79 'SERVICENOTIFICATIONID': 'id'
83 def __init__(self, type , status, command, command_call, ref, contact, t_to_go, \
84 contact_name='', host_name='', service_description='',
85 reason_type=1, state=0, ack_author='', ack_data='', \
86 escalated=False, contacts_notified=0, \
87 start_time=0, end_time=0, notification_type=0, id=None, \
88 notif_nb=1, timeout=10, env={}, module_type='fork', \
89 reactionner_tag='None'):
90 self.is_a = 'notification'
91 self.type = type
92 if id is None: #id != None is for copy call only
93 self.id = Action.id
94 Action.id += 1
97 self._in_timeout = False
98 self.timeout = timeout
99 self.status = status
100 self.exit_status = 3
101 self.command = command
102 self.command_call = command_call
103 self.output = None
104 self.ref = ref
105 self.env = env
106 self.module_type = module_type
107 #self.ref_type = ref_type
108 self.t_to_go = t_to_go
109 self.notif_nb = notif_nb
110 self.contact = contact
112 #For brok part
113 self.contact_name = contact_name
114 self.host_name = host_name
115 self.service_description = service_description
116 self.reason_type = reason_type
117 self.state = state
118 self.ack_author = ack_author
119 self.ack_data = ack_data
120 self.escalated = escalated
121 self.contacts_notified = contacts_notified
122 self.start_time = start_time
123 self.end_time = end_time
124 self.notification_type = notification_type
126 self.creation_time = time.time()
127 self.worker = 'none'
128 self.reactionner_tag = reactionner_tag
131 #return a copy of the check but just what is important for execution
132 #So we remove the ref and all
133 def copy_shell(self):
134 #We create a dummy check with nothing in it, jsut defaults values
135 return self.copy_shell__( Notification('', '', '', '', '', '', '', id=self.id) )
138 def is_launchable(self, t):
139 return t >= self.t_to_go
142 def is_administrative(self):
143 if self.type in ('PROBLEM', 'RECOVERY'):
144 return False
145 else:
146 return True
149 def __str__(self):
150 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)))
151 #return ''#str(self.__dict__)
154 def get_id(self):
155 return self.id
158 def get_return_from(self, n):
159 self.exit_status = n.exit_status
160 #self.output = c.output
161 #self.check_time = c.check_time
162 #self.execution_time = c.execution_time
165 #Fill data with info of item by looking at brok_type
166 #in props of properties or running_propterties
167 def fill_data_brok_from(self, data, brok_type):
168 cls = self.__class__
169 #Now config properties
170 for prop, entry in cls.properties.items():
171 if hasattr(prop, 'fill_brok'):
172 if brok_type in entry['fill_brok']:
173 data[prop] = getattr(self, prop)
176 #Get a brok with initial status
177 def get_initial_status_brok(self):
178 data = {'id': self.id}
180 self.fill_data_brok_from(data, 'full_status')
181 b = Brok('notification_raise', data)
182 return b
185 #Call by picle for dataify the coment
186 #because we DO NOT WANT REF in this pickleisation!
187 def __getstate__(self):
188 cls = self.__class__
189 # id is not in *_properties
190 res = {'id' : self.id}
191 for prop in cls.properties:
192 if hasattr(self, prop):
193 res[prop] = getattr(self, prop)
195 return res
198 # Inversed funtion of getstate
199 def __setstate__(self, state):
200 cls = self.__class__
201 self.id = state['id']
202 for prop in cls.properties:
203 if prop in state:
204 setattr(self, prop, state[prop])
205 # Hook for load of 0.4 notification : there were no
206 # creation time, must put one
207 if not hasattr(self, 'creation_time'):
208 self.creation_time = time.time()