some code...
This commit is contained in:
parent
f9c99d4070
commit
cac342582b
19 changed files with 788 additions and 0 deletions
0
lib/config/__init__.py
Normal file
0
lib/config/__init__.py
Normal file
36
lib/config/config.py
Normal file
36
lib/config/config.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import json
|
||||
from services import serviceList
|
||||
from filters import parseFilterList
|
||||
from members import parseMemberList
|
||||
from hosts import parseHostList
|
||||
from periods import parsePeriodList
|
||||
from hostgroups import parseHostGroupList
|
||||
#from layouts import *
|
||||
|
||||
|
||||
class Config:
|
||||
def __init__(self, configFile):
|
||||
self.configfile = configFile
|
||||
f = open(configFile)
|
||||
self.config = f.read()
|
||||
f.close()
|
||||
|
||||
self.dict = json.loads(self.config)
|
||||
|
||||
self.services = serviceList(self.dict['services'])
|
||||
|
||||
self.filters = parseFilterList(self.dict['filters'])
|
||||
|
||||
self.members = parseMemberList(self.dict['members'], self.filters)
|
||||
|
||||
self.periods = parsePeriodList(self.dict['periods'])
|
||||
|
||||
self.hosts = parseHostList(self.dict['hosts'], self.services)
|
||||
|
||||
self.hostgroups = parseHostGroupList(self.dict['hostgroups'],
|
||||
self.hosts,
|
||||
self.members,
|
||||
self.periods,
|
||||
self.services)
|
||||
|
||||
#self.layouts = LayoutList(self.dict['layouts'], self.hostgroups)
|
||||
18
lib/config/filters.py
Normal file
18
lib/config/filters.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
|
||||
class Filter:
|
||||
def __init__(self, name="", command="", priority=0, comment=""):
|
||||
self.name = name
|
||||
self.command = command
|
||||
self.priority = priority
|
||||
self.comment = comment
|
||||
|
||||
def __str__(self):
|
||||
return "Filter('Name: " + self.name + "', 'Command: " + self.command + "', 'Priority: " + str(
|
||||
self.priority) + "')"
|
||||
|
||||
def clone(self):
|
||||
return Filter(self.name, self.command, self.priority, self.comment)
|
||||
|
||||
|
||||
def parseFilterList(filters):
|
||||
return [Filter(name, **values) for name, values in filters.items()]
|
||||
57
lib/config/hostgroups.py
Normal file
57
lib/config/hostgroups.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
class HostGroup:
|
||||
def __init__(self, name, members="", hosts="", services="", threshold="", parent="", comment=""):
|
||||
self.name = name
|
||||
self.interval = 0
|
||||
self.members = members
|
||||
self.hosts = hosts
|
||||
self.services = services
|
||||
self.threshold = threshold
|
||||
self.parent = parent
|
||||
self.comment = comment
|
||||
|
||||
def __str__(self):
|
||||
ret = "HostGroup: " + self.name + " threshold: " + str(self.threshold) + " parent: " + self.parent + "\n"
|
||||
ret += "members: {\n"
|
||||
for itm in self.members:
|
||||
ret += str(itm) + "\n"
|
||||
ret += "}\n"
|
||||
ret += "hosts: {\n"
|
||||
for itm in self.hosts:
|
||||
ret += str(itm) + "\n"
|
||||
ret += "}\n"
|
||||
ret += "services: {\n"
|
||||
for itm in self.services:
|
||||
ret += str(itm) + "\n"
|
||||
ret += "}\n"
|
||||
return ret
|
||||
|
||||
|
||||
class HostGroupService:
|
||||
def __init__(self, service, periods):
|
||||
self.service = service
|
||||
self.periods = periods
|
||||
|
||||
def __str__(self):
|
||||
return "HostgroupService { " + str(self.service) + ", " + str(self.periods) + "}"
|
||||
|
||||
|
||||
def parseHostGroupList(hostgroups, hosts, members, periods, services):
|
||||
parsedHostGroups = []
|
||||
for hgname, hgValues in hostgroups.items():
|
||||
hostGroup = HostGroup(hgname)
|
||||
hostGroup.members = filter(lambda m: m.nameid in hgValues['members'], members)
|
||||
hostGroup.hosts = filter(lambda h: h.name in hgValues['hosts'], hosts)
|
||||
hostGroup.threshold = hgValues['threshold']
|
||||
if 'parent' in hgValues:
|
||||
hostGroup.parent = hgValues['parent']
|
||||
hostGroup.services = []
|
||||
for serviceName, servicePeriods in hgValues['services'].items():
|
||||
service = filter(lambda s: s.name in serviceName, services)
|
||||
if not service:
|
||||
print "warning: Service " + serviceName + " is not defined for Hostgroup " + hgname
|
||||
continue
|
||||
service = service[0]
|
||||
periods = filter(lambda p: p.name in servicePeriods, periods)
|
||||
hostGroup.services.append(HostGroupService(service, periods))
|
||||
parsedHostGroups.append(hostGroup)
|
||||
return parsedHostGroups
|
||||
105
lib/config/hosts.py
Normal file
105
lib/config/hosts.py
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
import re
|
||||
|
||||
|
||||
class Host:
|
||||
def __init__(self, name="", host="", parent="", services=None, comment=""):
|
||||
self.name = name
|
||||
self.host = host
|
||||
self.parent = parent
|
||||
self.services = services
|
||||
self.comment = comment
|
||||
|
||||
def __str__(self):
|
||||
ret = "Host('Name: " + self.name + "', 'access: " + self.host + "', "
|
||||
if self.parent != "":
|
||||
ret += "'parent: " + self.parent + "', "
|
||||
ret += "'HostServices: {"
|
||||
for s in self.services:
|
||||
ret += str(s) + "\n"
|
||||
ret += "}"
|
||||
return ret
|
||||
|
||||
|
||||
class HostService:
|
||||
def __init__(self, service, warning="", critical=""):
|
||||
self.service = service
|
||||
self.warning = warning
|
||||
self.critical = critical
|
||||
|
||||
def setCommand(self, command):
|
||||
self.service.command = command
|
||||
|
||||
def getCommand(self):
|
||||
return self.service.command
|
||||
|
||||
def __str__(self):
|
||||
ret = str(self.service)
|
||||
if self.warning:
|
||||
ret += "warning: " + str(self.warning)
|
||||
if self.critical:
|
||||
ret += "critical: " + str(self.critical)
|
||||
return ret;
|
||||
|
||||
|
||||
def parseHostList(hosts, services):
|
||||
'''parse the HostList and replace any command as nessesary '''
|
||||
#precompiled regexPattern which finds replacements in service strings
|
||||
pattern = re.compile("@(\w+)")
|
||||
#get a List of Host Objects and leave services unparsed for this moment
|
||||
parsedHosts = [Host(name, **values) for name, values in hosts.items()]
|
||||
#predefined dict to cache service replacements by name
|
||||
serviceReplacements = {}
|
||||
for host in parsedHosts:
|
||||
#list to store HostService Objects
|
||||
hostServices = []
|
||||
#lets start to parse the Service Dict.
|
||||
for servicename, serviceParams in host.services.items():
|
||||
#bool to check if the service is defined.
|
||||
found = False
|
||||
#to a real iteration. just pick the right service
|
||||
for service in services:
|
||||
if service.name != servicename: continue
|
||||
#indicate we found a service
|
||||
found = True
|
||||
#check to see if we already regexed our service command
|
||||
if service.name not in serviceReplacements:
|
||||
serviceReplacements[service.name] = pattern.findall(service.command)
|
||||
for params in serviceParams:
|
||||
#copy replacements from service command
|
||||
replacements = serviceReplacements[service.name][:]
|
||||
#for every Host.service.parameter we need a new HostService Object
|
||||
hostService = HostService(service.clone())
|
||||
#check if the ServiceParameter contain warnings or critical values
|
||||
if 'warning' in params:
|
||||
hostService.warning = params['warning']
|
||||
del params['warning']
|
||||
if 'critical' in params:
|
||||
hostService.critical = params['critical']
|
||||
del params['critical']
|
||||
#any remainig parm should be a replacement
|
||||
for parm in params:
|
||||
if parm not in replacements:
|
||||
print "warning: undefined parameter: " + parm + " in host " + host.name + " from service " + service.name
|
||||
continue
|
||||
#replace our ServiceCommand with the parameter_value (search, replacement, string)
|
||||
hostService.setCommand(re.sub('@' + parm, params[parm], hostService.getCommand()))
|
||||
replacements.remove(parm)
|
||||
#host will not be inside ServiceParameters, so check this also
|
||||
if 'host' in replacements:
|
||||
hostService.setCommand(re.sub('@host', host.host, hostService.getCommand()))
|
||||
replacements.remove('host')
|
||||
#replacements should be empty now.
|
||||
#If not we cannot use this command as some values are missing
|
||||
if replacements:
|
||||
print "warning: Hostservice " + servicename + " from host " + host.name + " is ignored because of missing replacements: " + str(
|
||||
replacements)
|
||||
else:
|
||||
#anything ok! add to our valid hostServices
|
||||
hostServices.append(hostService)
|
||||
#we could't find the service defined in this host. Service ignored!
|
||||
if not found:
|
||||
print "warning: Service " + servicename + " not defined in host " + host.name
|
||||
#replace host.service member by parsed HostService Objects
|
||||
host.services = hostServices
|
||||
return parsedHosts
|
||||
|
||||
50
lib/config/layouts.py
Normal file
50
lib/config/layouts.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
from ..core import logger
|
||||
|
||||
|
||||
class Layout:
|
||||
def __init__(self, myLayout):
|
||||
self.name = myLayout
|
||||
self.enabled = False
|
||||
self.hostgroups = []
|
||||
|
||||
def __str__(self):
|
||||
ret = "Layout: 'Name:" + str(self.name) + "', 'Enabled: " + str(self.enabled) + " "
|
||||
for group in self.hostgroups:
|
||||
ret += str(group)
|
||||
return ret
|
||||
|
||||
|
||||
class LayoutList:
|
||||
def __init__(self, layouts, hostgroups):
|
||||
self.layouts = []
|
||||
self.dict = layouts
|
||||
self.plugins = []
|
||||
for k, v in layouts.items():
|
||||
if k in ("plugins", "Plugins"):
|
||||
self.plugins = v
|
||||
continue
|
||||
else:
|
||||
l = Layout(k)
|
||||
for k1, v1 in v.items():
|
||||
if k1 in ("enabled", "Enabled"):
|
||||
l.enabled = v1
|
||||
elif k1 in ("hostgroups", "Hostgroups"):
|
||||
l.hostgroups = []
|
||||
for group in v1:
|
||||
h = None
|
||||
for hostg in hostgroups:
|
||||
if hostg.name == group:
|
||||
h = hostg
|
||||
break
|
||||
if h is not None:
|
||||
l.hostgroups.append(h)
|
||||
else:
|
||||
logger.logWarningConfig(file="hostgroups", missing=group)
|
||||
self.layouts.append(l)
|
||||
|
||||
def __str__(self):
|
||||
ret = ""
|
||||
ret += "Plugins: " + str(self.plugins) + "\n"
|
||||
for layout in self.layouts:
|
||||
ret += str(layout) + "\n"
|
||||
return ret
|
||||
44
lib/config/members.py
Normal file
44
lib/config/members.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import re
|
||||
|
||||
|
||||
class Member:
|
||||
def __init__(self, nameid, name="", phone="", comment="", parent="", filters=None):
|
||||
self.nameid = nameid
|
||||
self.name = name
|
||||
self.phone = phone
|
||||
self.filters = filters
|
||||
self.comment = comment
|
||||
self.parent = parent
|
||||
|
||||
def __str__(self):
|
||||
ret = "Member Id: " + self.nameid + " Name: " + self.name + " Filters: " + str(self.phone)
|
||||
for f in self.filters:
|
||||
ret += str(f)
|
||||
return ret
|
||||
|
||||
|
||||
class MemberFilter:
|
||||
def __init__(self, filt, Value):
|
||||
self.filt = filt
|
||||
self.value = Value
|
||||
|
||||
def __str__(self):
|
||||
return "Filter:" + str(self.filter) + " Value:" + self.value
|
||||
|
||||
|
||||
def parseMemberList(members, filters):
|
||||
parsedMembers = [Member(nameid, **values) for nameid, values in members.items()]
|
||||
for member in parsedMembers:
|
||||
mFilter = []
|
||||
for filtername, replacement in member.filters.items():
|
||||
found = False
|
||||
for filt in filters:
|
||||
if filt.name != filtername: continue
|
||||
found = True
|
||||
memberFilter = filt.clone()
|
||||
memberFilter.command = re.sub('@member', replacement, filt.command)
|
||||
mFilter.append(memberFilter)
|
||||
if not found:
|
||||
print "warning: filter: " + filtername + " is not defined in member " + member.name
|
||||
member.filters = mFilter
|
||||
return parsedMembers
|
||||
23
lib/config/periods.py
Normal file
23
lib/config/periods.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
class Period:
|
||||
def __init__(self, name="", year="*", month="*", day="*", week="*",
|
||||
day_of_week=None, hour="*", minute="*", second="0",
|
||||
comment=None):
|
||||
self.name = name
|
||||
self.year = year # 4-digit year number
|
||||
self.month = month # month number (1-12)
|
||||
self.day = day # day of the month (1-31)
|
||||
self.week = week # ISO week number (1-53)
|
||||
self.day_of_week = day_of_week # number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)
|
||||
self.hour = hour # hour (0-23)
|
||||
self.minute = minute # minute (0-59)
|
||||
self.second = second # second (0-59)
|
||||
self.comment = comment
|
||||
|
||||
def __str__(self):
|
||||
ret = "Period(Name: " + self.name + " Year: " + self.year + " Month: " + self.month + ")"
|
||||
return ret
|
||||
|
||||
|
||||
def parsePeriodList(periods):
|
||||
return [Period(name, **values) for name, values in periods.items()]
|
||||
|
||||
16
lib/config/services.py
Normal file
16
lib/config/services.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
class Service:
|
||||
def __init__(self, name="", command="", comment="", parser=""):
|
||||
self.name = name
|
||||
self.command = command
|
||||
self.comment = comment
|
||||
self.parser = parser
|
||||
|
||||
def __str__(self):
|
||||
return "Service('Name: " + self.name + "', 'Command: " + self.command + ", 'Parser: " + self.parser + "')"
|
||||
|
||||
def clone(self):
|
||||
return Service(self.name, self.command, self.comment)
|
||||
|
||||
|
||||
def serviceList(services):
|
||||
return [Service(name=key, **values) for key, values in services.items()]
|
||||
Loading…
Add table
Add a link
Reference in a new issue