Moved kwargs from __init__ to execute() in services, optimized logging and a lot of bug fixes and refactoring. Bug fixes too.

This commit is contained in:
Johannes Findeisen 2022-10-01 08:43:19 +02:00
commit d57fd97fbd
16 changed files with 98 additions and 98 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.14.dev1' __version__ = '0.19.15.dev1'
__author__ = 'Johannes Findeisen <you@hanez.org>' __author__ = 'Johannes Findeisen <you@hanez.org>'
logger = logging.getLogger('linspector') logger = logging.getLogger('linspector')

View file

@ -3,30 +3,42 @@ 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, msg): def log(level, msg):
frm = inspect.stack()[1] # only use inspect when log level NOTSET or DEBUG is enabled.
function_name = frm.function if logger.isEnabledFor(0) or logger.isEnabledFor(10):
module_name = inspect.getmodule(frm[0]).__name__ import inspect
line_number = str(frm.lineno) frm = inspect.stack()[1]
if level == 'critical': function_name = frm.function
logger.critical('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + 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)) str(msg))
if level == 'error': elif level == 'debug':
logger.error('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + logger.debug('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' +
str(msg)) str(msg))
elif level == 'warning': else:
logger.warning('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + if level == 'critical':
str(msg)) logger.critical(str(msg))
elif level == 'info': if level == 'error':
logger.info('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + logger.error(str(msg))
str(msg)) elif level == 'warning':
elif level == 'debug': logger.warning(str(msg))
logger.debug('[' + module_name + ']:[' + function_name + ']:[' + line_number + '] ' + elif level == 'info':
str(msg)) logger.info(str(msg))
elif level == 'debug':
logger.debug(str(msg))

View file

@ -51,7 +51,8 @@ class Monitor:
WARNING when a job has errors but not the threshold overridden WARNING when a job has errors but not the threshold overridden
RECOVER when a job recovers e.g. the threshold decrements (not implemented) RECOVER when a job recovers e.g. the threshold decrements (not implemented)
ERROR when a jobs threshold is overridden 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.status = "NONE"
self.last_execution = None self.last_execution = None
@ -94,7 +95,7 @@ class Monitor:
service_module = importlib.import_module(service_package) 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, **self.__args) service = service_module.create(configuration, environment)
self.__services[monitor_configuration.get('monitor', 'service').lower()] = service self.__services[monitor_configuration.get('monitor', 'service').lower()] = service
try: try:
if configuration.get_option('linspector', 'tasks') or \ if configuration.get_option('linspector', 'tasks') or \
@ -199,7 +200,7 @@ class Monitor:
#TaskExecutor.instance().schedule_task(monitor_information, task) #TaskExecutor.instance().schedule_task(monitor_information, task)
def handle_call(self): 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("handle call")
#logger.debug(self.service) #logger.debug(self.service)
if self.enabled: if self.enabled:
@ -207,7 +208,7 @@ class Monitor:
try: try:
self.last_execution = MonitorExecution(self.get_host()) self.last_execution = MonitorExecution(self.get_host())
#self.__services[self.__service].execute(self.last_execution) #self.__services[self.__service].execute(self.last_execution)
self.__services[self.__service].execute() self.__services[self.__service].execute(**self.__args)
except Exception as err: except Exception as err:
log('error', err) log('error', err)

View file

@ -8,7 +8,6 @@ from linspector.core.helpers import log
class Service: class Service:
def __init__(self, configuration, environment, **kwargs): def __init__(self, configuration, environment):
self.__configuration = configuration self.__configuration = configuration
self._environment = environment self._environment = environment
self.__kwargs = kwargs

View file

@ -7,17 +7,16 @@ from linspector.core.helpers import log
from linspector.core.service import Service from linspector.core.service import Service
def create(configuration, environment, **kwargs): def create(configuration, environment):
return HTTPKeywordService(configuration, environment, **kwargs) return HTTPKeywordService(configuration, environment)
class HTTPKeywordService(Service): class HTTPKeywordService(Service):
def __init__(self, configuration, environment, **kwargs): def __init__(self, configuration, environment):
super().__init__(configuration, environment, **kwargs) super().__init__(configuration, environment)
self.__configuration = configuration self.__configuration = configuration
self.__environment = environment self.__environment = environment
self.__kwargs = kwargs
def execute(self): def execute(self, **kwargs):
return return

