Add : set 'None' as the default poller_tag value, so a poller can get untaggued AND...
[shinken.git] / shinken / notification.py
blobf48bc4cdfde55d10fe5fb001922224ff3aa1458a
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'),
67 macros = {
68 'NOTIFICATIONTYPE': 'type',
69 'NOTIFICATIONRECIPIENTS': 'recipients',
70 'NOTIFICATIONISESCALATED': 'escaladed',
71 'NOTIFICATIONAUTHOR': 'author',
72 'NOTIFICATIONAUTHORNAME': 'author_name',
73 'NOTIFICATIONAUTHORALIAS': 'author_alias',
74 'NOTIFICATIONCOMMENT': 'comment',
75 'HOSTNOTIFICATIONNUMBER': 'notif_nb',
76 'HOSTNOTIFICATIONID': 'id',
77 'SERVICENOTIFICATIONNUMBER': 'notif_nb',
78 'SERVICENOTIFICATIONID': 'id'
82 def __init__(self, type , status, command, command_call, ref, contact, t_to_go, \
83 contact_name='', host_name='', service_description='',
84 reason_type=1, state=0, ack_author='', ack_data='', \
85 escalated=False, contacts_notified=0, \
86 start_time=0, end_time=0, notification_type=0, id=None, \
87 notif_nb=1, timeout=10, env={}, module_type='fork'):
88 self.is_a = 'notification'
89 self.type = type
90 if id is None: #id != None is for copy call only
91 self.id = Action.id
92 Action.id += 1
95 self._in_timeout = False
96 self.timeout = timeout
97 self.status = status
98 self.exit_status = 3
99 self.command = command
100 self.command_call = command_call
101 self.output = None
102 self.ref = ref
103 self.env = env
104 self.module_type = module_type
105 #self.ref_type = ref_type
106 self.t_to_go = t_to_go
107 self.notif_nb = notif_nb
108 self.contact = contact
110 #For brok part
111 self.contact_name = contact_name
112 self.host_name = host_name
113 self.service_description = service_description
114 self.reason_type = reason_type
115 self.state = state
116 self.ack_author = ack_author
117 self.ack_data = ack_data
118 self.escalated = escalated
119 self.contacts_notified = contacts_notified
120 self.start_time = start_time
121 self.end_time = end_time
122 self.notification_type = notification_type
124 self.creation_time = time.time()
125 self.worker = 'none'
128 #return a copy of the check but just what is important for execution
129 #So we remove the ref and all
130 def copy_shell(self):
131 #We create a dummy check with nothing in it, jsut defaults values
132 return self.copy_shell__( Notification('', '', '', '', '', '', '', id=self.id) )
135 def is_launchable(self, t):
136 return t >= self.t_to_go
139 def is_administrative(self):
140 if self.type in ('PROBLEM', 'RECOVERY'):
141 return False
142 else:
143 return True
146 def __str__(self):
147 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)))
148 #return ''#str(self.__dict__)
151 def get_id(self):
152 return self.id
155 def get_return_from(self, n):
156 self.exit_status = n.exit_status
157 #self.output = c.output
158 #self.check_time = c.check_time
159 #self.execution_time = c.execution_time
162 #Fill data with info of item by looking at brok_type
163 #in props of properties or running_propterties
164 def fill_data_brok_from(self, data, brok_type):
165 cls = self.__class__
166 #Now config properties
167 for prop, entry in cls.properties.items():
168 if hasattr(prop, 'fill_brok'):
169 if brok_type in entry['fill_brok']:
170 data[prop] = getattr(self, prop)
173 #Get a brok with initial status
174 def get_initial_status_brok(self):
175 data = {'id': self.id}
177 self.fill_data_brok_from(data, 'full_status')
178 b = Brok('notification_raise', data)
179 return b
182 #Call by picle for dataify the coment
183 #because we DO NOT WANT REF in this pickleisation!
184 def __getstate__(self):
185 cls = self.__class__
186 # id is not in *_properties
187 res = {'id' : self.id}
188 for prop in cls.properties:
189 if hasattr(self, prop):
190 res[prop] = getattr(self, prop)
192 return res
195 # Inversed funtion of getstate
196 def __setstate__(self, state):
197 cls = self.__class__
198 self.id = state['id']
199 for prop in cls.properties:
200 if prop in state:
201 setattr(self, prop, state[prop])
202 # Hook for load of 0.4 notification : there were no
203 # creation time, must put one
204 if not hasattr(self, 'creation_time'):
205 self.creation_time = time.time()