Enh: (Grégory Starck) still code clean for pyro wrapper in satellitelinks.
[shinken.git] / shinken / realm.py
blobbafd6fa4fda5d71d3f2f8c502db0a0b0952479cb
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 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
32 my_type = 'realm'
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'),
45 macros = {
46 'REALMNAME' : 'realm_name',
47 'REALMMEMBERS' : 'members',
51 def get_name(self):
52 return self.realm_name
55 def get_realms(self):
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(',')
66 else:
67 return []
70 #Use to make pyton properties
71 #TODO : change itemgroup function pythonize?
72 def pythonize(self):
73 cls = self.__class__
74 for prop in cls.properties:
75 try:
76 tab = cls.properties[prop]
77 f = tab.pythonize
78 old_val = getattr(self, prop)
79 new_val = f(old_val)
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
98 #calls... not GOOD!
99 if self.rec_tag:
100 print "Error : we've got a loop in realm definition", self.get_name()
101 if self.has('members'):
102 return self.members
103 else:
104 return ''
105 #Ok, not a loop, we tag it and continue
106 self.rec_tag = True
108 p_mbrs = self.get_realm_members()
109 for p_mbr in p_mbrs:
110 p = realms.find_by_name(p_mbr.strip())
111 if p is not None:
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'):
117 return self.members
118 else:
119 return ''
122 def get_schedulers(self):
123 r = []
124 for s in self.schedulers:
125 r.append(s)
126 return r
129 def get_all_schedulers(self):
130 r = []
131 for s in self.schedulers:
132 r.append(s)
133 for p in self.realms:
134 tmps = p.get_all_schedulers()
135 for s in tmps:
136 r.append(s)
137 return r
140 def get_pollers(self):
141 r = []
142 for p in self.pollers:
143 r.append(p)
144 return r
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()
151 for s in tmps:
152 r.append(s)
153 return r
157 def get_reactionners(self):
158 r = []
159 for p in self.reactionners:
160 r.append(p)
161 return r
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()
168 for s in tmps:
169 r.append(s)
170 return r
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):
197 self.nb_pollers = 0
198 for poller in self.pollers:
199 if not poller.spare:
200 self.nb_pollers += 1
201 for realm in self.higher_realms:
202 for poller in realm.pollers:
203 if not poller.spare and poller.manage_sub_realms:
204 self.nb_pollers += 1
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):
220 self.nb_brokers = 0
221 for broker in self.brokers:
222 if not broker.spare:
223 self.nb_brokers += 1
224 for realm in self.higher_realms:
225 for broker in realm.brokers:
226 if not broker.spare and broker.manage_sub_realms:
227 self.nb_brokers += 1
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')
247 else:
248 print "Sorry I do not have this kind of satellites : ", type
249 return []
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')
257 else:
258 print "Sorry I do not have this kind of satellites : ", type
259 return []
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')
267 else:
268 print "Sorry I do not have this kind of satellites : ", type
269 return 0
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()
301 self.count_pollers()
302 self.fill_potential_pollers()
303 self.count_brokers()
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'] = {}
314 #First our own level
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:
325 #Now pollers
326 for p in self.get_all_subs_pollers():
327 cfg = p.give_satellite_cfg()
328 broker.cfg['pollers'][p.id] = cfg
330 #Now reactionners
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
340 inner_class = Realm
343 def __len__(self):
344 return len(self.itemgroups)
347 def get_members_by_name(self, pname):
348 id = self.find_id_by_name(pname)
349 if id == None:
350 return []
351 return self.itemgroups[id].get_realms()
354 def linkify(self):
355 self.linkify_p_by_p()
356 #prepare list of satallites and confs
357 for p in self.itemgroups.values():
358 p.pollers = []
359 p.schedulers = []
360 p.reactionners = []
361 p.brokers = []
362 p.packs = []
363 p.confs = {}
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
372 new_mbrs = []
373 for mbr in mbrs:
374 new_mbr = self.find_by_name(mbr)
375 if new_mbr != None:
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
384 #So after they can
385 for p in self.itemgroups.values():
386 p.higher_realms = []
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
394 def explode(self):
395 #We do not want a same hg to be explode again and again
396 #so we tag it
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)
407 #We clean the tags
408 for tmp_p in self.itemgroups.values():
409 if hasattr(tmp_p, 'rec_tag'):
410 del tmp_p.rec_tag
411 del tmp_p.already_explode
414 def get_default(self):
415 for r in self:
416 if r.default:
417 return r
418 return None