From d0a8c8a170ea56264dae972593ed60cdc2d406c6 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sun, 29 Oct 2023 17:58:22 +0100 Subject: [PATCH 1/2] common/common.c, NEWS.adoc, docs/nut.dict: support NUT_QUIET_INIT_UPSNOTIFY envvar to hide upsnotify "failed...will not spam more" messages [#2136] Signed-off-by: Jim Klimov --- NEWS.adoc | 7 +++++++ common/common.c | 53 ++++++++++++++++++++++++++++++++++++++++++------- docs/nut.dict | 3 ++- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/NEWS.adoc b/NEWS.adoc index d28fb2224e..a2ad894a15 100644 --- a/NEWS.adoc +++ b/NEWS.adoc @@ -459,6 +459,13 @@ as part of https://github.com/networkupstools/nut/issues/1410 solution. - Extended Linux systemd support with optional notifications about daemon state (READY, RELOADING, STOPPING) and watchdog keep-alive messages [#1590] + * Normally *inability* to send such notifications (e.g. lack of systemd + or similar framework on the particular platform) would be reported once + per daemon uptime on its console log, to help troubleshooting situations + where such lack of notifications can cause automated service restarts. + These messages can be hidden by setting `NUT_QUIET_INIT_UPSNOTIFY=true` + environment variable in init-scripts on platforms where such frameworks + are not expected. [#2136] - Extended Linux systemd units with aliases named after the daemons: `nut-server.service` as `upsd.service`, and `nut-monitor.service` as diff --git a/common/common.c b/common/common.c index 7633a3defa..607bc97a5c 100644 --- a/common/common.c +++ b/common/common.c @@ -52,6 +52,7 @@ static int upsnotify_reported_disabled_systemd = 0; #endif /* Similarly for only reporting once if the notification subsystem is not built-in */ static int upsnotify_reported_disabled_notech = 0; +static int upsnotify_report_verbosity = -1; /* the reason we define UPS_VERSION as a static string, rather than a macro, is to make dependency tracking easier (only common.o depends @@ -796,6 +797,34 @@ int upsnotify(upsnotify_state_t state, const char *fmt, ...) # endif /* HAVE_SD_NOTIFY */ #endif /* WITH_LIBSYSTEMD */ + /* Were we asked to be quiet on the console? */ + if (upsnotify_report_verbosity < 0) { + char *quiet_init = getenv("NUT_QUIET_INIT_UPSNOTIFY"); + if (quiet_init == NULL) { + /* No envvar, default is to inform once on the console */ + upsnotify_report_verbosity = 0; + } else { + /* Envvar is set, does it tell us to be quiet? + * NOTE: Empty also means "yes" */ + if (*quiet_init == '\0' + || (strcasecmp(quiet_init, "true") + && strcasecmp(quiet_init, "yes") + && strcasecmp(quiet_init, "on") + && strcasecmp(quiet_init, "1") ) + ) { + upsdebugx(1, + "NUT_QUIET_INIT_UPSNOTIFY='%s' value " + "was not recognized, ignored", + quiet_init); + upsnotify_report_verbosity = 0; + } else { + /* Avoid the verbose message below + * (only seen with non-zero debug) */ + upsnotify_report_verbosity = 1; + } + } + } + /* Prepare the message (if any) as a string */ msgbuf[0] = '\0'; if (fmt) { @@ -832,14 +861,16 @@ int upsnotify(upsnotify_state_t state, const char *fmt, ...) NUT_UNUSED_VARIABLE(buf); NUT_UNUSED_VARIABLE(msglen); if (!upsnotify_reported_disabled_systemd) - upsdebugx(0, "%s: notify about state %i with libsystemd: " - "skipped for libcommonclient build, " - "will not spam more about it", __func__, state); + upsdebugx(upsnotify_report_verbosity, + "%s: notify about state %i with libsystemd: " + "skipped for libcommonclient build, " + "will not spam more about it", __func__, state); upsnotify_reported_disabled_systemd = 1; # else if (!getenv("NOTIFY_SOCKET")) { if (!upsnotify_reported_disabled_systemd) - upsdebugx(0, "%s: notify about state %i with libsystemd: " + upsdebugx(upsnotify_report_verbosity, + "%s: notify about state %i with libsystemd: " "was requested, but not running as a service unit now, " "will not spam more about it", __func__, state); @@ -1110,17 +1141,25 @@ int upsnotify(upsnotify_state_t state, const char *fmt, ...) ) { if (ret == -127) { if (!upsnotify_reported_disabled_notech) - upsdebugx(0, "%s: failed to notify about state %i: no notification tech defined, will not spam more about it", __func__, state); + upsdebugx(upsnotify_report_verbosity, + "%s: failed to notify about state %i: " + "no notification tech defined, " + "will not spam more about it", + __func__, state); upsnotify_reported_disabled_notech = 1; } else { - upsdebugx(6, "%s: failed to notify about state %i", __func__, state); + upsdebugx(6, + "%s: failed to notify about state %i", + __func__, state); } } #if defined(WITH_LIBSYSTEMD) && (WITH_LIBSYSTEMD) # if ! DEBUG_SYSTEMD_WATCHDOG if (state == NOTIFY_STATE_WATCHDOG && !upsnotify_reported_watchdog_systemd) { - upsdebugx(0, "%s: logged the systemd watchdog situation once, will not spam more about it", __func__); + upsdebugx(upsnotify_report_verbosity, + "%s: logged the systemd watchdog situation once, " + "will not spam more about it", __func__); upsnotify_reported_watchdog_systemd = 1; } # endif diff --git a/docs/nut.dict b/docs/nut.dict index 6431e49749..b36830c4f6 100644 --- a/docs/nut.dict +++ b/docs/nut.dict @@ -1,4 +1,4 @@ -personal_ws-1.1 en 3257 utf-8 +personal_ws-1.1 en 3258 utf-8 AAC AAS ABI @@ -1327,6 +1327,7 @@ UPSDESC UPSHOST UPSIMAGEPATH UPSLC +UPSNOTIFY UPSOutletSystemOutletDelayBeforeReboot UPSOutletSystemOutletDelayBeforeShutdown UPSOutletSystemOutletDelayBeforeStartup From b8699dfe3fc8cca051e101702a552d0b0b6bbad9 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sun, 29 Oct 2023 18:02:54 +0100 Subject: [PATCH 2/2] Suggest NUT_QUIET_INIT_UPSNOTIFY=true in sample init-scripts [#2136] Signed-off-by: Jim Klimov --- scripts/Aix/nut.init.in | 3 +++ scripts/HP-UX/nut-drvctl.sh | 3 +++ scripts/HP-UX/nut-upsd.sh | 3 +++ scripts/HP-UX/nut-upsmon.sh | 3 +++ scripts/RedHat/upsd.in | 3 +++ scripts/RedHat/upsmon.in | 3 +++ scripts/Solaris/nut.in | 4 ++++ scripts/Solaris/svc-nut-monitor.in | 4 ++++ scripts/Solaris/svc-nut-server.in | 4 ++++ scripts/Solaris8/S99upsmon | 5 ++++- 10 files changed, 34 insertions(+), 1 deletion(-) diff --git a/scripts/Aix/nut.init.in b/scripts/Aix/nut.init.in index 937e67f9dc..f8bd09444b 100755 --- a/scripts/Aix/nut.init.in +++ b/scripts/Aix/nut.init.in @@ -40,6 +40,9 @@ NUTUSER="@RUN_AS_USER@" NUTGROUP="@RUN_AS_GROUP@" NUT_VAR_LOCK="/var/locks/ups" +NUT_QUIET_INIT_UPSNOTIFY=true +export NUT_QUIET_INIT_UPSNOTIFY + if [ -f "$CONFIG" ] ; then . "$CONFIG" diff --git a/scripts/HP-UX/nut-drvctl.sh b/scripts/HP-UX/nut-drvctl.sh index 9f7286e11b..f153909f15 100755 --- a/scripts/HP-UX/nut-drvctl.sh +++ b/scripts/HP-UX/nut-drvctl.sh @@ -24,6 +24,9 @@ umask 022 PATH=/usr/sbin:/usr/bin:/sbin export PATH +NUT_QUIET_INIT_UPSNOTIFY=true +export NUT_QUIET_INIT_UPSNOTIFY + WHAT='NUT UPS driver (Network UPS Tools -- http://www.exploits.org/nut)' WHAT_PATH=/opt/nut/bin/upsdrvctl WHAT_CONFIG=/etc/rc.config.d/nut-drvctl diff --git a/scripts/HP-UX/nut-upsd.sh b/scripts/HP-UX/nut-upsd.sh index 454cfe8732..d5dcd36a3a 100755 --- a/scripts/HP-UX/nut-upsd.sh +++ b/scripts/HP-UX/nut-upsd.sh @@ -24,6 +24,9 @@ umask 022 PATH=/usr/sbin:/usr/bin:/sbin export PATH +NUT_QUIET_INIT_UPSNOTIFY=true +export NUT_QUIET_INIT_UPSNOTIFY + WHAT='NUT UPS daemon (Network UPS Tools -- http://www.exploits.org/nut)' WHAT_PATH=/opt/nut/sbin/upsd WHAT_CONFIG=/etc/rc.config.d/nut-upsd diff --git a/scripts/HP-UX/nut-upsmon.sh b/scripts/HP-UX/nut-upsmon.sh index 2c7fc95bef..88746431cb 100755 --- a/scripts/HP-UX/nut-upsmon.sh +++ b/scripts/HP-UX/nut-upsmon.sh @@ -24,6 +24,9 @@ umask 022 PATH=/usr/sbin:/usr/bin:/sbin export PATH +NUT_QUIET_INIT_UPSNOTIFY=true +export NUT_QUIET_INIT_UPSNOTIFY + WHAT='NUT UPS monitor (Network UPS Tools -- http://www.exploits.org/nut)' WHAT_PATH=/opt/nut/sbin/upsmon WHAT_CONFIG=/etc/rc.config.d/nut-upsmon diff --git a/scripts/RedHat/upsd.in b/scripts/RedHat/upsd.in index f44e7fdae0..c5063209b8 100644 --- a/scripts/RedHat/upsd.in +++ b/scripts/RedHat/upsd.in @@ -53,6 +53,9 @@ fi # if there are no config file, bail out [ -f "$UPSDCONF" ] && [ -f "$UPSCONF" ] || exit 0 +NUT_QUIET_INIT_UPSNOTIFY=true +export NUT_QUIET_INIT_UPSNOTIFY + runcmd() { echo -n "$1 " shift diff --git a/scripts/RedHat/upsmon.in b/scripts/RedHat/upsmon.in index a57d1229e8..9cee760500 100644 --- a/scripts/RedHat/upsmon.in +++ b/scripts/RedHat/upsmon.in @@ -32,6 +32,9 @@ if [ -n "$NUT_SBINDIR" -a -d "$NUT_SBINDIR" ]; then PATH="$NUT_SBINDIR:$PATH" fi +NUT_QUIET_INIT_UPSNOTIFY=true +export NUT_QUIET_INIT_UPSNOTIFY + # See how we are called. case "$1" in start) diff --git a/scripts/Solaris/nut.in b/scripts/Solaris/nut.in index 1c39603c37..12e82a48dc 100755 --- a/scripts/Solaris/nut.in +++ b/scripts/Solaris/nut.in @@ -7,6 +7,10 @@ NUT_SBIN_DIR="${NUT_DIR}/sbin" NUT_LIB_DIR="${NUT_DIR}/lib" CONFIG="@CONFPATH@/nut.conf" +# We anticipate some tighter integration with SMF later: +#NUT_QUIET_INIT_UPSNOTIFY=true +#export NUT_QUIET_INIT_UPSNOTIFY + if [ -f "$CONFIG" ] ; then . "$CONFIG" fi diff --git a/scripts/Solaris/svc-nut-monitor.in b/scripts/Solaris/svc-nut-monitor.in index c33b216a08..9e4129df1f 100755 --- a/scripts/Solaris/svc-nut-monitor.in +++ b/scripts/Solaris/svc-nut-monitor.in @@ -21,6 +21,10 @@ CONFIG="@CONFPATH@/nut.conf" NUTUSER="@RUN_AS_USER@" NUTGROUP="@RUN_AS_GROUP@" +# We anticipate some tighter integration with SMF later: +#NUT_QUIET_INIT_UPSNOTIFY=true +#export NUT_QUIET_INIT_UPSNOTIFY + if [ -f "$CONFIG" ] ; then . "$CONFIG" fi diff --git a/scripts/Solaris/svc-nut-server.in b/scripts/Solaris/svc-nut-server.in index 1419da7ec9..867c75fd0c 100755 --- a/scripts/Solaris/svc-nut-server.in +++ b/scripts/Solaris/svc-nut-server.in @@ -31,6 +31,10 @@ NUTGROUP="`svcprop -p nut/NUTGROUP $SMF_FMRI`" \ && [ -n "$NUTGROUP" ] \ || NUTGROUP="@RUN_AS_GROUP@" +# We anticipate some tighter integration with SMF later: +#NUT_QUIET_INIT_UPSNOTIFY=true +#export NUT_QUIET_INIT_UPSNOTIFY + if [ -f "$CONFIG" ] ; then . "$CONFIG" fi diff --git a/scripts/Solaris8/S99upsmon b/scripts/Solaris8/S99upsmon index 06ba753f4a..5030c9f798 100755 --- a/scripts/Solaris8/S99upsmon +++ b/scripts/Solaris8/S99upsmon @@ -11,6 +11,9 @@ export PATH UPSDPATH=/usr/local/ups/sbin +NUT_QUIET_INIT_UPSNOTIFY=true +export NUT_QUIET_INIT_UPSNOTIFY + # See how we are called. case "$1" in 'start') @@ -19,7 +22,7 @@ case "$1" in $UPSDPATH/upsmon >/dev/console 2>&1 touch /var/lock/subsys/upsmon fi - ;; + ;; 'stop') echo "NUT Stopping UPS monitor " /usr/bin/pkill -x upsmon