diff --git a/bin/linspector b/bin/linspector index 8651772..5cbea09 100755 --- a/bin/linspector +++ b/bin/linspector @@ -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 # version. -__version__ = '0.19.13.dev1' +__version__ = '0.19.14.dev1' __author__ = 'Johannes Findeisen ' logger = logging.getLogger('linspector') diff --git a/etc/linspector.conf b/etc/linspector.conf index b2bb3c8..7006ac3 100644 --- a/etc/linspector.conf +++ b/etc/linspector.conf @@ -4,7 +4,7 @@ error_receivers = admin@example.com log_file = ~/code/linspector/linspector/log/linspector.log ; available log levels are: "error", "warning", "info" and "debug". -log_level= info +log_level= x log_count = 5 log_size = 10485760 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 ; system is running other services you should lower this value when you have too high cpu load. max_processes = 24 +; if timezone is not set, UTC is used by default. 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 ; can be set to a lower value if you only have a small amount of services you are monitoring. i recommend the lowest diff --git a/linspector/core/configuration.py b/linspector/core/configuration.py index 3f67291..becfb9e 100644 --- a/linspector/core/configuration.py +++ b/linspector/core/configuration.py @@ -19,8 +19,7 @@ class Configuration: self.__configuration_path = configuration_path self.__environment = environment - log('info', __name__, 'reading configuration file: ' + configuration_path + - '/linspector.conf') + log('info', 'reading configuration file: ' + configuration_path + '/linspector.conf') if os.path.isfile(configuration_path + '/linspector.conf'): try: self.__configuration.read(configuration_path + '/linspector.conf', 'utf-8') diff --git a/linspector/core/helpers.py b/linspector/core/helpers.py index c43e883..07d315c 100644 --- a/linspector/core/helpers.py +++ b/linspector/core/helpers.py @@ -3,19 +3,30 @@ This file is part of Linspector (https://linspector.org/) Copyright (c) 2022 Johannes Findeisen . All Rights Reserved. See LICENSE (MIT license) """ +import inspect + from logging import getLogger 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': - logger.critical('[' + name + '] ' + str(msg)) + logger.critical('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + + str(msg)) if level == 'error': - logger.error('[' + name + '] ' + str(msg)) + logger.error('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + + str(msg)) elif level == 'warning': - logger.warning('[' + name + '] ' + str(msg)) + logger.warning('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + + str(msg)) elif level == 'info': - logger.info('[' + name + '] ' + str(msg)) + logger.info('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + + str(msg)) elif level == 'debug': - logger.debug('[' + name + '] ' + str(msg)) + logger.debug('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + + str(msg)) diff --git a/linspector/core/linspector.py b/linspector/core/linspector.py index ca00d0a..4d1e634 100644 --- a/linspector/core/linspector.py +++ b/linspector/core/linspector.py @@ -15,7 +15,7 @@ from linspector.core.helpers import log def job_function(monitor): - log('debug', __name__, monitor) + #log('debug', monitor) monitor.handle_call() @@ -31,13 +31,13 @@ class Linspector: self.__scheduler = scheduler # load plugins - log('info', __name__, 'loading plugins...') + log('info', 'loading plugins...') if configuration.get_option('linspector', 'plugins'): plugin_list = configuration.get_option('linspector', 'plugins') self.__plugin_list = plugin_list.split(',') for plugin_option in plugin_list.split(','): 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_module = importlib.import_module(plugin_package) plugin = plugin_module.get(configuration, environment, self) @@ -60,26 +60,42 @@ class Linspector: job_defaults=job_defaults) start_date = datetime.datetime.now() - log('debug', __name__, monitors.get_monitors()) + log('debug', monitors.get_monitors()) monitors = self.__monitors.get_monitors() for monitor in monitors: - log('debug', __name__, monitor) + log('debug', monitor) 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: 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) 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', start_date=new_start_date, - seconds=interval, timezone="CET", + seconds=interval, timezone=timezone, args=[monitors.get(monitor)]) monitor_job.set_job(scheduler_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()) if configuration.get_option('linspector', 'start_scheduler') == 'true': diff --git a/linspector/core/monitor.py b/linspector/core/monitor.py index bb8a144..66a4502 100644 --- a/linspector/core/monitor.py +++ b/linspector/core/monitor.py @@ -129,6 +129,16 @@ class Monitor: def get_interval(self): 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): return self.__service @@ -183,13 +193,13 @@ class Monitor: def handle_tasks(self, monitor_information): for task in self.__tasks: 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 # solution to execute them in a serial process. #TaskExecutor.instance().schedule_task(monitor_information, task) 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() #logger.debug("handle call") #logger.debug(self.service) @@ -199,14 +209,13 @@ class Monitor: self.last_execution = MonitorExecution(self.get_host()) self.service.execute(self.last_execution) except Exception as err: - log.debug('debug', __name__, err) + log.debug('debug', err) self.last_execution.set_execution_end() self.handle_threshold(self.service.get_threshold(), self.last_execution.was_successful()) - log('info', __name__, 'sadasd') #log.info("Job " + self.get_job_id() + # ", Code: " + str(self.last_execution.get_error_code()) + # ", Message: " + str(self.last_execution.get_message())) @@ -216,7 +225,7 @@ class Monitor: self.handle_tasks(self.monitor_information) else: - log('info', __name__, "job " + self.get_job_id() + " disabled") + log('info', "job " + self.get_job_id() + " disabled") #def get_host(self): # return self.host diff --git a/linspector/core/monitors.py b/linspector/core/monitors.py index b387f7d..1581ae9 100644 --- a/linspector/core/monitors.py +++ b/linspector/core/monitors.py @@ -23,12 +23,12 @@ class Monitors: self.__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: monitors_file_list = glob.glob(self.__configuration.get_configuration_path() + '/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: identifier = monitor_group + '_' + os.path.splitext(os.path.basename( monitor_file))[0] @@ -38,12 +38,12 @@ class Monitors: kwargs = {} for option in monitor_configuration.options('args'): - #print(option) value = monitor_configuration.get('args', option) + #log('debug', 'added option to kwargs: ' + option + ' = ' + value) kwargs[option] = value if kwargs: - log('debug', __name__, identifier + ' args ' + str(kwargs)) + log('debug', identifier + ' args ' + str(kwargs)) identifier = monitor_group + '_' + os.path.splitext(os.path.basename( monitor_file))[0] diff --git a/linspector/services/misc/dummy.py b/linspector/services/misc/dummy.py index d99f7ca..5bcbe67 100644 --- a/linspector/services/misc/dummy.py +++ b/linspector/services/misc/dummy.py @@ -20,5 +20,5 @@ class DummyService(Service): self.__kwargs = kwargs 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