diff --git a/lib/config/parser.py b/lib/config/parser.py index 9c858ad..93a37ac 100644 --- a/lib/config/parser.py +++ b/lib/config/parser.py @@ -108,10 +108,9 @@ class ConfigParser: if clazz in mods: return mods["class"] else: - mod = __import__(clazz) - #path = join(getcwd(), "lib", modPart, clazz + ".py") - #self.log.w(path) - #mod = imp.load_source(clazz, path) + #mod = __import__(clazz) + path = join("lib", modPart, clazz + ".py") + mod = imp.load_source(clazz, path) mods[clazz] = mod return mod @@ -123,6 +122,7 @@ class ConfigParser: :param objList: the list of objects which is iterated on :param modPart: the folder from the module (i.e tasks, parsers) :param items_func: function to get a pointer on the list of json-config-objects to replace. Takes one argument and + should return a list of :param class_check: currently unsupported """ for obj in objList: @@ -130,25 +130,15 @@ class ConfigParser: items = items_func(obj) for clazzItem in items: try: - if "class" not in clazzItem: - self.log.w("python says class is not in class item!") - self.log.w(modPart) - self.log.w(clazzItem) - self.log.w(clazzItem["class"]) clazz = clazzItem["class"] - path = "lib/" + modPart - sys.path.append(path) mod = self._load_module(clazz, modPart) 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!") + 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)) @@ -156,9 +146,7 @@ class ConfigParser: self.log.w("Key " + str(k) + " not in classItem " + str(clazzItem)) except Exception, e: self.log.w("Error while replacing class ( " + clazz + " ):" + str(e)) - finally: - if path in sys.path: - del sys.path[sys.path.index(path)] + del items[:] items.extend(repl) @@ -191,7 +179,7 @@ def parsePeriodList(name, values): return DatePeriod(name, **values) comp = ["weeks", "days", "hours", "minutes", "seconds", "start_date"] - if len([i for i in comp if i in values]) > 0 : + if len([i for i in comp if i in values]) > 0: return IntervalPeriod(name, **values) comp = ["year", "month", "day", "week", "day_of_week", "hour", "minute", "second"] diff --git a/lib/services/http.py b/lib/services/http.py index a23c42b..254bb4b 100644 --- a/lib/services/http.py +++ b/lib/services/http.py @@ -9,7 +9,7 @@ to just report this code and not use a parser. """ import urllib -from service import Service +from lib.services.service import Service class HttpService(Service): diff --git a/lib/services/ping.py b/lib/services/ping.py index 69d7f31..33668f8 100644 --- a/lib/services/ping.py +++ b/lib/services/ping.py @@ -3,7 +3,7 @@ The ping service in pure Python. """ # http://code.activestate.com/recipes/409689-icmplib-library-for-creating-and-reading-icmp-pack/ -from service import Service +from lib.services.service import Service class PingService(Service): diff --git a/lib/services/shell.py b/lib/services/shell.py index a1ebc32..65878fb 100644 --- a/lib/services/shell.py +++ b/lib/services/shell.py @@ -2,7 +2,7 @@ The shell service. This is for executing local shell commands and retrieve the output. """ -from service import Service +from lib.services.service import Service class ShellService(Service): diff --git a/lib/services/snmpget.py b/lib/services/snmpget.py index 15a0d7c..f59b7d2 100644 --- a/lib/services/snmpget.py +++ b/lib/services/snmpget.py @@ -3,7 +3,7 @@ The snmpget service in pure Python. """ #from pysnmp.entity.rfc3413.oneliner import cmdgen -from service import Service +from lib.services.service import Service class SnmpgetService(Service): diff --git a/lib/services/ssh.py b/lib/services/ssh.py index 44116a1..7bc083e 100644 --- a/lib/services/ssh.py +++ b/lib/services/ssh.py @@ -7,7 +7,7 @@ This service is using paramiko (http://www.lag.net/paramiko/). import paramiko import pprint import os -from service import Service +from lib.services.service import Service class SshService(Service): diff --git a/lib/services/tcpconnect.py b/lib/services/tcpconnect.py index c30cd46..358fcfa 100644 --- a/lib/services/tcpconnect.py +++ b/lib/services/tcpconnect.py @@ -6,7 +6,7 @@ not use a parser. """ import socket -from service import Service +from lib.services.service import Service class TcpconnectService(Service): diff --git a/linspector b/linspector index dbbfb89..c1f7f50 100755 --- a/linspector +++ b/linspector @@ -1,7 +1,7 @@ #!/usr/bin/python2.7 -tt __version__ = "0.4/TETRIS" -__default_config__ = "./linspector.json" +__default_config__ = "./minimal.json" import argparse import time diff --git a/minimal.json b/minimal.json new file mode 100644 index 0000000..1c459bc --- /dev/null +++ b/minimal.json @@ -0,0 +1,42 @@ +{ + "members":{ + "homer":{ + "name": "Homer Simpson", + "comment": "Security Inspector", + "tasks": [{"class":"email", "type": "donut", "args": {"rcpt": "homer_j_simpson@burnscorp.sp"}}] + } + }, + "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": "1965-2-24 04:00:00"} + }, + "hostgroups":{ + "power_plant":{ + "members": ["homer"], + "hosts": ["powerplant.springfield.com"], + "processors":[ + {"class": "mongodb", "args":{ "host": "mongodb.burnscorp.org", "user": "homer", "password": "useless", "database": "default" }} + ], + "services":[ + { + "class": "ping", + "fails": {"donut": 2000}, + "periods":["doh"], + "threshold": 500 + }, + { + "class": "tcpconnect", + "args": {"port": 23232}, + "periods": ["moes_time", "marges_birthday"], + "threshold": 0, + "comment": "my personal reminder, hehe" + } + ] + } + }, + "layouts":{ + "main":{"hostgroups": ["power_plant"], "enabled": true} + } + +} \ No newline at end of file