Package: nut-client
Source: nut
Version: 2.8.1-5
Severity: important
Tags: patch upstream fixed-upstream trixie
Forwarded: https://github.com/networkupstools/nut/issues/2454
X-Debbugs-Cc: [email protected]

Dear Maintainer,

since 2.8.1, upsmon declares a UPS dead on the first failed poll if it
was last seen not fully online, and shuts the host down at once. DEADTIME
is ignored. A secondary whose primary's upsd answers DRIVER-NOT-CONNECTED
or DATA-STALE for a few seconds (a USB reconnect of the driver, a restart
of upsd) while the UPS is on battery therefore shuts down immediately,
even with a full battery.

What happened here: a short outage (mains back after about a minute); the
primary's USB link to the UPS dropped for a few seconds, and the
secondary (trixie, nut-client 2.8.1-5, DEADTIME 45) logged

  UPS [ups@primary] was last known to be not fully online and currently
  is not communicating, assuming dead

and shut down within seconds. Since mains came back before the primary
commanded the UPS off, the outlets never lost power and the host stayed
off until someone pressed its power button.

Upstream: the check came with
https://github.com/networkupstools/nut/issues/2108 (2.8.1); issue
https://github.com/networkupstools/nut/issues/2454 reports the same
case; https://github.com/networkupstools/nut/pull/2462 (commit
ad19e07a4f6333c100fe7b40dfc8f0449f813cc5, released in 2.8.3) removes the
eight lines again, so a UPS gone on battery is promoted to OB+LB after
DEADTIME, as before 2.8.1. unstable/testing (2.8.5-1) have the fix;
trixie has not.

The attached patch is that upstream change (clients/upsmon.c only, the
NEWS hunk dropped), with a DEP-3 header. It applies on top of
2.8.1-5+deb13u1's series with an offset, no fuzz.

Test, in a debian:trixie container: dummy-ups with "ups.status: OB",
upsd, upsmon as a secondary with DEADTIME 10, HOSTSYNC 2, POLLFREQ 1, and
a SHUTDOWNCMD that only touches a file. The driver is stopped for 5 s,
then started again; later stopped for good.

  - 2.8.1-5 as in trixie: shuts down during the 5 s ("assuming dead").
  - 2.8.1-5+deb13u1 rebuilt with the patch: no shutdown during the 5 s;
    after the driver is stopped for good, "Promoting dead UPS" and the
    shutdown come 12 s later, i.e. after DEADTIME.

The test is attached too (test-upsmon-deadtime.sh): run it as root in a
throwaway debian:trixie container, with no arguments for the archive's
packages or with .deb files to install instead. It exits 1 on the
regression and 0 once fixed. Until trixie has the fix, we run a local
rebuild of 2.8.1-5+deb13u1 with this patch.

