Simplified logging, enabled start_date in monitor configuration and enabled timezone configuration.

This commit is contained in:
Johannes Findeisen 2022-10-01 06:05:30 +02:00
commit 2178452f39
8 changed files with 65 additions and 29 deletions

View file

@ -34,7 +34,7 @@ from linspector.core.monitors import Monitors
# i currently only set the 3rd number because the goal is that 0.19 will become the first stable # i currently only set the 3rd number because the goal is that 0.19 will become the first stable
# version. # version.
__version__ = '0.19.13.dev1' __version__ = '0.19.14.dev1'
__author__ = 'Johannes Findeisen <you@hanez.org>' __author__ = 'Johannes Findeisen <you@hanez.org>'
logger = logging.getLogger('linspector') logger = logging.getLogger('linspector')

View file

@ -4,7 +4,7 @@
error_receivers = admin@example.com error_receivers = admin@example.com
log_file = ~/code/linspector/linspector/log/linspector.log log_file = ~/code/linspector/linspector/log/linspector.log
; available log levels are: "error", "warning", "info" and "debug". ; available log levels are: "error", "warning", "info" and "debug".
log_level= info log_level= x
log_count = 5 log_count = 5
log_size = 10485760 log_size = 10485760
pid_file = /var/run/user/1000/linspector.pid pid_file = /var/run/user/1000/linspector.pid
@ -27,6 +27,7 @@ max_threads = 3500
; i recommend to set this to your number of cpu cores available when running on a dedicated linspector host. but if the ; i recommend to set this to your number of cpu cores available when running on a dedicated linspector host. but if the
; system is running other services you should lower this value when you have too high cpu load. ; system is running other services you should lower this value when you have too high cpu load.
max_processes = 24 max_processes = 24
; if timezone is not set, UTC is used by default.
timezone = CET timezone = CET
; this is for scheduling jobs to not run all at the same time. this should be set to the lowest interval you use. it ; this is for scheduling jobs to not run all at the same time. this should be set to the lowest interval you use. it
; can be set to a lower value if you only have a small amount of services you are monitoring. i recommend the lowest ; can be set to a lower value if you only have a small amount of services you are monitoring. i recommend the lowest

View file

@ -19,8 +19,7 @@ class Configuration:
self.__configuration_path = configuration_path self.__configuration_path = configuration_path
self.__environment = environment self.__environment = environment
log('info', __name__, 'reading configuration file: ' + configuration_path + log('info', 'reading configuration file: ' + configuration_path + '/linspector.conf')
'/linspector.conf')
if os.path.isfile(configuration_path + '/linspector.conf'): if os.path.isfile(configuration_path + '/linspector.conf'):
try: try:
self.__configuration.read(configuration_path + '/linspector.conf', 'utf-8') self.__configuration.read(configuration_path + '/linspector.conf', 'utf-8')

View file

