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'
37 'notificationway_name': StringProp(
38 fill_brok
=['full_status']),
39 'host_notifications_enabled': BoolProp(
41 fill_brok
=['full_status']),
42 'service_notifications_enabled': BoolProp(
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(
59 fill_brok
=['full_status']),
61 running_properties
= {}
67 #For debugging purpose only (nice name)
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
:
78 # If the criticity is not high enough, we bail out
79 if criticity
< self
.min_criticity
:
82 b
= self
.service_notification_period
.is_time_valid(t
)
83 if 'n' in self
.service_notification_options
:
85 t
= {'WARNING' : 'w', 'UNKNOWN' : 'u', 'CRITICAL' : 'c',
86 'RECOVERY' : 'r', 'FLAPPING' : 'f', 'DOWNTIME' : 's'}
89 return b
and t
[state
] in self
.service_notification_options
90 elif type == 'RECOVERY':
92 return b
and t
[type] in self
.service_notification_options
93 elif type == 'ACKNOWLEDGEMENT':
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
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
:
111 # If the criticity is not high enough, we bail out
112 if criticity
< self
.min_criticity
:
115 b
= self
.host_notification_period
.is_time_valid(t
)
116 if 'n' in self
.host_notification_options
:
118 t
= {'DOWN' : 'd', 'UNREACHABLE' : 'u', 'RECOVERY' : 'r',
119 'FLAPPING' : 'f', 'DOWNTIME' : 's'}
120 if type == 'PROBLEM':
122 return b
and t
[state
] in self
.host_notification_options
123 elif type == 'RECOVERY':
125 return b
and t
[type] in self
.host_notification_options
126 elif type == 'ACKNOWLEDGEMENT':
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
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? :)
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
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...
170 if not hasattr(self
, 'service_notification_commands') :
171 print self
.get_name()," : do not have any service_notification_commands defined"
174 for cmd
in self
.service_notification_commands
:
176 print self
.get_name()," : a service_notification_command is missing"
178 if not cmd
.is_valid():
179 print self
.get_name()," : a service_notification_command is invalid", cmd
.get_name()
182 if getattr(self
, 'service_notification_period', None) is None:
183 print self
.get_name()," : the service_notification_period is invalid"
187 if not hasattr(self
, 'host_notification_commands') :
188 print self
.get_name()," : do not have any host_notification_commands defined"
191 for cmd
in self
.host_notification_commands
:
193 print self
.get_name()," : a host_notification_command is missing"
195 if not cmd
.is_valid():
196 print self
.get_name()," : a host_notification_command is invalid", cmd
.get_name(), cmd
.__dict
__
199 if getattr(self
, 'host_notification_period', None) is None:
200 print self
.get_name()," : the host_notification_period is invalid"
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
={}):
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