Would you consider this for a trixie point release, like the CAL flag fix
(#1056190) in 2.8.1-5+deb13u1? The bug can make every secondary on a UPS
shut down on a glitch of a few seconds during any outage, and in setups
where the UPS is not cycled afterwards, leave them off.

Thank you!

Attachment: upsmon-honor-deadtime.patch
Description: Binary data

#!/bin/bash
# Does upsmon, as a secondary watching a UPS on battery, ride out a short
# comm loss and shut down only after DEADTIME? (upstream #2454, #2462)
#
# A dummy-ups driver reports OB; upsd serves it; upsmon's SHUTDOWNCMD only
# touches a file. The driver is stopped for 5 s (a glitch) and started
# again, then stopped for good (an outage).
#
#   ./test-upsmon-deadtime.sh                 nut-server, nut-client from apt
#   ./test-upsmon-deadtime.sh ./*.deb         these packages instead
#
# Exit 0: the glitch passes, the outage shuts down after DEADTIME (fixed).
# Exit 1: upsmon shut down on the glitch (the regression), or another failure.
#
# As root in a throwaway debian:trixie container (it overwrites /etc/nut),
# no systemd needed: the daemons run in the foreground of this script, e.g.
#   docker run --rm -v "$PWD":/t -w /t debian:trixie ./test-upsmon-deadtime.sh
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive
DEADTIME=10
GLITCH=5
T=/run/nut-test
LOG=$T/upsmon.log
SHUTDOWN=$T/shutdown

apt-get -qq update
if [ $# -gt 0 ]; then
  apt-get -qq install -y --no-install-recommends "$@" >/dev/null
else
  apt-get -qq install -y --no-install-recommends nut-server nut-client >/dev/null
fi
dpkg-query -W -f='${Package} ${Version}\n' nut-client nut-server libupsclient6t64

# --- configuration ----------------------------------------------------------

mkdir -p $T /run/nut
chown root:nut /run/nut && chmod 770 /run/nut
rm -f $SHUTDOWN $LOG

cat > /etc/nut/nut.conf <<EOF
MODE=netserver
EOF
cat > /etc/nut/ups.conf <<EOF
[dummy]
	driver = dummy-ups
	port = dummy.dev
EOF
cat > /etc/nut/dummy.dev <<EOF
ups.status: OB DISCHRG
battery.charge: 80
battery.runtime: 1500
device.mfr: test
device.model: dummy
EOF
cat > /etc/nut/upsd.conf <<EOF
LISTEN 127.0.0.1 3493
EOF
cat > /etc/nut/upsd.users <<EOF
[secondary]
	password = test
	upsmon secondary
EOF
# HOSTSYNC: a secondary waits it out once the UPS is OB+LB, after DEADTIME.
cat > /etc/nut/upsmon.conf <<EOF
MONITOR dummy@localhost 1 secondary test secondary
MINSUPPLIES 1
SHUTDOWNCMD "/usr/bin/touch $SHUTDOWN"
POWERDOWNFLAG $T/killpower
POLLFREQ 1
POLLFREQALERT 1
DEADTIME $DEADTIME
HOSTSYNC 2
FINALDELAY 0
EOF
chown root:nut /etc/nut/*.conf /etc/nut/upsd.users /etc/nut/dummy.dev
chmod 640 /etc/nut/*.conf /etc/nut/upsd.users /etc/nut/dummy.dev

# --- daemons ----------------------------------------------------------------

PIDS=()
cleanup() { kill "${PIDS[@]}" "${DRIVER:-}" 2>/dev/null || true; }
trap cleanup EXIT

start_driver() {
  /usr/lib/nut/dummy-ups -a dummy -u nut -F >>$T/driver.log 2>&1 &
  DRIVER=$!
}
stop_driver() {
  kill "$DRIVER"
  wait "$DRIVER" 2>/dev/null || true
}
fail() {
  echo "FAIL: $*" >&2
  echo "--- upsmon log" >&2
  cat $LOG >&2
  exit 1
}
# Wait up to $1 seconds for the shutdown file; true if it is there.
shut_down_within() {
  local i
  for ((i = 0; i < $1; i++)); do
    [ -f $SHUTDOWN ] && return 0
    sleep 1
  done
  [ -f $SHUTDOWN ]
}

start_driver
/usr/sbin/upsd -u nut -F >$T/upsd.log 2>&1 &
PIDS+=($!)
for _ in $(seq 20); do
  [ "$(upsc dummy@localhost ups.status 2>/dev/null)" = "OB DISCHRG" ] && break
  sleep 0.5
done
[ "$(upsc dummy@localhost ups.status 2>/dev/null)" = "OB DISCHRG" ] || {
  cat $T/driver.log $T/upsd.log >&2; echo "FAIL: upsd does not serve the dummy UPS" >&2; exit 1; }

/usr/sbin/upsmon -D >$LOG 2>&1 &
PIDS+=($!)
# A few polls on battery: upsmon knows the UPS is not on mains.
sleep 4
[ -f $SHUTDOWN ] && fail "upsmon shut down with the UPS just on battery"

# --- a glitch: the driver gone for a few seconds ----------------------------

echo "glitch: driver down for ${GLITCH}s (DEADTIME ${DEADTIME}s)"
stop_driver
glitch_shutdown=0
shut_down_within $GLITCH && glitch_shutdown=1
start_driver
# upsd reconnects to the driver, upsmon polls it again.
[ $glitch_shutdown = 1 ] || { sleep 4; [ -f $SHUTDOWN ] && glitch_shutdown=1; }

if [ $glitch_shutdown = 1 ]; then
  grep -q 'assuming dead' $LOG && echo "upsmon log: $(grep 'assuming dead' $LOG | head -1)"
  fail "upsmon shut down on a ${GLITCH}s glitch, ignoring DEADTIME ${DEADTIME}s"
fi
echo "OK: the ${GLITCH}s glitch passed without a shutdown"

# --- an outage: the driver gone for good ------------------------------------

echo "outage: driver down for good"
start=$(date +%s)
stop_driver
shut_down_within $((DEADTIME + 20)) || fail "upsmon did not shut down after DEADTIME with the UPS gone on battery"
elapsed=$(( $(date +%s) - start ))
[ $elapsed -ge $DEADTIME ] || fail "upsmon shut down after ${elapsed}s, before DEADTIME ${DEADTIME}s"
echo "OK: upsmon shut down ${elapsed}s into the outage (DEADTIME ${DEADTIME}s)"

Reply via email to