From 0e952b9c981124c650cd353d7c7cbbc2f0fb57fa Mon Sep 17 00:00:00 2001 From: Marek Wobst <3329682+mwobst@users.noreply.github.com> Date: Thu, 20 Jul 2023 06:09:57 +0200 Subject: [PATCH] Add module support for wrapper script calls (#14055) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add module support for wrapper script calls The scripts poller.php and discovery.php offer a module option (-m), which may be used to specify specific modules for polling/discovery, possibly for special (and then faster) testing or for example rediscovering the fdb table (on all hosts). Until now, this was not possible with the python wrapper scripts. Now they support a '-m' option, where comma separated module names may be passed. This will currently only work with poller and discovery, though. * Replace single quotation signs with double ones (empty strings only) * Fix more code lines (quotation signs, indentation) Also 'reduced' if-else-clause size at end of LibreNMS/wrapper.py (do not call method at each branch, instead prepare a value for modules) * Add commas after last parameter of dict+methode Also an additional comment sign before # * Fix two leftover single quot. signs … * doc: Add documentation for module support --- LibreNMS/wrapper.py | 28 +++++++++++++++++++++++++++- discovery-wrapper.py | 8 ++++++++ doc/Support/Discovery Support.md | 4 ++++ poller-wrapper.py | 8 ++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/LibreNMS/wrapper.py b/LibreNMS/wrapper.py index 7fae1fc0fb..9c7bbecb0c 100644 --- a/LibreNMS/wrapper.py +++ b/LibreNMS/wrapper.py @@ -44,6 +44,7 @@ import logging import os import queue +import re import sys import threading import time @@ -229,6 +230,7 @@ def poll_worker( log_dir, # Type: str wrapper_type, # Type: str debug, # Type: bool + modules="", # Type: string ): """ This function will fork off single instances of the php process, record @@ -279,6 +281,9 @@ def poll_worker( wrappers[wrapper_type]["executable"], ) command = "/usr/bin/env php {} -h {}".format(executable, device_id) + if modules is not None and len(str(modules).strip()): + module_str = re.sub("\s", "", str(modules).strip()) + command = command + " -m {}".format(module_str) if debug: command = command + " -d" exit_code, output = command_runner( @@ -327,6 +332,7 @@ def wrapper( config, # Type: dict log_dir, # Type: str _debug=False, # Type: bool + **kwargs, # Type: dict, may contain modules ): # -> None """ Actual code that runs various php scripts, in single node mode or distributed poller mode @@ -495,6 +501,7 @@ def wrapper( "log_dir": log_dir, "wrapper_type": wrapper_type, "debug": _debug, + "modules": kwargs.get("modules", ""), }, ) worker.setDaemon(True) @@ -615,6 +622,12 @@ if __name__ == "__main__": default=False, help="Enable debug output. WARNING: Leaving this enabled will consume a lot of disk space.", ) + parser.add_argument( + "-m", + "--modules", + default="", + help="Enable passing of a module string, modules are separated by comma", + ) parser.add_argument( dest="wrapper", @@ -628,6 +641,7 @@ if __name__ == "__main__": args = parser.parse_args() debug = args.debug + modules = args.modules or "" wrapper_type = args.wrapper amount_of_workers = args.threads @@ -654,4 +668,16 @@ if __name__ == "__main__": ) ) - wrapper(wrapper_type, amount_of_workers, config, log_dir, _debug=debug) + if wrapper_type in ["discovery", "poller"]: + modules_validated = modules + else: + modules_validated = "" # ignore module parameter + + wrapper( + wrapper_type, + amount_of_workers, + config, + log_dir, + _debug=debug, + modules=modules_validated, + ) diff --git a/discovery-wrapper.py b/discovery-wrapper.py index bb9920f9e9..839c397461 100755 --- a/discovery-wrapper.py +++ b/discovery-wrapper.py @@ -33,6 +33,13 @@ parser.add_argument( default=False, help="Enable debug output. WARNING: Leaving this enabled will consume a lot of disk space.", ) +parser.add_argument( + "-m", + "--modules", + dest="modules", + default="", + help="Enable passing of a module string, modules are separated by comma", +) args = parser.parse_args() config = LibreNMS.get_config_data(os.path.dirname(os.path.realpath(__file__))) @@ -59,5 +66,6 @@ wrapper.wrapper( amount_of_workers=amount_of_workers, config=config, log_dir=log_dir, + modules=args.modules or "", _debug=args.debug, ) diff --git a/doc/Support/Discovery Support.md b/doc/Support/Discovery Support.md index 3f622729e1..a5b9faa58d 100644 --- a/doc/Support/Discovery Support.md +++ b/doc/Support/Discovery Support.md @@ -48,6 +48,10 @@ If you need to debug the output of discovery-wrapper.py then you can add `-d` to the end of the command - it is NOT recommended to do this in cron. +You also may use `-m` to pass a list of comma-separated modules. +Please refer to [Command options](#command-options) of discovery.php. +Example: `/opt/librenms/discovery-wrapper.py 1 -m bgp-peers` + If you want to switch back to discovery.php then you can replace: `33 */6 * * * librenms /opt/librenms/discovery-wrapper.py 1 >> /dev/null 2>&1` diff --git a/poller-wrapper.py b/poller-wrapper.py index 85f3a1f240..4c6dc4c2ce 100755 --- a/poller-wrapper.py +++ b/poller-wrapper.py @@ -33,6 +33,13 @@ parser.add_argument( default=False, help="Enable debug output. WARNING: Leaving this enabled will consume a lot of disk space.", ) +parser.add_argument( + "-m", + "--modules", + dest="modules", + default="", + help="Enable passing of a module string, modules are separated by comma", +) args = parser.parse_args() config = LibreNMS.get_config_data(os.path.dirname(os.path.realpath(__file__))) @@ -59,5 +66,6 @@ wrapper.wrapper( amount_of_workers=amount_of_workers, config=config, log_dir=log_dir, + modules=args.modules or "", _debug=args.debug, )