/usr/lib/python2.7/dist-packages/shinken/dependencynode.py is in shinken-common 2.0.3-4.
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 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | #!/usr/bin/env 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 re
from shinken.log import logger
from shinken.util import filter_any, filter_none
from shinken.util import filter_host_by_name, filter_host_by_regex, filter_host_by_group, filter_host_by_template
from shinken.util import filter_service_by_name
from shinken.util import filter_service_by_regex_name
from shinken.util import filter_service_by_regex_host_name
from shinken.util import filter_service_by_host_name
from shinken.util import filter_service_by_bp_rule_label
from shinken.util import filter_service_by_hostgroup_name
from shinken.util import filter_service_by_host_template_name
from shinken.util import filter_service_by_servicegroup_name
from shinken.util import filter_host_by_bp_rule_label
from shinken.util import filter_service_by_host_bp_rule_label
"""
Here is a node class for dependency_node(s) and a factory to create them
"""
class DependencyNode(object):
def __init__(self):
self.operand = None
self.sons = []
# Of: values are a triple OK,WARN,CRIT
self.of_values = ('0', '0', '0')
self.is_of_mul = False
self.configuration_errors = []
self.not_value = False
def __str__(self):
return "Op:'%s' Val:'%s' Sons:'[%s]' IsNot:'%s'" % (self.operand, self.of_values, ','.join([str(s) for s in self.sons]), self.not_value)
def get_reverse_state(self, state):
# Warning is still warning
if state == 1:
return 1
if state == 0:
return 2
if state == 2:
return 0
# should not go here...
return state
# We will get the state of this node, by looking at the state of
# our sons, and apply our operand
def get_state(self):
#print "Ask state of me", self
# If we are a host or a service, wee just got the host/service
# hard state
if self.operand in ['host', 'service']:
return self.get_simple_node_state()
else:
return self.get_complex_node_state()
# Returns a simple node direct state (such as an host or a service). No
# calculation is needed
def get_simple_node_state(self):
state = self.sons[0].last_hard_state_id
#print "Get the hard state (%s) for the object %s" % (state, self.sons[0].get_name())
# Make DOWN look as CRITICAL (2 instead of 1)
if self.operand == 'host' and state == 1:
state = 2
# Maybe we are a NOT node, so manage this
if self.not_value:
# We inverse our states
if self.operand == 'host' and state == 1:
return 0
if self.operand == 'host' and state == 0:
return 1
# Critical -> OK
if self.operand == 'service' and state == 2:
return 0
# OK -> CRITICAL (warning is untouched)
if self.operand == 'service' and state == 0:
return 2
return state
# Calculates a complex node state based on its sons state, and its operator
def get_complex_node_state(self):
if self.operand == '|':
return self.get_complex_or_node_state()
elif self.operand == '&':
return self.get_complex_and_node_state()
# It's an Xof rule
else:
return self.get_complex_xof_node_state()
# Calculates a complex node state with an | operand
def get_complex_or_node_state(self):
# First we get the state of all our sons
states = [s.get_state() for s in self.sons]
# Next we calculate the best state
best_state = min(states)
# Then we handle eventual not value
if self.not_value:
return self.get_reverse_state(best_state)
return best_state
# Calculates a complex node state with an & operand
def get_complex_and_node_state(self):
# First we get the state of all our sons
states = [s.get_state() for s in self.sons]
# Next we calculate the worst state
if 2 in states:
worst_state = 2
else:
worst_state = max(states)
# Then we handle eventual not value
if self.not_value:
return self.get_reverse_state(worst_state)
return worst_state
# Calculates a complex node state with an Xof operand
def get_complex_xof_node_state(self):
# First we get the state of all our sons
states = [s.get_state() for s in self.sons]
# We search for OK, WARN or CRIT applications
# And we will choice between them
nb_search_ok = self.of_values[0]
nb_search_warn = self.of_values[1]
nb_search_crit = self.of_values[2]
# We look for each application
nb_sons = len(states)
nb_ok = len([s for s in states if s == 0])
nb_warn = len([s for s in states if s == 1])
nb_crit = len([s for s in states if s == 2])
#print "NB:", nb_ok, nb_warn, nb_crit
# Ok and Crit apply with their own values
# Warn can apply with warn or crit values
# so a W C can raise a Warning, but not enough for
# a critical
def get_state_for(nb_tot, nb_real, nb_search):
if nb_search.endswith('%'):
nb_search = int(nb_search[:-1])
if nb_search < 0:
# nb_search is negative, so +
nb_search = max(100 + nb_search, 0)
apply_for = float(nb_real) / nb_tot * 100 >= nb_search
else:
nb_search = int(nb_search)
if nb_search < 0:
# nb_search is negative, so +
nb_search = max(nb_tot + nb_search, 0)
apply_for = nb_real >= nb_search
return apply_for
ok_apply = get_state_for(nb_sons, nb_ok, nb_search_ok)
warn_apply = get_state_for(nb_sons, nb_warn + nb_crit, nb_search_warn)
crit_apply = get_state_for(nb_sons, nb_crit, nb_search_crit)
#print "What apply?", ok_apply, warn_apply, crit_apply
# return the worst state that apply
if crit_apply:
if self.not_value:
return self.get_reverse_state(2)
return 2
if warn_apply:
if self.not_value:
return self.get_reverse_state(1)
return 1
if ok_apply:
if self.not_value:
return self.get_reverse_state(0)
return 0
# Maybe even OK is not possible, if so, it depends if the admin
# ask a simple form Xof: or a multiple one A,B,Cof:
# the simple should give OK, the mult should give the worst state
if self.is_of_mul:
#print "Is mul, send 0"
if self.not_value:
return self.get_reverse_state(0)
return 0
else:
#print "not mul, return worst", worse_state
if 2 in states:
worst_state = 2
else:
worst_state = max(states)
if self.not_value:
return self.get_reverse_state(worst_state)
return worst_state
# return a list of all host/service in our node and below
def list_all_elements(self):
r = []
# We are a host/service
if self.operand in ['host', 'service']:
return [self.sons[0]]
for s in self.sons:
r.extend(s.list_all_elements())
# and uniq the result
return list(set(r))
# If we are a of: rule, we can get some 0 in of_values,
# if so, change them with NB sons instead
def switch_zeros_of_values(self):
nb_sons = len(self.sons)
# Need a list for assignment
self.of_values = list(self.of_values)
for i in [0, 1, 2]:
if self.of_values[i] == '0':
self.of_values[i] = str(nb_sons)
self.of_values = tuple(self.of_values)
# Check for empty (= not found) leaf nodes
def is_valid(self):
valid = True
if not self.sons:
valid = False
else:
for s in self.sons:
if isinstance(s, DependencyNode) and not s.is_valid():
self.configuration_errors.extend(s.configuration_errors)
valid = False
return valid
""" TODO: Add some comment about this class for the doc"""
class DependencyNodeFactory(object):
host_flags = "grlt"
service_flags = "grl"
def __init__(self, bound_item):
self.bound_item = bound_item
# the () will be eval in a recursiv way, only one level of ()
def eval_cor_pattern(self, pattern, hosts, services, running=False):
pattern = pattern.strip()
#print "***** EVAL ", pattern
complex_node = False
# Look if it's a complex pattern (with rule) or
# if it's a leaf ofit, like a host/service
for m in '()+&|':
if m in pattern:
complex_node = True
# If it's a simple node, evaluate it directly
if complex_node is False:
return self.eval_simple_cor_pattern(pattern, hosts, services, running)
else:
return self.eval_complex_cor_pattern(pattern, hosts, services, running)
# Checks if an expression is an Xof pattern, and parses its components if
# so. In such a case, once parsed, returns the cleaned patten.
def eval_xof_pattern(self, node, pattern):
p = "^(-?\d+%?),*(-?\d*%?),*(-?\d*%?) *of: *(.+)"
r = re.compile(p)
m = r.search(pattern)
if m is not None:
#print "Match the of: thing N=", m.groups()
node.operand = 'of:'
g = m.groups()
# We can have a Aof: rule, or a multiple A,B,Cof: rule.
mul_of = (g[1] != u'' and g[2] != u'')
# If multi got (A,B,C)
if mul_of:
node.is_of_mul = True
node.of_values = (g[0], g[1], g[2])
else: # if not, use A,0,0, we will change 0 after to put MAX
node.of_values = (g[0], '0', '0')
pattern = m.groups()[3]
return pattern
# Evaluate a complex correlation expression, such as an &, |, nested
# expressions in par, and so on.
def eval_complex_cor_pattern(self, pattern, hosts, services, running=False):
node = DependencyNode()
pattern = self.eval_xof_pattern(node, pattern)
in_par = False
tmp = ''
son_is_not = False # We keep is the next son will be not or not
stacked_par = 0
for c in pattern:
if c == '(':
stacked_par += 1
#print "INCREASING STACK TO", stacked_par
in_par = True
tmp = tmp.strip()
# Maybe we just start a par, but we got some things in tmp
# that should not be good in fact !
if stacked_par == 1 and tmp != '':
#TODO : real error
print "ERROR : bad expression near", tmp
continue
# If we are already in a par, add this (
# but not if it's the first one so
if stacked_par > 1:
tmp += c
elif c == ')':
#print "Need closeing a sub expression?", tmp
stacked_par -= 1
if stacked_par < 0:
# TODO : real error
print "Error : bad expression near", tmp, "too much ')'"
continue
if stacked_par == 0:
#print "THIS is closing a sub compress expression", tmp
tmp = tmp.strip()
o = self.eval_cor_pattern(tmp, hosts, services, running)
# Maybe our son was notted
if son_is_not:
o.not_value = True
son_is_not = False
node.sons.append(o)
in_par = False
# OK now clean the tmp so we start clean
tmp = ''
continue
# ok here we are still in a huge par, we just close one sub one
tmp += c
# Expressions in par will be parsed in a sub node after. So just
# stack pattern
elif in_par:
tmp += c
# Until here, we're not in par
# Manage the NOT for an expression. Only allow ! at the beginning
# of an host or an host,service expression.
elif c == '!':
tmp = tmp.strip()
if tmp and tmp[0] != '!':
print "Error : bad expression near", tmp, "wrong position for '!'"
continue
# Flags next node not state
son_is_not = True
# DO NOT keep the c in tmp, we consumed it
#print "MATCHING", c, pattern
elif c == '&' or c == '|':
# Oh we got a real cut in an expression, if so, cut it
#print "REAL & for cutting"
tmp = tmp.strip()
# Look at the rule viability
if node.operand is not None and node.operand != 'of:' and c != node.operand:
# Should be logged as a warning / info? :)
return None
if node.operand != 'of:':
node.operand = c
if tmp != '':
#print "Will analyse the current str", tmp
o = self.eval_cor_pattern(tmp, hosts, services, running)
# Maybe our son was notted
if son_is_not:
o.not_value = True
son_is_not = False
node.sons.append(o)
tmp = ''
# Maybe it's a classic character or we're in par, if so, continue
else:
tmp += c
# Be sure to manage the trainling part when the line is done
tmp = tmp.strip()
if tmp != '':
#print "Managing trainling part", tmp
o = self.eval_cor_pattern(tmp, hosts, services, running)
# Maybe our son was notted
if son_is_not:
o.not_value = True
son_is_not = False
#print "4end I've %s got new sons" % pattern , o
node.sons.append(o)
# We got our nodes, so we can update 0 values of of_values
# with the number of sons
node.switch_zeros_of_values()
return node
# Evaluate a simple correlation expression, such as an host, an host + a
# service, or expand an host or service expression.
def eval_simple_cor_pattern(self, pattern, hosts, services, running=False):
node = DependencyNode()
pattern = self.eval_xof_pattern(node, pattern)
#print "Try to find?", pattern
# If it's a not value, tag the node and find
# the name without this ! operator
if pattern.startswith('!'):
node.not_value = True
pattern = pattern[1:]
# Is the pattern an expression to be expanded?
if re.search(r"^([%s]+|\*):" % self.host_flags, pattern) or \
re.search(r",\s*([%s]+:.*|\*)$" % self.service_flags, pattern):
# o is just extracted its attributes, then trashed.
o = self.expand_expression(pattern, hosts, services, running)
if node.operand != 'of:':
node.operand = '&'
node.sons.extend(o.sons)
node.configuration_errors.extend(o.configuration_errors)
node.switch_zeros_of_values()
else:
node.operand = 'object'
obj, error = self.find_object(pattern, hosts, services)
if obj is not None:
# Set host or service
node.operand = obj.__class__.my_type
node.sons.append(obj)
else:
if running is False:
node.configuration_errors.append(error)
else:
# As business rules are re-evaluated at run time on
# each scheduling loop, if the rule becomes invalid
# because of a badly written macro modulation, it
# should be notified upper for the error to be
# displayed in the check output.
raise Exception(error)
return node
# We've got an object, like h1,db1 that mean the
# db1 service of the host db1, or just h1, that mean
# the host h1.
def find_object(self, pattern, hosts, services):
#print "Finding object", pattern
obj = None
error = None
is_service = False
# h_name, service_desc are , separated
elts = pattern.split(',')
host_name = elts[0].strip()
# If host_name is empty, use the host_name the business rule is bound to
if not host_name:
host_name = self.bound_item.host_name
# Look if we have a service
if len(elts) > 1:
is_service = True
service_description = elts[1].strip()
if is_service:
obj = services.find_srv_by_name_and_hostname(host_name, service_description)
if not obj:
error = "Business rule uses unknown service %s/%s" % (host_name, service_description)
else:
obj = hosts.find_by_name(host_name)
if not obj:
error = "Business rule uses unknown host %s" % (host_name,)
return obj, error
# Tries to expand a host or service expression into a dependency node tree
# using (host|service)group membership, regex, or labels as item selector.
def expand_expression(self, pattern, hosts, services, running=False):
error = None
node = DependencyNode()
node.operand = '&'
elts = [e.strip() for e in pattern.split(',')]
# If host_name is empty, use the host_name the business rule is bound to
if not elts[0]:
elts[0] = self.bound_item.host_name
filters = []
# Looks for hosts/services using appropriate filters
try:
if len(elts) > 1:
# We got a service expression
host_expr, service_expr = elts
filters.extend(self.get_srv_host_filters(host_expr))
filters.extend(self.get_srv_service_filters(service_expr))
items = services.find_by_filter(filters)
else:
# We got an host expression
host_expr = elts[0]
filters.extend(self.get_host_filters(host_expr))
items = hosts.find_by_filter(filters)
except re.error, e:
error = "Business rule uses invalid regex %s: %s" % (pattern, e)
else:
if not items:
error = "Business rule got an empty result for pattern %s" % pattern
# Checks if we got result
if error:
if running is False:
node.configuration_errors.append(error)
else:
# As business rules are re-evaluated at run time on
# each scheduling loop, if the rule becomes invalid
# because of a badly written macro modulation, it
# should be notified upper for the error to be
# displayed in the check output.
raise Exception(error)
return node
# Creates dependency node subtree
for item in items:
# Creates a host/service node
son = DependencyNode()
son.operand = item.__class__.my_type
son.sons.append(item)
# Appends it to wrapping node
node.sons.append(son)
node.switch_zeros_of_values()
return node
# Generates filter list on a hosts host_name
def get_host_filters(self, expr):
if expr == "*":
return [filter_any]
match = re.search(r"^([%s]+):(.*)" % self.host_flags, expr)
if match is None:
return [filter_host_by_name(expr)]
flags, expr = match.groups()
if "g" in flags:
return [filter_host_by_group(expr)]
elif "r" in flags:
return [filter_host_by_regex(expr)]
elif "l" in flags:
return [filter_host_by_bp_rule_label(expr)]
elif "t" in flags:
return [filter_host_by_template(expr)]
else:
return [filter_none]
# Generates filter list on services host_name
def get_srv_host_filters(self, expr):
if expr == "*":
return [filter_any]
match = re.search(r"^([%s]+):(.*)" % self.host_flags, expr)
if match is None:
return [filter_service_by_host_name(expr)]
flags, expr = match.groups()
if "g" in flags:
return [filter_service_by_hostgroup_name(expr)]
elif "r" in flags:
return [filter_service_by_regex_host_name(expr)]
elif "l" in flags:
return [filter_service_by_host_bp_rule_label(expr)]
elif "t" in flags:
return [filter_service_by_host_template_name(expr)]
else:
return [filter_none]
# Generates filter list on services service_description
def get_srv_service_filters(self, expr):
if expr == "*":
return [filter_any]
match = re.search(r"^([%s]+):(.*)" % self.service_flags, expr)
if match is None:
return [filter_service_by_name(expr)]
flags, expr = match.groups()
if "g" in flags:
return [filter_service_by_servicegroup_name(expr)]
elif "r" in flags:
return [filter_service_by_regex_name(expr)]
elif "l" in flags:
return [filter_service_by_bp_rule_label(expr)]
else:
return [filter_none]
|