changed task parsing and handling

This commit is contained in:
RafTim 2013-11-03 04:57:09 +01:00
commit a670770dde
7 changed files with 82 additions and 73 deletions

View file

@ -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)

View file

@ -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"
}
}
}

View file

@ -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
@ -75,3 +76,9 @@ class LinspectorConfig(object):
def get_period_by_name(self, name):
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

View file

@ -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():

View file

@ -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")

View file

@ -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()

View file

@ -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