Add : the full demoCA certification autority so people can sign their own keys.
[shinken.git] / shinken / hostgroup.py
blob7a557a3d0a1008856356120e393c92feaeb2cd9e
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 itemgroup import Itemgroup, Itemgroups
23 from shinken.property import UnusedProp, BoolProp, IntegerProp, FloatProp, CharProp, StringProp, ListProp
25 class Hostgroup(Itemgroup):
26 id = 1 #0 is always a little bit special... like in database
27 my_type = 'hostgroup'
29 properties={
30 'id': StringProp(
31 default=0,
32 fill_brok=['full_status']),
33 'hostgroup_name': StringProp(
34 fill_brok=['full_status']),
35 'alias': StringProp(
36 fill_brok=['full_status']),
37 'notes': StringProp(
38 default='',
39 fill_brok=['full_status']),
40 'notes_url': StringProp(
41 default='',
42 fill_brok=['full_status']),
43 'action_url': StringProp(
44 default='',
45 fill_brok=['full_status']),
46 'members': StringProp(
47 default='',
48 fill_brok=['full_status']),
49 #Shinken specific
50 'unknown_members': StringProp(
51 default=[]),
52 'configuration_errors' : StringProp(default = []),
55 macros = {
56 'HOSTGROUPALIAS' : 'alias',
57 'HOSTGROUPMEMBERS' : 'members',
58 'HOSTGROUPNOTES' : 'notes',
59 'HOSTGROUPNOTESURL' : 'notes_url',
60 'HOSTGROUPACTIONURL' : 'action_url'
64 def get_name(self):
65 return self.hostgroup_name
68 def get_hosts(self):
69 return getattr(self, 'members', '')
72 def get_hostgroup_members(self):
73 if self.has('hostgroup_members'):
74 return self.hostgroup_members.split(',')
75 else:
76 return []
79 #We fillfull properties with template ones if need
80 #Because hostgroup we call may not have it's members
81 #we call get_hosts_by_explosion on it
82 def get_hosts_by_explosion(self, hostgroups):
83 #First we tag the hg so it will not be explode
84 #if a son of it already call it
85 self.already_explode = True
87 #Now the recursiv part
88 #rec_tag is set to False avery HG we explode
89 #so if True here, it must be a loop in HG
90 #calls... not GOOD!
91 if self.rec_tag:
92 print "Error : we've got a loop in hostgroup definition", self.get_name()
93 return self.get_hosts()
94 #Ok, not a loop, we tag it and continue
95 self.rec_tag = True
97 hg_mbrs = self.get_hostgroup_members()
98 for hg_mbr in hg_mbrs:
99 hg = hostgroups.find_by_name(hg_mbr.strip())
100 if hg is not None:
101 value = hg.get_hosts_by_explosion(hostgroups)
102 if value is not None:
103 self.add_string_member(value)
105 return self.get_hosts()
109 class Hostgroups(Itemgroups):
110 name_property = "hostgroup_name" # is used for finding hostgroups
111 inner_class = Hostgroup
113 def get_members_by_name(self, hgname):
114 id = self.find_id_by_name(hgname)
115 if id == None:
116 return []
117 return self.itemgroups[id].get_hosts()
120 def linkify(self, hosts=None, realms=None):
121 self.linkify_hg_by_hst(hosts)
122 self.linkify_hg_by_realms(realms)
125 #We just search for each hostgroup the id of the hosts
126 #and replace the name by the id
127 def linkify_hg_by_hst(self, hosts):
128 for hg in self.itemgroups.values():
129 mbrs = hg.get_hosts()
130 #The new member list, in id
131 new_mbrs = []
132 for mbr in mbrs:
133 if mbr == '*':
134 new_mbrs.extend(hosts)
135 else:
136 h = hosts.find_by_name(mbr)
137 if h != None:
138 new_mbrs.append(h)
139 else:
140 hg.unknown_members.append(mbr)
142 #Make members uniq
143 new_mbrs = list(set(new_mbrs))
145 #We find the id, we remplace the names
146 hg.replace_members(new_mbrs)
148 #Now register us in our members
149 for h in hg.members:
150 h.hostgroups.append(hg)
151 #and be sure we are uniq in it
152 h.hostgroups = list(set(h.hostgroups))
155 #More than an explode function, but we need to already
156 #have members so... Will be really linkify just after
157 #And we explode realm in ours members, but do not overide
158 #a host realm value if it's already set
159 def linkify_hg_by_realms(self, realms):
160 #Now we explode the realm value if we've got one
161 #The group realm must not overide a host one (warning?)
162 for hg in self:
163 if not hasattr(hg, 'realm'): continue
165 r = realms.find_by_name(hg.realm.strip())
166 if r != None:
167 hg.realm = r
168 print "Hostgroup", hg.get_name(), "is in the realm", r.get_name()
169 else:
170 err = "The hostgroup %s got an unknown realm '%s'" % (hg.get_name(), hg.realm)
171 hg.configuration_errors.append(err)
172 hg.realm = None
173 continue
175 for h in hg:
176 if h is None: continue
177 if h.realm == None or h.got_default_realm: #default value not hasattr(h, 'realm'):
178 print "Apply a realm", hg.realm.get_name(), "to host", h.get_name(), "from a hostgroup rule (%s)" % hg.get_name()
179 h.realm = hg.realm
180 else:
181 if h.realm != hg.realm:
182 print "Warning : host", h.get_name(), "is not in the same realm than it's hostgroup", hg.get_name()
185 #Add a host string to a hostgroup member
186 #if the host group do not exist, create it
187 def add_member(self, hname, hgname):
188 id = self.find_id_by_name(hgname)
189 #if the id do not exist, create the hg
190 if id == None:
191 hg = Hostgroup({'hostgroup_name' : hgname, 'alias' : hgname, 'members' : hname})
192 self.add(hg)
193 else:
194 self.itemgroups[id].add_string_member(hname)
197 #Use to fill members with hostgroup_members
198 def explode(self):
199 #We do not want a same hg to be explode again and again
200 #so we tag it
201 for tmp_hg in self.itemgroups.values():
202 tmp_hg.already_explode = False
203 for hg in self.itemgroups.values():
204 if hg.has('hostgroup_members') and not hg.already_explode:
205 #get_hosts_by_explosion is a recursive
206 #function, so we must tag hg so we do not loop
207 for tmp_hg in self.itemgroups.values():
208 tmp_hg.rec_tag = False
209 hg.get_hosts_by_explosion(self)
211 #We clean the tags
212 for tmp_hg in self.itemgroups.values():
213 if hasattr(tmp_hg, 'rec_tag'):
214 del tmp_hg.rec_tag
215 del tmp_hg.already_explode