diff --git a/.gitignore b/.gitignore index 4132524..590d78f 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ distfiles files local log -plugins \ No newline at end of file +plugins +.metadata diff --git a/lib/config/hostgroups.py b/lib/config/hostgroups.py index ccc522c..b21099f 100644 --- a/lib/config/hostgroups.py +++ b/lib/config/hostgroups.py @@ -27,31 +27,33 @@ class HostGroup: class HostGroupService: - def __init__(self, service, periods): - self.service = service + def __init__(self, services, periods): + self.services = services self.periods = periods 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): 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.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(): - service = filter(lambda s: s.name in serviceName, services) - if len(service) == 0: - log.w("Service " + serviceName + " is not defined for Hostgroup " + hgname) - continue - service = service[0] - hostGroupPeriods = filter(lambda p: p.name in servicePeriods, periods) - hostGroup.services.append(HostGroupService(service, hostGroupPeriods)) + services = [] + for host in hosts: + service = host.getHostServiceByName(serviceName) + if service is not None: + services.append(service) + else: + 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) return parsedHostGroups diff --git a/lib/config/hosts.py b/lib/config/hosts.py index 8f76aa4..b3bb29a 100644 --- a/lib/config/hosts.py +++ b/lib/config/hosts.py @@ -8,6 +8,12 @@ class Host: self.parent = parent self.services = services 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): ret = "Host('Name: " + self.name + "', 'Access: " + self.host + "', " @@ -89,7 +95,11 @@ def parseHostList(hosts, services, log): 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())) + 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 should be empty now. #If not we cannot use this command as some values are missing diff --git a/lib/core/job.py b/lib/core/job.py index ae7df06..bbaf58a 100644 --- a/lib/core/job.py +++ b/lib/core/job.py @@ -5,15 +5,21 @@ execute. from command import Command +def generateId(): + i=0 + while True: + yield i + i+=1 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.hosts = hosts - self.service = service + self.hostServices = hostServices self.threshold = threshold 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 = [] def __str__(self): @@ -34,7 +40,10 @@ class JobInfo: return nextExecution 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... #but because of error, mentioned in NOTES,ruff, there is no ping i.e. diff --git a/linspector b/linspector index 44a79dd..66940cb 100755 --- a/linspector +++ b/linspector @@ -11,6 +11,7 @@ from lib.core.logger import Logger from lib.config.config import Config from apscheduler.scheduler import Scheduler +DEFAULT_CONFIG = "./linspector.minimal.json" def parseArgs(): parser = argparse.ArgumentParser( @@ -21,7 +22,7 @@ def parseArgs(): parser.add_argument("action", choices=["start", "stop", "restart", "attach"], 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("-c", "--config", default="./linspector.json", + parser.add_argument("-c", "--config", default=DEFAULT_CONFIG, help="select configfile to use") parser.add_argument("-l", "--logfile", default="./log/linspector.log", metavar="FILE", help="set logfile to use") @@ -42,13 +43,8 @@ def parseArgs(): def handleJob(jobInfo): - print "handlejobInfo" - print str(jobInfo) - print "executing: " + str(jobInfo.service.command) jobInfo.handleCall() - - def main(): args = parseArgs() log = Logger(args.logfile, args.loglevel) @@ -67,7 +63,7 @@ def main(): for hostGroupService in hg.services: 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) for period in hostGroupService.periods: log.d(period) diff --git a/linspector.minimal.json b/linspector.minimal.json index 1d7cf89..157b139 100644 --- a/linspector.minimal.json +++ b/linspector.minimal.json @@ -18,7 +18,7 @@ "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": { "hanez1": {"host": "www1.hanez.org", "services": {"ping":[{}]}}, @@ -31,7 +31,7 @@ "members": ["hanez"], "hosts": ["hanez1"], "threshold": 10, - "services": {"ping": ["every10Secs"]} + "services": {"ping": ["shortPeriod"]} } }, "layouts": {"production": {"hostgroups": ["all"], "enabled": true}