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
# version.
__version__ = '0.19.14.dev1'
__version__ = '0.19.15.dev1'
__author__ = 'Johannes Findeisen <you@hanez.org>'
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.
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))

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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"

View file

@ -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',

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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