From a821f97f9835f6377a3773a95ae73e166cd6cce2 Mon Sep 17 00:00:00 2001 From: "Rafael.Timmerberg" Date: Wed, 19 Jun 2013 01:29:11 +0200 Subject: [PATCH] fixed parsing, replaced non_workin instance_check with todo --- lib/config/parser.py | 56 ++++++++++++++++++++++++-------------- lib/config/periods.py | 2 +- lib/parsers/parser.py | 1 + lib/processors/mongodb.py | 6 +++- lib/processors/syslog.py | 5 +++- lib/services/ping.py | 4 +-- lib/services/service.py | 8 +++--- lib/services/shell.py | 2 +- lib/services/snmpget.py | 2 +- lib/services/ssh.py | 2 +- lib/services/tcpconnect.py | 4 ++- lib/tasks/email.py | 2 +- lib/tasks/sms.py | 2 +- lib/tasks/xmpp.py | 3 +- 14 files changed, 63 insertions(+), 36 deletions(-) diff --git a/lib/config/parser.py b/lib/config/parser.py index 535f764..5fc958d 100644 --- a/lib/config/parser.py +++ b/lib/config/parser.py @@ -1,13 +1,17 @@ from os.path import isfile import json import sys - +from os.path import join +from os import getcwd +import imp from layouts import Layout from hostgroups import HostGroup +import lib.services.ping from members import Member from periods import CronPeriod, DatePeriod, IntervalPeriod from lib.services.service import Service +print id(Service) from lib.processors.processor import Processor from lib.parsers.parser import Parser from lib.tasks.task import Task @@ -17,10 +21,7 @@ MOD_PROCESSORS = "processors" MOD_PARSERS = "parsers" MOD_TASKS = "tasks" -sys.path.append("lib/" + MOD_SERVICES) -sys.path.append("lib/" + MOD_PROCESSORS) -sys.path.append("lib/" + MOD_PARSERS) -sys.path.append("lib/" + MOD_TASKS) + KEY_LAYOUTS = "layouts" KEY_HOSTGROUPS = "hostgroups" @@ -143,6 +144,8 @@ class ConfigParser: return mods["class"] else: mod = __import__(clazz) + #path = join("lib", modPart, clazz + ".py") + #mod = imp.load_source(clazz, path) mods[clazz] = mod return mod @@ -153,19 +156,28 @@ class ConfigParser: for clazzItem in items: try: clazz = clazzItem["class"] + path = "lib/" + modPart + sys.path.append(path) mod = self._load_module(clazz, modPart) - item = mod.create(**clazzItem) - if class_check(item): - repl.append(item) - else: - self.log.w(" ignoring class " + clazzItem["class"] + "! It does not pass the class check!") + item = mod.create(clazzItem) + repl.append(item) + #TODO: activate the crappy classcheck if answer is provided + #http://stackoverflow.com/questions/17179440/ + self.log.d("warning: instance_check isn't working yet! TRUST_ALL = TRUE") + #if class_check(item): + # repl.append(item) + #else: + # self.log.w(" ignoring class " + clazzItem["class"] + "! It does not pass the class check!") except ImportError, err: self.log.w("could not import " + clazz + ": " + str(clazzItem) + "! reason") self.log.w(str(err)) - except KeyError: - self.log.w("Key 'class' not in classItem " + str(clazzItem)) - except Exception: - self.log.w("Error while replace: " + str(Exception)) + except KeyError, k: + self.log.w("Key '" + str(k) + "' not in classItem " + str(clazzItem)) + except Exception, e: + self.log.w("Error while replace: " + str(e)) + finally: + if path in sys.path: + del sys.path[sys.path.index(path)] del items[:] items.extend(repl) @@ -277,7 +289,8 @@ class FullConfigParser(ConfigParser): creator = parsePeriodList periods = self._create_raw_Object(self.jsonDict[KEY_PERIODS], "Period", creator) - + + #2. import and replace items_func = lambda hostgroup: hostgroup.get_services() class_check = lambda service: isinstance(service, Service) @@ -286,10 +299,15 @@ class FullConfigParser(ConfigParser): items_func = lambda hostgroup: hostgroup.get_processors() class_check = lambda processor: isinstance(processor, Processor) self.replace_with_import(hostgroups, MOD_PROCESSORS, items_func, class_check) - + + services = [] + for hg in hostgroups: + services.extend(hg.get_services()) + + items_func = lambda service: service.get_parser() class_check = lambda parser: isinstance(parser, Parser) - self.replace_with_import(hostgroups.services, MOD_PARSERS, items_func, class_check) + self.replace_with_import(services, MOD_PARSERS, items_func, class_check) items_func = lambda member: member.get_tasks() class_check = lambda task: isinstance(task, Task) @@ -300,9 +318,7 @@ class FullConfigParser(ConfigParser): id_get_func = lambda member: member.id self.replace_pointer(hostgroups, members, id_list_func, id_get_func) - services = [] - for hg in hostgroups: - services.extend(hg.get_services()) + id_list_func = lambda service: service.get_periods() id_get_func = lambda period: period.get_name() self.replace_pointer(services, periods, id_list_func, id_get_func) diff --git a/lib/config/periods.py b/lib/config/periods.py index 237a8fc..a0c2a56 100644 --- a/lib/config/periods.py +++ b/lib/config/periods.py @@ -2,7 +2,7 @@ class Period(object): def __init__(self, name): self.name = name - def getName(self): + def get_name(self): return self.name def createJob(self, scheduler, jobInfo, func): diff --git a/lib/parsers/parser.py b/lib/parsers/parser.py index 977b284..335b596 100644 --- a/lib/parsers/parser.py +++ b/lib/parsers/parser.py @@ -11,3 +11,4 @@ class Parser: def generate_parse_result(self, result): pass + diff --git a/lib/processors/mongodb.py b/lib/processors/mongodb.py index 62eda3b..c94c84c 100644 --- a/lib/processors/mongodb.py +++ b/lib/processors/mongodb.py @@ -7,4 +7,8 @@ from processor import Processor class Mongodb(Processor): def __init__(self): - pass \ No newline at end of file + pass + + +def create(kwargs): + return Mongodb(**kwargs) \ No newline at end of file diff --git a/lib/processors/syslog.py b/lib/processors/syslog.py index 05b4665..e2d0371 100644 --- a/lib/processors/syslog.py +++ b/lib/processors/syslog.py @@ -7,4 +7,7 @@ from processor import Processor class Syslog(Processor): def __init__(self): - pass \ No newline at end of file + pass + +def create(kwargs): + return Syslog(**kwargs) diff --git a/lib/services/ping.py b/lib/services/ping.py index fc07cc1..69d7f31 100644 --- a/lib/services/ping.py +++ b/lib/services/ping.py @@ -8,8 +8,8 @@ from service import Service class PingService(Service): def __init__(self, **kwargs): - super(PingService, self).__init__(**kwargs) + Service.__init__(self, **kwargs) -def create(self, **kwargs): +def create(kwargs): return PingService(**kwargs) \ No newline at end of file diff --git a/lib/services/service.py b/lib/services/service.py index 4d6d339..f77fef1 100644 --- a/lib/services/service.py +++ b/lib/services/service.py @@ -7,7 +7,7 @@ KEY_PERIODS = "periods" KEY_ARGS = "args" -class Service: +class Service(object): def __init__(self, **kwargs): self._args = {} @@ -36,10 +36,10 @@ class Service: self._periods = [] if KEY_PERIODS in kwargs: - self.addPeriods(kwargs[KEY_PERIODS]) + self.add_periods(kwargs[KEY_PERIODS]) self.errorcode = 0 - self.errormessage = "No Error!" + self.errormessage = None def add_arguments(self, args): for key, val in args.items(): @@ -51,7 +51,7 @@ class Service: def get_arguments(self): return self._args - def add_period(self, period): + def add_periods(self, period): if period is not None: if isinstance(period, list): self._periods.extend(period) diff --git a/lib/services/shell.py b/lib/services/shell.py index 59ad5e8..a1ebc32 100644 --- a/lib/services/shell.py +++ b/lib/services/shell.py @@ -22,5 +22,5 @@ class ShellService(Service): self.command.call() -def create(**kwargs): +def create(kwargs): return ShellService(**kwargs) \ No newline at end of file diff --git a/lib/services/snmpget.py b/lib/services/snmpget.py index bf516fb..15a0d7c 100644 --- a/lib/services/snmpget.py +++ b/lib/services/snmpget.py @@ -52,5 +52,5 @@ class SnmpgetService(Service): # print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) -def create(**kargs): +def create(kargs): return SnmpgetService(**kwargs) \ No newline at end of file diff --git a/lib/services/ssh.py b/lib/services/ssh.py index dc82884..44116a1 100644 --- a/lib/services/ssh.py +++ b/lib/services/ssh.py @@ -40,7 +40,7 @@ class SshService(Service): client.close() -def create(**kwargs): +def create(kwargs): return SshService(**kwargs) # def main(): diff --git a/lib/services/tcpconnect.py b/lib/services/tcpconnect.py index 1548698..3fcf151 100644 --- a/lib/services/tcpconnect.py +++ b/lib/services/tcpconnect.py @@ -6,11 +6,13 @@ not use a parser. """ import socket +from lib.config.services import Service from service import Service class TcpconnectService(Service): def __init__(self, **kwargs): + #Service.__init__(self, **kwargs) super(TcpconnectService, self).__init__(**kwargs) args = self.get_arguments() @@ -39,5 +41,5 @@ class TcpconnectService(Service): return -def create(**kwargs): +def create(kwargs): return TcpconnectService(**kwargs) \ No newline at end of file diff --git a/lib/tasks/email.py b/lib/tasks/email.py index 4453b4a..8593c84 100644 --- a/lib/tasks/email.py +++ b/lib/tasks/email.py @@ -18,5 +18,5 @@ class EmailTask(Task): pass -def creator(**taskDict): +def creator(taskDict): return EmailTask(**taskDict) \ No newline at end of file diff --git a/lib/tasks/sms.py b/lib/tasks/sms.py index 407f542..d352204 100644 --- a/lib/tasks/sms.py +++ b/lib/tasks/sms.py @@ -18,5 +18,5 @@ class SmsTask(Task): pass -def creator(**taskDict): +def creator(taskDict): return SmsTask(**taskDict) \ No newline at end of file diff --git a/lib/tasks/xmpp.py b/lib/tasks/xmpp.py index 694b766..4f3fc67 100644 --- a/lib/tasks/xmpp.py +++ b/lib/tasks/xmpp.py @@ -18,5 +18,6 @@ class XmppTask(Task): pass -def creator(**taskDict): + +def creator(taskDict): return XmppTask(taskDict) \ No newline at end of file