changed task parsing and handling
This commit is contained in:
parent
4be1a6ecb9
commit
a670770dde
7 changed files with 82 additions and 73 deletions
|
|
@ -93,7 +93,7 @@ def handle_job(jobInfo):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
global linspector_configuration
|
global linConf
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
|
|
||||||
logfile = path.expanduser(args.logfile)
|
logfile = path.expanduser(args.logfile)
|
||||||
|
|
@ -109,7 +109,7 @@ def main():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
config_parser = FullConfigParser()
|
config_parser = FullConfigParser()
|
||||||
linspector_configuration, core = config_parser.parse_config(args.config)
|
linConf, core = config_parser.parse_config(args.config)
|
||||||
except Exception, msg:
|
except Exception, msg:
|
||||||
print("Configuration error: " + str(msg) + ". Exiting now.")
|
print("Configuration error: " + str(msg) + ". Exiting now.")
|
||||||
logger.error(msg)
|
logger.error(msg)
|
||||||
|
|
@ -124,7 +124,7 @@ def main():
|
||||||
start_date = datetime.datetime.now()
|
start_date = datetime.datetime.now()
|
||||||
time_delta = 0
|
time_delta = 0
|
||||||
jobs = []
|
jobs = []
|
||||||
for layout in linspector_configuration.get_enabled_layouts():
|
for layout in linConf.get_enabled_layouts():
|
||||||
for hostgroup in layout.get_hostgroups():
|
for hostgroup in layout.get_hostgroups():
|
||||||
for service in hostgroup.get_services():
|
for service in hostgroup.get_services():
|
||||||
for host in hostgroup.get_hosts():
|
for host in hostgroup.get_hosts():
|
||||||
|
|
@ -136,13 +136,14 @@ def main():
|
||||||
hostgroup.get_members(),
|
hostgroup.get_members(),
|
||||||
hostgroup.get_processors(),
|
hostgroup.get_processors(),
|
||||||
core,
|
core,
|
||||||
|
linConf.get_task_list(),
|
||||||
hostgroup)
|
hostgroup)
|
||||||
scheduler_job = period.createJob(scheduler, job, handle_job, start_date=new_start_date)
|
scheduler_job = period.createJob(scheduler, job, handle_job, start_date=new_start_date)
|
||||||
if scheduler_job is not None:
|
if scheduler_job is not None:
|
||||||
job.set_job(scheduler_job)
|
job.set_job(scheduler_job)
|
||||||
jobs.append(job)
|
jobs.append(job)
|
||||||
|
|
||||||
interface = LinspectorInterface(jobs, scheduler, linspector_configuration)
|
interface = LinspectorInterface(jobs, scheduler, linConf)
|
||||||
|
|
||||||
if "jsonrpc_backend" in core and core["jsonrpc_backend"]:
|
if "jsonrpc_backend" in core and core["jsonrpc_backend"]:
|
||||||
jsonrpc = JsonrpcBackend(interface, core)
|
jsonrpc = JsonrpcBackend(interface, core)
|
||||||
|
|
|
||||||
|
|
@ -1,74 +1,59 @@
|
||||||
{
|
{
|
||||||
|
"tasks": {
|
||||||
|
|
||||||
|
"mail":{
|
||||||
|
"host": "a.mx.systemchaos.org",
|
||||||
|
"port": 25,
|
||||||
|
"from": "linspector@systemchaos.org",
|
||||||
|
"username": "you@hanez.org",
|
||||||
|
"password": "uffff"
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
"members":{
|
"members":{
|
||||||
"homer":{
|
"root":{
|
||||||
"name": "Homer Simpson",
|
"name": "root",
|
||||||
"comment": "Security Inspector",
|
"_tasks": [
|
||||||
"tasks": [
|
{ "class":"jabber", "type": "jabber", "args": { "rcpt": "hanez@systemchaos.org" }}
|
||||||
{"class":"mail", "type": "donut", "args": {"rcpt": "homer_j_simpson@example.sp"}},
|
|
||||||
{"class":"mail", "type": "donut", "args": {"rcpt": "homer_j_simpson@example.sp"}}
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"periods": {
|
"periods": {
|
||||||
"doh": {"seconds": 10, "comment": "OMG, this means work"},
|
"fast": { "seconds": 15 },
|
||||||
"moes_time": {"minute":"0", "hour": "12", "day_of_week": "4", "comment": "much better"},
|
"medium": { "seconds": 120 },
|
||||||
"marges_birthday": { "date": "2017-2-24 04:00:00"}
|
"slow": { "seconds": 480 }
|
||||||
},
|
},
|
||||||
"hostgroups":{
|
"hostgroups":{
|
||||||
"power_plant":{
|
"a":{
|
||||||
"members": ["homer"],
|
"members": ["hanez"],
|
||||||
"hosts": ["powerplant.example.com"],
|
"hosts": [ "a", "b", "c", "d", "e"],
|
||||||
"processors":[
|
|
||||||
{"class": "mongodb", "args":{ "host": "mongodb.example.org", "user": "homer", "password": "useless", "database": "default" }}
|
|
||||||
],
|
|
||||||
"services":[
|
"services":[
|
||||||
{
|
{ "class": "dummy", "args": { "sleep": 1, "fail": 1 }, "periods": ["fast"], "threshold": 3 },
|
||||||
"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"],
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"layouts":{
|
"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":{
|
"core":{
|
||||||
|
"instance_name": "Master Monitor 01 (monitor01.systemchaos.org)",
|
||||||
"max_logfile_size": 1024000,
|
"max_logfile_size": 1024000,
|
||||||
"max_logfile_count": 4,
|
"max_logfile_count": 4,
|
||||||
"max_worker_threads": 8,
|
"max_worker_threads": 1000,
|
||||||
"members":[ "root" ],
|
"threshold_handling": "reset",
|
||||||
"tasks":{
|
"shutdown_wait": "False",
|
||||||
"jabber":{
|
"members":[ "linspector", "root" ],
|
||||||
"host": "example.org",
|
"jsonrpc_backend": "True",
|
||||||
"port": 5222,
|
"backends": {
|
||||||
"username": "USERNAME",
|
"jsonrpc": {
|
||||||
"password": "PASSWORD"
|
"_comment": "https://github.com/linspector/linspector/wiki/Configuration-Core-Backends-JsonRPC",
|
||||||
},
|
"host": "127.0.0.1",
|
||||||
"mail":{
|
"port": 10123,
|
||||||
"host": "localhost",
|
"username": "linspector",
|
||||||
"port": 25,
|
"password": "linspector"
|
||||||
"from": "linspector@example.org"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ class LinspectorConfig(object):
|
||||||
self._hostgroups = None
|
self._hostgroups = None
|
||||||
self._members = None
|
self._members = None
|
||||||
self._periods = None
|
self._periods = None
|
||||||
|
self._tasks = None
|
||||||
|
|
||||||
def set_hostgroups(self, hostgroups):
|
def set_hostgroups(self, hostgroups):
|
||||||
self._hostgroups = hostgroups
|
self._hostgroups = hostgroups
|
||||||
|
|
@ -75,3 +76,9 @@ class LinspectorConfig(object):
|
||||||
|
|
||||||
def get_period_by_name(self, name):
|
def get_period_by_name(self, name):
|
||||||
return self._get_by_name(self.get_periods(), 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
|
||||||
|
|
@ -32,7 +32,7 @@ from periods import CronPeriod, DatePeriod, IntervalPeriod
|
||||||
from linspector.services.service import Service
|
from linspector.services.service import Service
|
||||||
from linspector.processors.processor import Processor
|
from linspector.processors.processor import Processor
|
||||||
from linspector.parsers.parser import Parser
|
from linspector.parsers.parser import Parser
|
||||||
from linspector.tasks.task import Task
|
from linspector.tasks.TaskList import TaskList
|
||||||
|
|
||||||
MOD_SERVICES = "services"
|
MOD_SERVICES = "services"
|
||||||
MOD_PROCESSORS = "processors"
|
MOD_PROCESSORS = "processors"
|
||||||
|
|
@ -44,6 +44,7 @@ KEY_HOSTGROUPS = "hostgroups"
|
||||||
KEY_MEMBERS = "members"
|
KEY_MEMBERS = "members"
|
||||||
KEY_PERIODS = "periods"
|
KEY_PERIODS = "periods"
|
||||||
KEY_CORE = "core"
|
KEY_CORE = "core"
|
||||||
|
KEY_TASKS = "tasks"
|
||||||
|
|
||||||
logger = getLogger(__name__)
|
logger = getLogger(__name__)
|
||||||
|
|
||||||
|
|
@ -69,7 +70,7 @@ class ConfigParser:
|
||||||
self._loadedMods = {MOD_SERVICES: {}, MOD_PROCESSORS: {}, MOD_TASKS: {}, MOD_PARSERS: {}}
|
self._loadedMods = {MOD_SERVICES: {}, MOD_PROCESSORS: {}, MOD_TASKS: {}, MOD_PARSERS: {}}
|
||||||
|
|
||||||
def _create_new_config_dict(self):
|
def _create_new_config_dict(self):
|
||||||
return {"members": {}, "periods": {}, "hostgroups": {}, "layouts": {}, "core": {}}
|
return {"members": {}, "periods": {}, "hostgroups": {}, "layouts": {}, "core": {}, "tasks": {}}
|
||||||
|
|
||||||
def create_config(self, config):
|
def create_config(self, config):
|
||||||
configDict = self._create_new_config_dict()
|
configDict = self._create_new_config_dict()
|
||||||
|
|
@ -217,6 +218,9 @@ def parsePeriodList(name, values):
|
||||||
|
|
||||||
|
|
||||||
class FullConfigParser(ConfigParser):
|
class FullConfigParser(ConfigParser):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def parse_config(self, configFilename):
|
def parse_config(self, configFilename):
|
||||||
"""
|
"""
|
||||||
parses the json configuration and returns a list of layouts,
|
parses the json configuration and returns a list of layouts,
|
||||||
|
|
@ -232,7 +236,7 @@ class FullConfigParser(ConfigParser):
|
||||||
|
|
||||||
self.dict = self._read_config_file(configFilename)
|
self.dict = self._read_config_file(configFilename)
|
||||||
|
|
||||||
# first step
|
#1.
|
||||||
creator = lambda name, values: Layout(name, **values)
|
creator = lambda name, values: Layout(name, **values)
|
||||||
layouts = self._create_raw_Object(self.dict[KEY_LAYOUTS], "Layout", creator)
|
layouts = self._create_raw_Object(self.dict[KEY_LAYOUTS], "Layout", creator)
|
||||||
|
|
||||||
|
|
@ -245,6 +249,12 @@ class FullConfigParser(ConfigParser):
|
||||||
creator = parsePeriodList
|
creator = parsePeriodList
|
||||||
periods = self._create_raw_Object(self.dict[KEY_PERIODS], "Period", creator)
|
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
|
#2. import and replace
|
||||||
items_func = lambda hostgroup: hostgroup.get_services()
|
items_func = lambda hostgroup: hostgroup.get_services()
|
||||||
class_check = lambda service: isinstance(service, Service)
|
class_check = lambda service: isinstance(service, Service)
|
||||||
|
|
@ -254,9 +264,9 @@ class FullConfigParser(ConfigParser):
|
||||||
class_check = lambda processor: isinstance(processor, Processor)
|
class_check = lambda processor: isinstance(processor, Processor)
|
||||||
self.replace_with_import(self.hostgroups, MOD_PROCESSORS, items_func, class_check)
|
self.replace_with_import(self.hostgroups, MOD_PROCESSORS, items_func, class_check)
|
||||||
|
|
||||||
items_func = lambda member: member.get_tasks()
|
#items_func = lambda member: member.get_tasks()
|
||||||
class_check = lambda task: isinstance(task, Task)
|
#class_check = lambda task: isinstance(task, Task)
|
||||||
self.replace_with_import(members, MOD_TASKS, items_func, class_check)
|
#self.replace_with_import(members, MOD_TASKS, items_func, class_check)
|
||||||
|
|
||||||
services = []
|
services = []
|
||||||
for hg in self.hostgroups:
|
for hg in self.hostgroups:
|
||||||
|
|
@ -284,6 +294,8 @@ class FullConfigParser(ConfigParser):
|
||||||
linConf.set_hostgroups(self.hostgroups)
|
linConf.set_hostgroups(self.hostgroups)
|
||||||
linConf.set_members(members)
|
linConf.set_members(members)
|
||||||
linConf.set_periods(periods)
|
linConf.set_periods(periods)
|
||||||
|
linConf.set_task_list(taskList)
|
||||||
|
|
||||||
|
|
||||||
for hg in self.hostgroups:
|
for hg in self.hostgroups:
|
||||||
for service in hg.get_services():
|
for service in hg.get_services():
|
||||||
|
|
|
||||||
|
|
@ -35,12 +35,13 @@ def generateId():
|
||||||
|
|
||||||
|
|
||||||
class Job:
|
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.service = service
|
||||||
self.host = host
|
self.host = host
|
||||||
self.members = members
|
self.members = members
|
||||||
self.processors = processors
|
self.processors = processors
|
||||||
self.core = core
|
self.core = core
|
||||||
|
self.taskList = taskList
|
||||||
self.hostgroup = hostgroup
|
self.hostgroup = hostgroup
|
||||||
#TODO: limit the size of this list; else it is a memory leak
|
#TODO: limit the size of this list; else it is a memory leak
|
||||||
self.jobInfos = []
|
self.jobInfos = []
|
||||||
|
|
@ -90,9 +91,8 @@ class Job:
|
||||||
|
|
||||||
def handle_alarm(self, jobInfo, thresholdOffset):
|
def handle_alarm(self, jobInfo, thresholdOffset):
|
||||||
for member in self.members:
|
for member in self.members:
|
||||||
#TODO: Put Tasks in a run queue and execute them in a background thread. FIFO! Reduces delay in core.
|
self.taskList.execute_task_infos(jobInfo.get_message(), member.get_tasks())
|
||||||
for task in member.get_tasks():
|
|
||||||
task.execute(jobInfo.get_message(), self.core)
|
|
||||||
|
|
||||||
def handle_call(self):
|
def handle_call(self):
|
||||||
logger.debug("handle call")
|
logger.debug("handle call")
|
||||||
|
|
|
||||||
|
|
@ -51,17 +51,20 @@ class MailTask(Task):
|
||||||
#self.set_task_type(kwargs["type"])
|
#self.set_task_type(kwargs["type"])
|
||||||
#self.recipient = kwargs["args"]["rcpt"]
|
#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 = MIMEText(msg)
|
||||||
message['Subject'] = msg
|
message['Subject'] = msg
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
message['Date'] = now.strftime("%a, %d %b %Y %H:%M:%S")
|
message['Date'] = now.strftime("%a, %d %b %Y %H:%M:%S")
|
||||||
#TODO: totally unstable just to use values from core. make checks before...!
|
#TODO: totally unstable just to use values from core. make checks before...!
|
||||||
message['From'] = self.fromName
|
message['From'] = self.fromName
|
||||||
message['To'] = self.recipient
|
message['To'] = taskArgs["recipient"]
|
||||||
s = smtplib.SMTP(self.host, self.port)
|
s = smtplib.SMTP(self.host, self.port)
|
||||||
s.login(self.userName, self.password)
|
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()
|
s.quit()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,9 +30,10 @@ class Task:
|
||||||
|
|
||||||
def set_task_type(self, taskType):
|
def set_task_type(self, taskType):
|
||||||
self._taskType = taskType
|
self._taskType = taskType
|
||||||
|
return self
|
||||||
|
|
||||||
def get_task_type(self):
|
def get_task_type(self):
|
||||||
return self._taskType
|
return self._taskType
|
||||||
|
|
||||||
def execute(self, msg):
|
def execute(self, msg, args):
|
||||||
pass
|
pass
|
||||||
Loading…
Add table
Add a link
Reference in a new issue