View file

@ -7,19 +7,18 @@ from linspector.core.helpers import log
from linspector.core.service import Service from linspector.core.service import Service
def create(configuration, environment, **kwargs): def create(configuration, environment):
return DummyService(configuration, environment, **kwargs) return DummyService(configuration, environment)
class DummyService(Service): class DummyService(Service):
def __init__(self, configuration, environment, **kwargs): def __init__(self, configuration, environment):
super().__init__(configuration, environment) super().__init__(configuration, environment)
self.__configuration = configuration self.__configuration = configuration
self.__environment = environment 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))
#log('debug', 'dummy object @' + str(self) + str(self.__kwargs['foo'])) #log('debug', 'dummy object @' + str(self) + str(self.__kwargs['foo']))
return return

View file

@ -7,17 +7,16 @@ from linspector.core.helpers import log
from linspector.core.service import Service from linspector.core.service import Service
def create(configuration, environment, **kwargs): def create(configuration, environment):
return RandomService(configuration, environment, **kwargs) return RandomService(configuration, environment)
class RandomService(Service): class RandomService(Service):
def __init__(self, configuration, environment, **kwargs): def __init__(self, configuration, environment):
super().__init__(configuration, environment, **kwargs) super().__init__(configuration, environment)
self.__configuration = configuration self.__configuration = configuration
self.__environment = environment self.__environment = environment
self.__kwargs = kwargs
def execute(self): def execute(self, **kwargs):
return return

View file

@ -9,18 +9,17 @@ from linspector.core.helpers import log
from linspector.core.service import Service from linspector.core.service import Service
def create(configuration, environment, **kwargs): def create(configuration, environment):
return FritzboxUplinkService(configuration, environment, **kwargs) return FritzboxUplinkService(configuration, environment)
class FritzboxUplinkService(Service): class FritzboxUplinkService(Service):
def __init__(self, configuration, environment, **kwargs): def __init__(self, configuration, environment):
super().__init__(configuration, environment, **kwargs) super().__init__(configuration, environment)
self.__configuration = configuration self.__configuration = configuration
self.__environment = environment self.__environment = environment
self.__kwargs = kwargs
def execute(self): def execute(self, **kwargs):
log('debug', 'fritzboxuplink object @' + str(self)) log('debug', 'fritzboxuplink object @' + str(self))
return return

View file

@ -7,17 +7,16 @@ from linspector.core.helpers import log
from linspector.core.service import Service from linspector.core.service import Service
def create(configuration, environment, **kwargs): def create(configuration, environment):
return PingService(configuration, environment, **kwargs) return PingService(configuration, environment)
class PingService(Service): class PingService(Service):
def __init__(self, configuration, environment, **kwargs): def __init__(self, configuration, environment):
super().__init__(configuration, environment, **kwargs) super().__init__(configuration, environment)
self.__configuration = configuration self.__configuration = configuration
self.__environment = environment self.__environment = environment
self.__kwargs = kwargs
def execute(self): def execute(self, **kwargs):
return return

View file

@ -9,19 +9,18 @@ from linspector.core.helpers import log
from linspector.core.service import Service from linspector.core.service import Service
def create(configuration, environment, **kwargs): def create(configuration, environment):
return PortService(configuration, environment, **kwargs) return PortService(configuration, environment)
class PortService(Service): class PortService(Service):
def __init__(self, configuration, environment, **kwargs): def __init__(self, configuration, environment):
super().__init__(configuration, environment, **kwargs) super().__init__(configuration, environment)
self.__configuration = configuration self.__configuration = configuration
self.__environment = environment self.__environment = environment
self.__kwargs = kwargs
def execute(self, execution): def execute(self, execution, **kwargs):
error_code = 0 error_code = 0
msg = "Connection successful established" msg = "Connection successful established"

View file

