linspector-old/linspector/core/job.py

165 lines
5.3 KiB
Python
Raw Normal View History

2013-05-16 23:03:00 +02:00
"""
2013-05-27 20:27:57 +02:00
This is what job_function needs as parameter for each job to successfully
2013-05-16 23:03:00 +02:00
execute.
2013-10-08 00:08:47 +02:00
2013-10-24 23:29:47 +02:00
Copyright (c) 2011-2013 by Johannes Findeisen and Rafael Timmerberg
2013-10-08 00:08:47 +02:00
This file is part of Linspector (http://linspector.org).
Linspector is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
2013-05-16 23:03:00 +02:00
"""
2013-08-14 20:36:35 +02:00
2013-07-02 23:30:31 +02:00
from datetime import datetime
from binascii import crc32
from logging import getLogger
from linspector.tasks.task import TaskExecutor
logger = getLogger(__name__)
2013-05-30 04:20:35 +02:00
2013-08-13 22:48:51 +02:00
2013-07-02 23:30:31 +02:00
class Job:
def __init__(self, service, host, members, core, hostgroup):
2013-06-21 02:20:58 +02:00
self.service = service
2013-08-14 00:39:45 +02:00
self.host = host
self.members = members
self.core = core
2013-10-10 23:16:36 +02:00
self.hostgroup = hostgroup
self.job_infos = []
self.job_index = -1
self.job_info_size = 10
self.job_threshold = 0
self.job_overall_fails = 0
self.job_overall_wins = 0
self.enabled = True
self.result = None
self.scheduler_job = None
self.execution_begin = datetime.now()
self.execution_end = None
self.errorcode = -1
self.message = None
self.execution_success = False
self.jobHex = self.hex_string()
"""
NONE job was not executed
OK when everything is fine
WARNING when a job has errors but not the threshold overridden
RECOVER when a job recovers e.g. the threshold decrements (not implemented)
ERROR when a jobs threshold is overridden
UNKNOWN when a job throws an exception which is not handled by the job itself (not implemented)
"""
self.status = "NONE"
2013-07-02 23:30:31 +02:00
2013-05-27 01:44:28 +02:00
def __str__(self):
2013-07-02 23:30:31 +02:00
return str(self.__dict__)
def __hex__(self):
2013-10-20 01:55:19 +02:00
return hex(crc32(str(self.hostgroup) + str(self.host) + str(self.service)))
2013-10-10 22:24:05 +02:00
def hex_string(self):
ret = self.__hex__()
if ret[0] == "-":
2013-10-10 22:07:21 +02:00
ret = ret[3:]
else:
ret = ret[2:]
2013-10-10 22:24:05 +02:00
while len(ret) < 8:
ret = "0" + ret
2013-10-10 22:07:21 +02:00
return ret
def set_job(self, scheduler_job):
self.scheduler_job = scheduler_job
2013-06-21 02:20:58 +02:00
2013-10-19 00:55:24 +02:00
def set_enabled(self, enabled=True):
self.enabled = enabled
2013-10-19 00:55:24 +02:00
def add_job_info(self, job_info):
self.job_index += 1
if self.job_index > self.job_info_size:
self.job_index = 0
self.job_infos[self.job_index] = job_info
2013-11-03 05:47:44 +01:00
2013-11-06 05:07:58 +01:00
def handle_threshold(self, service_threshold, execution_successful):
if execution_successful:
if self.job_threshold > 0:
if "threshold_reset" in self.core and self.core["threshold_reset"]:
2013-11-05 02:57:41 +01:00
logger.info("Job " + self.hex_string() + ", Threshold Reset")
self.job_threshold = 0
else:
2013-11-05 02:57:41 +01:00
logger.info("Job " + self.hex_string() + ", Threshold Decrement")
self.job_threshold -= 1
self.status = "OK"
self.job_overall_wins += 1
2013-08-14 00:39:45 +02:00
else:
self.status = "WARNING"
self.job_overall_fails += 1
self.job_threshold += 1
2013-08-14 00:39:45 +02:00
if self.job_threshold >= service_threshold:
2013-11-05 02:57:41 +01:00
logger.info("Job " + self.hex_string() + ", Threshold reached!")
self.status = "ERROR"
self.handle_alarm()
2013-08-14 00:39:45 +02:00
def handle_alarm(self):
2013-10-19 00:55:24 +02:00
for member in self.members:
for task in member.get_tasks():
TaskExecutor.Instance().schedule_task(self.get_message(), task)
2013-11-03 04:57:09 +01:00
2013-06-21 02:20:58 +02:00
def handle_call(self):
logger.debug("handle call")
logger.debug(self.service)
if self.enabled:
2013-10-11 00:03:18 +02:00
try:
self.service.execute(self)
self.set_execution_end()
self.handle_threshold(self.service.get_threshold(), self.was_execution_successful())
2013-11-05 04:48:03 +01:00
logger.info("Job " + self.hex_string() +
", Code: " + str(self.get_errorcode()) +
", Message: " + str(self.get_message()))
2013-08-14 00:39:45 +02:00
self.reset_errorcode(-1)
self.set_execution_successful(False)
2013-10-11 00:03:18 +02:00
except Exception, e:
logger.debug(e)
2013-10-11 00:19:53 +02:00
else:
logger.info("Job " + self.hex_string() + " disabled")
2013-10-11 00:03:18 +02:00
def reset_errorcode(self, errorcode):
self.errorcode = errorcode
2013-08-15 00:35:16 +02:00
def get_host(self):
return self.host
2013-07-02 23:30:31 +02:00
def set_result(self, result):
self.result = result
def set_execution_end(self):
self.execution_end = datetime.now()
2013-07-02 23:30:31 +02:00
def set_execution_successful(self, successful):
self.execution_success = successful
2013-08-15 00:35:16 +02:00
def was_execution_successful(self):
return self.execution_success
2013-08-15 00:35:16 +02:00
def set_message(self, msg):
self.message = msg
2013-08-15 00:35:16 +02:00
def get_message(self):
return self.message
2013-08-15 00:35:16 +02:00
def set_errorcode(self, errcode):
self.errorcode = errcode
2013-08-15 00:35:16 +02:00
def get_errorcode(self):
return self.errorcode