From b94097567cbb116025f54772609eef7b9a8e3f4e Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Thu, 24 Aug 2023 09:58:22 +0200 Subject: [PATCH] system: defer config reload to SIGHUP in gateway watcher This should considerably lower CPU usage as reported a few times. We do need to bring in pcntl PHP module in order to get that done easily in the script. PR: https://forum.opnsense.org/index.php?topic=35219.0 --- Makefile | 3 ++- src/etc/inc/plugins.inc.d/dpinger.inc | 17 +++++++++++------ src/opnsense/scripts/routes/gateway_watcher.php | 17 +++++++++++++---- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index cee72ad64..c1b2f85a8 100644 --- a/Makefile +++ b/Makefile @@ -167,9 +167,10 @@ CORE_DEPENDS?= ca_root_nss \ php${CORE_PHP}-gettext \ php${CORE_PHP}-google-api-php-client \ php${CORE_PHP}-ldap \ + php${CORE_PHP}-pcntl \ php${CORE_PHP}-pdo \ - php${CORE_PHP}-pecl-radius \ php${CORE_PHP}-pear-Crypt_CHAP \ + php${CORE_PHP}-pecl-radius \ php${CORE_PHP}-phalcon \ php${CORE_PHP}-phpseclib \ php${CORE_PHP}-session \ diff --git a/src/etc/inc/plugins.inc.d/dpinger.inc b/src/etc/inc/plugins.inc.d/dpinger.inc index 048bd8a52..f12d5c06d 100644 --- a/src/etc/inc/plugins.inc.d/dpinger.inc +++ b/src/etc/inc/plugins.inc.d/dpinger.inc @@ -319,12 +319,17 @@ function dpinger_configure_do($verbose = false, $gwname = null, $bootup = false) } if (count(dpinger_services())) { - /* use a separate script to produce the monitor alerts which runs forever */ - mwexecf( - '/usr/sbin/daemon -f -p %s /usr/local/opnsense/scripts/routes/gateway_watcher.php %s', - ['/var/run/gateway_watcher.pid', 'interface routes alarm'], - true - ); + if (isvalidpid('/var/run/gateway_watcher.pid')) { + /* indicate that the configuration needs a reload */ + killbypid('/var/run/gateway_watcher.pid', 'HUP'); + } else { + /* use a separate script to produce the monitor alerts which runs forever */ + mwexecf( + '/usr/sbin/daemon -f -p %s /usr/local/opnsense/scripts/routes/gateway_watcher.php %s', + ['/var/run/gateway_watcher.pid', 'interface routes alarm'], + true + ); + } } else { killbypid('/var/run/gateway_watcher.pid'); } diff --git a/src/opnsense/scripts/routes/gateway_watcher.php b/src/opnsense/scripts/routes/gateway_watcher.php index e67debbff..248650205 100755 --- a/src/opnsense/scripts/routes/gateway_watcher.php +++ b/src/opnsense/scripts/routes/gateway_watcher.php @@ -31,7 +31,18 @@ require_once 'config.inc'; require_once 'util.inc'; require_once 'interfaces.inc'; +function signalhandler($signal) +{ + global $config; + + OPNsense\Core\Config::getInstance()->forceReload(); + $config = parse_config(); + + syslog(LOG_NOTICE, 'Reloaded gateway watcher configuration on SIGHUP'); +} + openlog('dpinger', LOG_DAEMON, LOG_LOCAL4); +pcntl_signal(SIGHUP, 'signalhandler'); $action = !empty($argv[1]) ? $argv[1] : null; @@ -43,12 +54,10 @@ $mode = []; sleep($wait); while (1) { - $alarm = false; - - OPNsense\Core\Config::getInstance()->forceReload(); - $config = parse_config(); + pcntl_signal_dispatch(); $status = return_gateways_status(); + $alarm = false; /* clear known gateways in first step to flush unknown in second step */ $cleanup = $mode;