152 lines
No EOL
5 KiB
Python
152 lines
No EOL
5 KiB
Python
"""
|
|
Copyright (c) 2011-2013 "Johannes Findeisen and Rafael Timmerberg"
|
|
|
|
This file is part of Linspector (http://linspector.org).
|
|
|
|
Linspector 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.
|
|
|
|
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
"""
|
|
|
|
from logging import getLogger
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
class HostGroupException(Exception):
|
|
def __init__(self, msg):
|
|
self.msg = msg
|
|
|
|
def __str__(self):
|
|
return repr(self.msg)
|
|
|
|
|
|
class HostGroupMissingArgumentException(HostGroupException):
|
|
def __init__(self, missingArgument, hostgroupName):
|
|
super(HostGroupMissingArgumentException, self).__init__("no " + missingArgument + " defined for Hostgroup " + hostgroupName)
|
|
|
|
|
|
class HostGroup(object):
|
|
def __init__(self, name, **kwargs):
|
|
self.name = name
|
|
tmp = "members"
|
|
self.members = []
|
|
if not tmp in kwargs:
|
|
raise HostGroupMissingArgumentException(tmp, name)
|
|
self.add_members(kwargs[tmp])
|
|
|
|
tmp = "hosts"
|
|
self.hosts = []
|
|
if not tmp in kwargs:
|
|
raise HostGroupMissingArgumentException(tmp, name)
|
|
self.add_hosts(kwargs[tmp])
|
|
|
|
tmp = "services"
|
|
self.__services = []
|
|
if not tmp in kwargs:
|
|
raise HostGroupMissingArgumentException(tmp, name)
|
|
self.add_services(kwargs[tmp])
|
|
|
|
self.parents = []
|
|
tmp = "parents"
|
|
if tmp in kwargs:
|
|
self.add_parents(kwargs[tmp])
|
|
|
|
tmp = "processors"
|
|
self.processors = []
|
|
if tmp in kwargs:
|
|
self.add_processors(kwargs[tmp])
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
def _to_config_dict(self, configDict):
|
|
me = {}
|
|
me["members"] = [member.nameid for member in self.get_members()]
|
|
me["hosts"] = self.hosts
|
|
me["parents"] = [hg.get_name() for hg in self.get_parents()]
|
|
#TODO implement delegation
|
|
#me["services"] = [service._to_config_dict(configDict) for service in self.get_services()]
|
|
#me["processors"] = [processor._to_config_dict(configDict) for processor in self.get_processors()]
|
|
configDict["hostgroups"][self.get_name()] = me
|
|
|
|
def __add_internal(self, l, item):
|
|
if isinstance(item, list):
|
|
l.extend(item)
|
|
else:
|
|
l.append(item)
|
|
|
|
def add_members(self, member):
|
|
self.__add_internal(self.get_members(), member)
|
|
|
|
def add_hosts(self, host):
|
|
self.__add_internal(self.get_hosts(), host)
|
|
|
|
def add_processors(self, processor):
|
|
self.__add_internal(self.get_processors(), processor)
|
|
|
|
def add_parents(self, parent):
|
|
self.__add_internal(self.get_parents(), parent)
|
|
|
|
def add_services(self, services):
|
|
self.__add_internal(self.get_services(), services)
|
|
|
|
def get_parents(self):
|
|
return self.parents
|
|
|
|
def get_processors(self):
|
|
return self.processors
|
|
|
|
def get_services(self):
|
|
return self.__services
|
|
|
|
def get_hosts(self):
|
|
return self.hosts
|
|
|
|
def get_name(self):
|
|
return self.name
|
|
|
|
def get_members(self):
|
|
return self.members
|
|
|
|
|
|
class HostGroupService:
|
|
def __init__(self, services, periods):
|
|
self.services = services
|
|
self.periods = periods
|
|
|
|
def __str__(self):
|
|
return "HostgroupService { " + str([str(s) for s in self.services]) + ", " + str([p.name for p in self.periods]) + "}"
|
|
|
|
|
|
def parseHostGroupList(hostgroups, hosts, members, periods, services):
|
|
parsedHostGroups = []
|
|
for hgname, hgValues in hostgroups.items():
|
|
hostGroup = HostGroup(hgname)
|
|
hostGroup.members = [m for m in members if m.nameid in hgValues['members']]
|
|
hostGroup.hosts = [h for h in hosts if h.name in hgValues['hosts']]
|
|
hostGroup.threshold = hgValues['threshold']
|
|
if 'parent' in hgValues:
|
|
hostGroup.parent = hgValues['parent']
|
|
hostGroup.services = []
|
|
for serviceName, servicePeriods in hgValues['services'].items():
|
|
services = []
|
|
for host in hosts:
|
|
service = host.getHostServiceByName(serviceName)
|
|
if service is not None:
|
|
services.append(service)
|
|
else:
|
|
logger.warning("could not find HostService(" + str(serviceName) + ") for host " + host.name)
|
|
hostGroupPeriods = [p for p in periods if p.name in servicePeriods]
|
|
hostGroup.services.append(HostGroupService(services, hostGroupPeriods))
|
|
parsedHostGroups.append(hostGroup)
|
|
return parsedHostGroups |