diff --git a/bin/linspector b/bin/linspector index 34f5a49..80e8c16 100755 --- a/bin/linspector +++ b/bin/linspector @@ -37,7 +37,7 @@ logger = logging.getLogger('linspector') # i currently only increase the 3rd number because the goal is that 0.19.* will become the first # stable version. -__version__ = '0.19.42.dev1' +__version__ = '0.19.43.dev1' __author__ = 'Johannes Findeisen ' diff --git a/bin/lish b/bin/lish new file mode 100755 index 0000000..eaccd7a --- /dev/null +++ b/bin/lish @@ -0,0 +1,181 @@ +#!/usr/bin/python3 -d +""" +This file is part of Linspector (https://linspector.org/) +Copyright (c) 2022 Johannes Findeisen . All Rights Reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next +paragraph) shall be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +""" +import argparse +import logging +import logging.handlers +import os +import sys + +from linspector.core.configuration import Configuration +from linspector.core.environment import Environment +from linspector.core.helpers import log +from linspector.core.linspector import Linspector +from linspector.core.monitors import Monitors + +logger = logging.getLogger('linspector') + +# i currently only increase the 3rd number because the goal is that 0.19.* will become the first +# stable version. +__version__ = '0.0.1.dev1' +__author__ = 'Johannes Findeisen ' + + +def parse_args(): + parser = argparse.ArgumentParser( + description='lish is part of the linspector project and is a CLI client to connect to a ' + 'linspector instance configured to load the rpc plugin for remote connections.', + epilog='author: ' + __author__, + prog='linspector') + + parser.add_argument('host', metavar='HOST', + help='the configuration path to use') + + 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() + + +def main(): + args = parse_args() + environment = Environment() + monitors = None + notifications = {} + plugins = {} + # the scheduler is a dict to make it possible in the future to run more than one scheduler in + # one linspector instance. need to think about it a little more... + scheduler = {} + services = {} + tasks = {} + + if args.stdout: + 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) + logger.addHandler(stdout_handler) + + try: + configuration = Configuration(args.configuration_path, environment, log) + logger.debug('configuration dump: ' + configuration.dump_to_ini()) + except Exception as err: + log('critical', '[linspector] configuration error: {0}'.format(err)) + sys.exit(1) + + if configuration.get_option('linspector', 'log_file'): + log_file = os.path.expanduser(configuration.get_option('linspector', 'log_file')) + if not os.path.exists(os.path.dirname(log_file)): + os.makedirs(os.path.dirname(log_file)) + + log_file_formatter = \ + 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')) + log_file_size_bytes = int(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')) + else: + # default log file size is 10000000 bytes (10MiB) + log_file_size_bytes = int(10000000) + + if configuration.get_option('linspector', 'log_file_count'): + log_file_count = int(configuration.get_option('linspector', 'log_file_count')) + else: + # default log file count is 1. + log_file_count = 1 + + log_file_handler = logging.handlers.RotatingFileHandler(log_file, + maxBytes=log_file_size_bytes, + backupCount=log_file_count) + + log_file_handler.setFormatter(log_file_formatter) + logger.addHandler(log_file_handler) + + log_level = 'None' + # critical errors will always show up even when no log_level is set. this is most silent. + logger.setLevel(logging.CRITICAL) + if configuration.get_option('linspector', 'log_level'): + log_level = str(configuration.get_option('linspector', 'log_level')) + if log_level == "error": + logger.setLevel(logging.ERROR) + elif log_level == "warning": + logger.setLevel(logging.WARNING) + elif log_level == "info": + logger.setLevel(logging.INFO) + elif log_level == "debug": + logger.setLevel(logging.DEBUG) + #elif configuration.get_option('linspector', 'log_level') != 'error' != 'warning' \ + # != 'info' != 'debug': + # logger.warning('[linspector] log level: "' + log_level + '" not found!') + + try: + monitors = Monitors(configuration, environment, log, notifications, services, tasks) + except Exception as err: + log('warning', '[linspector] monitor initialization error: {0}'.format(err)) + + try: + linspector = Linspector(configuration, environment, log, monitors, plugins, scheduler) + #linspector.print_debug() + except Exception as err: + log('critical', '[linspector] core initialization error: {0}'.format(err)) + sys.exit(1) + + # daemon initialization + if args.daemon: + try: + from linspector.core.linspectord import Linspectord + linspectord = Linspectord(configuration, environment, linspector, log) + # do handling of restart, start and stop commands but for now "start" is enough... ;) + if args.kill: + log('info', '[linspector] stopping daemon.') + linspectord.stop() + elif args.restart: + log('info', '[linspector] restarting daemon.') + linspectord.restart() + else: + log('info', '[linspector] starting daemon.') + linspectord.start() + except Exception as err: + log('critical', '[linspector] daemon error: {0}'.format(err)) + sys.exit(1) + else: + try: + while True: + pass + except KeyboardInterrupt: + log('info', '[linspector] program terminated by user!') + + +if __name__ == '__main__': + main() diff --git a/etc/linspector.conf b/etc/linspector.conf index d29c399..13f2a93 100644 --- a/etc/linspector.conf +++ b/etc/linspector.conf @@ -15,7 +15,7 @@ log_file_size = 5 log_file_size_bytes = 100000 pid_file = /var/run/user/1000/linspector.pid ; plugins separated by ','. no whitespaces allowed! not case sensitive. -plugins = lish,httpserver +plugins = rpc,httpserver ; globally configured tasks will always run on all monitors when no task is configured there. if tasks are configured in ; a monitor then maybe only run tasks from the dedicated monitor. maybe it is a good idea to run global tasks in every ; monitor and the monitor can add tasks to the global settings... need to think about it. diff --git a/linspector/plugins/httpserver.py b/linspector/plugins/httpserver.py index 632dcbd..ebfd609 100644 --- a/linspector/plugins/httpserver.py +++ b/linspector/plugins/httpserver.py @@ -21,7 +21,6 @@ class HTTPServerPlugin(Plugin): self.__configuration = configuration self.__environment = environment self.__linspector = linspector - self.__log = log @cherrypy.expose def index(self): diff --git a/linspector/plugins/lish.py b/linspector/plugins/rpc.py similarity index 87% rename from linspector/plugins/lish.py rename to linspector/plugins/rpc.py index 3724a26..0bdb492 100644 --- a/linspector/plugins/lish.py +++ b/linspector/plugins/rpc.py @@ -7,11 +7,11 @@ from linspector.core.plugin import Plugin def create(configuration, environment, linspector, log): - return LishPlugin(configuration, environment, linspector, log) + return RPCPlugin(configuration, environment, linspector, log) # TODO: check for all required configuration options and set defaults if needed. -class LishPlugin(Plugin): +class RPCPlugin(Plugin): def __init__(self, configuration, environment, linspector, log): super().__init__(configuration, environment, linspector, log) self.__configuration = configuration