*Remove the dot from some import paths, so python 2.4 is happy
[shinken.git] / shinken / objects / contact.py
blob8c6f497a8acf9d6b0c02065103f8ef57fec9454f
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 item import Item, Items
24 from shinken.util import to_split, to_bool, strip_and_uniq
25 from shinken.property import UnusedProp, BoolProp, IntegerProp, FloatProp, CharProp, StringProp, ListProp
26 from shinken.log import logger
29 _special_properties = ( 'service_notification_commands', 'host_notification_commands',
30 'service_notification_period', 'host_notification_period',
31 'service_notification_options', 'host_notification_options',
32 'host_notification_commands', 'contact_name' )
34 _simple_way_parameters = ( 'service_notification_period', 'host_notification_period',
35 'service_notification_options', 'host_notification_options',
36 'service_notification_commands', 'host_notification_commands',
37 'min_criticity' )
40 class Contact(Item):
41 id = 1#0 is always special in database, so we do not take risk here
42 my_type = 'contact'
44 properties={
45 'contact_name' : StringProp(fill_brok=['full_status']),
46 'alias' : StringProp(default='none', fill_brok=['full_status']),
47 'contactgroups' : StringProp(default='', fill_brok=['full_status']),
48 'host_notifications_enabled' : BoolProp(default='1', fill_brok=['full_status']),
49 'service_notifications_enabled' : BoolProp(default='1', fill_brok=['full_status']),
50 'host_notification_period' : StringProp(fill_brok=['full_status']),
51 'service_notification_period' : StringProp(fill_brok=['full_status']),
52 'host_notification_options' : ListProp(fill_brok=['full_status']),
53 'service_notification_options' : ListProp(fill_brok=['full_status']),
54 'host_notification_commands' : StringProp(fill_brok=['full_status']),
55 'service_notification_commands' : StringProp(fill_brok=['full_status']),
56 'min_criticity' : IntegerProp(default = '0', fill_brok=['full_status']),
57 'email' : StringProp(default='none', fill_brok=['full_status']),
58 'pager' : StringProp(default='none', fill_brok=['full_status']),
59 'address1' : StringProp(default='none', fill_brok=['full_status']),
60 'address2' : StringProp(default='none', fill_brok=['full_status']),
61 'address3' : StringProp(default='none', fill_brok=['full_status']),
62 'address4' : StringProp(default='none', fill_brok=['full_status']),
63 'address5' : StringProp(default='none', fill_brok=['full_status']),
64 'address6' : StringProp(default='none', fill_brok=['full_status']),
65 'can_submit_commands' : BoolProp(default='0', fill_brok=['full_status']),
66 'retain_status_information' : BoolProp(default='1', fill_brok=['full_status']),
67 'notificationways' : StringProp(default=''),
70 running_properties = {
71 #All errors and warning raised during the configuration parsing
72 #and taht will raised real warning/errors during the is_correct
73 'configuration_warnings' : ListProp(default=[]),
74 'configuration_errors' : ListProp(default=[]),
75 'downtimes': StringProp(
76 default=[],
77 fill_brok=['full_status'],
78 retention=True),
82 macros = {
83 'CONTACTNAME' : 'contact_name',
84 'CONTACTALIAS' : 'alias',
85 'CONTACTEMAIL' : 'email',
86 'CONTACTPAGER' : 'pager',
87 'CONTACTADDRESS1' : 'address1',
88 'CONTACTADDRESS2' : 'address2',
89 'CONTACTADDRESS3' : 'address3',
90 'CONTACTADDRESS4' : 'address4',
91 'CONTACTADDRESS5' : 'address5',
92 'CONTACTADDRESS6' : 'address6',
93 'CONTACTGROUPNAME' : 'get_groupname',
94 'CONTACTGROUPNAMES' : 'get_groupnames'
98 #For debugging purpose only (nice name)
99 def get_name(self):
100 return self.contact_name
103 #Search for notification_options with state and if t is
104 #in service_notification_period
105 def want_service_notification(self, t, state, type, criticity):
106 if not self.service_notifications_enabled:
107 return False
109 # If we are in downtime, we do nto want notification
110 for dt in self.downtimes:
111 if dt.is_in_effect:
112 return False
114 #Now the rest is for sub notificationways. If one is OK, we are ok
115 for nw in self.notificationways:
116 nw_b = nw.want_service_notification(t, state, type, criticity)
117 if nw_b:
118 return True
120 #Oh... no one is ok for it? so no, sorry
121 return False
124 #Search for notification_options with state and if t is in
125 #host_notification_period
126 def want_host_notification(self, t, state, type, criticity):
127 if not self.host_notifications_enabled:
128 return False
130 # If we are in downtime, we do nto want notification
131 for dt in self.downtimes:
132 if dt.is_in_effect:
133 return False
135 #Now it's all for sub notificationways. If one is OK, we are OK
136 for nw in self.notificationways:
137 nw_b = nw.want_host_notification(t, state, type, criticity)
138 if nw_b:
139 return True
141 #Oh, nobody..so NO :)
142 return False
145 #Useless function from now
146 def clean(self):
147 pass
150 #Call to get our commands to launch a Notification
151 def get_notification_commands(self, type):
152 r = []
153 #service_notification_commands for service
154 notif_commands_prop = type+'_notification_commands'
155 for nw in self.notificationways:
156 r.extend(getattr(nw, notif_commands_prop))
157 return r
161 #Check is required prop are set:
162 #contacts OR contactgroups is need
163 def is_correct(self):
164 state = True #guilty or not? :)
165 cls = self.__class__
167 #All of the above are checks in the notificationways part
168 for prop in cls.properties:
169 if prop not in _special_properties:
170 if not hasattr(self, prop) and cls.properties[prop].required:
171 print self.get_name(), " : I do not have", prop
172 state = False #Bad boy...
174 #There is a case where there is no nw : when there is not special_prop defined
175 #at all!!
176 if self.notificationways == []:
177 for p in _special_properties:
178 print self.get_name()," : I'm missing the property %s" % p
179 state = False
181 if hasattr(self, 'contact_name'):
182 for c in cls.illegal_object_name_chars:
183 if c in self.contact_name:
184 logger.log("%s : My contact_name got the caracter %s that is not allowed." % (self.get_name(), c))
185 state = False
186 else:
187 if hasattr(self, 'alias'): #take the alias if we miss the contact_name
188 self.contact_name = self.alias
189 return state
193 # Raise a log entry when a downtime begins
194 # CONTACT DOWNTIME ALERT: test_contact;STARTED; Contact has entered a period of scheduled downtime
195 def raise_enter_downtime_log_entry(self):
196 logger.log("CONTACT DOWNTIME ALERT: %s;STARTED; Contact has entered a period of scheduled downtime" % self.get_name())
199 # Raise a log entry when a downtime has finished
200 # CONTACT DOWNTIME ALERT: test_contact;STOPPED; Contact has exited from a period of scheduled downtime
201 def raise_exit_downtime_log_entry(self):
202 logger.log("CONTACT DOWNTIME ALERT: %s;STOPPED; Contact has exited from a period of scheduled downtime" % self.get_name())
205 # Raise a log entry when a downtime prematurely ends
206 # CONTACT DOWNTIME ALERT: test_contact;CANCELLED; Contact has entered a period of scheduled downtime
207 def raise_cancel_downtime_log_entry(self):
208 logger.log("CONTACT DOWNTIME ALERT: %s;CANCELLED; Scheduled downtime for contact has been cancelled." % self.get_name())
211 class Contacts(Items):
212 name_property = "contact_name"
213 inner_class = Contact
215 def linkify(self, timeperiods, commands, notificationways):
216 self.linkify_with_timeperiods(timeperiods, 'service_notification_period')
217 self.linkify_with_timeperiods(timeperiods, 'host_notification_period')
218 self.linkify_command_list_with_commands(commands, 'service_notification_commands')
219 self.linkify_command_list_with_commands(commands, 'host_notification_commands')
220 self.linkify_with_notificationways(notificationways)
222 #We've got a notificationways property with , separated contacts names
223 #and we want have a list of NotificationWay
224 def linkify_with_notificationways(self, notificationways):
225 for i in self:
226 if not hasattr(i, 'notificationways'): continue
227 new_notificationways = []
228 for nw_name in strip_and_uniq(i.notificationways.split(',')):
229 nw = notificationways.find_by_name(nw_name)
230 if nw != None:
231 new_notificationways.append(nw)
232 else: #TODO: What?
233 pass
234 #Get the list, but first make elements uniq
235 i.notificationways = list(set(new_notificationways))
239 #We look for contacts property in contacts and
240 def explode(self, contactgroups, notificationways):
241 #Contactgroups property need to be fullfill for got the informations
242 self.apply_partial_inheritance('contactgroups')
244 #Register ourself into the contactsgroups we are in
245 for c in self:
246 if c.is_tpl() or not (hasattr(c, 'contact_name') and hasattr(c, 'contactgroups')):
247 continue
248 for cg in c.contactgroups.split(','):
249 contactgroups.add_member(c.contact_name, cg.strip())
251 #Now create a notification way with the simple parameter of the
252 #contacts
253 for c in self:
254 if not c.is_tpl():
255 need_notificationway = False
256 params = {}
257 for p in _simple_way_parameters:
258 if hasattr(c, p):
259 need_notificationway = True
260 params[p] = getattr(c, p)
262 if need_notificationway:
263 #print "Create notif way with", params
264 cname = getattr(c, 'contact_name', getattr(c, 'alias', ''))
265 nw_name = cname+'_inner_notificationway'
266 notificationways.new_inner_member(nw_name, params)
268 if not hasattr(c, 'notificationways'):
269 c.notificationways = nw_name
270 else:
271 c.notificationways = c.notificationways + ',' +nw_name