@ -3,19 +3,30 @@ This file is part of Linspector (https://linspector.org/)
Copyright (c) 2022 Johannes Findeisen <you@hanez.org>. All Rights Reserved. Copyright (c) 2022 Johannes Findeisen <you@hanez.org>. All Rights Reserved.
See LICENSE (MIT license) See LICENSE (MIT license)
""" """
import inspect
from logging import getLogger from logging import getLogger
logger = getLogger('linspector') logger = getLogger('linspector')
def log(level, name, msg): def log(level, msg):
frm = inspect.stack()[1]
function_name = frm.function
module_name = inspect.getmodule(frm[0]).__name__
line_number = str(frm.lineno)
if level == 'critical': if level == 'critical':
logger.critical('[' + name + '] ' + str(msg)) logger.critical('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' +
str(msg))
if level == 'error': if level == 'error':
logger.error('[' + name + '] ' + str(msg)) logger.error('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' +
str(msg))
elif level == 'warning': elif level == 'warning':
logger.warning('[' + name + '] ' + str(msg)) logger.warning('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' +
str(msg))
elif level == 'info': elif level == 'info':
logger.info('[' + name + '] ' + str(msg)) logger.info('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' +
str(msg))
elif level == 'debug': elif level == 'debug':
logger.debug('[' + name + '] ' + str(msg)) logger.debug('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' +
str(msg))

View file

@ -15,7 +15,7 @@ from linspector.core.helpers import log
def job_function(monitor): def job_function(monitor):
log('debug', __name__, monitor) #log('debug', monitor)
monitor.handle_call() monitor.handle_call()
@ -31,13 +31,13 @@ class Linspector:
self.__scheduler = scheduler self.__scheduler = scheduler
# load plugins # load plugins
log('info', __name__, 'loading plugins...') log('info', 'loading plugins...')
if configuration.get_option('linspector', 'plugins'): if configuration.get_option('linspector', 'plugins'):
plugin_list = configuration.get_option('linspector', 'plugins') plugin_list = configuration.get_option('linspector', 'plugins')
self.__plugin_list = plugin_list.split(',') self.__plugin_list = plugin_list.split(',')
for plugin_option in plugin_list.split(','): for plugin_option in plugin_list.split(','):
if plugin_option not in plugins: if plugin_option not in plugins:
log('info', __name__, 'loading plugin: ' + plugin_option) log('info', 'loading plugin: ' + plugin_option)
plugin_package = 'linspector.plugins.' + plugin_option.lower() plugin_package = 'linspector.plugins.' + plugin_option.lower()
plugin_module = importlib.import_module(plugin_package) plugin_module = importlib.import_module(plugin_package)
plugin = plugin_module.get(configuration, environment, self) plugin = plugin_module.get(configuration, environment, self)
@ -60,26 +60,42 @@ class Linspector:
job_defaults=job_defaults) job_defaults=job_defaults)
start_date = datetime.datetime.now() start_date = datetime.datetime.now()
log('debug', __name__, monitors.get_monitors()) log('debug', monitors.get_monitors())
monitors = self.__monitors.get_monitors() monitors = self.__monitors.get_monitors()
for monitor in monitors: for monitor in monitors:
log('debug', __name__, monitor) log('debug', monitor)
if configuration.get_option('linspector', 'delta_range'): if configuration.get_option('linspector', 'delta_range'):
time_delta = round(random.uniform(0.00, float(configuration.get_option('linspector', 'delta_range'))), 2) time_delta = round(random.uniform(0.00, float(
configuration.get_option('linspector', 'delta_range'))), 2)
else: else:
time_delta = round(random.uniform(0.00, 60.00), 2) time_delta = round(random.uniform(0.00, 60.00), 2)
new_start_date = start_date + datetime.timedelta(seconds=time_delta) #x = monitors.get(monitor).get_monitor_configuration_option('args', 'start_date')
#y = x.get_monitor_configuration_option('args', 'start_date')
#z = y.get_option('args', 'start_date')
#a =
#print(x)
if monitors.get(monitor).get_monitor_configuration_option('args', 'start_date'):
new_start_date = \
monitors.get(monitor).get_monitor_configuration_option('args', 'start_date')
else:
new_start_date = start_date + datetime.timedelta(seconds=time_delta)
monitor_job = monitors.get(monitor) monitor_job = monitors.get(monitor)
interval = monitor_job.get_interval() interval = monitor_job.get_interval()
if configuration.get_option('linspector', 'timezone'):
timezone = configuration.get_option('linspector', 'timezone')
else:
timezone = 'UTC'
scheduler_job = scheduler['linspector'].add_job(job_function, 'interval', scheduler_job = scheduler['linspector'].add_job(job_function, 'interval',
start_date=new_start_date, start_date=new_start_date,
seconds=interval, timezone="CET", seconds=interval, timezone=timezone,
args=[monitors.get(monitor)]) args=[monitors.get(monitor)])
monitor_job.set_job(scheduler_job) monitor_job.set_job(scheduler_job)
self.__jobs.append(monitor_job) self.__jobs.append(monitor_job)
log('info', __name__, 'scheduling job ' + monitor + ' with delta ' + str(time_delta) + log('info', 'scheduling job ' + monitor + ' with delta ' + str(time_delta) +
' @' + str(new_start_date) + ' running service ' + monitor_job.get_service()) ' @' + str(new_start_date) + ' running service ' + monitor_job.get_service())
if configuration.get_option('linspector', 'start_scheduler') == 'true': if configuration.get_option('linspector', 'start_scheduler') == 'true':

View file

