From a670770dde74b9db9e6d2d287d56ba42f5ba9615 Mon Sep 17 00:00:00 2001 From: RafTim Date: Sun, 3 Nov 2013 04:57:09 +0100 Subject: [PATCH] changed task parsing and handling --- bin/linspector | 9 ++-- examples/minimal.json | 93 ++++++++++++++++--------------------- linspector/config/config.py | 9 +++- linspector/config/parser.py | 24 +++++++--- linspector/core/job.py | 8 ++-- linspector/tasks/mail.py | 9 ++-- linspector/tasks/task.py | 3 +- 7 files changed, 82 insertions(+), 73 deletions(-) diff --git a/bin/linspector b/bin/linspector index 0282350..b30ce07 100755 --- a/bin/linspector +++ b/bin/linspector @@ -93,7 +93,7 @@ def handle_job(jobInfo): def main(): - global linspector_configuration + global linConf args = parse_args() logfile = path.expanduser(args.logfile) @@ -109,7 +109,7 @@ def main(): try: config_parser = FullConfigParser() - linspector_configuration, core = config_parser.parse_config(args.config) + linConf, core = config_parser.parse_config(args.config) except Exception, msg: print("Configuration error: " + str(msg) + ". Exiting now.") logger.error(msg) @@ -124,7 +124,7 @@ def main(): start_date = datetime.datetime.now() time_delta = 0 jobs = [] - for layout in linspector_configuration.get_enabled_layouts(): + for layout in linConf.get_enabled_layouts(): for hostgroup in layout.get_hostgroups(): for service in hostgroup.get_services(): for host in hostgroup.get_hosts(): @@ -136,13 +136,14 @@ def main(): hostgroup.get_members(), hostgroup.get_processors(), core, + linConf.get_task_list(), hostgroup) scheduler_job = period.createJob(scheduler, job, handle_job, start_date=new_start_date) if scheduler_job is not None: job.set_job(scheduler_job) jobs.append(job) - interface = LinspectorInterface(jobs, scheduler, linspector_configuration) + interface = LinspectorInterface(jobs, scheduler, linConf) if "jsonrpc_backend" in core and core["jsonrpc_backend"]: jsonrpc = JsonrpcBackend(interface, core) diff --git a/examples/minimal.json b/examples/minimal.json index 8f7e3a4..f831165 100644 --- a/examples/minimal.json +++ b/examples/minimal.json @@ -1,74 +1,59 @@ { + "tasks": { + + "mail":{ + "host": "a.mx.systemchaos.org", + "port": 25, + "from": "linspector@systemchaos.org", + "username": "you@hanez.org", + "password": "uffff" + } + + }, "members":{ - "homer":{ - "name": "Homer Simpson", - "comment": "Security Inspector", - "tasks": [ - {"class":"mail", "type": "donut", "args": {"rcpt": "homer_j_simpson@example.sp"}}, - {"class":"mail", "type": "donut", "args": {"rcpt": "homer_j_simpson@example.sp"}} + "root":{ + "name": "root", + "_tasks": [ + { "class":"jabber", "type": "jabber", "args": { "rcpt": "hanez@systemchaos.org" }} ] } }, "periods": { - "doh": {"seconds": 10, "comment": "OMG, this means work"}, - "moes_time": {"minute":"0", "hour": "12", "day_of_week": "4", "comment": "much better"}, - "marges_birthday": { "date": "2017-2-24 04:00:00"} + "fast": { "seconds": 15 }, + "medium": { "seconds": 120 }, + "slow": { "seconds": 480 } }, "hostgroups":{ - "power_plant":{ - "members": ["homer"], - "hosts": ["powerplant.example.com"], - "processors":[ - {"class": "mongodb", "args":{ "host": "mongodb.example.org", "user": "homer", "password": "useless", "database": "default" }} - ], + "a":{ + "members": ["hanez"], + "hosts": [ "a", "b", "c", "d", "e"], "services":[ - { - "class": "ping", - "fails": {"donut": 2000}, - "periods": ["doh"], - "threshold": 50 - }, - { - "class": "ping", - "fails": {"donut": 1000}, - "periods": ["doh"], - "threshold": 100 - }, - { - "class": "tcpconnect", - "args": {"port": 23232}, - "periods": ["doh"], - "fails": {"donut": 0}, - "threshold": 0, - "comment": "my personal reminder, hehe" - }, - { - "template": "tcpconnect_80", - "threshold": 10, - "periods": ["moes_time"], - } + { "class": "dummy", "args": { "sleep": 1, "fail": 1 }, "periods": ["fast"], "threshold": 3 }, ] } }, "layouts":{ - "main":{"hostgroups": ["power_plant"], "enabled": true} + "main":{ + "hostgroups": [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o" ], + "enabled": true + } }, "core":{ + "instance_name": "Master Monitor 01 (monitor01.systemchaos.org)", "max_logfile_size": 1024000, "max_logfile_count": 4, - "max_worker_threads": 8, - "members":[ "root" ], - "tasks":{ - "jabber":{ - "host": "example.org", - "port": 5222, - "username": "USERNAME", - "password": "PASSWORD" - }, - "mail":{ - "host": "localhost", - "port": 25, - "from": "linspector@example.org" + "max_worker_threads": 1000, + "threshold_handling": "reset", + "shutdown_wait": "False", + "members":[ "linspector", "root" ], + "jsonrpc_backend": "True", + "backends": { + "jsonrpc": { + "_comment": "https://github.com/linspector/linspector/wiki/Configuration-Core-Backends-JsonRPC", + "host": "127.0.0.1", + "port": 10123, + "username": "linspector", + "password": "linspector" } } } diff --git a/linspector/config/config.py b/linspector/config/config.py index 57f315a..b802781 100644 --- a/linspector/config/config.py +++ b/linspector/config/config.py @@ -30,6 +30,7 @@ class LinspectorConfig(object): self._hostgroups = None self._members = None self._periods = None + self._tasks = None def set_hostgroups(self, hostgroups): self._hostgroups = hostgroups @@ -74,4 +75,10 @@ class LinspectorConfig(object): return self._get_by_name(self.get_members(), name) def get_period_by_name(self, name): - return self._get_by_name(self.get_periods(), name) \ No newline at end of file + return self._get_by_name(self.get_periods(), name) + + def set_task_list(self, taskList): + self.taskList = taskList + + def get_task_list(self): + return self.taskList \ No newline at end of file diff --git a/linspector/config/parser.py b/linspector/config/parser.py index 819ab5d..fe57f1b 100644 --- a/linspector/config/parser.py +++ b/linspector/config/parser.py @@ -32,7 +32,7 @@ from periods import CronPeriod, DatePeriod, IntervalPeriod from linspector.services.service import Service from linspector.processors.processor import Processor from linspector.parsers.parser import Parser -from linspector.tasks.task import Task +from linspector.tasks.TaskList import TaskList MOD_SERVICES = "services" MOD_PROCESSORS = "processors" @@ -44,6 +44,7 @@ KEY_HOSTGROUPS = "hostgroups" KEY_MEMBERS = "members" KEY_PERIODS = "periods" KEY_CORE = "core" +KEY_TASKS = "tasks" logger = getLogger(__name__) @@ -69,7 +70,7 @@ class ConfigParser: self._loadedMods = {MOD_SERVICES: {}, MOD_PROCESSORS: {}, MOD_TASKS: {}, MOD_PARSERS: {}} def _create_new_config_dict(self): - return {"members": {}, "periods": {}, "hostgroups": {}, "layouts": {}, "core": {}} + return {"members": {}, "periods": {}, "hostgroups": {}, "layouts": {}, "core": {}, "tasks": {}} def create_config(self, config): configDict = self._create_new_config_dict() @@ -217,6 +218,9 @@ def parsePeriodList(name, values): class FullConfigParser(ConfigParser): + + + def parse_config(self, configFilename): """ parses the json configuration and returns a list of layouts, @@ -232,7 +236,7 @@ class FullConfigParser(ConfigParser): self.dict = self._read_config_file(configFilename) - # first step + #1. creator = lambda name, values: Layout(name, **values) layouts = self._create_raw_Object(self.dict[KEY_LAYOUTS], "Layout", creator) @@ -245,6 +249,12 @@ class FullConfigParser(ConfigParser): creator = parsePeriodList periods = self._create_raw_Object(self.dict[KEY_PERIODS], "Period", creator) + #1.1 get Tasks + creator = lambda name, values: self._load_module(name, MOD_TASKS).create(values).set_task_type(name) + tasks = self._create_raw_Object(self.dict[KEY_TASKS], "Task", creator) + taskList = TaskList(tasks) + + #2. import and replace items_func = lambda hostgroup: hostgroup.get_services() class_check = lambda service: isinstance(service, Service) @@ -254,9 +264,9 @@ class FullConfigParser(ConfigParser): class_check = lambda processor: isinstance(processor, Processor) self.replace_with_import(self.hostgroups, MOD_PROCESSORS, items_func, class_check) - items_func = lambda member: member.get_tasks() - class_check = lambda task: isinstance(task, Task) - self.replace_with_import(members, MOD_TASKS, items_func, class_check) + #items_func = lambda member: member.get_tasks() + #class_check = lambda task: isinstance(task, Task) + #self.replace_with_import(members, MOD_TASKS, items_func, class_check) services = [] for hg in self.hostgroups: @@ -284,6 +294,8 @@ class FullConfigParser(ConfigParser): linConf.set_hostgroups(self.hostgroups) linConf.set_members(members) linConf.set_periods(periods) + linConf.set_task_list(taskList) + for hg in self.hostgroups: for service in hg.get_services(): diff --git a/linspector/core/job.py b/linspector/core/job.py index 4ef8e81..f52b679 100644 --- a/linspector/core/job.py +++ b/linspector/core/job.py @@ -35,12 +35,13 @@ def generateId(): class Job: - def __init__(self, service, host, members, processors, core, hostgroup): + def __init__(self, service, host, members, processors, core, taskList, hostgroup): self.service = service self.host = host self.members = members self.processors = processors self.core = core + self.taskList = taskList self.hostgroup = hostgroup #TODO: limit the size of this list; else it is a memory leak self.jobInfos = [] @@ -90,9 +91,8 @@ class Job: def handle_alarm(self, jobInfo, thresholdOffset): for member in self.members: - #TODO: Put Tasks in a run queue and execute them in a background thread. FIFO! Reduces delay in core. - for task in member.get_tasks(): - task.execute(jobInfo.get_message(), self.core) + self.taskList.execute_task_infos(jobInfo.get_message(), member.get_tasks()) + def handle_call(self): logger.debug("handle call") diff --git a/linspector/tasks/mail.py b/linspector/tasks/mail.py index 921821e..773e63c 100644 --- a/linspector/tasks/mail.py +++ b/linspector/tasks/mail.py @@ -51,17 +51,20 @@ class MailTask(Task): #self.set_task_type(kwargs["type"]) #self.recipient = kwargs["args"]["rcpt"] - def execute(self, msg, core): + def execute(self, msg, taskArgs): + if "recipient" not in taskArgs: + raise "could not execute Mail Task! No recipient given!" + message = MIMEText(msg) message['Subject'] = msg now = datetime.datetime.now() message['Date'] = now.strftime("%a, %d %b %Y %H:%M:%S") #TODO: totally unstable just to use values from core. make checks before...! message['From'] = self.fromName - message['To'] = self.recipient + message['To'] = taskArgs["recipient"] s = smtplib.SMTP(self.host, self.port) s.login(self.userName, self.password) - s.sendmail(self.fromName, self.recipient, message.as_string()) + s.sendmail(self.fromName, taskArgs["recipient"], message.as_string()) s.quit() diff --git a/linspector/tasks/task.py b/linspector/tasks/task.py index 82be48f..00c42f8 100644 --- a/linspector/tasks/task.py +++ b/linspector/tasks/task.py @@ -30,9 +30,10 @@ class Task: def set_task_type(self, taskType): self._taskType = taskType + return self def get_task_type(self): return self._taskType - def execute(self, msg): + def execute(self, msg, args): pass \ No newline at end of file