Fix : get back LiveStatus as default.
[shinken.git] / shinken / objects / notificationway.py
blobf1fb6fcf264d42e70b0d7b74d4079c0241e23736
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 from shinken.objects.item import Item, Items
26 from shinken.property import BoolProp, IntegerProp, StringProp, ListProp
29 _special_properties = ( 'service_notification_commands', 'host_notification_commands',
30 'service_notification_period', 'host_notification_period' )
33 class NotificationWay(Item):
34 id = 1#0 is always special in database, so we do not take risk here
35 my_type = 'notificationway'
37 properties = {
38 'notificationway_name': StringProp (fill_brok=['full_status']),
39 'host_notifications_enabled': BoolProp (default='1', fill_brok=['full_status']),
40 'service_notifications_enabled':BoolProp (default='1', fill_brok=['full_status']),
41 'host_notification_period': StringProp (fill_brok=['full_status']),
42 'service_notification_period': StringProp (fill_brok=['full_status']),
43 'host_notification_options': ListProp (fill_brok=['full_status']),
44 'service_notification_options': ListProp (fill_brok=['full_status']),
45 'host_notification_commands': StringProp (fill_brok=['full_status']),
46 'service_notification_commands':StringProp (fill_brok=['full_status']),
47 'min_criticity': IntegerProp(default = '0', fill_brok=['full_status']),
50 running_properties = {}
52 macros = {}
54 #For debugging purpose only (nice name)
55 def get_name(self):
56 return self.notificationway_name
59 #Search for notification_options with state and if t is
60 #in service_notification_period
61 def want_service_notification(self, t, state, type, criticity):
62 if not self.service_notifications_enabled:
63 return False
65 # If the criticity is not high enough, we bail out
66 if criticity < self.min_criticity:
67 return False
69 b = self.service_notification_period.is_time_valid(t)
70 if 'n' in self.service_notification_options:
71 return False
72 t = {'WARNING' : 'w', 'UNKNOWN' : 'u', 'CRITICAL' : 'c',
73 'RECOVERY' : 'r', 'FLAPPING' : 'f', 'DOWNTIME' : 's'}
74 if type == 'PROBLEM':
75 if state in t:
76 return b and t[state] in self.service_notification_options
77 elif type == 'RECOVERY':
78 if type in t:
79 return b and t[type] in self.service_notification_options
80 elif type == 'ACKNOWLEDGEMENT':
81 return b
82 elif type in ('FLAPPINGSTART', 'FLAPPINGSTOP', 'FLAPPINGDISABLED'):
83 return b and 'f' in self.service_notification_options
84 elif type in ('DOWNTIMESTART', 'DOWNTIMEEND', 'DOWNTIMECANCELLED'):
85 #No notification when a downtime was cancelled. Is that true??
86 # According to the documentation we need to look at _host_ options
87 return b and 's' in self.host_notification_options
89 return False
92 #Search for notification_options with state and if t is in
93 #host_notification_period
94 def want_host_notification(self, t, state, type, criticity):
95 if not self.host_notifications_enabled:
96 return False
98 # If the criticity is not high enough, we bail out
99 if criticity < self.min_criticity:
100 return False
102 b = self.host_notification_period.is_time_valid(t)
103 if 'n' in self.host_notification_options:
104 return False
105 t = {'DOWN' : 'd', 'UNREACHABLE' : 'u', 'RECOVERY' : 'r',
106 'FLAPPING' : 'f', 'DOWNTIME' : 's'}
107 if type == 'PROBLEM':
108 if state in t:
109 return b and t[state] in self.host_notification_options
110 elif type == 'RECOVERY':
111 if type in t:
112 return b and t[type] in self.host_notification_options
113 elif type == 'ACKNOWLEDGEMENT':
114 return b
115 elif type in ('FLAPPINGSTART', 'FLAPPINGSTOP', 'FLAPPINGDISABLED'):
116 return b and 'f' in self.host_notification_options
117 elif type in ('DOWNTIMESTART', 'DOWNTIMEEND', 'DOWNTIMECANCELLED'):
118 return b and 's' in self.host_notification_options
120 return False
123 def clean(self):
124 pass
127 #Call to get our commands to launch a Notification
128 def get_notification_commands(self, type):
129 #service_notification_commands for service
130 notif_commands_prop = type+'_notification_commands'
131 notif_commands = getattr(self, notif_commands_prop)
132 return notif_commands
136 #Check is required prop are set:
137 #contacts OR contactgroups is need
138 def is_correct(self):
139 state = True #guilty or not? :)
140 cls = self.__class__
142 #A null notif way is a notif way that will do nothing (service = n, hot =n)
143 is_null_notifway = False
144 if hasattr(self, 'service_notification_options') and self.service_notification_options==['n']:
145 if hasattr(self, 'host_notification_options') and self.host_notification_options==['n']:
146 is_null_notifway = True
147 return True
149 for prop, entry in cls.properties.items():
150 if prop not in _special_properties:
151 if not hasattr(self, prop) and entry.required:
152 print self.get_name(), " : I do not have", prop
153 state = False #Bad boy...
155 #Ok now we manage special cases...
156 #Service part
157 if not hasattr(self, 'service_notification_commands') :
158 print self.get_name()," : do not have any service_notification_commands defined"
159 state = False
160 else:
161 for cmd in self.service_notification_commands:
162 if cmd is None:
163 print self.get_name()," : a service_notification_command is missing"
164 state = False
165 if not cmd.is_valid():
166 print self.get_name()," : a service_notification_command is invalid", cmd.get_name()
167 state = False
169 if getattr(self, 'service_notification_period', None) is None:
170 print self.get_name()," : the service_notification_period is invalid"
171 state = False
173 #Now host part
174 if not hasattr(self, 'host_notification_commands') :
175 print self.get_name()," : do not have any host_notification_commands defined"
176 state = False
177 else:
178 for cmd in self.host_notification_commands:
179 if cmd is None :
180 print self.get_name()," : a host_notification_command is missing"
181 state = False
182 if not cmd.is_valid():
183 print self.get_name()," : a host_notification_command is invalid", cmd.get_name(), cmd.__dict__
184 state = False
186 if getattr(self, 'host_notification_period', None) is None:
187 print self.get_name()," : the host_notification_period is invalid"
188 state = False
190 return state
195 class NotificationWays(Items):
196 name_property = "notificationway_name"
197 inner_class = NotificationWay
199 def linkify(self, timeperiods, commands):
200 self.linkify_with_timeperiods(timeperiods, 'service_notification_period')
201 self.linkify_with_timeperiods(timeperiods, 'host_notification_period')
202 self.linkify_command_list_with_commands(commands, 'service_notification_commands')
203 self.linkify_command_list_with_commands(commands, 'host_notification_commands')
206 def new_inner_member(self, name=None, params={}):
207 if name is None:
208 name = NotificationWay.id
209 params['notificationway_name'] = name
210 #print "Asking a new inner notificationway from name %s with params %s" % (name, params)
211 nw = NotificationWay(params)
212 self.items[nw.id] = nw