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
|
2013-10-10 21:46:29 +02:00
|
|
|
from binascii import crc32
|
2013-10-27 02:28:20 +01:00
|
|
|
from logging import getLogger
|
2013-11-09 02:20:20 +01:00
|
|
|
from linspector.tasks.task import TaskExecutor
|
2013-10-27 02:28:20 +01:00
|
|
|
|
|
|
|
|
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:
|
2013-11-08 05:52:47 +01:00
|
|
|
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
|
2013-08-15 01:26:28 +02:00
|
|
|
self.members = members
|
2013-09-20 01:23:50 +02:00
|
|
|
self.core = core
|
2013-10-10 23:16:36 +02:00
|
|
|
self.hostgroup = hostgroup
|
2013-11-05 00:24:31 +01:00
|
|
|
self.job_infos = []
|
|
|
|
|
self.job_index = -1
|
|
|
|
|
self.job_info_size = 10
|
|
|
|
|
self.job_threshold = 0
|
2013-11-06 02:59:35 +01:00
|
|
|
self.job_overall_fails = 0
|
|
|
|
|
self.job_overall_wins = 0
|
2013-11-05 00:24:31 +01:00
|
|
|
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()
|
2013-11-06 02:54:59 +01:00
|
|
|
"""
|
|
|
|
|
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__)
|
|
|
|
|
|
2013-10-10 21:46:29 +02:00
|
|
|
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 21:46:29 +02:00
|
|
|
|
2013-10-10 22:24:05 +02:00
|
|
|
def hex_string(self):
|
2013-10-10 21:46:29 +02:00
|
|
|
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-10-10 21:46:29 +02:00
|
|
|
|
2013-11-05 00:24:31 +01:00
|
|
|
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):
|
2013-11-05 00:24:31 +01:00
|
|
|
self.enabled = enabled
|
2013-10-19 00:55:24 +02:00
|
|
|
|
2013-11-05 00:24:31 +01: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:
|
2013-11-05 00:24:31 +01:00
|
|
|
if self.job_threshold > 0:
|
2013-11-06 02:54:59 +01:00
|
|
|
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")
|
2013-11-05 00:24:31 +01:00
|
|
|
self.job_threshold = 0
|
2013-10-11 02:07:55 +02:00
|
|
|
else:
|
2013-11-05 02:57:41 +01:00
|
|
|
logger.info("Job " + self.hex_string() + ", Threshold Decrement")
|
2013-11-05 00:24:31 +01:00
|
|
|
self.job_threshold -= 1
|
2013-11-06 02:54:59 +01:00
|
|
|
|
|
|
|
|
self.status = "OK"
|
2013-11-06 02:59:35 +01:00
|
|
|
self.job_overall_wins += 1
|
2013-08-14 00:39:45 +02:00
|
|
|
else:
|
2013-11-06 02:54:59 +01:00
|
|
|
self.status = "WARNING"
|
2013-11-06 02:59:35 +01:00
|
|
|
self.job_overall_fails += 1
|
2013-11-05 00:24:31 +01:00
|
|
|
self.job_threshold += 1
|
2013-08-14 00:39:45 +02:00
|
|
|
|
2013-11-05 00:24:31 +01:00
|
|
|
if self.job_threshold >= service_threshold:
|
2013-11-05 02:57:41 +01:00
|
|
|
logger.info("Job " + self.hex_string() + ", Threshold reached!")
|
2013-11-06 02:54:59 +01:00
|
|
|
self.status = "ERROR"
|
2013-11-05 00:24:31 +01:00
|
|
|
self.handle_alarm()
|
2013-08-14 00:39:45 +02:00
|
|
|
|
2013-11-05 00:24:31 +01:00
|
|
|
def handle_alarm(self):
|
2013-10-19 00:55:24 +02:00
|
|
|
for member in self.members:
|
2013-11-08 05:52:47 +01:00
|
|
|
for task in member.get_tasks():
|
2013-11-09 02:20:20 +01:00
|
|
|
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):
|
2013-10-27 02:28:20 +01:00
|
|
|
logger.debug("handle call")
|
|
|
|
|
logger.debug(self.service)
|
2013-11-05 00:24:31 +01:00
|
|
|
if self.enabled:
|
2013-10-11 00:03:18 +02:00
|
|
|
try:
|
2013-11-05 00:24:31 +01:00
|
|
|
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
|
|
|
|
2013-11-05 00:24:31 +01:00
|
|
|
self.reset_errorcode(-1)
|
|
|
|
|
self.set_execution_successful(False)
|
2013-10-11 00:03:18 +02:00
|
|
|
except Exception, e:
|
2013-10-27 02:28:20 +01:00
|
|
|
logger.debug(e)
|
2013-10-11 00:19:53 +02:00
|
|
|
else:
|
2013-11-05 02:23:43 +01:00
|
|
|
logger.info("Job " + self.hex_string() + " disabled")
|
2013-10-11 00:03:18 +02:00
|
|
|
|
2013-11-05 00:24:31 +01: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):
|
2013-11-05 00:24:31 +01:00
|
|
|
self.execution_end = datetime.now()
|
2013-07-02 23:30:31 +02:00
|
|
|
|
|
|
|
|
def set_execution_successful(self, successful):
|
2013-11-05 00:24:31 +01:00
|
|
|
self.execution_success = successful
|
2013-08-15 00:35:16 +02:00
|
|
|
|
|
|
|
|
def was_execution_successful(self):
|
2013-11-05 00:24:31 +01:00
|
|
|
return self.execution_success
|
2013-08-15 00:35:16 +02:00
|
|
|
|
|
|
|
|
def set_message(self, msg):
|
2013-11-05 00:24:31 +01:00
|
|
|
self.message = msg
|
2013-08-15 00:35:16 +02:00
|
|
|
|
|
|
|
|
def get_message(self):
|
2013-11-05 00:24:31 +01:00
|
|
|
return self.message
|
2013-08-15 00:35:16 +02:00
|
|
|
|
|
|
|
|
def set_errorcode(self, errcode):
|
2013-11-05 00:24:31 +01:00
|
|
|
self.errorcode = errcode
|
2013-08-15 00:35:16 +02:00
|
|
|
|
|
|
|
|
def get_errorcode(self):
|
2013-11-05 00:24:31 +01:00
|
|
|
return self.errorcode
|