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 shinken
.itemgroup
import Itemgroup
, Itemgroups
23 from shinken
.util
import to_bool
24 from shinken
.property import UnusedProp
, BoolProp
, IntegerProp
, FloatProp
, CharProp
, StringProp
, ListProp
26 #It change from hostgroup Class because there is no members
27 #propertie, just the realm_members that we rewrite on it.
30 class Realm(Itemgroup
):
31 id = 1 #0 is always a little bit special... like in database
34 properties
={'id': IntegerProp(default
=0, fill_brok
=['full_status']),
35 'realm_name': StringProp(fill_brok
=['full_status']),
36 #'alias': {'required': True, 'fill_brok' : ['full_status']},
37 #'notes': {'required': False, 'default':'', 'fill_brok' : ['full_status']},
38 #'notes_url': {'required': False, 'default':'', 'fill_brok' : ['full_status']},
39 #'action_url': {'required': False, 'default':'', 'fill_brok' : ['full_status']},
40 'realm_members' : StringProp(default
=''),#No status_broker_name because it put hosts, not host_name
41 'higher_realms' : StringProp(default
=''),
42 'default' : BoolProp(default
='0'),
46 'REALMNAME' : 'realm_name',
47 'REALMMEMBERS' : 'members',
52 return self
.realm_name
56 return self
.realm_members
59 def add_string_member(self
, member
):
60 self
.realm_members
+= ','+member
63 def get_realm_members(self
):
64 if self
.has('realm_members'):
65 return self
.realm_members
.split(',')
70 #Use to make pyton properties
71 #TODO : change itemgroup function pythonize?
74 for prop
in cls
.properties
:
76 tab
= cls
.properties
[prop
]
78 old_val
= getattr(self
, prop
)
80 #print "Changing ", old_val, "by", new_val
81 setattr(self
, prop
, new_val
)
82 except AttributeError , exp
:
83 #print self.get_name(), ' : ', exp
84 pass # Will be catch at the is_correct moment
87 #We fillfull properties with template ones if need
88 #Because hostgroup we call may not have it's members
89 #we call get_hosts_by_explosion on it
90 def get_realms_by_explosion(self
, realms
):
91 #First we tag the hg so it will not be explode
92 #if a son of it already call it
93 self
.already_explode
= True
95 #Now the recursiv part
96 #rec_tag is set to False avery HG we explode
97 #so if True here, it must be a loop in HG
100 print "Error : we've got a loop in realm definition", self
.get_name()
101 if self
.has('members'):
105 #Ok, not a loop, we tag it and continue
108 p_mbrs
= self
.get_realm_members()
110 p
= realms
.find_by_name(p_mbr
.strip())
112 value
= p
.get_realms_by_explosion(realms
)
113 if value
is not None:
114 self
.add_string_member(value
)
116 if self
.has('members'):
122 def get_schedulers(self
):
124 for s
in self
.schedulers
:
129 def get_all_schedulers(self
):
131 for s
in self
.schedulers
:
133 for p
in self
.realms
:
134 tmps
= p
.get_all_schedulers()
140 def get_pollers(self
):
142 for p
in self
.pollers
:
147 def get_all_subs_pollers(self
):
148 r
= self
.get_pollers()
149 for p
in self
.realm_members
:
150 tmps
= p
.get_all_subs_pollers()
157 def get_reactionners(self
):
159 for p
in self
.reactionners
:
164 def get_all_subs_reactionners(self
):
165 r
= self
.get_reactionners()
166 for p
in self
.realm_members
:
167 tmps
= p
.get_all_subs_reactionners()
173 def count_reactionners(self
):
174 self
.nb_reactionners
= 0
175 for reactionner
in self
.reactionners
:
176 if not reactionner
.spare
:
177 self
.nb_reactionners
+= 1
178 for realm
in self
.higher_realms
:
179 for reactionner
in realm
.reactionners
:
180 if not reactionner
.spare
and reactionner
.manage_sub_realms
:
181 self
.nb_reactionners
+= 1
182 print self
.get_name(),"Count reactionners :", self
.nb_reactionners
185 def fill_potential_reactionners(self
):
186 self
.potential_reactionners
= []
187 for reactionner
in self
.reactionners
:
188 self
.potential_reactionners
.append(reactionner
)
189 for realm
in self
.higher_realms
:
190 for reactionner
in realm
.reactionners
:
191 if reactionner
.manage_sub_realms
:
192 self
.potential_reactionners
.append(reactionner
)
193 print self
.get_name(),"Add potential reactionners :", len(self
.potential_reactionners
)
196 def count_pollers(self
):
198 for poller
in self
.pollers
:
201 for realm
in self
.higher_realms
:
202 for poller
in realm
.pollers
:
203 if not poller
.spare
and poller
.manage_sub_realms
:
205 print self
.get_name(),"Count pollers :", self
.nb_pollers
208 def fill_potential_pollers(self
):
209 self
.potential_pollers
= []
210 for poller
in self
.pollers
:
211 self
.potential_pollers
.append(poller
)
212 for realm
in self
.higher_realms
:
213 for poller
in realm
.pollers
:
214 if poller
.manage_sub_realms
:
215 self
.potential_pollers
.append(poller
)
216 print self
.get_name(),"Add potential pollers :", len(self
.potential_pollers
)
219 def count_brokers(self
):
221 for broker
in self
.brokers
:
224 for realm
in self
.higher_realms
:
225 for broker
in realm
.brokers
:
226 if not broker
.spare
and broker
.manage_sub_realms
:
228 print self
.get_name(),"Count brokers :", self
.nb_brokers
231 def fill_potential_brokers(self
):
232 self
.potential_brokers
= []
233 for broker
in self
.brokers
:
234 self
.potential_brokers
.append(broker
)
235 for realm
in self
.higher_realms
:
236 for broker
in realm
.brokers
:
237 if broker
.manage_sub_realms
:
238 self
.potential_brokers
.append(broker
)
239 print self
.get_name(),"Add potential brokers :", len(self
.potential_brokers
)
242 #Return the list of satellites of a certain type
243 #like reactionner -> self.reactionners
244 def get_satellties_by_type(self
, type):
245 if hasattr(self
, type+'s'):
246 return getattr(self
, type+'s')
248 print "Sorry I do not have this kind of satellites : ", type
252 #Return the list of potentials satellites of a certain type
253 #like reactionner -> self.potential_reactionners
254 def get_potential_satellites_by_type(self
, type):
255 if hasattr(self
, 'potential_'+type+'s'):
256 return getattr(self
, 'potential_'+type+'s')
258 print "Sorry I do not have this kind of satellites : ", type
262 #Return the list of potentials satellites of a certain type
263 #like reactionner -> self.nb_reactionners
264 def get_nb_of_must_have_satellites(self
, type):
265 if hasattr(self
, 'nb_'+type+'s'):
266 return getattr(self
, 'nb_'+type+'s')
268 print "Sorry I do not have this kind of satellites : ", type
272 #Fill dict of realms for managing the satellites confs
273 def prepare_for_satellites_conf(self
):
274 self
.to_satellites
= {}
275 self
.to_satellites
['reactionner'] = {}
276 self
.to_satellites
['poller'] = {}
277 self
.to_satellites
['broker'] = {}
279 self
.to_satellites_nb_assigned
= {}
280 self
.to_satellites_nb_assigned
['reactionner'] = {}
281 self
.to_satellites_nb_assigned
['poller'] = {}
282 self
.to_satellites_nb_assigned
['broker'] = {}
284 self
.to_satellites_nb_assigned
= {}
285 self
.to_satellites_nb_assigned
['reactionner'] = {}
286 self
.to_satellites_nb_assigned
['poller'] = {}
287 self
.to_satellites_nb_assigned
['broker'] = {}
289 self
.to_satellites_need_dispatch
= {}
290 self
.to_satellites_need_dispatch
['reactionner'] = {}
291 self
.to_satellites_need_dispatch
['poller'] = {}
292 self
.to_satellites_need_dispatch
['broker'] = {}
294 self
.to_satellites_managed_by
= {}
295 self
.to_satellites_managed_by
['reactionner'] = {}
296 self
.to_satellites_managed_by
['poller'] = {}
297 self
.to_satellites_managed_by
['broker'] = {}
299 self
.count_reactionners()
300 self
.fill_potential_reactionners()
302 self
.fill_potential_pollers()
304 self
.fill_potential_brokers()
307 #TODO: find a better name...
308 #TODO : and if he goes active?
309 def fill_broker_with_poller_reactionner_links(self
, broker
):
310 #First we create/void theses links
311 broker
.cfg
['pollers'] = {}
312 broker
.cfg
['reactionners'] = {}
315 for p
in self
.get_pollers():
316 cfg
= p
.give_satellite_cfg()
317 broker
.cfg
['pollers'][p
.id] = cfg
319 for r
in self
.get_reactionners():
320 cfg
= r
.give_satellite_cfg()
321 broker
.cfg
['reactionners'][r
.id] = cfg
323 #Then sub if we must to it
324 if broker
.manage_sub_realms
:
326 for p
in self
.get_all_subs_pollers():
327 cfg
= p
.give_satellite_cfg()
328 broker
.cfg
['pollers'][p
.id] = cfg
331 for r
in self
.get_all_subs_reactionners():
332 cfg
= r
.give_satellite_cfg()
333 broker
.cfg
['reactionners'][r
.id] = cfg
335 print "***** Broker Me %s got a poller/reactionner link : %s and %s" % (broker
.get_name(), broker
.cfg
['pollers'], broker
.cfg
['reactionners'])
338 class Realms(Itemgroups
):
339 name_property
= "realm_name" # is used for finding hostgroups
344 return len(self
.itemgroups
)
347 def get_members_by_name(self
, pname
):
348 id = self
.find_id_by_name(pname
)
351 return self
.itemgroups
[id].get_realms()
355 self
.linkify_p_by_p()
356 #prepare list of satallites and confs
357 for p
in self
.itemgroups
.values():
366 #We just search for each realm the others realms
367 #and replace the name by the realm
368 def linkify_p_by_p(self
):
369 for p
in self
.itemgroups
.values():
370 mbrs
= p
.get_realm_members()
371 #The new member list, in id
374 new_mbr
= self
.find_by_name(mbr
)
376 new_mbrs
.append(new_mbr
)
377 #We find the id, we remplace the names
378 p
.realm_members
= new_mbrs
379 print "For realm", p
.get_name()
380 for m
in p
.realm_members
:
381 print "Member:", m
.get_name()
383 #Now put higher realm in sub realms
385 for p
in self
.itemgroups
.values():
388 for p
in self
.itemgroups
.values():
389 for sub_p
in p
.realm_members
:
390 sub_p
.higher_realms
.append(p
)
393 #Use to fill members with hostgroup_members
395 #We do not want a same hg to be explode again and again
397 for tmp_p
in self
.itemgroups
.values():
398 tmp_p
.already_explode
= False
399 for p
in self
.itemgroups
.values():
400 if p
.has('realm_members') and not p
.already_explode
:
401 #get_hosts_by_explosion is a recursive
402 #function, so we must tag hg so we do not loop
403 for tmp_p
in self
.itemgroups
.values():
404 tmp_p
.rec_tag
= False
405 p
.get_realms_by_explosion(self
)
408 for tmp_p
in self
.itemgroups
.values():
409 if hasattr(tmp_p
, 'rec_tag'):
411 del tmp_p
.already_explode
414 def get_default(self
):