Added file logging and a new parameter (-s) for logging to stdout.

This commit is contained in:
Johannes Findeisen 2022-09-29 02:59:40 +02:00
commit dbb0e23091
5 changed files with 66 additions and 18 deletions

View file

@ -21,6 +21,7 @@ class Configuration:
self.__configuration_path = configuration_path
self.__environment = environment
logger.info('reading configuration file: ' + configuration_path + '/linspector.conf')
if os.path.isfile(configuration_path + '/linspector.conf'):
try:
self.__configuration.read(configuration_path + '/linspector.conf', 'utf-8')
@ -39,7 +40,7 @@ class Configuration:
section_list = glob.glob(configuration_path + '/' + target_section + '/*.conf')
for section_file in section_list:
#print(__file__ + ' (60): ' + section_file)
#print(__file__ + ' (45): ' + section_file)
configuration = configparser.ConfigParser()
configuration.read(section_file, 'utf-8')
for source_section in configuration.sections():

View file

@ -24,11 +24,13 @@ class Linspector:
self.__plugins = plugins
# load plugins
logger.info('loading plugins...')
if configuration.get_option('linspector', 'plugins'):
plugin_list = configuration.get_option('linspector', 'plugins')
self.__plugin_list = plugin_list.split(',')
for plugin_option in plugin_list.split(','):
if plugin_option not in plugins:
logger.info('loading plugin: ' + plugin_option)
plugin_package = 'linspector.plugins.' + plugin_option.lower()
plugin_module = importlib.import_module(plugin_package)
plugin = plugin_module.get(configuration, environment, self)

View file

@ -14,6 +14,7 @@ from logging import getLogger
logger = getLogger('linspector')
# TODO: there is a bug when stopping the daemon. the pid_file is not being deleted. NEEDS A FIX!
class Linspectord:
def __init__(self, configuration, environment, linspector):
@ -23,7 +24,7 @@ class Linspectord:
try:
self.__pid_file = configuration.get_option('linspector', 'pid_file')
except Exception as err:
logger.error(str('daemonize error (no pid_file set): {0}'.format(err)))
logger.critical(str('daemonize error (no pid_file set): {0}'.format(err)))
def daemonize(self):
# daemonize the class using the UNIX double fork mechanism.
@ -35,7 +36,7 @@ class Linspectord:
# exit first parent.
sys.exit(0)
except OSError as err:
logger.error(str('fork #1 failed: {0}'.format(err)))
logger.critical(str('fork #1 failed: {0}'.format(err)))
sys.exit(1)
# decouple from parent environment.
@ -50,7 +51,7 @@ class Linspectord:
# Exit from second parent.
sys.exit(0)
except OSError as err:
logger.error(str('fork #2 failed: {0}'.format(err)))
logger.critical(str('fork #2 failed: {0}'.format(err)))
sys.exit(1)
# redirect standard file descriptors.
@ -76,7 +77,7 @@ class Linspectord:
def start(self):
# start the daemon. check for a pidfile to see if the daemon already runs before.
logger.info('starting daemon using pid_file: ' + self.__pid_file)
try:
with open(self.__pid_file, 'r') as pf:
pid = int(pf.read().strip())
@ -85,7 +86,7 @@ class Linspectord:
if pid:
message = 'pid_file {0} already exist. daemon already running?'
logger.error(str(message.format(self.__pid_file)))
logger.critical(str(message.format(self.__pid_file)))
sys.exit(1)
# start the daemon.
@ -94,7 +95,7 @@ class Linspectord:
def stop(self):
# stop the daemon.
logger.info('stopping daemon using pid_file: ' + self.__pid_file)
# get the pid from the pid file.
try:
with open(self.__pid_file, 'r') as pf:
@ -118,15 +119,18 @@ class Linspectord:
if os.path.exists(self.__pid_file):
os.remove(self.__pid_file)
else:
logger.error(str(err.args))
logger.critical(str(err.args))
sys.exit(1)
def restart(self):
# restart the daemon.
logger.info('restarting daemon using pid_file: ' + self.__pid_file)
self.stop()
self.start()
# maybe this function can be removed in the future because Linspector should rund endless when
# starting scheduled jobs. need to cover this in the future. for now, it is useful for testing
# while development because the daemon even runs when internally is nothing to do.
@staticmethod
def run():
while True: