/usr/lib/python2.7/dist-packages/shinken/objects/realm.py is in shinken-common 1.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | #!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2009-2012:
# Gabes Jean, naparuba@gmail.com
# Gerhard Lausser, Gerhard.Lausser@consol.de
# Gregory Starck, g.starck@gmail.com
# Hartmut Goebel, h.goebel@goebel-consult.de
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.
import copy
from item import Item
from itemgroup import Itemgroup, Itemgroups
from shinken.property import BoolProp, IntegerProp, StringProp
from shinken.log import logger
# It change from hostgroup Class because there is no members
# properties, just the realm_members that we rewrite on it.
class Realm(Itemgroup):
id = 1 # zero is always a little bit special... like in database
my_type = 'realm'
properties = Itemgroup.properties.copy()
properties.update({
'id': IntegerProp(default=0, fill_brok=['full_status']),
'realm_name': StringProp(fill_brok=['full_status']),
'realm_members': StringProp(default=''), # No status_broker_name because it put hosts, not host_name
'higher_realms': StringProp(default=''),
'default': BoolProp(default='0'),
'broker_complete_links': BoolProp(default='0'),
#'alias': {'required': True, 'fill_brok': ['full_status']},
#'notes': {'required': False, 'default':'', 'fill_brok': ['full_status']},
#'notes_url': {'required': False, 'default':'', 'fill_brok': ['full_status']},
#'action_url': {'required': False, 'default':'', 'fill_brok': ['full_status']},
})
running_properties = Item.running_properties.copy()
running_properties.update({
'serialized_confs': StringProp(default={}),
})
macros = {
'REALMNAME': 'realm_name',
'REALMMEMBERS': 'members',
}
def get_name(self):
return self.realm_name
def get_realms(self):
return self.realm_members
def add_string_member(self, member):
self.realm_members += ',' + member
def get_realm_members(self):
if self.has('realm_members'):
return [r.strip() for r in self.realm_members.split(',')]
else:
return []
# Use to make python properties
# TODO: change itemgroup function pythonize?
def pythonize(self):
cls = self.__class__
for prop, tab in cls.properties.items():
try:
old_val = getattr(self, prop)
new_val = tab.pythonize(old_val)
#print "Changing ", old_val, "by", new_val
setattr(self, prop, new_val)
except AttributeError, exp:
pass # Will be catch at the is_correct moment
# We fillfull properties with template ones if need
# Because hostgroup we call may not have it's members
# we call get_hosts_by_explosion on it
def get_realms_by_explosion(self, realms):
# First we tag the hg so it will not be explode
# if a son of it already call it
self.already_explode = True
# Now the recursive part
# rec_tag is set to False every HG we explode
# so if True here, it must be a loop in HG
# calls... not GOOD!
if self.rec_tag:
err = "Error: we've got a loop in realm definition %s" % self.get_name()
self.configuration_errors.append(err)
if self.has('members'):
return self.members
else:
return ''
# Ok, not a loop, we tag it and continue
self.rec_tag = True
p_mbrs = self.get_realm_members()
for p_mbr in p_mbrs:
p = realms.find_by_name(p_mbr.strip())
if p is not None:
value = p.get_realms_by_explosion(realms)
if value is not None:
self.add_string_member(value)
if self.has('members'):
return self.members
else:
return ''
def get_all_subs_pollers(self):
r = copy.copy(self.pollers)
for p in self.realm_members:
tmps = p.get_all_subs_pollers()
for s in tmps:
r.append(s)
return r
def get_all_subs_reactionners(self):
r = copy.copy(self.reactionners)
for p in self.realm_members:
tmps = p.get_all_subs_reactionners()
for s in tmps:
r.append(s)
return r
def count_reactionners(self):
self.nb_reactionners = 0
for reactionner in self.reactionners:
if not reactionner.spare:
self.nb_reactionners += 1
for realm in self.higher_realms:
for reactionner in realm.reactionners:
if not reactionner.spare and reactionner.manage_sub_realms:
self.nb_reactionners += 1
def fill_potential_reactionners(self):
self.potential_reactionners = []
for reactionner in self.reactionners:
self.potential_reactionners.append(reactionner)
for realm in self.higher_realms:
for reactionner in realm.reactionners:
if reactionner.manage_sub_realms:
self.potential_reactionners.append(reactionner)
def count_pollers(self):
self.nb_pollers = 0
for poller in self.pollers:
if not poller.spare:
self.nb_pollers += 1
for realm in self.higher_realms:
for poller in realm.pollers:
if not poller.spare and poller.manage_sub_realms:
self.nb_pollers += 1
def fill_potential_pollers(self):
self.potential_pollers = []
for poller in self.pollers:
self.potential_pollers.append(poller)
for realm in self.higher_realms:
for poller in realm.pollers:
if poller.manage_sub_realms:
self.potential_pollers.append(poller)
def count_brokers(self):
self.nb_brokers = 0
for broker in self.brokers:
if not broker.spare:
self.nb_brokers += 1
for realm in self.higher_realms:
for broker in realm.brokers:
if not broker.spare and broker.manage_sub_realms:
self.nb_brokers += 1
def fill_potential_brokers(self):
self.potential_brokers = []
for broker in self.brokers:
self.potential_brokers.append(broker)
for realm in self.higher_realms:
for broker in realm.brokers:
if broker.manage_sub_realms:
self.potential_brokers.append(broker)
def count_receivers(self):
self.nb_receivers = 0
for receiver in self.receivers:
if not receiver.spare:
self.nb_receivers += 1
for realm in self.higher_realms:
for receiver in realm.receivers:
if not receiver.spare and receiver.manage_sub_realms:
self.nb_receivers += 1
def fill_potential_receivers(self):
self.potential_receivers = []
for broker in self.receivers:
self.potential_receivers.append(broker)
for realm in self.higher_realms:
for broker in realm.receivers:
if broker.manage_sub_realms:
self.potential_receivers.append(broker)
# Return the list of satellites of a certain type
# like reactionner -> self.reactionners
def get_satellties_by_type(self, type):
if hasattr(self, type + 's'):
return getattr(self, type + 's')
else:
logger.debug("[realm] do not have this kind of satellites: %s" % type)
return []
# Return the list of potentials satellites of a certain type
# like reactionner -> self.potential_reactionners
def get_potential_satellites_by_type(self, type):
if hasattr(self, 'potential_' + type + 's'):
return getattr(self, 'potential_' + type + 's')
else:
logger.debug("[realm] do not have this kind of satellites: %s" % type)
return []
# Return the list of potentials satellites of a certain type
# like reactionner -> self.nb_reactionners
def get_nb_of_must_have_satellites(self, type):
if hasattr(self, 'nb_' + type + 's'):
return getattr(self, 'nb_' + type + 's')
else:
logger.debug("[realm] do not have this kind of satellites: %s" % type)
return 0
# Fill dict of realms for managing the satellites confs
def prepare_for_satellites_conf(self):
self.to_satellites = {}
self.to_satellites['reactionner'] = {}
self.to_satellites['poller'] = {}
self.to_satellites['broker'] = {}
self.to_satellites['receiver'] = {}
self.to_satellites_need_dispatch = {}
self.to_satellites_need_dispatch['reactionner'] = {}
self.to_satellites_need_dispatch['poller'] = {}
self.to_satellites_need_dispatch['broker'] = {}
self.to_satellites_need_dispatch['receiver'] = {}
self.to_satellites_managed_by = {}
self.to_satellites_managed_by['reactionner'] = {}
self.to_satellites_managed_by['poller'] = {}
self.to_satellites_managed_by['broker'] = {}
self.to_satellites_managed_by['receiver'] = {}
self.count_reactionners()
self.fill_potential_reactionners()
self.count_pollers()
self.fill_potential_pollers()
self.count_brokers()
self.fill_potential_brokers()
self.count_receivers()
self.fill_potential_receivers()
s = "%s: (in/potential) (schedulers:%d) (pollers:%d/%d) (reactionners:%d/%d) (brokers:%d/%d) (receivers:%d/%d)" % \
(self.get_name(),
len(self.schedulers),
self.nb_pollers, len(self.potential_pollers),
self.nb_reactionners, len(self.potential_reactionners),
self.nb_brokers, len(self.potential_brokers),
self.nb_receivers, len(self.potential_receivers)
)
logger.info(s)
# TODO: find a better name...
# TODO: and if he goes active?
def fill_broker_with_poller_reactionner_links(self, broker):
# First we create/void theses links
broker.cfg['pollers'] = {}
broker.cfg['reactionners'] = {}
# First our own level
for p in self.pollers:
cfg = p.give_satellite_cfg()
broker.cfg['pollers'][p.id] = cfg
for r in self.reactionners:
cfg = r.give_satellite_cfg()
broker.cfg['reactionners'][r.id] = cfg
# Then sub if we must to it
if broker.manage_sub_realms:
# Now pollers
for p in self.get_all_subs_pollers():
cfg = p.give_satellite_cfg()
broker.cfg['pollers'][p.id] = cfg
# Now reactionners
for r in self.get_all_subs_reactionners():
cfg = r.give_satellite_cfg()
broker.cfg['reactionners'][r.id] = cfg
# Get a conf package of satellites links that can be useful for
# a scheduler
def get_satellites_links_for_scheduler(self):
cfg = {}
# First we create/void theses links
cfg['pollers'] = {}
cfg['reactionners'] = {}
# First our own level
for p in self.pollers:
c = p.give_satellite_cfg()
cfg['pollers'][p.id] = c
for r in self.reactionners:
c = r.give_satellite_cfg()
cfg['reactionners'][r.id] = c
#print "***** Preparing a satellites conf for a scheduler", cfg
return cfg
class Realms(Itemgroups):
name_property = "realm_name" # is used for finding hostgroups
inner_class = Realm
def get_members_by_name(self, pname):
realm = self.find_by_name(pname)
if realm is None:
return []
return realm.get_realms()
def linkify(self):
self.linkify_p_by_p()
# prepare list of satellites and confs
for p in self:
p.pollers = []
p.schedulers = []
p.reactionners = []
p.brokers = []
p.receivers = []
p.packs = []
p.confs = {}
# We just search for each realm the others realms
# and replace the name by the realm
def linkify_p_by_p(self):
for p in self.items.values():
mbrs = p.get_realm_members()
# The new member list, in id
new_mbrs = []
for mbr in mbrs:
new_mbr = self.find_by_name(mbr)
if new_mbr is not None:
new_mbrs.append(new_mbr)
# We find the id, we replace the names
p.realm_members = new_mbrs
# Now put higher realm in sub realms
# So after they can
for p in self.items.values():
p.higher_realms = []
for p in self.items.values():
for sub_p in p.realm_members:
sub_p.higher_realms.append(p)
# Use to fill members with hostgroup_members
def explode(self):
# We do not want a same hg to be explode again and again
# so we tag it
for tmp_p in self.items.values():
tmp_p.already_explode = False
for p in self:
if p.has('realm_members') and not p.already_explode:
# get_hosts_by_explosion is a recursive
# function, so we must tag hg so we do not loop
for tmp_p in self:
tmp_p.rec_tag = False
p.get_realms_by_explosion(self)
# We clean the tags
for tmp_p in self.items.values():
if hasattr(tmp_p, 'rec_tag'):
del tmp_p.rec_tag
del tmp_p.already_explode
def get_default(self):
for r in self:
if getattr(r, 'default', False):
return r
return None
def prepare_for_satellites_conf(self):
for r in self:
r.prepare_for_satellites_conf()
|