whitespacing.... :)

This commit is contained in:
Johannes Findeisen 2013-06-18 02:58:40 +02:00
commit 883e59a3d1
22 changed files with 66 additions and 109 deletions

View file

@ -44,16 +44,14 @@ class HostGroup:
def _to_config_dict(self, configDict):
me = {}
me["members"] = [member.nameid for member in self.get_members()]
me["hosts"] = self.hosts
me["hosts"] = self.hosts
me["parents"] = [hg.get_name() for hg in self.get_parents()]
#TODO implement delegation
#me["services"] = [service._to_config_dict(configDict) for service in self.get_services()]
#me["processors"] = [processor._to_config_dict(configDict) for processor in self.get_processors()]
configDict["hostgroups"][self.get_name()] = me
def __add_internal(self,l,item):
def __add_internal(self, l, item):
if isinstance(item, list):
l.extend(item)
else:
@ -91,7 +89,6 @@ class HostGroup:
def get_members(self):
return self.members
def __str__(self):
ret = "HostGroup: " + self.name + " threshold: " + str(self.threshold) + " parent: " + self.parent + "\n"
@ -140,4 +137,4 @@ def parseHostGroupList(hostgroups, hosts, members, periods, services, log):
hostGroupPeriods = [p for p in periods if p.name in servicePeriods]
hostGroup.services.append(HostGroupService(services, hostGroupPeriods))
parsedHostGroups.append(hostGroup)
return parsedHostGroups
return parsedHostGroups

View file

@ -115,5 +115,4 @@ def parseHostList(hosts, services, log):
log.w("Service " + servicename + " not defined in host " + host.name)
#replace host.service member by parsed HostService Objects
host.services = hostServices
return parsedHosts
return parsedHosts

View file

@ -16,7 +16,6 @@ class Layout:
else:
self._hostgroups = hostgroups
def _to_config_dict(self, configDict):
me = {}
me["hostgroups"] = [hg.name for hg in self.get_hostgroups()]
@ -24,8 +23,7 @@ class Layout:
configDict["layouts"][self.get_name()] = me
for hostgroup in self.get_hostgroups():
hostgroup._to_config_dict(configDict)
def get_name(self):
return self._name
@ -35,7 +33,6 @@ class Layout:
def get_hostgroups(self):
return self._hostgroups
def __str__(self):
ret = "Layout: 'Name:" + str(self.name) + "', 'Enabled: " + str(self.enabled) + " "
for group in self.hostgroups:
@ -78,4 +75,4 @@ class LayoutList:
ret += "Plugins: " + str(self.plugins) + "\n"
for layout in self.layouts:
ret += str(layout) + "\n"
return ret
return ret

View file

@ -27,8 +27,6 @@ class Member:
for f in self.filters:
ret += str(f)
return ret
class MemberFilter:
@ -56,4 +54,4 @@ def parseMemberList(members, filters, log):
if not found:
log.w("filter: " + filtername + " is not defined in member " + member.name)
member.filters = mFilter
return parsedMembers
return parsedMembers

View file

