diff --git a/TODO.md b/TODO.md index afcde8b..c367bee 100644 --- a/TODO.md +++ b/TODO.md @@ -22,3 +22,4 @@ - Write documentation and inline documentation. - Add kwargs to notifications, tasks and maybe plugins. - Add date and cron based scheduling to linspector.py to make the full use of APScheduler. +- Make Linspector Windows compatible (not so important) diff --git a/bin/linspector b/bin/linspector index 8d4c05e..37d51ca 100755 --- a/bin/linspector +++ b/bin/linspector @@ -18,7 +18,7 @@ from linspector.environment import Environment from linspector.linspector import Linspector from linspector.monitors import Monitors -__version__ = '0.21.9' +__version__ = '0.22.0' __author__ = 'Johannes Findeisen ' diff --git a/etc/linspector.conf b/etc/linspector.conf index 568f0a8..5f3a13e 100644 --- a/etc/linspector.conf +++ b/etc/linspector.conf @@ -25,9 +25,12 @@ logfile_level = DEBUG logfile_size = 10MB notifications = + plugins = -tasks = + +tasks = mariadb ; timezone can be set to a remote timezone to make monitors run at the remote time. this can be overridden in each ; monitor configuration. timezone = CET + diff --git a/etc/tasks/mariadb.conf b/etc/tasks/mariadb.conf new file mode 100644 index 0000000..90c48db --- /dev/null +++ b/etc/tasks/mariadb.conf @@ -0,0 +1,4 @@ +[mariadb] +database = uplink +password = PASSWORD +user = root diff --git a/linspector/configuration.py b/linspector/configuration.py index f42e09f..52b4274 100644 --- a/linspector/configuration.py +++ b/linspector/configuration.py @@ -12,15 +12,15 @@ import os # options in the "linspector" section of linspector.conf. maybe log warnings if setting to default? class Configuration: def __init__(self, configuration_path, log): - self.__configuration = configparser.ConfigParser() - self.__configuration_path = configuration_path - self.__log = log + self._configuration = configparser.ConfigParser() + self._configuration_path = configuration_path + self._log = log log.info('message=reading configuration configfile=' + configuration_path + '/linspector.conf') if os.path.isfile(configuration_path + '/linspector.conf'): try: - self.__configuration.read(configuration_path + '/linspector.conf', 'utf-8') + self._configuration.read(configuration_path + '/linspector.conf', 'utf-8') except Exception as err: raise Exception('something went wrong reading the configuration file ' 'linspector.conf in the configuration root path! ({0})'.format(err)) @@ -31,8 +31,8 @@ class Configuration: # add keys and values defined in sub dirs and configuration ini files. for target_section in ['notifications', 'plugins', 'services', 'tasks']: # check if section exists before adding content. if not exists add the section. - if not self.__configuration.has_section(target_section): - self.__configuration.add_section(target_section) + if not self._configuration.has_section(target_section): + self._configuration.add_section(target_section) section_list = glob.glob(configuration_path + '/' + target_section + '/*.conf') for section_file in section_list: @@ -42,38 +42,38 @@ class Configuration: for source_section in configuration.sections(): source_section_options = configuration.options(source_section) for source_section_option in source_section_options: - self.__configuration.set(target_section, source_section + '_' + - source_section_option, - configuration.get(source_section, - source_section_option)) + self._configuration.set(target_section, source_section + '_' + + source_section_option, + configuration.get(source_section, + source_section_option)) # print('configuration dump: ' + self.dump_to_ini()) def dump_to_ini(self): dump = '' i = 0 - for section in self.__configuration.sections(): + for section in self._configuration.sections(): if i < 1: dump = dump + '[' + section + ']\n' else: dump = dump + '\n[' + section + ']\n' - options = self.__configuration.options(section) + options = self._configuration.options(section) for option in options: - dump = dump + option + " = " + self.__configuration.get(section, option) + '\n' + dump = dump + option + " = " + self._configuration.get(section, option) + '\n' i = 1 return dump def get_configuration_path(self): - return self.__configuration_path + return self._configuration_path def get_option(self, section, option): - if self.__configuration.has_option(section, option): - return self.__configuration.get(section, option) + if self._configuration.has_option(section, option): + return self._configuration.get(section, option) else: return None # this function should be used with care because it edits the main configuration. maybe it can # be used for dynamic runtime configuration later but i need to think about it. def set_option(self, section, option, value): - if not self.__configuration.has_option(section, option): - self.__configuration.set(section, option, value) + if not self._configuration.has_option(section, option): + self._configuration.set(section, option, value) diff --git a/linspector/environment.py b/linspector/environment.py index 073cdd4..794ad6a 100644 --- a/linspector/environment.py +++ b/linspector/environment.py @@ -12,14 +12,14 @@ class Environment: """ def __init__(self, log): - self.__env = {} - self.__log = log + self._env = {} + self._log = log def get_env_var(self, key): - if key in self.__env: - return self.__env[key] + if key in self._env: + return self._env[key] else: - self.__log.warning('environment var "' + key + '" not found! could be that it is ' + self._log.warning('environment var "' + key + '" not found! could be that it is ' 'set later at runtime. if you ' 'encounter any errors executing ' 'linspector, something is wrong ' @@ -32,8 +32,8 @@ class Environment: return None def set_env_var(self, key, value): - if self.__env[key]: - self.__log('warning', __name__, 'environment var "' + key + + if self._env[key]: + self._log('warning', _name_, 'environment var "' + key + ' existed and was overwritten!') - self.__env[key] = value + self._env[key] = value diff --git a/linspector/linspector.py b/linspector/linspector.py index f3b0ffb..77c94a2 100644 --- a/linspector/linspector.py +++ b/linspector/linspector.py @@ -27,21 +27,21 @@ def job_execution(log, monitor): class Linspector: def __init__(self, configuration, environment, log, monitors, plugins, scheduler): - self.__configuration = configuration - self.__environment = environment - self.__jobs = [] - self.__log = log - self.__monitors = monitors - self.__plugin_list = [] - self.__plugins = plugins - self.__scheduler = scheduler + self._configuration = configuration + self._environment = environment + self._jobs = [] + self._log = log + self._monitors = monitors + self._plugin_list = [] + self._plugins = plugins + self._scheduler = scheduler # load plugins log.info('message=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 self.__plugin_list: + self._plugin_list = plugin_list.split(',') + for plugin_option in self._plugin_list: if plugin_option not in plugins: log.info('loading plugin: ' + plugin_option) plugin_package = 'linspector.plugins.' + plugin_option.lower() @@ -75,14 +75,14 @@ class Linspector: # every scheduler job (monitor) must only exist once. 'max_instances': 1 } - self.__scheduler['linspector'] = BackgroundScheduler(jobstores=jobstores, + self._scheduler['linspector'] = BackgroundScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults, timezone=utc) start_date = datetime.datetime.now() log.debug(monitors.get_monitors()) - monitors = self.__monitors.get_monitors() + monitors = self._monitors.get_monitors() for monitor in monitors: log.debug(monitor) if configuration.get_option('linspector', 'delta_range'): @@ -121,7 +121,7 @@ class Linspector: args=[log, monitors.get(monitor)]) monitor_job.set_job(scheduler_job) - self.__jobs.append(monitor_job) + self._jobs.append(monitor_job) log.info('identifier=' + monitor + ' host=' + monitors.get(monitor).get_host() + ' service=' + monitor_job.get_service() + @@ -130,4 +130,4 @@ class Linspector: ' message=scheduling job') if configuration.get_option('linspector', 'start_scheduler') == 'true': - self.__scheduler['linspector'].start() + self._scheduler['linspector'].start() diff --git a/linspector/linspectord.py b/linspector/linspectord.py index 6f44b59..d45db05 100644 --- a/linspector/linspectord.py +++ b/linspector/linspectord.py @@ -13,12 +13,12 @@ import time # TODO: there is a bug when stopping the daemon. the pid_file is not being deleted. NEEDS A FIX! class Linspectord: def __init__(self, configuration, environment, linspector, log): - self.__configuration = configuration - self.__environment = environment - self.__linspector = linspector - self.__log = log + self._configuration = configuration + self._environment = environment + self._linspector = linspector + self._log = log try: - self.__pid_file = configuration.get_option('linspector', 'pid_file') + self._pid_file = configuration.get_option('linspector', 'pid_file') except Exception as err: log.critical('daemonize error (no pid_file set): {0}'.str(format(err))) @@ -32,7 +32,7 @@ class Linspectord: # exit first parent. sys.exit(0) except OSError as err: - self.__log.critical('fork #1 failed: {0}'.str(format(err))) + self._log.critical('fork #1 failed: {0}'.str(format(err))) sys.exit(1) # decouple from parent environment. @@ -47,7 +47,7 @@ class Linspectord: # Exit from second parent. sys.exit(0) except OSError as err: - self.__log.critical('fork #2 failed: {0}'.str(format(err))) + self._log.critical('fork #2 failed: {0}'.str(format(err))) sys.exit(1) # redirect standard file descriptors. @@ -65,24 +65,24 @@ class Linspectord: atexit.register(self.delete_pid) pid = str(os.getpid()) - with open(self.__pid_file, 'w+') as f: + with open(self._pid_file, 'w+') as f: f.write(pid + '\n') def delete_pid(self): - os.remove(self.__pid_file) + os.remove(self._pid_file) def start(self): # start the daemon. check for a pidfile to see if the daemon already runs before. - self.__log.info('starting daemon using pid_file: ' + str(self.__pid_file)) + self._log.info('starting daemon using pid_file: ' + str(self._pid_file)) try: - with open(self.__pid_file, 'r') as pf: + with open(self._pid_file, 'r') as pf: pid = int(pf.read().strip()) except IOError: pid = None if pid: message = 'pid_file {0} already exist. daemon already running?' - self.__log.critical(str(message.format(self.__pid_file))) + self._log.critical(str(message.format(self._pid_file))) sys.exit(1) # start the daemon. @@ -91,17 +91,17 @@ class Linspectord: def stop(self): # stop the daemon. - self.__log.info('stopping daemon using pid_file: ' + str(self.__pid_file)) + self._log.info('stopping daemon using pid_file: ' + str(self._pid_file)) # get the pid from the pid file. try: - with open(self.__pid_file, 'r') as pf: + with open(self._pid_file, 'r') as pf: pid = int(pf.read().strip()) except IOError: pid = None if not pid: message = 'pid_file {0} does not exist. daemon not running?' - self.__log.error(str(message.format(self.__pid_file))) + self._log.error(str(message.format(self._pid_file))) return # not an error in a restart # try killing the daemon process. @@ -112,15 +112,15 @@ class Linspectord: except OSError as err: e = str(err.args) if e.find('no such process') > 0: - if os.path.exists(self.__pid_file): - os.remove(self.__pid_file) + if os.path.exists(self._pid_file): + os.remove(self._pid_file) else: - self.__log.critical(str(err.args)) + self._log.critical(str(err.args)) sys.exit(1) def restart(self): # restart the daemon. - self.__log.info('restarting daemon using pid_file: ' + str(self.__pid_file)) + self._log.info('restarting daemon using pid_file: ' + str(self._pid_file)) self.stop() self.start() diff --git a/linspector/model.py b/linspector/model.py index 8c92508..a245c8a 100644 --- a/linspector/model.py +++ b/linspector/model.py @@ -10,6 +10,6 @@ See LICENSE (MIT license). class Model: def __init__(self, configuration, environment, log): - self.__configuration = configuration - self.__environment = environment - self.__log = log + self._configuration = configuration + self._environment = environment + self._log = log diff --git a/linspector/monitor.py b/linspector/monitor.py index a4e8ed0..e22e01f 100644 --- a/linspector/monitor.py +++ b/linspector/monitor.py @@ -10,46 +10,47 @@ import importlib class Monitor: def __init__(self, configuration, environment, identifier, log, monitor_configuration, notifications, services, tasks, kwargs): - self.__args = kwargs - self.__configuration = configuration - self.__enabled = True - self.__environment = environment - self.__host = monitor_configuration.get('monitor', 'host') + self._args = kwargs + self._configuration = configuration + self._enabled = True + self._environment = environment + self._host = monitor_configuration.get('monitor', 'host') try: - self.__hostgroups = monitor_configuration.get('monitor', 'hostgroups') + self._hostgroups = monitor_configuration.get('monitor', 'hostgroups') except configparser.NoOptionError as err: - self.__hostgroups = "None" + self._hostgroups = "None" - self.__identifier = identifier - self.__job_threshold = 0 + self._identifier = identifier + self._job_threshold = 0 try: - self.__interval = int(monitor_configuration.get('monitor', 'interval')) + self._interval = int(monitor_configuration.get('monitor', 'interval')) except Exception as err: log.warning('no interval set in identifier ' + identifier + ', trying to get a monitor configuration setting. error: ' + str(err)) try: - self.__interval = int(configuration.get_option('linspector', 'default_interval')) + self._interval = int(configuration.get_option('linspector', 'default_interval')) log.warning('set default_interval as per core configuration with ' - 'identifier: ' + identifier + ' to: ' + str(self.__interval)) + 'identifier: ' + identifier + ' to: ' + str(self._interval)) except Exception as err: log.warning('no default_interval found in core configuration for identifier ' + identifier + ', set to default interval 300 seconds. error: ' + str(err)) # default interval is 300 seconds (5 minutes) if not set in the monitor # configuration args or a default_interval in the core configuration. - self.__interval = 300 + self._interval = 300 - self.__log = log - self.__monitor_configuration = monitor_configuration - self.__notification_list = [] - self.__notifications = notifications - self.__scheduler_job = None + self._log = log + self._monitor_configuration = monitor_configuration + self._notification_list = [] + self._notifications = notifications + self._result = None + self._scheduler_job = None try: - self.__service = monitor_configuration.get('monitor', 'service') + self._service = monitor_configuration.get('monitor', 'service') except Exception as err: # if no service is set in the monitor configuration, the service is set to misc.dummy # instead. just to make Linspector run but with no real result. @@ -59,20 +60,21 @@ class Monitor: 'Linspector will run. ' 'error: ' + str(err)) - self.__service = 'misc.dummy' + self._service = 'misc.dummy' - self.__services = services - self.__task_list = [] # put tasks for the dedicated job here. - self.__tasks = tasks + self._services = services + self._task = None + self._task_list = [] # put tasks for the dedicated job here. + self._tasks = tasks """ - NONE job was not executed - OK when everything is fine - WARNING when a job has errors but the threshold is not overridden - RECOVER when a job recovers e.g. the threshold decrements (not implemented) - ERROR when a jobs error threshold is overridden - UNKNOWN when a job throws an exception which is not handled by the job itself (not - implemented) + NONE job was not executed: -1 + OK when everything is fine: 0 + WARNING when a job has errors but the threshold is not overridden: 1 + RECOVER when a job recovers e.g. the threshold decrements (not implemented): 2 + ERROR when a jobs error threshold is overridden: 3 + UNKNOWN when a job throws an exception which is not handled by the job itself (not + implemented) :4 self.status = "NONE" self.last_execution = None """ @@ -94,7 +96,7 @@ class Monitor: else: notification_list = None - self.__notification_list = notification_list.split(',') + self._notification_list = notification_list.split(',') for notification_option in notification_list.split(','): if notification_option not in notifications: @@ -103,86 +105,85 @@ class Monitor: notification = notification_module.create(configuration, environment, log) notifications[notification_option.lower()] = notification except configparser.NoOptionError as err: - self.__notifications = notifications + self._notifications = notifications - if self.__monitor_configuration.get('monitor', 'service'): + if self._monitor_configuration.get('monitor', 'service'): if monitor_configuration.get('monitor', 'service') not in services: service_package = 'linspector.services.' + \ monitor_configuration.get('monitor', 'service').lower() service_module = importlib.import_module(service_package) - self.__service = monitor_configuration.get('monitor', 'service').lower() + self._service = monitor_configuration.get('monitor', 'service').lower() service = service_module.create(configuration, environment, log) - self.__services[monitor_configuration.get('monitor', 'service').lower()] = service - try: - if configuration.get_option('linspector', 'tasks') or \ - monitor_configuration.get('args', 'tasks'): + self._services[monitor_configuration.get('monitor', 'service').lower()] = service - if configuration.get_option('linspector', 'tasks') and \ - monitor_configuration.get('args', 'tasks'): + if self._configuration.get_option('linspector', 'tasks'): + self._log.info(self._configuration.get_option('linspector', 'tasks')) - task_list = configuration.get_option('linspector', 'tasks') + ',' + \ - monitor_configuration.get('args', 'tasks') - elif configuration.get_option('linspector', 'tasks'): - task_list = configuration.get_option('linspector', 'tasks') - elif monitor_configuration.get('args', 'tasks'): - task_list = monitor_configuration.get('args', 'tasks') - else: - task_list = None + task_list = self._configuration.get_option('linspector', 'tasks') - self.__task_list = task_list.split(',') - - for task_option in task_list.split(','): - if task_option not in tasks: - task_package = 'linspector.tasks.' + task_option.lower() - task_module = importlib.import_module(task_package) - task = task_module.create(configuration, environment, log) - tasks[task_option.lower()] = task - except configparser.NoOptionError: - self.__tasks = tasks + self._task_list = task_list.split(',') + self._log.info(self._task_list) + for task in self._task_list: + if self._task is None: + task_package = 'linspector.tasks.' + task + task_module = importlib.import_module(task_package) + self._task = task_module.create(self._configuration, self._environment, + self._log) def execute(self): - self.__log.debug('identifier=' + self.__identifier + ' object=' + str(self)) - self.__log.debug('identifier=' + self.__identifier + ' message=handle call to service') + self._log.debug('identifier=' + self._identifier + ' object=' + str(self)) + self._log.debug('identifier=' + self._identifier + ' message=handle call to service') - if self.__enabled: + if self._enabled: try: - self.__services[self.__service].execute(self.__identifier, self, self.__service, - **self.__args) + self._result = self._services[self._service].execute(self._identifier, self, + self._service, **self._args) + if self._task is not None: + self._task.execute() + + print("task: " + str(self._task)) + print("identifier: " + self._identifier) + print("service: " + self._service) + print("status: " + self._result['status']) + print("message: " + self._result['message']) + print("json: " + str(self._result)) + except Exception as err: - self.__log.error(err) + self._log.error(err) + else: - self.__log.info('identifier=' + self.__identifier + ' message=is disabled') + self._log.info('identifier=' + self._identifier + ' message=is disabled') def get_host(self): - return self.__host + return self._host def get_hostgroups(self): - return self.__hostgroups + return self._hostgroups def get_identifier(self): - return self.__identifier + return self._identifier def get_interval(self): - return self.__interval + return self._interval def get_monitor_configuration(self): - return self.__monitor_configuration + 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) + if self._monitor_configuration.has_option(section, option): + return self._monitor_configuration.get(section, option) else: return None def get_service(self): - return self.__service + return self._service def set_enabled(self, enabled=True): - self.__enabled = enabled + self._enabled = enabled def set_job(self, scheduler_job): - self.__scheduler_job = scheduler_job + self._scheduler_job = scheduler_job - def __str__(self): - return str(self.__dict__) + def _str_(self): + return str(self._dict_) diff --git a/linspector/monitors.py b/linspector/monitors.py index 41755b4..f06f5e8 100644 --- a/linspector/monitors.py +++ b/linspector/monitors.py @@ -18,18 +18,18 @@ from linspector.monitor import Monitor # to lish which walks thrue all scheduled jobs and when an unknown monitor is found, schedule it. class Monitors: def __init__(self, configuration, environment, log, notifications, services, tasks): - self.__configuration = configuration - self.__environment = environment - self.__log = log - self.__notifications = notifications - self.__monitors = {} - self.__services = services - self.__tasks = tasks + self._configuration = configuration + self._environment = environment + self._log = log + self._notifications = notifications + self._monitors = {} + self._services = services + self._tasks = tasks - monitor_groups = os.listdir(self.__configuration.get_configuration_path() + '/monitors/') + monitor_groups = os.listdir(self._configuration.get_configuration_path() + '/monitors/') log.debug('monitor groups: ' + str(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') log.debug('monitor files: ' + str(monitors_file_list)) @@ -56,17 +56,17 @@ class Monitors: # create Monitor() object and copy monitor_configuration for each instance because # they else refer to the same object? copy.deepcopy(monitor_configuration)??? - self.__monitors[identifier] = Monitor(self.__configuration, - self.__environment, - identifier, - self.__log, - monitor_configuration, - self.__notifications, - self.__services, - self.__tasks, - kwargs) + self._monitors[identifier] = Monitor(self._configuration, + self._environment, + identifier, + self._log, + monitor_configuration, + self._notifications, + self._services, + self._tasks, + kwargs) del kwargs def get_monitors(self): - return self.__monitors + return self._monitors diff --git a/linspector/notification.py b/linspector/notification.py index ea4ed32..14edba0 100644 --- a/linspector/notification.py +++ b/linspector/notification.py @@ -8,6 +8,6 @@ See LICENSE (MIT license). class Notification: def __init__(self, configuration, environment, log): super().__init__() - self.__configuration = configuration - self.__environment = environment - self.__log = log + self._configuration = configuration + self._environment = environment + self._log = log diff --git a/linspector/plugin.py b/linspector/plugin.py index a0d82cd..d10435f 100644 --- a/linspector/plugin.py +++ b/linspector/plugin.py @@ -7,7 +7,7 @@ See LICENSE (MIT license). class Plugin: def __init__(self, configuration, environment, linspector, log): - self.__configuration = configuration - self.__environment = environment - self.__linspector = linspector - self.__log = log + self._configuration = configuration + self._environment = environment + self._linspector = linspector + self._log = log diff --git a/linspector/plugins/api.py b/linspector/plugins/api.py index bd2a6c8..6f9dcf5 100644 --- a/linspector/plugins/api.py +++ b/linspector/plugins/api.py @@ -17,10 +17,10 @@ def create(configuration, environment, linspector, log): class APIPlugin(Plugin): def __init__(self, configuration, environment, linspector, log): super().__init__(configuration, environment, linspector, log) - self.__configuration = configuration - self.__environment = environment - self.__linspector = linspector - self.__log = log + self._configuration = configuration + self._environment = environment + self._linspector = linspector + self._log = log def run(self): return diff --git a/linspector/plugins/httpd.py b/linspector/plugins/httpd.py index a5e43ae..4d29cae 100644 --- a/linspector/plugins/httpd.py +++ b/linspector/plugins/httpd.py @@ -19,9 +19,9 @@ class HTTPDPlugin(Plugin): def __init__(self, configuration, environment, linspector, log): super().__init__(configuration, environment, linspector, log) - self.__configuration = configuration - self.__environment = environment - self.__linspector = linspector + self._configuration = configuration + self._environment = environment + self._linspector = linspector @cherrypy.expose def index(self): @@ -30,21 +30,21 @@ class HTTPDPlugin(Plugin): @cherrypy.expose def configuration(self): return '[monipy-' + \ - self.__environment.get_env_var("__version__") + '@' + \ - self.__environment.get_env_var("_hostname") + '] configuration
' + \
-            json.dumps(vars(self.__configuration), sort_keys=True, indent=4) + \
+            json.dumps(vars(self._configuration), sort_keys=True, indent=4) + \
             '
