From e665933bfbea74a36c0033da96feb53e92d546e8 Mon Sep 17 00:00:00 2001 From: Johannes Findeisen Date: Wed, 5 Oct 2022 05:50:13 +0200 Subject: [PATCH] Some more logging stuff... --- bin/linspector | 28 ++++++++++++++--------- etc/linspector.conf | 5 ++-- etc/monitors/network1/gateway.conf | 4 +++- etc/monitors/test/test10.conf | 2 +- etc/monitors/test/test2.conf | 1 + etc/monitors/test/test3.conf | 3 ++- linspector/core/configuration.py | 8 ++++--- linspector/services/misc/dummy.py | 2 +- linspector/services/net/fritzboxuplink.py | 2 +- 9 files changed, 34 insertions(+), 21 deletions(-) diff --git a/bin/linspector b/bin/linspector index 1afa3aa..b7ec7f6 100755 --- a/bin/linspector +++ b/bin/linspector @@ -34,7 +34,7 @@ from linspector.core.monitors import Monitors # i currently only increase the 3rd number because the goal is that 0.19 will become the first # stable version. -__version__ = '0.19.29.dev1' +__version__ = '0.19.30.dev1' __author__ = 'Johannes Findeisen ' logger = logging.getLogger('linspector') @@ -61,6 +61,9 @@ def parse_args(): parser.add_argument('-s', '--stdout', default=False, dest='stdout', action='store_true', help='log to stdout') + parser.add_argument('-V', '--verbose', default=False, dest='verbose', action='store_true', + help='log in debug mode') + parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + str(__version__)) return parser.parse_args() @@ -79,9 +82,13 @@ def main(): tasks = {} if args.stdout: - # setting pre initialization default log level to INFO. this changes after initialization - # of the configuration. maybe there are better solutions...? - logger.setLevel(logging.INFO) + if args.verbose: + logger.setLevel(logging.DEBUG) + else: + # setting pre initialization default log level to INFO. this changes after + # initialization of the configuration. maybe there are better solutions...? + logger.setLevel(logging.INFO) + stdout_formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s') stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler.setFormatter(stdout_formatter) @@ -89,7 +96,7 @@ def main(): try: configuration = Configuration(args.configuration_path, environment) - #configuration.dump_to_ini() + logger.debug('configuration dump: ' + configuration.dump_to_ini()) except Exception as err: logger.critical('[linspector] configuration error: {0}'.format(err)) sys.exit(1) @@ -103,15 +110,14 @@ def main(): logging.Formatter('[%(asctime)s]:[%(levelname)s]:[%(name)s]:%(message)s') if configuration.get_option('linspector', 'log_file_size'): - log_file_size_mb = int(configuration.get_option('linspector', 'log_file_size')) - b = 1000000 - log_file_size_bytes = int(log_file_size_mb * b) + log_file_size_mb = float(configuration.get_option('linspector', 'log_file_size')) + log_file_size_bytes = float(log_file_size_mb * 1000000) elif configuration.get_option('linspector', 'log_file_size_bytes'): - log_file_size_bytes = int(configuration.get_option('linspector', - 'log_file_size_bytes')) + log_file_size_bytes = float(configuration.get_option('linspector', + 'log_file_size_bytes')) else: # default log file size is 10MB - log_file_size_bytes = 10485760 + log_file_size_bytes = float(10485760) if configuration.get_option('linspector', 'log_file_count'): log_file_count = int(configuration.get_option('linspector', 'log_file_count')) diff --git a/etc/linspector.conf b/etc/linspector.conf index c69ca3f..35ad480 100644 --- a/etc/linspector.conf +++ b/etc/linspector.conf @@ -5,9 +5,10 @@ error_receivers = admin@example.com ; available log levels are: "error", "warning", "info" and "debug". log_level= debug log_file = ~/code/linspector/linspector/log/linspector.log -; number of log files to be kept. +; number of log files to be kept. i recommend to use the lowest sensible value for keeping performance hight. so set +; the size of the log file higher to increase the log history. log_file_count = 50 -; log file size in megabytes as int. +; log file size in megabytes as float. log_file_size = 1 ; log file size in bytes as int. you can set to bytes if you want to set a value lower then 1 megabyte. but it will ; only be used when log_file_size is not set. diff --git a/etc/monitors/network1/gateway.conf b/etc/monitors/network1/gateway.conf index a1b722b..a68d8ec 100644 --- a/etc/monitors/network1/gateway.conf +++ b/etc/monitors/network1/gateway.conf @@ -7,7 +7,9 @@ service = net.fritzboxuplink hosts = 192.168.0.1,192.168.23.24,@group1,@testgroup1 interval = 60 -; optional options when using notifications, plugins, services or tasks: +; optional options when using notifications, plugins, services or tasks. btw. they still can be a required argument by +; a corresponding notification, plugin, service or task. they will need to check required args by themself and throw an +; exception if something fails. linspector never knows about required args: [args] ; notifications separated by ','. no whitespaces allowed. the case is not important. notifications = sms,emaiL diff --git a/etc/monitors/test/test10.conf b/etc/monitors/test/test10.conf index 7e40ba7..7c212c7 100644 --- a/etc/monitors/test/test10.conf +++ b/etc/monitors/test/test10.conf @@ -1,7 +1,7 @@ [monitor] description = Test10 service = misc.dummy -interval = 60 +interval = 120 hosts = 192.168.10.10 [args] diff --git a/etc/monitors/test/test2.conf b/etc/monitors/test/test2.conf index 698872e..4bc6b10 100644 --- a/etc/monitors/test/test2.conf +++ b/etc/monitors/test/test2.conf @@ -5,3 +5,4 @@ interval = 10 hosts = 192.168.10.10 [args] +test_dummy_arg = nothing diff --git a/etc/monitors/test/test3.conf b/etc/monitors/test/test3.conf index 79dfa8a..caf95cd 100644 --- a/etc/monitors/test/test3.conf +++ b/etc/monitors/test/test3.conf @@ -1,7 +1,8 @@ [monitor] description = Test3 service = misc.dummy -interval = 120 +interval = 5 hosts = 192.168.10.10 [args] +foo = bar diff --git a/linspector/core/configuration.py b/linspector/core/configuration.py index 1d57447..55c4de0 100644 --- a/linspector/core/configuration.py +++ b/linspector/core/configuration.py @@ -50,16 +50,18 @@ class Configuration: source_section_option)) def dump_to_ini(self): + dump = '' i = 0 for section in self.__configuration.sections(): if i < 1: - print('[' + section + ']') + dump = dump + '[' + section + ']\n' else: - print('\n[' + section + ']') + dump = dump + '\n[' + section + ']\n' options = self.__configuration.options(section) for option in options: - print(option + " = " + self.__configuration.get(section, option)) + dump = dump + option + " = " + self.__configuration.get(section, option) + '\n' i = 1 + return dump def get_configuration_path(self): return self.__configuration_path diff --git a/linspector/services/misc/dummy.py b/linspector/services/misc/dummy.py index 98f8be7..1c2ecac 100644 --- a/linspector/services/misc/dummy.py +++ b/linspector/services/misc/dummy.py @@ -19,6 +19,6 @@ class DummyService(Service): self.__environment = environment def execute(self, **kwargs): - log('debug', 'DummyService object ' + str(self)) + log('debug', 'DummyService object ' + str(self) + ' using kwargs: ' + str(kwargs)) #log('debug', 'dummy object @' + str(self) + str(self.__kwargs['foo'])) return diff --git a/linspector/services/net/fritzboxuplink.py b/linspector/services/net/fritzboxuplink.py index 3347141..c5ac24b 100644 --- a/linspector/services/net/fritzboxuplink.py +++ b/linspector/services/net/fritzboxuplink.py @@ -21,5 +21,5 @@ class FritzboxUplinkService(Service): self.__environment = environment def execute(self, **kwargs): - log('debug', 'FritzboxUplinkService object ' + str(self)) + log('debug', 'FritzboxUplinkService object ' + str(self) + ' using kwargs: ' + str(kwargs)) return