linspector-old/linspector/core/job.py

224 lines
7.2 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_threshold = 0
self.enabled = True
self.scheduler_job = None
self.job_id = 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-11-11 02:21:16 +01:00
self.last_execution = None
2013-11-11 20:38:32 +01:00
self.job_information = JobInformation(self.job_id, hostgroup, host, service, members)
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
2013-11-11 03:34:51 +01:00
def get_job_id(self):
return self.job_id
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
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-11 03:34:51 +01:00
logger.info("Job " + self.get_job_id() + ", Threshold Reset")
self.job_threshold = 0
else:
2013-11-11 03:34:51 +01:00
logger.info("Job " + self.get_job_id() + ", Threshold Decrement")
self.job_threshold -= 1
self.status = "OK"
2013-11-11 20:38:32 +01:00
self.job_information.inc_job_overall_wins()
2013-08-14 00:39:45 +02:00
else:
self.status = "WARNING"
2013-11-11 20:38:32 +01:00
self.job_information.inc_job_overall_fails()
self.job_threshold += 1
2013-08-14 00:39:45 +02:00
if self.job_threshold >= service_threshold:
2013-11-11 03:34:51 +01:00
logger.info("Job " + self.get_job_id() + ", Threshold reached!")
self.status = "ERROR"
2013-11-09 04:29:49 +01:00
2013-11-11 01:44:29 +01:00
def handle_tasks(self, msg):
2013-10-19 00:55:24 +02:00
for member in self.members:
for task in member.get_tasks():
if self.status.lower() == task.get_task_type().lower():
2013-11-09 04:29:49 +01:00
logger.debug("Executing Task of type: " + self.status)
2013-11-11 01:44:29 +01:00
TaskExecutor.Instance().schedule_task(msg, 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-11-17 02:20:35 +01:00
self.job_information.set_execution_start()
2013-11-11 01:44:29 +01:00
self.last_execution = None
2013-10-11 00:03:18 +02:00
try:
2013-11-17 02:20:35 +01:00
self.last_execution = JobExecution(self.get_host(), self.job_information)
2013-11-11 01:44:29 +01:00
self.service.execute(self.last_execution)
2013-10-11 00:03:18 +02:00
except Exception, e:
logger.debug(e)
2013-11-11 01:44:29 +01:00
2013-11-17 02:20:35 +01:00
self.job_information.set_execution_end()
2013-11-11 01:44:29 +01:00
self.handle_threshold(self.service.get_threshold(), self.last_execution.was_successful())
2013-11-11 03:34:51 +01:00
logger.info("Job " + self.get_job_id() +
2013-11-11 04:29:32 +01:00
", Code: " + str(self.last_execution.get_error_code()) +
", Message: " + str(self.last_execution.get_message()))
2013-11-11 01:44:29 +01:00
self.handle_tasks(self.last_execution.get_response_message(self))
2013-10-11 00:19:53 +02:00
else:
2013-11-11 03:34:51 +01:00
logger.info("Job " + self.get_job_id() + " disabled")
2013-10-11 00:03:18 +02:00
2013-08-15 00:35:16 +02:00
def get_host(self):
return self.host
2013-07-02 23:30:31 +02:00
def get_hostgroup(self):
return self.hostgroup
2013-07-02 23:30:31 +02:00
class JobExecution(object):
2013-11-17 02:20:35 +01:00
def __init__(self, host, job_information):
self.execution_start = datetime.now()
self.execution_end = -1
self.host = host
self.error_code = -1
2013-11-11 01:44:29 +01:00
self.message = None
self.kwargs = None
def get_host_name(self):
return self.host
2013-08-15 00:35:16 +02:00
def get_message(self):
return self.message
2013-08-15 00:35:16 +02:00
def get_kwargs(self):
return self.kwargs
def set_execution_end(self):
self.execution_end = datetime.now()
def get_error_code(self):
return self.error_code
def was_successful(self):
return self.get_error_code() == 0
2013-11-11 01:44:29 +01:00
def set_result(self, error_code=0, message="", kwargs=None):
self.error_code = error_code
self.message = message
self.kwargs = kwargs
def get_response_message(self, job):
2013-11-11 03:34:51 +01:00
msg = str(job.status) + " [" + job.service.get_config_name() + ": " + str(job.get_job_id()) + "] " + \
2013-11-11 01:44:29 +01:00
str(job.get_hostgroup()) + " " + str(job.get_host())
if self.get_message() is not None:
msg += " " + str(self.get_message())
2013-11-11 01:44:29 +01:00
if self.get_kwargs() is not None:
msg += " " + str(self.get_kwargs())
return msg
class JobInformation(object):
def __init__(self, job_id, hostgroup, host, service, members):
self.job_id = job_id
self.hostgroup = hostgroup
self.host = host
self.service = service
self.members = members
2013-11-17 02:20:35 +01:00
self.execution_start = -1
self.execution_end = -1
self.period = None
self.next_run = None
self.runs = 0
self.enabled = None
self.threshold = 0
self.fails = 0
self.job_overall_fails = 0
self.job_overall_wins = 0
self.last_execution = None
self.last_run = None
self.last_fail = None
self.last_success = None
self.last_disabled = None
self.last_enabled = None
self.last_threshold_override = None
self.last_escalation = None
self.status = "NONE"
2013-11-17 02:20:35 +01:00
def set_execution_end(self):
self.execution_end = datetime.now()
def set_execution_start(self):
self.execution_start = datetime.now()
def get_job_overall_fails(self):
return self.job_overall_fails
def inc_job_overall_fails(self):
2013-11-11 20:38:32 +01:00
self.job_overall_fails += 1
def get_job_overall_wins(self):
return self.job_overall_wins
def inc_job_overall_wins(self):
self.job_overall_wins += 1