@ -1,6 +1,5 @@
from os.path import isfile
import json
import imp
import sys
from layouts import Layout
@ -8,13 +7,11 @@ from hostgroups import HostGroup
from members import Member
from periods import CronPeriod, DatePeriod, IntervalPeriod
from lib.services.service import Service
from lib.processors.processor import Processor
from lib.parsers.parser import Parser
from lib.tasks.task import Task
MOD_SERVICES = "services"
MOD_PROCESSORS = "processors"
MOD_PARSERS = "parsers"
@ -40,15 +37,14 @@ class ConfigurationException(Exception):
return repr(self.msg)
class ConfigParser:
def __init__(self, log):
'''
"""
initializes a new ConfigParser Object
params:
log: pre configured logger Object to post messages while parsing"
'''
"""
self.log = log
self.hostgroups = {}
self.members = {}
@ -65,12 +61,12 @@ class ConfigParser:
layout._to_config_dict(configDict)
def _read_json_config(self, configFilename):
'''
"""
reads the config File and returns a dictionary, while lowering the first keys
params:
configFilename: the path under which the configuration file should be found
'''
"""
if not isfile(configFilename):
msg = "config file not found at " + str(configFilename)
raise ConfigurationException(msg, self.log)
@ -84,11 +80,11 @@ class ConfigParser:
return json.loads(config)
def _get_as_list(self, configValue):
'''
"""
In some cases the config permits to define a list or a single value.
returns the value as list
'''
"""
return configValue if isinstance(configValue, list) else [configValue]
def _create_raw_Object(self, jsonDict, msgName, creator):
@ -114,9 +110,9 @@ class ConfigParser:
return layouts
def create_hostgroups_from_json(self, jsonHostGroups):
'''
"""
creates Hostgroups from the jsonConfig
'''
"""
hostgroups = []
for hgName, hgValues in jsonHostGroups.items():
try:
@ -128,9 +124,9 @@ class ConfigParser:
return hostgroups
def create_members_from_json(self, jsonMembers):
'''
"""
creates Members from the jsonConfig
'''
"""
members = []
for memberName, memberValues in jsonMembers.items():
try:
@ -172,7 +168,6 @@ class ConfigParser:
self.log.w("Error while replace: " + str(Exception))
del items[:]
items.extend(repl)
def replace_pointer(self, objectList, replObjectList, id_list_func, id_get_func):
for obj in objectList:
@ -186,12 +181,8 @@ class ConfigParser:
del idList[:]
idList.extend(replacements)
def parse_config(self, configFilename):
'''
"""
parses the json configuration and returns a list of layouts,
which contains all nessesary information of the config file.
It will only parse nessesary Objects.
@ -202,7 +193,7 @@ class ConfigParser:
params:
configFilename: indicates which configuration file to parse
'''
"""
self.jsonDict = self._read_json_config(configFilename)
@ -214,8 +205,7 @@ class ConfigParser:
for layout in layouts:
for hgName in layout.get_hostgroups():
hostgroupNames.add(hgName)
jsonHostgroups = {}
for hgName in hostgroupNames:
if not hgName in self.jsonDict[KEY_HOSTGROUPS]:
@ -244,8 +234,6 @@ class ConfigParser:
self.members = self.create_members_from_json(jsonMembers)
def parsePeriodList(name, values):
if "date" in values:
return DatePeriod(name, **values)
@ -257,18 +245,13 @@ def parsePeriodList(name, values):
comp = ["year", "month", "day", "week", "day_of_week", "hour", "minute", "second"]
if len([i for i in comp if i in values]) > 0 :
return CronPeriod(name, **values)
else:
raise ConfigurationException("could not determine correct Period(" + repr(values)+").")
class FullConfigParser(ConfigParser):
def parse_config(self, configFilename):
'''
"""
parses the json configuration and returns a list of layouts,
which contains all nessesary information of the config file.
parses the full config
@ -279,7 +262,7 @@ class FullConfigParser(ConfigParser):
params:
configFilename: indicates which configuration file to parse
'''
"""
self.jsonDict = self._read_json_config(configFilename)
# first step
@ -328,8 +311,4 @@ class FullConfigParser(ConfigParser):
id_get_func = lambda hostgroup: hostgroup.get_name()
self.replace_pointer(layouts, hostgroups, id_list_func, id_get_func)
return layouts
return layouts

View file

@ -66,8 +66,4 @@ class DatePeriod(Period):
return ret
def createJob(self, scheduler, jobInfo, func):
return scheduler.add_date_job(func, self.date, jobInfo)
return scheduler.add_date_job(func, self.date, jobInfo)

View file

@ -1,4 +1,3 @@
class Parser:
def __init__(self):
pass

View file

@ -4,7 +4,7 @@ The MongoDB processor
from processor import Processor
class Mongodb(Processor):
def __init__(self):
pass
pass

View file

@ -1,8 +1,10 @@
'''
"""
Created on Jun 16, 2013
@author: rafael
'''
"""
class Processor:
def __init__(self):
pass

View file

@ -1,5 +0,0 @@
"""
The snmpget service in pure Python.
"""
# http://pysnmp.sourceforge.net/

View file

@ -40,7 +40,6 @@ class HttpService(Service):
def needs_arguments(self):
return True
def execute(self):
params = urllib.urlencode(self.params)
@ -50,6 +49,7 @@ class HttpService(Service):
f = urllib.urlopen(self.protocol + "://" + self._host + ":" + self.port + self.path, params)
#print f.read()
def create(**kwargs):
return HttpService(**kwargs)

View file

@ -5,10 +5,11 @@ The ping service in pure Python.
# http://code.activestate.com/recipes/409689-icmplib-library-for-creating-and-reading-icmp-pack/
from service import Service
class PingService(Service):
def __init__(self, **kwargs):
super(PingService, self).__init__(**kwargs)
def create(self, **kwargs):
return PingService(**kwargs)
return PingService(**kwargs)

View file

@ -6,6 +6,7 @@ KEY_FAILS = "fails"
KEY_PERIODS = "periods"
KEY_ARGS = "args"
class Service:
def __init__(self, **kwargs):
@ -87,8 +88,7 @@ class Service:
def get_parser(self):
return self._parser
def add_parser(self, parser):
if parser is not None:
if isinstance(parser, list):
@ -115,7 +115,6 @@ class Service:
result = []
for parser in self.get_parser():
result.append(self._parser._parse(executionResult))
def handle_result(self, parseResult):
pass
pass

View file

@ -17,10 +17,10 @@ class ShellService(Service):
def needs_arguments(self):
return True
def execute(self):
self.command.call()
def create(**kwargs):
return ShellService(**kwargs)

View file

@ -50,6 +50,7 @@ class SnmpgetService(Service):
# else:
# for name, val in varBinds:
# print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
def create(**kargs):
return SnmpgetService(**kwargs)

View file

@ -39,6 +39,7 @@ class SshService(Service):
print '... ' + line.strip('\n')
client.close()
def create(**kwargs):
return SshService(**kwargs)

View file

@ -18,7 +18,6 @@ class TcpconnectService(Service):
self.port = args["port"]
else:
raise Exception("There is no port set")
def needs_arguments(self):
return True
@ -38,6 +37,7 @@ class TcpconnectService(Service):
sock.close()
return
def create(**kwargs):
return TcpconnectService(**kwargs)
return TcpconnectService(**kwargs)

View file

@ -4,6 +4,7 @@ The email task.
from lib.tasks.task import Task
class EmailTask(Task):
def __init__(self, **kwargs):
if not "type" in kwargs:
@ -12,13 +13,10 @@ class EmailTask(Task):
raise Exception("typeDict " + str(kwargs) + " has nor arguments!")
self.set_task_type(kwargs["type"])
self.recipient = kwargs["args"]["rcpt"]
def execute_task(self, msg):
pass
def creator(**taskDict):
return EmailTask(**taskDict)

View file

@ -4,6 +4,7 @@ The sms task.
from task import Task
class SmsTask(Task):
def __init__(self, **kwargs):
if not "type" in kwargs:
@ -12,13 +13,10 @@ class SmsTask(Task):
raise Exception("typeDict " + str(kwargs) + " has no arguments!")
self.set_task_type(kwargs["type"])
self.recipient = kwargs["args"]["rcpt"]
def execute_task(self, msg):
pass
def creator(**taskDict):
return SmsTask(**taskDict)
return SmsTask(**taskDict)

View file

@ -1,35 +1,36 @@
'''
"""
Created on Jun 15, 2013
@author: Rafael Timmerberg
'''
"""
class Task:
'''
"""
Base class for all built-in Tasks.
'''
"""
def set_task_type(self, taskType):
'''
"""
sets the type of this task.
Be aware! this method can only get called once!
params:
taskType: the type of this task
'''
"""
if hasattr(self, "_taskType"):
raise Exception("taskType is only allowed to set once!")
self.taskType = taskType
def get_task_type(self):
'''
"""
returns the type set by set_type_task
'''
"""
return self._taskType
def execute_task(self, msg):
'''
"""
this is the method tasks usually override.
It gets called anytime the task should get executed
@ -37,11 +38,11 @@ class Task:
params:
msg: the msg for this task
'''
"""
pass
def _execute(self,taskType, msg):
'''
def _execute(self, taskType, msg):
"""
internal method which gets called for any member in a hostgroup.
It determines if it has an appropriate type by comparing taskType with get_task_type().
Calls execute_task() if the type matches
@ -52,9 +53,8 @@ class Task:
return:
True if execute_task() is called succesfully, else False
'''
"""
if self.get_task_type() == taskType:
self.execute_task(msg)
return True
return False
return False

View file

@ -4,6 +4,7 @@ The xmpp task.
from task import Task
class XmppTask(Task):
def __init__(self, **kwargs):
if not "type" in kwargs:
@ -12,13 +13,10 @@ class XmppTask(Task):
raise Exception("typeDict " + str(kwargs) + " has nor arguments!")
self.set_task_type(kwargs["type"])
self.recipient = kwargs["args"]["rcpt"]
def execute_task(self, msg):
pass
def creator(**taskDict):
return XmppTask(taskDict)