Merge branch 'master' of ssh://naparuba@shinken.git.sourceforge.net/gitroot/shinken...
[shinken.git] / shinken / contactgroup.py
blob21fe6b20406dc2aa993855aedd3440a41366e28f
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 #Contactgroups are groups for contacts
24 #They are just used for the config read and explode by elements
26 from itemgroup import Itemgroup, Itemgroups
27 from shinken.property import UnusedProp, BoolProp, IntegerProp, FloatProp, CharProp, StringProp, ListProp
29 class Contactgroup(Itemgroup):
30 id = 1
31 my_type = 'contactgroup'
33 properties={
34 'id': IntegerProp(
35 default=0,
36 fill_brok=['full_status']),
37 'contactgroup_name': StringProp(
38 fill_brok=['full_status']),
39 'alias': StringProp(
40 fill_brok=['full_status']),
41 'members': StringProp(
42 fill_brok=['full_status']),
43 #Shinken specific
44 'unknown_members': StringProp(
45 default=[]),
46 'configuration_errors' : StringProp(default = []),
48 macros = {
49 'CONTACTGROUPALIAS' : 'alias',
50 'CONTACTGROUPMEMBERS' : 'get_members'
54 def get_contacts(self):
55 return self.members
58 def get_name(self):
59 return self.contactgroup_name
62 def get_contactgroup_members(self):
63 if self.has('contactgroup_members'):
64 return self.contactgroup_members.split(',')
65 else:
66 return []
69 #We fillfull properties with template ones if need
70 #Because hostgroup we call may not have it's members
71 #we call get_hosts_by_explosion on it
72 def get_contacts_by_explosion(self, contactgroups):
73 #First we tag the hg so it will not be explode
74 #if a son of it already call it
75 self.already_explode = True
77 #Now the recursiv part
78 #rec_tag is set to False avery CG we explode
79 #so if True here, it must be a loop in HG
80 #calls... not GOOD!
81 if self.rec_tag:
82 print "Error : we've got a loop in contactgroup definition", self.get_name()
83 if self.has('members'):
84 return self.members
85 else:
86 return ''
87 #Ok, not a loop, we tag it and continue
88 self.rec_tag = True
90 cg_mbrs = self.get_contactgroup_members()
91 for cg_mbr in cg_mbrs:
92 cg = contactgroups.find_by_name(cg_mbr.strip())
93 if cg is not None:
94 value = cg.get_contacts_by_explosion(contactgroups)
95 if value is not None:
96 self.add_string_member(value)
97 if self.has('members'):
98 return self.members
99 else:
100 return ''
103 class Contactgroups(Itemgroups):
104 name_property = "contactgroup_name" # is used for finding contactgroup
105 inner_class = Contactgroup
107 def get_members_by_name(self, cgname):
108 id = self.find_id_by_name(cgname)
109 if id == None:
110 return []
111 return self.itemgroups[id].get_contacts()
114 def add_contactgroup(self, cg):
115 self.itemgroups[cg.id] = cg
118 def linkify(self, contacts):
119 self.linkify_cg_by_cont(contacts)
122 #We just search for each host the id of the host
123 #and replace the name by the id
124 def linkify_cg_by_cont(self, contacts):
125 for id in self.itemgroups:
126 mbrs = self.itemgroups[id].get_contacts()
128 #The new member list, in id
129 new_mbrs = []
130 for mbr in mbrs:
131 m = contacts.find_by_name(mbr)
132 #Maybe the contact is missing, if so, must be put in unknown_members
133 if m != None:
134 new_mbrs.append(m)
135 else:
136 self.itemgroups[id].unknown_members.append(mbr)
138 #Make members uniq
139 new_mbrs = list(set(new_mbrs))
141 #We find the id, we remplace the names
142 self.itemgroups[id].replace_members(new_mbrs)
145 #Add a contact string to a contact member
146 #if the contact group do not exist, create it
147 def add_member(self, cname, cgname):
148 id = self.find_id_by_name(cgname)
149 #if the id do not exist, create the cg
150 if id == None:
151 cg = Contactgroup({'contactgroup_name' : cgname, 'alias' : cgname, 'members' : cname})
152 self.add_contactgroup(cg)
153 else:
154 self.itemgroups[id].add_string_member(cname)
157 #Use to fill members with contactgroup_members
158 def explode(self):
159 #We do not want a same hg to be explode again and again
160 #so we tag it
161 for tmp_cg in self.itemgroups.values():
162 tmp_cg.already_explode = False
164 for cg in self.itemgroups.values():
165 if cg.has('contactgroup_members') and not cg.already_explode:
166 #get_contacts_by_explosion is a recursive
167 #function, so we must tag hg so we do not loop
168 for tmp_cg in self.itemgroups.values():
169 tmp_cg.rec_tag = False
170 cg.get_contacts_by_explosion(self)
172 #We clean the tags
173 for tmp_cg in self.itemgroups.values():
174 if hasattr(tmp_cg, 'rec_tag'):
175 del tmp_cg.rec_tag
176 del tmp_cg.already_explode