/usr/lib/python2.7/dist-packages/shinken/objects/servicegroup.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 | #!/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/>.
from itemgroup import Itemgroup, Itemgroups
from shinken.property import StringProp
from shinken.log import logger
class Servicegroup(Itemgroup):
id = 1 # zero is always a little bit special... like in database
my_type = 'servicegroup'
properties = Itemgroup.properties.copy()
properties.update({
'id': StringProp(default=0, fill_brok=['full_status']),
'servicegroup_name': StringProp(fill_brok=['full_status']),
'alias': StringProp(fill_brok=['full_status']),
'notes': StringProp(default='', fill_brok=['full_status']),
'notes_url': StringProp(default='', fill_brok=['full_status']),
'action_url': StringProp(default='', fill_brok=['full_status']),
})
macros = {
'SERVICEGROUPALIAS': 'alias',
'SERVICEGROUPMEMBERS': 'members',
'SERVICEGROUPNOTES': 'notes',
'SERVICEGROUPNOTESURL': 'notes_url',
'SERVICEGROUPACTIONURL': 'action_url'
}
def get_services(self):
if self.has('members'):
return self.members
else:
return ''
def get_name(self):
return self.servicegroup_name
def get_servicegroup_members(self):
if self.has('servicegroup_members'):
return self.servicegroup_members.split(',')
else:
return []
# 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_services_by_explosion(self, servicegroups):
# 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:
logger.error("[servicegroup::%s] got a loop in servicegroup definition" % self.get_name())
if self.has('members'):
return self.members
else:
return ''
# Ok, not a loop, we tag it and continue
self.rec_tag = True
sg_mbrs = self.get_servicegroup_members()
for sg_mbr in sg_mbrs:
sg = servicegroups.find_by_name(sg_mbr.strip())
if sg is not None:
value = sg.get_services_by_explosion(servicegroups)
if value is not None:
self.add_string_member(value)
if self.has('members'):
return self.members
else:
return ''
class Servicegroups(Itemgroups):
name_property = "servicegroup_name" # is used for finding servicegroup
inner_class = Servicegroup
def linkify(self, services):
self.linkify_sg_by_srv(services)
# We just search for each host the id of the host
# and replace the name by the id
# TODO: very slow for hight services, so search with host list,
# not service one
def linkify_sg_by_srv(self, services):
for sg in self:
mbrs = sg.get_services()
# The new member list, in id
new_mbrs = []
seek = 0
host_name = ''
if (len(mbrs) == 1):
sg.unknown_members.append('%s' % mbrs[0])
for mbr in mbrs:
if seek % 2 == 0:
host_name = mbr.strip()
else:
service_desc = mbr.strip()
find = services.find_srv_by_name_and_hostname(host_name, service_desc)
if find is not None:
new_mbrs.append(find)
else:
sg.unknown_members.append('%s,%s' % (host_name, service_desc))
seek += 1
# Make members uniq
new_mbrs = list(set(new_mbrs))
# We find the id, we replace the names
sg.replace_members(new_mbrs)
for s in sg.members:
s.servicegroups.append(sg)
# and make this uniq
s.servicegroups = list(set(s.servicegroups))
# Add a service string to a service member
# if the service group do not exist, create it
def add_member(self, cname, sgname):
sg = self.find_by_name(sgname)
# if the id do not exist, create the cg
if sg is None:
sg = Servicegroup({'servicegroup_name': sgname, 'alias': sgname, 'members': cname})
self.add(sg)
else:
sg.add_string_member(cname)
# Use to fill members with contactgroup_members
def explode(self):
# We do not want a same hg to be explode again and again
# so we tag it
for sg in self:
sg.already_explode = False
for sg in self:
if sg.has('servicegroup_members') and not sg.already_explode:
# get_services_by_explosion is a recursive
# function, so we must tag hg so we do not loop
for sg2 in self:
sg2.rec_tag = False
sg.get_services_by_explosion(self)
# We clean the tags
for sg in self:
try:
del sg.rec_tag
except AttributeError:
pass
del sg.already_explode
|