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 #Contactgroups are groups for contacts
23 #They are just used for the config read and explode by elements
25 from itemgroup
import Itemgroup
, Itemgroups
26 from shinken
.property import UnusedProp
, BoolProp
, IntegerProp
, FloatProp
, CharProp
, StringProp
, ListProp
28 class Contactgroup(Itemgroup
):
30 my_type
= 'contactgroup'
35 fill_brok
=['full_status']),
36 'contactgroup_name': StringProp(
37 fill_brok
=['full_status']),
39 fill_brok
=['full_status']),
40 'members': StringProp(
41 fill_brok
=['full_status']),
43 'unknown_members': StringProp(
45 'configuration_errors' : StringProp(default
= []),
48 'CONTACTGROUPALIAS' : 'alias',
49 'CONTACTGROUPMEMBERS' : 'get_members'
53 def get_contacts(self
):
58 return self
.contactgroup_name
61 def get_contactgroup_members(self
):
62 if self
.has('contactgroup_members'):
63 return self
.contactgroup_members
.split(',')
68 #We fillfull properties with template ones if need
69 #Because hostgroup we call may not have it's members
70 #we call get_hosts_by_explosion on it
71 def get_contacts_by_explosion(self
, contactgroups
):
72 #First we tag the hg so it will not be explode
73 #if a son of it already call it
74 self
.already_explode
= True
76 #Now the recursiv part
77 #rec_tag is set to False avery CG we explode
78 #so if True here, it must be a loop in HG
81 print "Error : we've got a loop in contactgroup definition", self
.get_name()
82 if self
.has('members'):
86 #Ok, not a loop, we tag it and continue
89 cg_mbrs
= self
.get_contactgroup_members()
90 for cg_mbr
in cg_mbrs
:
91 cg
= contactgroups
.find_by_name(cg_mbr
.strip())
93 value
= cg
.get_contacts_by_explosion(contactgroups
)
95 self
.add_string_member(value
)
96 if self
.has('members'):
102 class Contactgroups(Itemgroups
):
103 name_property
= "contactgroup_name" # is used for finding contactgroup
104 inner_class
= Contactgroup
106 def get_members_by_name(self
, cgname
):
107 id = self
.find_id_by_name(cgname
)
110 return self
.itemgroups
[id].get_contacts()
113 def add_contactgroup(self
, cg
):
114 self
.itemgroups
[cg
.id] = cg
117 def linkify(self
, contacts
):
118 self
.linkify_cg_by_cont(contacts
)
121 #We just search for each host the id of the host
122 #and replace the name by the id
123 def linkify_cg_by_cont(self
, contacts
):
124 for id in self
.itemgroups
:
125 mbrs
= self
.itemgroups
[id].get_contacts()
127 #The new member list, in id
130 m
= contacts
.find_by_name(mbr
)
131 #Maybe the contact is missing, if so, must be put in unknown_members
135 self
.itemgroups
[id].unknown_members
.append(mbr
)
138 new_mbrs
= list(set(new_mbrs
))
140 #We find the id, we remplace the names
141 self
.itemgroups
[id].replace_members(new_mbrs
)
144 #Add a contact string to a contact member
145 #if the contact group do not exist, create it
146 def add_member(self
, cname
, cgname
):
147 id = self
.find_id_by_name(cgname
)
148 #if the id do not exist, create the cg
150 cg
= Contactgroup({'contactgroup_name' : cgname
, 'alias' : cgname
, 'members' : cname
})
151 self
.add_contactgroup(cg
)
153 self
.itemgroups
[id].add_string_member(cname
)
156 #Use to fill members with contactgroup_members
158 #We do not want a same hg to be explode again and again
160 for tmp_cg
in self
.itemgroups
.values():
161 tmp_cg
.already_explode
= False
163 for cg
in self
.itemgroups
.values():
164 if cg
.has('contactgroup_members') and not cg
.already_explode
:
165 #get_contacts_by_explosion is a recursive
166 #function, so we must tag hg so we do not loop
167 for tmp_cg
in self
.itemgroups
.values():
168 tmp_cg
.rec_tag
= False
169 cg
.get_contacts_by_explosion(self
)
172 for tmp_cg
in self
.itemgroups
.values():
173 if hasattr(tmp_cg
, 'rec_tag'):
175 del tmp_cg
.already_explode