Some more logging stuff...

This commit is contained in:
Johannes Findeisen 2022-10-05 05:50:13 +02:00
commit e665933bfb
9 changed files with 34 additions and 21 deletions

View file

@ -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 # i currently only increase the 3rd number because the goal is that 0.19 will become the first
# stable version. # stable version.
__version__ = '0.19.29.dev1' __version__ = '0.19.30.dev1'
__author__ = 'Johannes Findeisen <you@hanez.org>' __author__ = 'Johannes Findeisen <you@hanez.org>'
logger = logging.getLogger('linspector') logger = logging.getLogger('linspector')
@ -61,6 +61,9 @@ def parse_args():
parser.add_argument('-s', '--stdout', default=False, dest='stdout', action='store_true', parser.add_argument('-s', '--stdout', default=False, dest='stdout', action='store_true',
help='log to stdout') 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__)) parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + str(__version__))
return parser.parse_args() return parser.parse_args()
@ -79,9 +82,13 @@ def main():
tasks = {} tasks = {}
if args.stdout: if args.stdout:
# setting pre initialization default log level to INFO. this changes after initialization if args.verbose:
# of the configuration. maybe there are better solutions...? logger.setLevel(logging.DEBUG)
logger.setLevel(logging.INFO) 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_formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s')
stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(stdout_formatter) stdout_handler.setFormatter(stdout_formatter)
@ -89,7 +96,7 @@ def main():
try: try:
configuration = Configuration(args.configuration_path, environment) configuration = Configuration(args.configuration_path, environment)
#configuration.dump_to_ini() logger.debug('configuration dump: ' + configuration.dump_to_ini())
except Exception as err: except Exception as err:
logger.critical('[linspector] configuration error: {0}'.format(err)) logger.critical('[linspector] configuration error: {0}'.format(err))
sys.exit(1) sys.exit(1)
@ -103,15 +110,14 @@ def main():
logging.Formatter('[%(asctime)s]:[%(levelname)s]:[%(name)s]:%(message)s') logging.Formatter('[%(asctime)s]:[%(levelname)s]:[%(name)s]:%(message)s')
if configuration.get_option('linspector', 'log_file_size'): if configuration.get_option('linspector', 'log_file_size'):
log_file_size_mb = int(configuration.get_option('linspector', 'log_file_size')) log_file_size_mb = float(configuration.get_option('linspector', 'log_file_size'))
b = 1000000 log_file_size_bytes = float(log_file_size_mb * 1000000)
log_file_size_bytes = int(log_file_size_mb * b)
elif configuration.get_option('linspector', 'log_file_size_bytes'): elif configuration.get_option('linspector', 'log_file_size_bytes'):
log_file_size_bytes = int(configuration.get_option('linspector', log_file_size_bytes = float(configuration.get_option('linspector',
'log_file_size_bytes')) 'log_file_size_bytes'))
else: else:
# default log file size is 10MB # 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'): if configuration.get_option('linspector', 'log_file_count'):
log_file_count = int(configuration.get_option('linspector', 'log_file_count')) log_file_count = int(configuration.get_option('linspector', 'log_file_count'))

View file

@ -5,9 +5,10 @@ error_receivers = admin@example.com
; available log levels are: "error", "warning", "info" and "debug". ; available log levels are: "error", "warning", "info" and "debug".
log_level= debug log_level= debug
log_file = ~/code/linspector/linspector/log/linspector.log 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_count = 50
; log file size in megabytes as int. ; log file size in megabytes as float.
log_file_size = 1 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 ; 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. ; only be used when log_file_size is not set.

View file

@ -7,7 +7,9 @@ service = net.fritzboxuplink
hosts = 192.168.0.1,192.168.23.24,@group1,@testgroup1 hosts = 192.168.0.1,192.168.23.24,@group1,@testgroup1
interval = 60 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] [args]
; notifications separated by ','. no whitespaces allowed. the case is not important. ; notifications separated by ','. no whitespaces allowed. the case is not important.
notifications = sms,emaiL notifications = sms,emaiL

View file

@ -1,7 +1,7 @@
[monitor] [monitor]
description = Test10 description = Test10
service = misc.dummy service = misc.dummy
interval = 60 interval = 120
hosts = 192.168.10.10 hosts = 192.168.10.10
[args] [args]

View file

@ -5,3 +5,4 @@ interval = 10
hosts = 192.168.10.10 hosts = 192.168.10.10
[args] [args]
test_dummy_arg = nothing

View file

@ -1,7 +1,8 @@
[monitor] [monitor]
description = Test3 description = Test3
service = misc.dummy service = misc.dummy
interval = 120 interval = 5
hosts = 192.168.10.10 hosts = 192.168.10.10
[args] [args]
foo = bar

View file

@ -50,16 +50,18 @@ class Configuration:
source_section_option)) source_section_option))
def dump_to_ini(self): def dump_to_ini(self):
dump = ''
i = 0 i = 0
for section in self.__configuration.sections(): for section in self.__configuration.sections():
if i < 1: if i < 1:
print('[' + section + ']') dump = dump + '[' + section + ']\n'
else: else:
print('\n[' + section + ']') dump = dump + '\n[' + section + ']\n'
options = self.__configuration.options(section) options = self.__configuration.options(section)
for option in options: for option in options:
print(option + " = " + self.__configuration.get(section, option)) dump = dump + option + " = " + self.__configuration.get(section, option) + '\n'
i = 1 i = 1
return dump
def get_configuration_path(self): def get_configuration_path(self):
return self.__configuration_path return self.__configuration_path

View file

@ -19,6 +19,6 @@ class DummyService(Service):
self.__environment = environment self.__environment = environment
def execute(self, **kwargs): 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'])) #log('debug', 'dummy object @' + str(self) + str(self.__kwargs['foo']))
return return

View file

@ -21,5 +21,5 @@ class FritzboxUplinkService(Service):
self.__environment = environment self.__environment = environment
def execute(self, **kwargs): def execute(self, **kwargs):
log('debug', 'FritzboxUplinkService object ' + str(self)) log('debug', 'FritzboxUplinkService object ' + str(self) + ' using kwargs: ' + str(kwargs))
return return