@ -129,6 +129,16 @@ class Monitor:
def get_interval(self): def get_interval(self):
return self.__interval return self.__interval
def get_monitor_configuration(self):
return self.__monitor_configuration
def get_monitor_configuration_option(self, section, option):
if self.__monitor_configuration.has_option(section, option):
return self.__monitor_configuration.get(section, option)
else:
return None
return self.__monitor_configuration
def get_service(self): def get_service(self):
return self.__service return self.__service
@ -183,13 +193,13 @@ class Monitor:
def handle_tasks(self, monitor_information): def handle_tasks(self, monitor_information):
for task in self.__tasks: for task in self.__tasks:
if self.status.lower() in task.get_task_type().lower(): if self.status.lower() in task.get_task_type().lower():
log('debug', __name__, 'executing task of type: ' + self.status) log('debug', 'executing task of type: ' + self.status)
# tasks can but should not be executed here. putting them in a queue is the better # tasks can but should not be executed here. putting them in a queue is the better
# solution to execute them in a serial process. # solution to execute them in a serial process.
#TaskExecutor.instance().schedule_task(monitor_information, task) #TaskExecutor.instance().schedule_task(monitor_information, task)
def handle_call(self): def handle_call(self):
#log('info', __name__, "handle call to identifier: " + self.__identifier) log('info', "handle call to identifier: " + self.__identifier)
self.__services[self.__service].execute() self.__services[self.__service].execute()
#logger.debug("handle call") #logger.debug("handle call")
#logger.debug(self.service) #logger.debug(self.service)
@ -199,14 +209,13 @@ class Monitor:
self.last_execution = MonitorExecution(self.get_host()) self.last_execution = MonitorExecution(self.get_host())
self.service.execute(self.last_execution) self.service.execute(self.last_execution)
except Exception as err: except Exception as err:
log.debug('debug', __name__, err) log.debug('debug', err)
self.last_execution.set_execution_end() self.last_execution.set_execution_end()
self.handle_threshold(self.service.get_threshold(), self.handle_threshold(self.service.get_threshold(),
self.last_execution.was_successful()) self.last_execution.was_successful())
log('info', __name__, 'sadasd')
#log.info("Job " + self.get_job_id() + #log.info("Job " + self.get_job_id() +
# ", Code: " + str(self.last_execution.get_error_code()) + # ", Code: " + str(self.last_execution.get_error_code()) +
# ", Message: " + str(self.last_execution.get_message())) # ", Message: " + str(self.last_execution.get_message()))
@ -216,7 +225,7 @@ class Monitor:
self.handle_tasks(self.monitor_information) self.handle_tasks(self.monitor_information)
else: else:
log('info', __name__, "job " + self.get_job_id() + " disabled") log('info', "job " + self.get_job_id() + " disabled")
#def get_host(self): #def get_host(self):
# return self.host # return self.host

View file

@ -23,12 +23,12 @@ class Monitors:
self.__monitors = {} self.__monitors = {}
monitor_groups = os.listdir(self.__configuration.get_configuration_path() + '/monitors/') monitor_groups = os.listdir(self.__configuration.get_configuration_path() + '/monitors/')
log('debug', __name__, 'monitor groups: ' + str(monitor_groups)) log('debug', 'monitor groups: ' + str(monitor_groups))
for monitor_group in monitor_groups: for monitor_group in monitor_groups:
monitors_file_list = glob.glob(self.__configuration.get_configuration_path() + monitors_file_list = glob.glob(self.__configuration.get_configuration_path() +
'/monitors/' + monitor_group + '/*.conf') '/monitors/' + monitor_group + '/*.conf')
log('debug', __name__, 'monitor files: ' + str(monitors_file_list)) log('debug', 'monitor files: ' + str(monitors_file_list))
for monitor_file in monitors_file_list: for monitor_file in monitors_file_list:
identifier = monitor_group + '_' + os.path.splitext(os.path.basename( identifier = monitor_group + '_' + os.path.splitext(os.path.basename(
monitor_file))[0] monitor_file))[0]
@ -38,12 +38,12 @@ class Monitors:
kwargs = {} kwargs = {}
for option in monitor_configuration.options('args'): for option in monitor_configuration.options('args'):
#print(option)
value = monitor_configuration.get('args', option) value = monitor_configuration.get('args', option)
#log('debug', 'added option to kwargs: ' + option + ' = ' + value)
kwargs[option] = value kwargs[option] = value
if kwargs: if kwargs:
log('debug', __name__, identifier + ' args ' + str(kwargs)) log('debug', identifier + ' args ' + str(kwargs))
identifier = monitor_group + '_' + os.path.splitext(os.path.basename( identifier = monitor_group + '_' + os.path.splitext(os.path.basename(
monitor_file))[0] monitor_file))[0]

View file

@ -20,5 +20,5 @@ class DummyService(Service):
self.__kwargs = kwargs self.__kwargs = kwargs
def execute(self): def execute(self):
log('debug', __name__, 'dummy object @' + str(self) + str(self.__kwargs['foo'])) log('debug', 'dummy object @' + str(self) + str(self.__kwargs['foo']))
return return