librenms/librenms-service.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
2.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import argparse
import logging
import os
import sys
import threading
import LibreNMS
from logging import info
2021-03-28 16:02:33 +00:00
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="LibreNMS Service - manages polling and other periodic processes"
)
parser.add_argument(
"-g",
"--group",
nargs="+",
type=int,
help="Set the poller group for this poller",
2021-03-28 16:02:33 +00:00
)
parser.add_argument("-v", "--verbose", action="count", help="Show verbose output.")
parser.add_argument("-d", "--debug", action="store_true", help="Show debug output.")
parser.add_argument(
"-m",
"--multiple",
action="store_true",
help="Allow multiple instances of the service.",
)
parser.add_argument(
"-t",
"--timestamps",
action="store_true",
help="Include timestamps in the logs (not normally needed for syslog/journald",
)
args = parser.parse_args()
if args.timestamps:
2021-03-28 16:02:33 +00:00
logging.basicConfig(
format="%(asctime)s %(threadName)s(%(levelname)s):%(message)s"
)
else:
2021-03-28 16:02:33 +00:00
logging.basicConfig(format="%(threadName)s(%(levelname)s):%(message)s")
if args.verbose:
logging.getLogger().setLevel(logging.INFO)
Full Python code fusion / refactor and hardening 2nd edition (#13188) * New service/discovery/poller wrapper * Convert old wrapper scripts to bootstrap loaders for wrapper.py * Move wrapper.py to LibreNMS module directory * Reformat files * File reformatting * bootstrap files reformatting * Fusion service and wrapper database connections and get_config_data functions * Moved subprocess calls to command_runner * LibreNMS library and __init__ fusion * Reformat files * Normalize logging use * Reformatting code * Fix missing argument for error log * Fix refactor typo in DBConfig class * Add default timeout for config.php data fetching * distributed discovery should finish with a timestamp instead of an epoch * Fix docstring inside dict prevents service key to work * Fix poller insert statement * Fix service wrapper typo * Update docstring since we changed function behavior * Normalize SQL statements * Convert optparse to argparse * Revert discovery thread number * Handle debug logging * Fix file option typo * Reformat code * Add credits to source package * Rename logs depending on the wrapper type * Cap max logfile size to 10MB * Reformat code * Add exception for Redis < 5.0 * Make sure we always log something from service * Fix bogus description * Add an error message on missing config file * Improve error message when .env file cannot be loaded * Improve wrapper logging * Fix cron run may fail when environment path is not set * Add missing -wrapper suffix for logs * Conform to prior naming scheme * Linter fix * Add inline copy of command_runner * Another linter fix * Raise exception after logging * Updated inline command_runner * Add command_runner to requirements * I guess I love linter fixes ;) * Don't spawn more threads than devices * Fix typo in log call * Add exit codes to log on error, add command line to debug log * Add thread name to error message * Log errors in end message for easier debugging * Typo fix * In love of linting
2021-09-27 19:24:25 +00:00
elif args.debug:
logging.getLogger().setLevel(logging.DEBUG)
Full Python code fusion / refactor and hardening 2nd edition (#13188) * New service/discovery/poller wrapper * Convert old wrapper scripts to bootstrap loaders for wrapper.py * Move wrapper.py to LibreNMS module directory * Reformat files * File reformatting * bootstrap files reformatting * Fusion service and wrapper database connections and get_config_data functions * Moved subprocess calls to command_runner * LibreNMS library and __init__ fusion * Reformat files * Normalize logging use * Reformatting code * Fix missing argument for error log * Fix refactor typo in DBConfig class * Add default timeout for config.php data fetching * distributed discovery should finish with a timestamp instead of an epoch * Fix docstring inside dict prevents service key to work * Fix poller insert statement * Fix service wrapper typo * Update docstring since we changed function behavior * Normalize SQL statements * Convert optparse to argparse * Revert discovery thread number * Handle debug logging * Fix file option typo * Reformat code * Add credits to source package * Rename logs depending on the wrapper type * Cap max logfile size to 10MB * Reformat code * Add exception for Redis < 5.0 * Make sure we always log something from service * Fix bogus description * Add an error message on missing config file * Improve error message when .env file cannot be loaded * Improve wrapper logging * Fix cron run may fail when environment path is not set * Add missing -wrapper suffix for logs * Conform to prior naming scheme * Linter fix * Add inline copy of command_runner * Another linter fix * Raise exception after logging * Updated inline command_runner * Add command_runner to requirements * I guess I love linter fixes ;) * Don't spawn more threads than devices * Fix typo in log call * Add exit codes to log on error, add command line to debug log * Add thread name to error message * Log errors in end message for easier debugging * Typo fix * In love of linting
2021-09-27 19:24:25 +00:00
else:
logging.getLogger().setLevel(logging.WARNING)
info("Configuring LibreNMS service")
try:
service = LibreNMS.Service()
except Exception as e:
# catch any initialization errors and print the message instead of a stack trace
print(e)
sys.exit(2)
service.config.single_instance = args.multiple
if args.group:
if isinstance(args.group, list):
service.config.group = args.group
else:
service.config.group = [args.group]
2021-03-28 16:02:33 +00:00
info(
"Entering main LibreNMS service loop on {}/{}...".format(
os.getpid(), threading.current_thread().name
)
)
service.start()