' @cherrypy.expose def environment(self): return '[monipy-' + \ - self.__environment.get_env_var("__version__") + '@' + \ - self.__environment.get_env_var("_hostname") + '] environment
' + \
-            json.dumps(vars(self.__environment), sort_keys=True, indent=4) + \
+            json.dumps(vars(self._environment), sort_keys=True, indent=4) + \
             '
' @cherrypy.expose @@ -81,8 +81,8 @@ class HTTPDPlugin(Plugin): # }) cherrypy.config.update({ 'global': { - 'server.socket_host': self.__configuration.get_httpserver_host(), - 'server.socket_port': self.__configuration.get_httpserver_port(), + 'server.socket_host': self._configuration.get_httpserver_host(), + 'server.socket_port': self._configuration.get_httpserver_port(), 'environment': 'production' } }) diff --git a/linspector/plugins/lish.py b/linspector/plugins/lish.py index 32064b2..a890094 100644 --- a/linspector/plugins/lish.py +++ b/linspector/plugins/lish.py @@ -17,10 +17,10 @@ def create(configuration, environment, linspector, log): class LishPlugin(Plugin): def __init__(self, configuration, environment, linspector, log): super().__init__(configuration, environment, linspector, log) - self.__configuration = configuration - self.__environment = environment - self.__linspector = linspector - self.__log = log + self._configuration = configuration + self._environment = environment + self._linspector = linspector + self._log = log def run(self): return diff --git a/linspector/plugins/rpc.py b/linspector/plugins/rpc.py index dd322f5..dff255b 100644 --- a/linspector/plugins/rpc.py +++ b/linspector/plugins/rpc.py @@ -14,10 +14,10 @@ def create(configuration, environment, linspector, log): class RPCPlugin(Plugin): def __init__(self, configuration, environment, linspector, log): super().__init__(configuration, environment, linspector, log) - self.__configuration = configuration - self.__environment = environment - self.__linspector = linspector - self.__log = log + self._configuration = configuration + self._environment = environment + self._linspector = linspector + self._log = log def run(self): return diff --git a/linspector/plugins/syslog.py b/linspector/plugins/syslog.py index 6183830..ea359ce 100644 --- a/linspector/plugins/syslog.py +++ b/linspector/plugins/syslog.py @@ -16,10 +16,10 @@ def create(configuration, environment, linspector, log): class SyslogPlugin(Plugin): def __init__(self, configuration, environment, linspector, log): super().__init__(configuration, environment, linspector, log) - self.__configuration = configuration - self.__environment = environment - self.__linspector = linspector - self.__log = log + self._configuration = configuration + self._environment = environment + self._linspector = linspector + self._log = log def run(self): return diff --git a/linspector/services/misc/dummy.py b/linspector/services/misc/dummy.py index fb441cc..91c8c10 100644 --- a/linspector/services/misc/dummy.py +++ b/linspector/services/misc/dummy.py @@ -21,4 +21,5 @@ class DummyService(Service): ' host=' + monitor.get_host() + ' service=' + service + ' status=' + 'OK') - return True + + return {"status": 'OK', "message": "Hello from Dummy Service."} diff --git a/linspector/services/misc/random.py b/linspector/services/misc/random.py index 8164c1f..3912b8e 100644 --- a/linspector/services/misc/random.py +++ b/linspector/services/misc/random.py @@ -39,4 +39,7 @@ class RandomService(Service): ' service=' + service + ' status=' + ('OK' if status == 0 else 'ERROR') + ' message=' + sha512) - return True + + result = {"status": ('OK' if status == 0 else 'ERROR'), "message": sha512} + + return result diff --git a/linspector/services/net/speedtest.py b/linspector/services/net/speedtest.py index ae39f38..e8672b4 100644 --- a/linspector/services/net/speedtest.py +++ b/linspector/services/net/speedtest.py @@ -18,26 +18,26 @@ def create(configuration, environment, log): class SpeedtestService(Service): def __init__(self, configuration, environment, log): super().__init__(configuration, environment, log) - self.__configuration = configuration - self.__environment = environment - self.__log = log + self._configuration = configuration + self._environment = environment + self._log = log - self.__speedtest_maximum_speed = None - self.__speedtest_average_speed = None - self.__speedtest_time_elapsed = None + self._speedtest_maximum_speed = None + self._speedtest_average_speed = None + self._speedtest_time_elapsed = None def execute(self, **kwargs): while True: tmp_time = time.localtime(calendar.timegm(time.gmtime())) - self.__environment.set_env_var('_speedtest_last_run_date', + self._environment.set_env_var('_speedtest_last_run_date', time.strftime('%Y-%m-%d %H:%M:%S', tmp_time)) - self.__environment.set_env_var('_speedtest_last_run_timestamp', + self._environment.set_env_var('_speedtest_last_run_timestamp', calendar.timegm(time.gmtime())) start = time.perf_counter() - request = requests.get(self.__configuration.get_speedtest_url(), stream=True) + request = requests.get(self._configuration.get_speedtest_url(), stream=True) size = int(request.headers.get('Content-Length')) downloaded = 0.0 total_mbps = 0.0 @@ -54,21 +54,21 @@ class SpeedtestService(Service): total_chunks += 1 total_mbps += mbps - self.__speedtest_average_speed = total_mbps / total_chunks - self.__environment.set_env_var('_speedtest_average_speed_megabyte_per_second', - str(round(self.__speedtest_average_speed))) + self._speedtest_average_speed = total_mbps / total_chunks + self._environment.set_env_var('_speedtest_average_speed_megabyte_per_second', + str(round(self._speedtest_average_speed))) - self.__speedtest_maximum_speed = maximum_speed - self.__environment.set_env_var('_speedtest_maximum_speed_megabyte_per_second', - str(round(self.__speedtest_maximum_speed))) + self._speedtest_maximum_speed = maximum_speed + self._environment.set_env_var('_speedtest_maximum_speed_megabyte_per_second', + str(round(self._speedtest_maximum_speed))) - self.__speedtest_time_elapsed = time.perf_counter() - start - self.__environment.set_env_var('_speedtest_time_elapsed', - str(self.__speedtest_time_elapsed)) - self.__log.info('speedtest average: ' + str(self.__speedtest_average_speed) + - ', max: ' + str(self.__speedtest_maximum_speed) + - ', time: ' + str(self.__speedtest_time_elapsed)) + self._speedtest_time_elapsed = time.perf_counter() - start + self._environment.set_env_var('_speedtest_time_elapsed', + str(self._speedtest_time_elapsed)) + self._log.info('speedtest average: ' + str(self._speedtest_average_speed) + + ', max: ' + str(self._speedtest_maximum_speed) + + ', time: ' + str(self._speedtest_time_elapsed)) else: - self.__log.warning('could not calculate download speed!') + self._log.warning('could not calculate download speed!') - time.sleep(self.__configuration.get_speedtest_interval()) + time.sleep(self._configuration.get_speedtest_interval()) diff --git a/linspector/services/vendor/avm/is_connected.py b/linspector/services/vendor/avm/is_connected.py index 88bd502..98f0a5a 100644 --- a/linspector/services/vendor/avm/is_connected.py +++ b/linspector/services/vendor/avm/is_connected.py @@ -15,6 +15,7 @@ def create(configuration, environment, log): class IsConnectedService(Service): def execute(self, identifier, monitor, service, **kwargs): + self._log.debug('identifier=' + identifier + ' service=' + service + ' object=' + str(self) + @@ -34,7 +35,6 @@ class IsConnectedService(Service): ' service=' + service + ' status=' + ('OK' if fc.is_connected else 'ERROR')) - if fc.is_connected: - return True - else: - return False + result = {"status": ('OK' if fc.is_connected else 'ERROR'), "message": "CUSTOM"} + + return result diff --git a/linspector/singleton.py b/linspector/singleton.py index c55da79..9ac5dfc 100644 --- a/linspector/singleton.py +++ b/linspector/singleton.py @@ -19,7 +19,7 @@ class Singleton: no restrictions that apply to the decorated class. To get the singleton instance, use the `Instance` method. Trying - to use `__call__` will result in a `TypeError` being raised. + to use `_call_` will result in a `TypeError` being raised. Limitations: The decorated class cannot be inherited from. @@ -42,8 +42,8 @@ class Singleton: self._instance = self._decorated() return self._instance - def __call__(self): + def _call_(self): raise TypeError('Singletons must be accessed through `Instance()`.') - def __instancecheck__(self, inst): + def _instancecheck_(self, inst): return isinstance(inst, self._decorated) diff --git a/linspector/task.py b/linspector/task.py index 080192e..29d7637 100644 --- a/linspector/task.py +++ b/linspector/task.py @@ -15,10 +15,10 @@ KEY_CLASS = "class" class Task: def __init__(self, configuration, environment, log, **kwargs): - self.__args = {} - self.__configuration = configuration - self.__environment = environment - self.__log = log + self._args = {} + self._configuration = configuration + self._environment = environment + self._log = log if KEY_ARGS in kwargs: self.add_arguments(kwargs[KEY_ARGS]) @@ -42,10 +42,10 @@ class Task: def add_arguments(self, args): for key, val in args.items(): - self.__args[key] = val + self._args[key] = val def get_arguments(self): - return self.__args + return self._args # def set_member(self, member): # self.member = member @@ -69,9 +69,9 @@ class Task: @Singleton class TaskRunner: def __init__(self, configuration, environment, log): - self.__configuration = configuration - self.__environment = environment - self.__log = log + self._configuration = configuration + self._environment = environment + self._log = log self.queue = Queue() self.task_infos = [] task_thread = Thread(target=self._run_worker_thread) @@ -86,12 +86,12 @@ class TaskRunner: try: msg, task = self.queue.get() if task: - self.__log.debug('starting task execution...') + self._log.debug('starting task execution...') # task.execute(msg) self.queue.task_done() except Exception as err: - self.__log.error('error ' + str(err)) + self._log.error('error ' + str(err)) def is_instant_end(self): return self._instant_end diff --git a/linspector/tasks/csv.py b/linspector/tasks/csv.py index 87727ac..f847c43 100644 --- a/linspector/tasks/csv.py +++ b/linspector/tasks/csv.py @@ -14,6 +14,9 @@ def create(configuration, environment, log): class CSVTask(Task): def __init__(self, configuration, environment, log): super().__init__(configuration, environment, log) - self.__configuration = configuration - self.__environment = environment - self.__log = log + self._configuration = configuration + self._environment = environment + self._log = log + + def execute(self): + print("Hello from CSV Task...") diff --git a/linspector/tasks/file.py b/linspector/tasks/file.py index ab0b19b..5af850a 100644 --- a/linspector/tasks/file.py +++ b/linspector/tasks/file.py @@ -14,6 +14,6 @@ def create(configuration, environment, log): class FileTask(Task): def __init__(self, configuration, environment, log): super().__init__(configuration, environment, log) - self.__configuration = configuration - self.__environment = environment - self.__log = log + self._configuration = configuration + self._environment = environment + self._log = log diff --git a/linspector/tasks/mariadb.py b/linspector/tasks/mariadb.py index 4d572ce..4256fdb 100644 --- a/linspector/tasks/mariadb.py +++ b/linspector/tasks/mariadb.py @@ -14,6 +14,9 @@ def create(configuration, environment, log): class MariaDBTask(Task): def __init__(self, configuration, environment, log): super().__init__(configuration, environment, log) - self.__configuration = configuration - self.__environment = environment - self.__log = log + self._configuration = configuration + self._environment = environment + self._log = log + + def execute(self): + print("Hello from MariaDB Task...") diff --git a/linspector/tasks/redis.py b/linspector/tasks/redis.py index 248ebd7..6500b28 100644 --- a/linspector/tasks/redis.py +++ b/linspector/tasks/redis.py @@ -14,6 +14,6 @@ def create(configuration, environment, log): class RedisTask(Task): def __init__(self, configuration, environment, log): super().__init__(configuration, environment, log) - self.__configuration = configuration - self.__environment = environment - self.__log = log + self._configuration = configuration + self._environment = environment + self._log = log diff --git a/linspector/tasks/splunk.py b/linspector/tasks/splunk.py index 73766bb..817eb91 100644 --- a/linspector/tasks/splunk.py +++ b/linspector/tasks/splunk.py @@ -14,6 +14,6 @@ def create(configuration, environment, log): class SplunkTask(Task): def __init__(self, configuration, environment, log): super().__init__(configuration, environment, log) - self.__configuration = configuration - self.__environment = environment - self.__log = log + self._configuration = configuration + self._environment = environment + self._log = log diff --git a/linspector/tasks/sqlite.py b/linspector/tasks/sqlite.py index 4d9b8bf..8c487ac 100644 --- a/linspector/tasks/sqlite.py +++ b/linspector/tasks/sqlite.py @@ -14,6 +14,6 @@ def create(configuration, environment, log): class SQLiteTask(Task): def __init__(self, configuration, environment, log): super().__init__(configuration, environment, log) - self.__configuration = configuration - self.__environment = environment - self.__log = log + self._configuration = configuration + self._environment = environment + self._log = log diff --git a/linspector/tasks/syslog.py b/linspector/tasks/syslog.py index e067b1c..33caa0c 100644 --- a/linspector/tasks/syslog.py +++ b/linspector/tasks/syslog.py @@ -14,6 +14,6 @@ def create(configuration, environment, log): class SyslogTask(Task): def __init__(self, configuration, environment, log): super().__init__(configuration, environment, log) - self.__configuration = configuration - self.__environment = environment - self.__log = log + self._configuration = configuration + self._environment = environment + self._log = log