diff --git a/bin/linspector b/bin/linspector index 5cbea09..340d9a2 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.14.dev1' +__version__ = '0.19.15.dev1' __author__ = 'Johannes Findeisen ' logger = logging.getLogger('linspector') diff --git a/linspector/core/helpers.py b/linspector/core/helpers.py index 07d315c..7a3722c 100644 --- a/linspector/core/helpers.py +++ b/linspector/core/helpers.py @@ -3,30 +3,42 @@ 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, 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('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + + # only use inspect when log level NOTSET or DEBUG is enabled. + if logger.isEnabledFor(0) or logger.isEnabledFor(10): + import inspect + 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('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + + str(msg)) + if level == 'error': + logger.error('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + + str(msg)) + elif level == 'warning': + logger.warning('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + + str(msg)) + elif level == 'info': + logger.info('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + str(msg)) - if level == 'error': - logger.error('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + - str(msg)) - elif level == 'warning': - logger.warning('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + - str(msg)) - elif level == 'info': - logger.info('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + - str(msg)) - elif level == 'debug': - logger.debug('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + - str(msg)) + elif level == 'debug': + logger.debug('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + + str(msg)) + else: + if level == 'critical': + logger.critical(str(msg)) + if level == 'error': + logger.error(str(msg)) + elif level == 'warning': + logger.warning(str(msg)) + elif level == 'info': + logger.info(str(msg)) + elif level == 'debug': + logger.debug(str(msg)) diff --git a/linspector/core/monitor.py b/linspector/core/monitor.py index fc55cc6..ce884fa 100644 --- a/linspector/core/monitor.py +++ b/linspector/core/monitor.py @@ -51,7 +51,8 @@ class Monitor: 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) + UNKNOWN when a job throws an exception which is not handled by the job itself (not + implemented) """ self.status = "NONE" self.last_execution = None @@ -94,7 +95,7 @@ class Monitor: service_module = importlib.import_module(service_package) self.__service = monitor_configuration.get('monitor', 'service').lower() - service = service_module.create(configuration, environment, **self.__args) + service = service_module.create(configuration, environment) self.__services[monitor_configuration.get('monitor', 'service').lower()] = service try: if configuration.get_option('linspector', 'tasks') or \ @@ -199,7 +200,7 @@ class Monitor: #TaskExecutor.instance().schedule_task(monitor_information, task) def handle_call(self): - log('info', "handle call to identifier: " + self.__identifier) + log('info', 'handle call to monitor with identifier: ' + self.__identifier) #logger.debug("handle call") #logger.debug(self.service) if self.enabled: @@ -207,7 +208,7 @@ class Monitor: try: self.last_execution = MonitorExecution(self.get_host()) #self.__services[self.__service].execute(self.last_execution) - self.__services[self.__service].execute() + self.__services[self.__service].execute(**self.__args) except Exception as err: log('error', err) diff --git a/linspector/core/service.py b/linspector/core/service.py index 3d9692a..8daa76a 100644 --- a/linspector/core/service.py +++ b/linspector/core/service.py @@ -8,7 +8,6 @@ from linspector.core.helpers import log class Service: - def __init__(self, configuration, environment, **kwargs): + def __init__(self, configuration, environment): self.__configuration = configuration self._environment = environment - self.__kwargs = kwargs diff --git a/linspector/services/http/httpkeyword.py b/linspector/services/http/httpkeyword.py index fea5a6f..c2620f0 100644 --- a/linspector/services/http/httpkeyword.py +++ b/linspector/services/http/httpkeyword.py @@ -7,17 +7,16 @@ from linspector.core.helpers import log from linspector.core.service import Service -def create(configuration, environment, **kwargs): - return HTTPKeywordService(configuration, environment, **kwargs) +def create(configuration, environment): + return HTTPKeywordService(configuration, environment) class HTTPKeywordService(Service): - def __init__(self, configuration, environment, **kwargs): - super().__init__(configuration, environment, **kwargs) + def __init__(self, configuration, environment): + super().__init__(configuration, environment) self.__configuration = configuration self.__environment = environment - self.__kwargs = kwargs - def execute(self): + def execute(self, **kwargs): return diff --git a/linspector/services/misc/dummy.py b/linspector/services/misc/dummy.py index 260f75a..8a1821e 100644 --- a/linspector/services/misc/dummy.py +++ b/linspector/services/misc/dummy.py @@ -7,19 +7,18 @@ from linspector.core.helpers import log from linspector.core.service import Service -def create(configuration, environment, **kwargs): - return DummyService(configuration, environment, **kwargs) +def create(configuration, environment): + return DummyService(configuration, environment) class DummyService(Service): - def __init__(self, configuration, environment, **kwargs): + def __init__(self, configuration, environment): super().__init__(configuration, environment) self.__configuration = configuration self.__environment = environment - self.__kwargs = kwargs - def execute(self): + def execute(self, **kwargs): log('debug', 'dummy object @' + str(self)) #log('debug', 'dummy object @' + str(self) + str(self.__kwargs['foo'])) return diff --git a/linspector/services/misc/random.py b/linspector/services/misc/random.py index 76d5c0b..c88286a 100644 --- a/linspector/services/misc/random.py +++ b/linspector/services/misc/random.py @@ -7,17 +7,16 @@ from linspector.core.helpers import log from linspector.core.service import Service -def create(configuration, environment, **kwargs): - return RandomService(configuration, environment, **kwargs) +def create(configuration, environment): + return RandomService(configuration, environment) class RandomService(Service): - def __init__(self, configuration, environment, **kwargs): - super().__init__(configuration, environment, **kwargs) + def __init__(self, configuration, environment): + super().__init__(configuration, environment) self.__configuration = configuration self.__environment = environment - self.__kwargs = kwargs - def execute(self): + def execute(self, **kwargs): return diff --git a/linspector/services/net/fritzboxuplink.py b/linspector/services/net/fritzboxuplink.py index 1dcf8dd..bb67e99 100644 --- a/linspector/services/net/fritzboxuplink.py +++ b/linspector/services/net/fritzboxuplink.py @@ -9,18 +9,17 @@ from linspector.core.helpers import log from linspector.core.service import Service -def create(configuration, environment, **kwargs): - return FritzboxUplinkService(configuration, environment, **kwargs) +def create(configuration, environment): + return FritzboxUplinkService(configuration, environment) class FritzboxUplinkService(Service): - def __init__(self, configuration, environment, **kwargs): - super().__init__(configuration, environment, **kwargs) + def __init__(self, configuration, environment): + super().__init__(configuration, environment) self.__configuration = configuration self.__environment = environment - self.__kwargs = kwargs - def execute(self): + def execute(self, **kwargs): log('debug', 'fritzboxuplink object @' + str(self)) return diff --git a/linspector/services/net/ping.py b/linspector/services/net/ping.py index ec1c252..e675e03 100644 --- a/linspector/services/net/ping.py +++ b/linspector/services/net/ping.py @@ -7,17 +7,16 @@ from linspector.core.helpers import log from linspector.core.service import Service -def create(configuration, environment, **kwargs): - return PingService(configuration, environment, **kwargs) +def create(configuration, environment): + return PingService(configuration, environment) class PingService(Service): - def __init__(self, configuration, environment, **kwargs): - super().__init__(configuration, environment, **kwargs) + def __init__(self, configuration, environment): + super().__init__(configuration, environment) self.__configuration = configuration self.__environment = environment - self.__kwargs = kwargs - def execute(self): + def execute(self, **kwargs): return diff --git a/linspector/services/net/port.py b/linspector/services/net/port.py index fe1c19d..3cf9c54 100644 --- a/linspector/services/net/port.py +++ b/linspector/services/net/port.py @@ -9,19 +9,18 @@ from linspector.core.helpers import log from linspector.core.service import Service -def create(configuration, environment, **kwargs): - return PortService(configuration, environment, **kwargs) +def create(configuration, environment): + return PortService(configuration, environment) class PortService(Service): - def __init__(self, configuration, environment, **kwargs): - super().__init__(configuration, environment, **kwargs) + def __init__(self, configuration, environment): + super().__init__(configuration, environment) self.__configuration = configuration self.__environment = environment - self.__kwargs = kwargs - def execute(self, execution): + def execute(self, execution, **kwargs): error_code = 0 msg = "Connection successful established" diff --git a/linspector/services/net/speedtest.py b/linspector/services/net/speedtest.py index e6daa9c..3435cdf 100644 --- a/linspector/services/net/speedtest.py +++ b/linspector/services/net/speedtest.py @@ -11,23 +11,22 @@ from linspector.core.helpers import log from linspector.core.service import Service -def create(configuration, environment, **kwargs): - return SpeedtestService(configuration, environment, **kwargs) +def create(configuration, environment): + return SpeedtestService(configuration, environment) class SpeedtestService(Service): - def __init__(self, configuration, environment, **kwargs): - super().__init__(configuration, environment, **kwargs) + def __init__(self, configuration, environment): + super().__init__(configuration, environment) self.__configuration = configuration self.__environment = environment - self.__kwargs = kwargs self.__speedtest_maximum_speed = None self.__speedtest_average_speed = None self.__speedtest_time_elapsed = None - def execute(self): + def execute(self, **kwargs): while True: tmp_time = time.localtime(calendar.timegm(time.gmtime())) self.__environment.set_env_var('_speedtest_last_run_date', diff --git a/linspector/services/net/ssh.py b/linspector/services/net/ssh.py index f3604d7..b8f552d 100644 --- a/linspector/services/net/ssh.py +++ b/linspector/services/net/ssh.py @@ -11,19 +11,18 @@ from linspector.core.helpers import log from linspector.core.service import Service -def create(configuration, environment, **kwargs): - return SSHService(configuration, environment, **kwargs) +def create(configuration, environment): + return SSHService(configuration, environment) class SSHService(Service): - def __init__(self, configuration, environment, **kwargs): - super().__init__(configuration, environment, **kwargs) + def __init__(self, configuration, environment): + super().__init__(configuration, environment) self.__configuration = configuration self.__environment = environment - self.__kwargs = kwargs - def execute(self): + def execute(self, **kwargs): path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa') key = paramiko.RSAKey.from_private_key_file(path) diff --git a/linspector/services/net/tcpconnect.py b/linspector/services/net/tcpconnect.py index d4f0271..1bc7916 100644 --- a/linspector/services/net/tcpconnect.py +++ b/linspector/services/net/tcpconnect.py @@ -7,18 +7,17 @@ from linspector.core.helpers import log from linspector.core.service import Service -def create(configuration, environment, **kwargs): - return TCPConnectService(configuration, environment, **kwargs) +def create(configuration, environment): + return TCPConnectService(configuration, environment) # TODO: check for all required configuration options and set defaults if needed. class TCPConnectService(Service): - def __init__(self, configuration, environment, **kwargs): - super().__init__(configuration, environment, **kwargs) + def __init__(self, configuration, environment): + super().__init__(configuration, environment) self.__configuration = configuration self.__environment = environment - self.__kwargs = kwargs - def execute(self): + def execute(self, **kwargs): return diff --git a/linspector/services/snmp/get.py b/linspector/services/snmp/get.py index b1a8d59..72abaab 100644 --- a/linspector/services/snmp/get.py +++ b/linspector/services/snmp/get.py @@ -9,17 +9,16 @@ from linspector.core.helpers import log from linspector.core.service import Service -def create(configuration, environment, **kwargs): - return GetService(configuration, environment, **kwargs) +def create(configuration, environment): + return GetService(configuration, environment) class GetService(Service): - def __init__(self, configuration, environment, **kwargs): - super().__init__(configuration, environment, **kwargs) + def __init__(self, configuration, environment): + super().__init__(configuration, environment) self.__configuration = configuration self.__environment = environment - self.__kwargs = kwargs - def execute(self): + def execute(self, **kwargs): return diff --git a/linspector/services/sys/shell.py b/linspector/services/sys/shell.py index 08c3441..c244407 100644 --- a/linspector/services/sys/shell.py +++ b/linspector/services/sys/shell.py @@ -7,18 +7,17 @@ from linspector.core.helpers import log from linspector.core.service import Service -def create(configuration, environment, **kwargs): - return ShellService(configuration, environment, **kwargs) +def create(configuration, environment): + return ShellService(configuration, environment) class ShellService(Service): - def __init__(self, configuration, environment, **kwargs): - super().__init__(configuration, environment, **kwargs) + def __init__(self, configuration, environment): + super().__init__(configuration, environment) self.__configuration = configuration self.__environment = environment - self.__kwargs = kwargs - def execute(self): + def execute(self, **kwargs): return diff --git a/linspector/services/sys/uptime.py b/linspector/services/sys/uptime.py index a6bb1ef..42ee401 100644 --- a/linspector/services/sys/uptime.py +++ b/linspector/services/sys/uptime.py @@ -7,17 +7,16 @@ from linspector.core.helpers import log from linspector.core.service import Service -def get(configuration, environment, **kwargs): - return UptimeService(configuration, environment, **kwargs) +def get(configuration, environment): + return UptimeService(configuration, environment) class UptimeService(Service): - def __init__(self, configuration, environment, **kwargs): - super().__init__(configuration, environment, **kwargs) + def __init__(self, configuration, environment): + super().__init__(configuration, environment) self.__configuration = configuration self.__environment = environment - self.__kwargs = kwargs - def execute(self): + def execute(self, **kwargs): return