From 0bfa4b3daabca6f1095d7717e5eb5277b17f1adf Mon Sep 17 00:00:00 2001 From: Johannes Findeisen Date: Fri, 20 Sep 2013 01:23:50 +0200 Subject: [PATCH] added core config to jobs and tasks and made use of core variables raw in the tasks --- lib/core/job.py | 13 +++++++------ lib/services/tcpconnect.py | 12 ++++++------ lib/tasks/jabber.py | 9 +++++---- lib/tasks/mail.py | 8 ++++---- linspector | 2 +- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/core/job.py b/lib/core/job.py index 2d08c4c..6f8be0c 100644 --- a/lib/core/job.py +++ b/lib/core/job.py @@ -14,11 +14,12 @@ def generateId(): class Job: - def __init__(self, service, host, members, processors): + def __init__(self, service, host, members, processors, core): self.service = service self.host = host self.members = members self.processors = processors + self.core = core self.jobInfos = [] self.jobThreshold = 0 @@ -31,7 +32,7 @@ class Job: def set_job(self, job): self.job = job - def handle_threshold(self, serviceThreshold, executionSucessful): + def handle_threshold(self, jobInfo, serviceThreshold, executionSucessful): if executionSucessful: if self.jobThreshold > 0: self.jobThreshold -= 1 @@ -40,12 +41,12 @@ class Job: if self.jobThreshold >= serviceThreshold: self.log.d("Threshold reached!") - self.handle_alarm(self.jobThreshold - serviceThreshold) + self.handle_alarm(jobInfo, self.jobThreshold - serviceThreshold) - def handle_alarm(self, thresholdOffset): + def handle_alarm(self, jobInfo, thresholdOffset): for member in self.service.get_hostgroup().get_members(): for task in member.get_tasks(): - task.execute("Task executed for host: " + self.host) + task.execute(jobInfo.get_message(), self.core) def handle_call(self): self.log.d("handle call") @@ -55,7 +56,7 @@ class Job: self.service._execute(jobInfo) jobInfo.set_execution_end() - self.handle_threshold(self.service.get_threshold(), jobInfo.was_execution_successful()) + self.handle_threshold(jobInfo, self.service.get_threshold(), jobInfo.was_execution_successful()) self.log.d("Code: " + str(jobInfo.get_errorcode()) + ", Message: " + str(jobInfo.get_message())) diff --git a/lib/services/tcpconnect.py b/lib/services/tcpconnect.py index 8a498fa..86b1b66 100644 --- a/lib/services/tcpconnect.py +++ b/lib/services/tcpconnect.py @@ -27,21 +27,21 @@ class TcpconnectService(Service): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error, msg: jobInfo.set_errorcode(2) - jobInfo.set_message("Could not create socket to host: " + jobInfo.get_host() + " on port: " + - str(self.port) + " (" + str(msg) + ")") + jobInfo.set_message("[tcpconnect] Could not create socket to host: " + jobInfo.get_host() + + " on port: " + str(self.port) + " (" + str(msg) + ")") try: sock.connect((jobInfo.get_host(), self.port)) except socket.error, msg: jobInfo.set_errorcode(1) - jobInfo.set_message("Could not establish connection to host: " + jobInfo.get_host() + " on port: " + - str(self.port) + " (" + str(msg) + ")") + jobInfo.set_message("[tcpconnect] Could not establish connection to host: " + jobInfo.get_host() + + " on port: " + str(self.port) + " (" + str(msg) + ")") if jobInfo.get_errorcode() == -1: jobInfo.set_execution_successful(True) jobInfo.set_errorcode(0) - jobInfo.set_message("Connection successful established to host: " + jobInfo.get_host() + " on port: " + - str(self.port)) + jobInfo.set_message("[tcpconnect] Connection successful established to host: " + jobInfo.get_host() + + " on port: " + str(self.port)) sock.close() diff --git a/lib/tasks/jabber.py b/lib/tasks/jabber.py index fbee02d..ba258f3 100644 --- a/lib/tasks/jabber.py +++ b/lib/tasks/jabber.py @@ -17,10 +17,11 @@ class JabberTask(Task): self.set_task_type(kwargs["type"]) self.recipient = kwargs["args"]["rcpt"] - def execute(self, msg): - client = xmpp.Client('systemchaos.org') - client.connect(server=('systemchaos.org', 5222)) - client.auth('linspector', 'PASSWORD', 'alert') + def execute(self, msg, core): + #TODO: totally unstable just to use values from core. make checks before...! + client = xmpp.Client(core["tasks"]["jabber"]["host"]) + client.connect(server=(core["tasks"]["jabber"]["host"], core["tasks"]["jabber"]["port"])) + client.auth(core["tasks"]["jabber"]["username"], core["tasks"]["jabber"]["password"], 'alert') client.sendInitPresence() message = xmpp.Message(self.recipient, msg) message.setAttr('type', 'chat') diff --git a/lib/tasks/mail.py b/lib/tasks/mail.py index 177fe86..ca3280c 100644 --- a/lib/tasks/mail.py +++ b/lib/tasks/mail.py @@ -19,14 +19,14 @@ class MailTask(Task): self.set_task_type(kwargs["type"]) self.recipient = kwargs["args"]["rcpt"] - def execute(self, msg): + def execute(self, msg, core): message = MIMEText(msg) - message['Subject'] = 'Warning from Linspector' + message['Subject'] = msg now = datetime.datetime.now() message['Date'] = now.strftime("%a, %d %b %Y %H:%M:%S") - message['From'] = "warning@linspector.org" + message['From'] = core["tasks"]["mail"]["from"] message['To'] = self.recipient - s = smtplib.SMTP('localhost') + s = smtplib.SMTP(core["tasks"]["mail"]["host"], core["tasks"]["mail"]["port"]) s.sendmail("warning@linspector.org", self.recipient, message.as_string()) s.quit() diff --git a/linspector b/linspector index 22100c5..a3f63b4 100755 --- a/linspector +++ b/linspector @@ -67,7 +67,7 @@ def main(): for service in hostgroup.get_services(): for host in hostgroup.get_hosts(): for period in service.get_periods(): - job = Job(service, host, hostgroup.get_members(), hostgroup.get_processors()) + job = Job(service, host, hostgroup.get_members(), hostgroup.get_processors(), core) schedulerJob = period.createJob(scheduler, job, handleJob) if schedulerJob is not None: job.set_job(schedulerJob)