/usr/lib/python2.7/dist-packages/shinken/objects/itemgroup.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 | #!/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/>.
# And itemgroup is like a item, but it's a group of items :)
from item import Item, Items
from shinken.brok import Brok
from shinken.property import StringProp
from shinken.log import logger
# TODO: subclass Item & Items for Itemgroup & Itemgroups?
class Itemgroup(Item):
id = 0
properties = Item.properties.copy()
properties.update({
'members': StringProp(fill_brok=['full_status']),
# Shinken specific
'unknown_members': StringProp(default=[]),
})
def __init__(self, params={}):
self.id = self.__class__.id
self.__class__.id += 1
self.init_running_properties()
for key in params:
# delistify attributes if there is only one value
params[key] = self.compact_unique_attr_value(params[key])
setattr(self, key, params[key])
# Copy the groups properties EXCEPT the members
# members need to be fill after manually
def copy_shell(self):
cls = self.__class__
old_id = cls.id
new_i = cls() # create a new group
new_i.id = self.id # with the same id
cls.id = old_id # Reset the Class counter
# Copy all properties
for prop in cls.properties:
if prop is not 'members':
if self.has(prop):
val = getattr(self, prop)
setattr(new_i, prop, val)
# but no members
new_i.members = []
return new_i
# Change the members like item1 ,item2 to ['item1' , 'item2']
# so a python list :)
# We also strip elements because spaces Stinks!
def pythonize(self):
v = getattr(self, 'members', '')
# Maybe it's a multi-property like multi hostgroups entries
# if so "flatten" it
if isinstance(v, list):
v = ','.join(v)
self.members = [mbr for mbr in
(m.strip() for m in v.split(','))
if mbr != '']
def replace_members(self, members):
self.members = members
# If a prop is absent and is not required, put the default value
def fill_default(self):
cls = self.__class__
for prop, entry in cls.properties.items():
if not hasattr(self, prop) and not entry.required:
value = entry.default
setattr(self, prop, value)
def add_string_member(self, member):
if hasattr(self, 'members'):
self.members += ',' + member
else:
self.members = member
def __str__(self):
return str(self.__dict__) + '\n'
def __iter__(self):
return self.members.__iter__()
def __delitem__(self, i):
try:
self.members.remove(i)
except ValueError:
pass
# a item group is correct if all members actually exists,
# so if unknown_members is still []
def is_correct(self):
res = True
if self.unknown_members != []:
for m in self.unknown_members:
logger.error("[itemgroup::%s] as %s, got unknown member %s" % (self.get_name(), self.__class__.my_type, m))
res = False
if self.configuration_errors != []:
for err in self.configuration_errors:
logger.error("[itemgroup] %s" % err)
res = False
return res
def has(self, prop):
return hasattr(self, prop)
# Get a brok with hostgroup info (like id, name)
# members is special: list of (id, host_name) for database info
def get_initial_status_brok(self):
cls = self.__class__
data = {}
# Now config properties
for prop, entry in cls.properties.items():
if entry.fill_brok != []:
if self.has(prop):
data[prop] = getattr(self, prop)
# Here members is just a bunch of host, I need name in place
data['members'] = []
for i in self.members:
# it look like lisp! ((( ..))), sorry....
data['members'].append((i.id, i.get_name()))
b = Brok('initial_' + cls.my_type + '_status', data)
return b
class Itemgroups(Items):
# If a prop is absent and is not required, put the default value
def fill_default(self):
for i in self:
i.fill_default()
def add(self, ig):
self.items[ig.id] = ig
def get_members_by_name(self, gname):
g = self.find_by_name(gname)
if g is None:
return []
return getattr(g, 'members', [])
|