Merge branch 'master' of ssh://naparuba@shinken.git.sourceforge.net/gitroot/shinken...
[shinken.git] / shinken / notificationway.py
blob002ce2592cd58a7547f01a6d0bcf4b497c3dfb94
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
7 #This file is part of Shinken.
9 #Shinken is free software: you can redistribute it and/or modify
10 #it under the terms of the GNU Affero General Public License as published by
11 #the Free Software Foundation, either version 3 of the License, or
12 #(at your option) any later version.
14 #Shinken is distributed in the hope that it will be useful,
15 #but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 #GNU Affero General Public License for more details.
19 #You should have received a copy of the GNU Affero General Public License
20 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
23 from shinken.item import Item, Items
24 from shinken.util import to_split, to_bool
25 from shinken.property import UnusedProp, BoolProp, IntegerProp, FloatProp, CharProp, StringProp, ListProp
28 _special_properties = ( 'service_notification_commands', 'host_notification_commands',
29 'service_notification_period', 'host_notification_period' )
32 class NotificationWay(Item):
33 id = 1#0 is always special in database, so we do not take risk here
34 my_type = 'notificationway'
36 properties={
37 'notificationway_name': StringProp(
38 fill_brok=['full_status']),
39 'host_notifications_enabled': BoolProp(
40 default='1',
41 fill_brok=['full_status']),
42 'service_notifications_enabled': BoolProp(
43 default='1',
44 fill_brok=['full_status']),
45 'host_notification_period': StringProp(
46 fill_brok=['full_status']),
47 'service_notification_period': StringProp(
48 fill_brok=['full_status']),
49 'host_notification_options': ListProp(
50 fill_brok=['full_status']),
51 'service_notification_options': ListProp(
52 fill_brok=['full_status']),
53 'host_notification_commands': StringProp(
54 fill_brok=['full_status']),
55 'service_notification_commands': StringProp(
56 fill_brok=['full_status']),
57 'min_criticity' : IntegerProp(
58 default = '0',
59 fill_brok=['full_status']),
61 running_properties = {}
63 macros = {
67 #For debugging purpose only (nice name)
68 def get_name(self):
69 return self.notificationway_name
72 #Search for notification_options with state and if t is
73 #in service_notification_period
74 def want_service_notification(self, t, state, type, criticity):
75 if not self.service_notifications_enabled:
76 return False
78 # If the criticity is not high enough, we bail out
79 if criticity < self.min_criticity:
80 return False
82 b = self.service_notification_period.is_time_valid(t)
83 if 'n' in self.service_notification_options:
84 return False
85 t = {'WARNING' : 'w', 'UNKNOWN' : 'u', 'CRITICAL' : 'c',
86 'RECOVERY' : 'r', 'FLAPPING' : 'f', 'DOWNTIME' : 's'}
87 if type == 'PROBLEM':
88 if state in t:
89 return b and t[state] in self.service_notification_options
90 elif type == 'RECOVERY':
91 if type in t:
92 return b and t[type] in self.service_notification_options
93 elif type == 'ACKNOWLEDGEMENT':
94 return b
95 elif type in ('FLAPPINGSTART', 'FLAPPINGSTOP', 'FLAPPINGDISABLED'):
96 return b and 'f' in self.service_notification_options
97 elif type in ('DOWNTIMESTART', 'DOWNTIMEEND', 'DOWNTIMECANCELLED'):
98 #No notification when a downtime was cancelled. Is that true??
99 # According to the documentation we need to look at _host_ options
100 return b and 's' in self.host_notification_options
102 return False
105 #Search for notification_options with state and if t is in
106 #host_notification_period
107 def want_host_notification(self, t, state, type, criticity):
108 if not self.host_notifications_enabled:
109 return False
111 # If the criticity is not high enough, we bail out
112 if criticity < self.min_criticity:
113 return False
115 b = self.host_notification_period.is_time_valid(t)
116 if 'n' in self.host_notification_options:
117 return False
118 t = {'DOWN' : 'd', 'UNREACHABLE' : 'u', 'RECOVERY' : 'r',
119 'FLAPPING' : 'f', 'DOWNTIME' : 's'}
120 if type == 'PROBLEM':
121 if state in t:
122 return b and t[state] in self.host_notification_options
123 elif type == 'RECOVERY':
124 if type in t:
125 return b and t[type] in self.host_notification_options
126 elif type == 'ACKNOWLEDGEMENT':
127 return b
128 elif type in ('FLAPPINGSTART', 'FLAPPINGSTOP', 'FLAPPINGDISABLED'):
129 return b and 'f' in self.host_notification_options
130 elif type in ('DOWNTIMESTART', 'DOWNTIMEEND', 'DOWNTIMECANCELLED'):
131 return b and 's' in self.host_notification_options
133 return False
136 def clean(self):
137 pass
140 #Call to get our commands to launch a Notification
141 def get_notification_commands(self, type):
142 #service_notification_commands for service
143 notif_commands_prop = type+'_notification_commands'
144 notif_commands = getattr(self, notif_commands_prop)
145 return notif_commands
149 #Check is required prop are set:
150 #contacts OR contactgroups is need
151 def is_correct(self):
152 state = True #guilty or not? :)
153 cls = self.__class__
155 #A null notif way is a notif way that will do nothing (service = n, hot =n)
156 is_null_notifway = False
157 if hasattr(self, 'service_notification_options') and self.service_notification_options==['n']:
158 if hasattr(self, 'host_notification_options') and self.host_notification_options==['n']:
159 is_null_notifway = True
160 return True
162 for prop in cls.properties:
163 if prop not in _special_properties:
164 if not hasattr(self, prop) and cls.properties[prop].required:
165 print self.get_name(), " : I do not have", prop
166 state = False #Bad boy...
168 #Ok now we manage special cases...
169 #Service part
170 if not hasattr(self, 'service_notification_commands') :
171 print self.get_name()," : do not have any service_notification_commands defined"
172 state = False
173 else:
174 for cmd in self.service_notification_commands:
175 if cmd == None:
176 print self.get_name()," : a service_notification_command is missing"
177 state = False
178 if not cmd.is_valid():
179 print self.get_name()," : a service_notification_command is invalid", cmd.get_name()
180 state = False
182 if getattr(self, 'service_notification_period', None) is None:
183 print self.get_name()," : the service_notification_period is invalid"
184 state = False
186 #Now host part
187 if not hasattr(self, 'host_notification_commands') :
188 print self.get_name()," : do not have any host_notification_commands defined"
189 state = False
190 else:
191 for cmd in self.host_notification_commands:
192 if cmd == None :
193 print self.get_name()," : a host_notification_command is missing"
194 state = False
195 if not cmd.is_valid():
196 print self.get_name()," : a host_notification_command is invalid", cmd.get_name(), cmd.__dict__
197 state = False
199 if getattr(self, 'host_notification_period', None) is None:
200 print self.get_name()," : the host_notification_period is invalid"
201 state = False
203 return state
208 class NotificationWays(Items):
209 name_property = "notificationway_name"
210 inner_class = NotificationWay
212 def linkify(self, timeperiods, commands):
213 self.linkify_with_timeperiods(timeperiods, 'service_notification_period')
214 self.linkify_with_timeperiods(timeperiods, 'host_notification_period')
215 self.linkify_command_list_with_commands(commands, 'service_notification_commands')
216 self.linkify_command_list_with_commands(commands, 'host_notification_commands')
219 def new_inner_member(self, name=None, params={}):
220 if name == None:
221 name = NotificationWay.id
222 params['notificationway_name'] = name
223 #print "Asking a new inner notificationway from name %s with params %s" % (name, params)
224 nw = NotificationWay(params)
225 self.items[nw.id] = nw