*Code cleanup in livestatus.
[shinken.git] / shinken / contact.py
blob548344da4b36854b811281b3ff311fc7f33ca57c
1 #!/usr/bin/env python
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
6 #This file is part of Shinken.
8 #Shinken is free software: you can redistribute it and/or modify
9 #it under the terms of the GNU Affero General Public License as published by
10 #the Free Software Foundation, either version 3 of the License, or
11 #(at your option) any later version.
13 #Shinken is distributed in the hope that it will be useful,
14 #but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 #GNU Affero General Public License for more details.
18 #You should have received a copy of the GNU Affero General Public License
19 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
22 from item import Item, Items
23 from util import to_split, to_bool, strip_and_uniq
25 class Contact(Item):
26 id = 1#0 is always special in database, so we do not take risk here
27 my_type = 'contact'
29 properties={
30 'contact_name' : {'required' : True, 'fill_brok' : ['full_status']},
31 'alias' : {'required' : False, 'default' : 'none', 'fill_brok' : ['full_status']},
32 'contactgroups' : {'required' : False, 'default' : '', 'fill_brok' : ['full_status']},
33 'host_notifications_enabled' : {'required' : False, 'default' : '1', 'pythonize' : to_bool, 'fill_brok' : ['full_status']},
34 'service_notifications_enabled' : {'required' : False, 'default' : '1', 'pythonize' : to_bool, 'fill_brok' : ['full_status']},
35 'host_notification_period' : {'required' : True, 'fill_brok' : ['full_status']},
36 'service_notification_period' : {'required' : True, 'fill_brok' : ['full_status']},
37 'host_notification_options' : {'required' : True, 'pythonize' : to_split, 'fill_brok' : ['full_status']},
38 'service_notification_options' : {'required' : True, 'pythonize' : to_split, 'fill_brok' : ['full_status']},
39 'host_notification_commands' : {'required' : True, 'fill_brok' : ['full_status']},
40 'service_notification_commands' : {'required' : True, 'fill_brok' : ['full_status']},
41 'email' : {'required' : False, 'default' : 'none', 'fill_brok' : ['full_status']},
42 'pager' : {'required' : False, 'default' : 'none', 'fill_brok' : ['full_status']},
43 'address1' : {'required' : False, 'default' : 'none', 'fill_brok' : ['full_status']},
44 'address2' : {'required' : False, 'default' : 'none', 'fill_brok' : ['full_status']},
45 'address3' : {'required' : False, 'default' : 'none', 'fill_brok' : ['full_status']},
46 'address4' : {'required' : False, 'default' : 'none', 'fill_brok' : ['full_status']},
47 'address5' : {'required' : False, 'default' : 'none', 'fill_brok' : ['full_status']},
48 'address6' : {'required' : False, 'default' : 'none', 'fill_brok' : ['full_status']},
49 'can_submit_commands' : {'required' : False, 'default' : '0', 'pythonize' : to_bool, 'fill_brok' : ['full_status']},
50 'retain_status_information' : {'required' : False, 'default' : '1', 'pythonize' : to_bool, 'fill_brok' : ['full_status']},
51 'notificationways' : {'required' : False, 'default' : ''},
54 running_properties = {
55 #All errors and warning raised during the configuration parsing
56 #and taht will raised real warning/errors during the is_correct
57 'configuration_warnings' : {'default' : []},
58 'configuration_errors' : {'default' : []},
62 macros = {
63 'CONTACTNAME' : 'contact_name',
64 'CONTACTALIAS' : 'alias',
65 'CONTACTEMAIL' : 'email',
66 'CONTACTPAGER' : 'pager',
67 'CONTACTADDRESS1' : 'address1',
68 'CONTACTADDRESS2' : 'address2',
69 'CONTACTADDRESS3' : 'address3',
70 'CONTACTADDRESS4' : 'address4',
71 'CONTACTADDRESS5' : 'address5',
72 'CONTACTADDRESS6' : 'address6',
73 'CONTACTGROUPNAME' : 'get_groupname',
74 'CONTACTGROUPNAMES' : 'get_groupnames'
78 #For debugging purpose only (nice name)
79 def get_name(self):
80 return self.contact_name
83 #Search for notification_options with state and if t is
84 #in service_notification_period
85 def want_service_notification(self, t, state, type):
86 if not self.service_notifications_enabled:
87 return False
89 #Now the rest is for sub notificationways. If one is OK, we are ok
90 for nw in self.notificationways:
91 nw_b = nw.want_service_notification(t, state, type)
92 if nw_b:
93 return True
95 #Oh... no one is ok for it? so no, sorry
96 return False
99 #Search for notification_options with state and if t is in
100 #host_notification_period
101 def want_host_notification(self, t, state, type):
102 if not self.host_notifications_enabled:
103 return False
105 #Now it's all for sub notificationways. If one is OK, we are OK
106 for nw in self.notificationways:
107 nw_b = nw.want_host_notification(t, state, type)
108 if nw_b:
109 return True
111 #Oh, nobody..so NO :)
112 return False
115 #Useless function from now
116 def clean(self):
117 pass
120 #Call to get our commands to launch a Notification
121 def get_notification_commands(self, type):
122 r = []
123 #service_notification_commands for service
124 notif_commands_prop = type+'_notification_commands'
125 for nw in self.notificationways:
126 r.extend(getattr(nw, notif_commands_prop))
127 return r
131 #Check is required prop are set:
132 #contacts OR contactgroups is need
133 def is_correct(self):
134 state = True #guilty or not? :)
135 cls = self.__class__
137 #All of the above are checks in the notificationways part
138 special_properties = ['service_notification_commands', 'service_notification_commands', \
139 'service_notification_period', 'host_notification_period', \
140 'service_notification_options', 'host_notification_options', \
141 'host_notification_commands', 'contact_name']
143 for prop in cls.properties:
144 if prop not in special_properties:
145 if not hasattr(self, prop) and cls.properties[prop]['required']:
146 print self.get_name(), " : I do not have", prop
147 state = False #Bad boy...
149 #There is a case where there is no nw : when there is not special_prop defined
150 #at all!!
151 if self.notificationways == []:
152 for p in special_properties:
153 print self.get_name()," : I'm missing the property %s" % p
154 state = False
156 if hasattr(self, 'contact_name'):
157 for c in cls.illegal_object_name_chars:
158 if c in self.contact_name:
159 Log().log("%s : My contact_name got the caracter %s that is not allowed." % (self.get_name(), c))
160 state = False
161 else:
162 if hasattr(self, 'alias'): #take the alias if we miss the contact_name
163 self.contact_name = self.alias
164 return state
169 class Contacts(Items):
170 name_property = "contact_name"
171 inner_class = Contact
173 def linkify(self, timeperiods, commands, notificationways):
174 self.linkify_with_timeperiods(timeperiods, 'service_notification_period')
175 self.linkify_with_timeperiods(timeperiods, 'host_notification_period')
176 self.linkify_command_list_with_commands(commands, 'service_notification_commands')
177 self.linkify_command_list_with_commands(commands, 'host_notification_commands')
178 self.linkify_with_notificationways(notificationways)
180 #We've got a notificationways property with , separated contacts names
181 #and we want have a list of NotificationWay
182 def linkify_with_notificationways(self, notificationways):
183 for i in self:
184 if hasattr(i, 'notificationways'):
185 notificationways_tab = i.notificationways.split(',')
186 notificationways_tab = strip_and_uniq(notificationways_tab)
187 new_notificationways = []
188 for nw_name in notificationways_tab:
189 nw = notificationways.find_by_name(nw_name)
190 if nw != None:
191 new_notificationways.append(nw)
192 else: #TODO: What?
193 pass
194 #Get the list, but first make elements uniq
195 i.notificationways = list(set(new_notificationways))
199 #We look for contacts property in contacts and
200 def explode(self, contactgroups, notificationways):
201 #Contactgroups property need to be fullfill for got the informations
202 self.apply_partial_inheritance('contactgroups')
204 #Register ourself into the contactsgroups we are in
205 for c in self:
206 if not c.is_tpl():
207 if hasattr(c, 'contact_name'):
208 cname = c.contact_name
209 if hasattr(c, 'contactgroups'):
210 cgs = c.contactgroups.split(',')
211 for cg in cgs:
212 contactgroups.add_member(cname, cg.strip())
214 #Now create a notification way with the simple parameter of the
215 #contacts
216 simple_way_parameters = ['service_notification_period', 'host_notification_period', \
217 'service_notification_options', 'host_notification_options', \
218 'service_notification_commands', 'host_notification_commands']
219 for c in self:
220 if not c.is_tpl():
221 need_notificationway = False
222 params = {}
223 for p in simple_way_parameters:
224 if hasattr(c, p):
225 need_notificationway = True
226 params[p] = getattr(c, p)
228 if need_notificationway:
229 #print "Create notif way with", params
230 if hasattr(c, 'contact_name'):
231 cname = c.contact_name
232 else: #Will be change with an unique id, but will be an error in the end
233 if hasattr(c, 'alias'):
234 cname = c.alias
235 else:
236 cname = ''
237 nw_name = cname+'_inner_notificationway'
238 notificationways.new_inner_member(nw_name, params)
240 if not hasattr(c, 'notificationways'):
241 c.notificationways = nw_name
242 else:
243 c.notificationways = c.notificationways + ',' +nw_name