From ca6b424473d37468abdc117b4fa840ab45bf88bb Mon Sep 17 00:00:00 2001 From: Johannes Findeisen Date: Thu, 29 Sep 2022 22:21:31 +0200 Subject: [PATCH] Added basic scheduler code. --- bin/linspector | 5 +++-- etc/linspector.conf | 5 +++++ linspector/core/linspector.py | 32 ++++++++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/bin/linspector b/bin/linspector index 5994b88..6139618 100755 --- a/bin/linspector +++ b/bin/linspector @@ -32,7 +32,7 @@ from linspector.core.environment import Environment from linspector.core.linspector import Linspector from linspector.core.monitors import Monitors -__version__ = '0.19.8' +__version__ = '0.19.9' __author__ = 'Johannes Findeisen ' logger = logging.getLogger('linspector') @@ -70,6 +70,7 @@ def main(): monitors = None notifications = {} plugins = {} + scheduler = {} services = {} tasks = {} @@ -123,7 +124,7 @@ def main(): logger.warning('[linspector] monitor initialization error: {0}'.format(err)) try: - linspector = Linspector(configuration, environment, monitors, plugins) + linspector = Linspector(configuration, environment, monitors, plugins, scheduler) except Exception as err: logger.critical('[linspector] core initialization error: {0}'.format(err)) sys.exit(1) diff --git a/etc/linspector.conf b/etc/linspector.conf index c4449e6..cc81b1a 100644 --- a/etc/linspector.conf +++ b/etc/linspector.conf @@ -21,6 +21,11 @@ notifications = SMS run_mode = cron ; members to send notifications to if something internally in Linspector went wrong. members = superadmin@example.com,developers@example.com +; scheduler configuration +start_scheduler = true +max_threads = 3500 +max_processes = 100 +timezone = CET ; hostgroup parents; if the hostgroup "group1" is down, don't alert for the hosts in group2. see TODO.txt for more ; information. diff --git a/linspector/core/linspector.py b/linspector/core/linspector.py index ba26bd6..935416c 100644 --- a/linspector/core/linspector.py +++ b/linspector/core/linspector.py @@ -7,21 +7,26 @@ import importlib from logging import getLogger -#from apscheduler.schedulers.background import BackgroundScheduler -#from apscheduler.jobstores.memory import MemoryJobStore -#from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor +from apscheduler.schedulers.background import BackgroundScheduler +from apscheduler.jobstores.memory import MemoryJobStore +from apscheduler.executors.pool import ThreadPoolExecutor # , ProcessPoolExecutor logger = getLogger('linspector') +def job_function(job): + job.handle_call() + + class Linspector: - def __init__(self, configuration, environment, monitors, plugins): + def __init__(self, configuration, environment, monitors, plugins, scheduler): self.__configuration = configuration self.__environment = environment self.__monitors = monitors self.__plugin_list = None self.__plugins = plugins + self.__scheduler = scheduler # load plugins logger.info('loading plugins...') @@ -36,6 +41,25 @@ class Linspector: plugin = plugin_module.get(configuration, environment, self) plugins[plugin_option.lower()] = plugin + jobstores = { + 'memory': MemoryJobStore() + } + executors = { + 'default': ThreadPoolExecutor(int(configuration.get_option('linspector', + 'max_threads'))), + #'default': ProcessPoolExecutor(int(configuration.get_option('linspector', + # 'max_processes'))) + } + job_defaults = { + 'max_instances': 10000 + } + self.__scheduler['linspector'] = BackgroundScheduler(jobstores=jobstores, + executors=executors, + job_defaults=job_defaults) + + if configuration.get_option('linspector', 'start_scheduler') == 'true': + self.__scheduler['linspector'].start() + # this function is just for testing purposes and can be removed some day def print_debug(self): # example on how to access the monitor objects in monitors