fixed parsing of services

This commit is contained in:
Rafael.Timmerberg 2013-05-29 22:52:54 +02:00
commit 400bfa7d07
6 changed files with 45 additions and 27 deletions

1
.gitignore vendored
View file

@ -11,3 +11,4 @@ files
local local
log log
plugins plugins
.metadata

View file

@ -27,31 +27,33 @@ class HostGroup:
class HostGroupService: class HostGroupService:
def __init__(self, service, periods): def __init__(self, services, periods):
self.service = service self.services = services
self.periods = periods self.periods = periods
def __str__(self): def __str__(self):
return "HostgroupService { " + str(self.service) + ", " + str(self.periods) + "}" 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, log): def parseHostGroupList(hostgroups, hosts, members, periods, services, log):
parsedHostGroups = [] parsedHostGroups = []
for hgname, hgValues in hostgroups.items(): for hgname, hgValues in hostgroups.items():
hostGroup = HostGroup(hgname) hostGroup = HostGroup(hgname)
hostGroup.members = filter(lambda m: m.nameid in hgValues['members'], members) hostGroup.members = [m for m in members if m.nameid in hgValues['members']]
hostGroup.hosts = filter(lambda h: h.name in hgValues['hosts'], hosts) hostGroup.hosts = [h for h in hosts if h.name in hgValues['hosts']]
hostGroup.threshold = hgValues['threshold'] hostGroup.threshold = hgValues['threshold']
if 'parent' in hgValues: if 'parent' in hgValues:
hostGroup.parent = hgValues['parent'] hostGroup.parent = hgValues['parent']
hostGroup.services = [] hostGroup.services = []
for serviceName, servicePeriods in hgValues['services'].items(): for serviceName, servicePeriods in hgValues['services'].items():
service = filter(lambda s: s.name in serviceName, services) services = []
if len(service) == 0: for host in hosts:
log.w("Service " + serviceName + " is not defined for Hostgroup " + hgname) service = host.getHostServiceByName(serviceName)
continue if service is not None:
service = service[0] services.append(service)
hostGroupPeriods = filter(lambda p: p.name in servicePeriods, periods) else:
hostGroup.services.append(HostGroupService(service, hostGroupPeriods)) log.w("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) parsedHostGroups.append(hostGroup)
return parsedHostGroups return parsedHostGroups

View file

@ -9,6 +9,12 @@ class Host:
self.services = services self.services = services
self.comment = comment self.comment = comment
def getHostServiceByName(self, serviceName):
for hostService in self.services:
if serviceName == hostService.service.name:
return hostService.service
return None
def __str__(self): def __str__(self):
ret = "Host('Name: " + self.name + "', 'Access: " + self.host + "', " ret = "Host('Name: " + self.name + "', 'Access: " + self.host + "', "
if self.parent != "": if self.parent != "":
@ -89,7 +95,11 @@ def parseHostList(hosts, services, log):
replacements.remove(parm) replacements.remove(parm)
#host will not be inside ServiceParameters, so check this also #host will not be inside ServiceParameters, so check this also
if 'host' in replacements: if 'host' in replacements:
hostService.setCommand(re.sub('@host', host.host, hostService.getCommand())) log.d("replacing host in " + hostService.getCommand())
comm= re.sub('@host', host.host, hostService.getCommand())
hostService.setCommand(comm)
log.d("new Command: " +comm)
log.d("set in hostService: " + str(hostService))
replacements.remove('host') replacements.remove('host')
#replacements should be empty now. #replacements should be empty now.
#If not we cannot use this command as some values are missing #If not we cannot use this command as some values are missing

View file

@ -5,15 +5,21 @@ execute.
from command import Command from command import Command
def generateId():
i=0
while True:
yield i
i+=1
class JobInfo: class JobInfo:
def __init__(self, hostgroupname, members, hosts, service, threshold, parent=None): def __init__(self, hostgroupname, members, hosts, hostServices, threshold, parent=None):
self.members = members self.members = members
self.hosts = hosts self.hosts = hosts
self.service = service self.hostServices = hostServices
self.threshold = threshold self.threshold = threshold
self.parent = parent self.parent = parent
self.name = hostgroupname + "_" + service.name self.name = generateId()
#self.name = hostgroupname + str([str("_" + s.service.name ) for s in hostServices])
self.jobs = [] self.jobs = []
def __str__(self): def __str__(self):
@ -34,7 +40,10 @@ class JobInfo:
return nextExecution return nextExecution
def handleCall(self): def handleCall(self):
print "about to call command " + str(self.service.command) self.log.d("handle call")
self.log.d([str(s) for s in self.hostServices])
#must find real service command stored in hosts... #must find real service command stored in hosts...
#but because of error, mentioned in NOTES,ruff, there is no ping i.e. #but because of error, mentioned in NOTES,ruff, there is no ping i.e.

View file

@ -11,6 +11,7 @@ from lib.core.logger import Logger
from lib.config.config import Config from lib.config.config import Config
from apscheduler.scheduler import Scheduler from apscheduler.scheduler import Scheduler
DEFAULT_CONFIG = "./linspector.minimal.json"
def parseArgs(): def parseArgs():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@ -21,7 +22,7 @@ def parseArgs():
parser.add_argument("action", choices=["start", "stop", "restart", "attach"], parser.add_argument("action", choices=["start", "stop", "restart", "attach"],
help="defines if linspector should beeing attached, started, stopped or restarted.") help="defines if linspector should beeing attached, started, stopped or restarted.")
parser.add_argument("--version", action="version", version="%(prog)s " + str(__version__)) parser.add_argument("--version", action="version", version="%(prog)s " + str(__version__))
parser.add_argument("-c", "--config", default="./linspector.json", parser.add_argument("-c", "--config", default=DEFAULT_CONFIG,
help="select configfile to use") help="select configfile to use")
parser.add_argument("-l", "--logfile", default="./log/linspector.log", metavar="FILE", parser.add_argument("-l", "--logfile", default="./log/linspector.log", metavar="FILE",
help="set logfile to use") help="set logfile to use")
@ -42,13 +43,8 @@ def parseArgs():
def handleJob(jobInfo): def handleJob(jobInfo):
print "handlejobInfo"
print str(jobInfo)
print "executing: " + str(jobInfo.service.command)
jobInfo.handleCall() jobInfo.handleCall()
def main(): def main():
args = parseArgs() args = parseArgs()
log = Logger(args.logfile, args.loglevel) log = Logger(args.logfile, args.loglevel)
@ -67,7 +63,7 @@ def main():
for hostGroupService in hg.services: for hostGroupService in hg.services:
log.d(hostGroupService) log.d(hostGroupService)
jobInfo = JobInfo(hg.name, hg.members, hg.hosts, hostGroupService.service, hg.threshold, hg.parent) jobInfo = JobInfo(hg.name, hg.members, hg.hosts, hostGroupService.services, hg.threshold, hg.parent)
jobInfo.setLogger(log) jobInfo.setLogger(log)
for period in hostGroupService.periods: for period in hostGroupService.periods:
log.d(period) log.d(period)

View file

@ -18,7 +18,7 @@
"filters": {"email": "you@hanez.org"} "filters": {"email": "you@hanez.org"}
} }
}, },
"periods": {"every10Secs": {"seconds": "10", "comment": "Interval job; every 10 seconds"}}, "periods": {"shortPeriod": {"seconds": 5, "comment": "Interval job; every 10 seconds"}},
"hosts": "hosts":
{ {
"hanez1": {"host": "www1.hanez.org", "services": {"ping":[{}]}}, "hanez1": {"host": "www1.hanez.org", "services": {"ping":[{}]}},
@ -31,7 +31,7 @@
"members": ["hanez"], "members": ["hanez"],
"hosts": ["hanez1"], "hosts": ["hanez1"],
"threshold": 10, "threshold": 10,
"services": {"ping": ["every10Secs"]} "services": {"ping": ["shortPeriod"]}
} }
}, },
"layouts": {"production": {"hostgroups": ["all"], "enabled": true} "layouts": {"production": {"hostgroups": ["all"], "enabled": true}