@ -11,23 +11,22 @@ from linspector.core.helpers import log
from linspector.core.service import Service from linspector.core.service import Service
def create(configuration, environment, **kwargs): def create(configuration, environment):
return SpeedtestService(configuration, environment, **kwargs) return SpeedtestService(configuration, environment)
class SpeedtestService(Service): class SpeedtestService(Service):
def __init__(self, configuration, environment, **kwargs): def __init__(self, configuration, environment):
super().__init__(configuration, environment, **kwargs) super().__init__(configuration, environment)
self.__configuration = configuration self.__configuration = configuration
self.__environment = environment self.__environment = environment
self.__kwargs = kwargs
self.__speedtest_maximum_speed = None self.__speedtest_maximum_speed = None
self.__speedtest_average_speed = None self.__speedtest_average_speed = None
self.__speedtest_time_elapsed = None self.__speedtest_time_elapsed = None
def execute(self): def execute(self, **kwargs):
while True: while True:
tmp_time = time.localtime(calendar.timegm(time.gmtime())) 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',

View file

@ -11,19 +11,18 @@ from linspector.core.helpers import log
from linspector.core.service import Service from linspector.core.service import Service
def create(configuration, environment, **kwargs): def create(configuration, environment):
return SSHService(configuration, environment, **kwargs) return SSHService(configuration, environment)
class SSHService(Service): class SSHService(Service):
def __init__(self, configuration, environment, **kwargs): def __init__(self, configuration, environment):
super().__init__(configuration, environment, **kwargs) super().__init__(configuration, environment)
self.__configuration = configuration self.__configuration = configuration
self.__environment = environment self.__environment = environment
self.__kwargs = kwargs
def execute(self): def execute(self, **kwargs):
path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa') path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa')
key = paramiko.RSAKey.from_private_key_file(path) key = paramiko.RSAKey.from_private_key_file(path)

View file

@ -7,18 +7,17 @@ from linspector.core.helpers import log
from linspector.core.service import Service from linspector.core.service import Service
def create(configuration, environment, **kwargs): def create(configuration, environment):
return TCPConnectService(configuration, environment, **kwargs) return TCPConnectService(configuration, environment)
# TODO: check for all required configuration options and set defaults if needed. # TODO: check for all required configuration options and set defaults if needed.
class TCPConnectService(Service): class TCPConnectService(Service):
def __init__(self, configuration, environment, **kwargs): def __init__(self, configuration, environment):
super().__init__(configuration, environment, **kwargs) super().__init__(configuration, environment)
self.__configuration = configuration self.__configuration = configuration
self.__environment = environment self.__environment = environment
self.__kwargs = kwargs
def execute(self): def execute(self, **kwargs):
return return

View file

@ -9,17 +9,16 @@ from linspector.core.helpers import log
from linspector.core.service import Service from linspector.core.service import Service
def create(configuration, environment, **kwargs): def create(configuration, environment):
return GetService(configuration, environment, **kwargs) return GetService(configuration, environment)
class GetService(Service): class GetService(Service):
def __init__(self, configuration, environment, **kwargs): def __init__(self, configuration, environment):
super().__init__(configuration, environment, **kwargs) super().__init__(configuration, environment)
self.__configuration = configuration self.__configuration = configuration
self.__environment = environment self.__environment = environment
self.__kwargs = kwargs
def execute(self): def execute(self, **kwargs):
return return

View file

@ -7,18 +7,17 @@ from linspector.core.helpers import log
from linspector.core.service import Service from linspector.core.service import Service
def create(configuration, environment, **kwargs): def create(configuration, environment):
return ShellService(configuration, environment, **kwargs) return ShellService(configuration, environment)
class ShellService(Service): class ShellService(Service):
def __init__(self, configuration, environment, **kwargs): def __init__(self, configuration, environment):
super().__init__(configuration, environment, **kwargs) super().__init__(configuration, environment)
self.__configuration = configuration self.__configuration = configuration
self.__environment = environment self.__environment = environment
self.__kwargs = kwargs
def execute(self): def execute(self, **kwargs):
return return

View file

@ -7,17 +7,16 @@ from linspector.core.helpers import log
from linspector.core.service import Service from linspector.core.service import Service
def get(configuration, environment, **kwargs): def get(configuration, environment):
return UptimeService(configuration, environment, **kwargs) return UptimeService(configuration, environment)
class UptimeService(Service): class UptimeService(Service):
def __init__(self, configuration, environment, **kwargs): def __init__(self, configuration, environment):
super().__init__(configuration, environment, **kwargs) super().__init__(configuration, environment)
self.__configuration = configuration self.__configuration = configuration
self.__environment = environment self.__environment = environment
self.__kwargs = kwargs
def execute(self): def execute(self, **kwargs):
return return