[Nut-upsuser] $UPSNAME inconsistent in upssched behavior

2023-10-21 Thread Jim Shank via Nut-upsuser
OS name and version: Ubuntu 22.04.3 LTS
NUT version: 2.7.4-14ubuntu2
NUT installation method: sudo apt install nut
Exact device name: unrelated but is a Cyberpower CST1500SUC

I've noticed that the $UPSNAME environment variable isn't predictable when
running the CMDSCRIPT. I have 1 real UPS and a dummy-ups configured for
heartbeat. When the actual UPS goes onbatt, upssched-cmd reads the $USPNAME
variable to craft the message and gets "heartbeat@localhost" instead of
"shanknas_ups@localhost". This causes the script to get errors when polling
the status and battery.charge via upsc for messages. Could it be that the
longest running timer (heartbeat) is somehow "locking" the UPSNAME variable
and the latest event (onbatt from shanknas_ups@localhost) isn't able to be
override it?

*ups.conf*
pollinterval = 1
maxretry = 3

[shanknas_ups]
driver = "usbhid-ups"
port = "auto"
vendorid = "0764"
productid = "0601"
product = "CST1500SUC"
serial = "REDACTED"
vendor = "CPS"
desc = "CyberPower CST1500SUC - Jim's Office"

[heartbeat]
driver = "dummy-ups"
port = "heartbeat.seq"
desc = "Watch over NUT"

*heartbeat.seq*
# heartbeat.seq -- 10 minute heartbeat
ups.status: OL
TIMER 300
ups.status: OB
TIMER 300

*upssched-cmd *(current, MSG was slightly modified)
#!/bin/bash -u

logger -i -t upssched-cmd Calling upssched-cmd $1 $UPSNAME

#UPSNAME=shanknas_ups


STATUS=$( upsc $UPSNAME ups.status > /dev/stdout 2> /dev/null)
CHARGE=$( upsc $UPSNAME battery.charge > /dev/stdout 2> /dev/null )
CHMSG="[$STATUS]:$CHARGE%"

case $1 in
online) MSG="$UPSNAME, $CHMSG - power supply has been restored." ;;
onbatt) MSG="$UPSNAME, $CHMSG - power failure!" ;;
lowbatt) MSG="$UPSNAME, $CHMSG - we have reached a low battery threshold!"
;;
powerdown) MSG="ShankNAS is shutting down!" ;;
commbad) MSG="$UPSNAME, $CHMSG - we've lost comms with the UPS!" ;;
commok) MSG="$UPSNAME, $CHMSG - comms restored" ;;
heartbeat-failure-timer) MSG="NUT heartbeat failure!" ;;
replacebatt) MSG="$UPSNAME, $CHMSG - please replace the battery" ;;
*) logger -i -t upssched-cmd "Bad arg: \"$1\", $CHMSG"
exit 1 ;;
esac

# Log what happened
logger -t upssched-cmd $MSG

# Send via PushOver
curl -s \
--form-string "token=$PUSHOVER_TOKEN" \
--form-string "user=$PUSHOVER_USER" \
--form-string "message=$MSG" \
https://api.pushover.net/1/messages.json


*upssched.conf*
CMDSCRIPT /bin/upssched-cmd
PIPEFN /run/nut/upssched.pipe
LOCKFN /run/nut/upssched.lock

AT ONBATT shanknas_ups@localhost START-TIMER onbatt 30
AT ONLINE shanknas_ups@localhost CANCEL-TIMER onbatt online
AT LOWBATT * EXECUTE lowbatt
AT COMMBAD * START-TIMER commbad 30
AT COMMOK * CANCEL-TIMER commbad commok
AT NOCOMM * EXECUTE commbad
AT SHUTDOWN * EXECUTE powerdown
AT SHUTDOWN * EXECUTE powerdown
AT REPLBATT * EXECUTE replacebatt

# Restart timer which completes only if the dummy-ups heart beat
# has stopped. See timer values in heartbeat.conf
AT ONBATT heartbeat@localhost CANCEL-TIMER heartbeat-failure-timer
AT ONBATT heartbeat@localhost START-TIMER heartbeat-failure-timer 660

*Logs for nut-* after plug pull*
Oct 20 14:07:02 shanknas upssched[542596]: Cancelling timer:
heartbeat-failure-timer
Oct 20 14:07:02 shanknas upssched[542596]: New timer:
heartbeat-failure-timer (660 seconds)
Oct 20 14:14:17 shanknas upssched[542596]: New timer: onbatt (30 seconds)
Oct 20 14:14:47 shanknas upssched[542596]: Event: onbatt
Oct 20 14:14:47 shanknas upssched-cmd[696002]: Calling upssched-cmd onbatt
heartbeat@localhost
Oct 20 14:14:47 shanknas upssched-cmd[696008]: heartbeat@localhost, [OL]:%
- power failure!
Oct 20 14:17:02 shanknas upssched[542596]: Cancelling timer:
heartbeat-failure-timer
Oct 20 14:17:02 shanknas upssched[542596]: New timer:
heartbeat-failure-timer (660 seconds)
Oct 20 14:27:02 shanknas upssched[542596]: Cancelling timer:
heartbeat-failure-timer
Oct 20 14:27:02 shanknas upssched[542596]: New timer:
heartbeat-failure-timer (660 seconds)
Oct 20 14:34:01 shanknas usbhid-ups[3319430]: libusb_get_string: error
sending control message: Broken pipe
Oct 20 14:37:02 shanknas upssched[542596]: Cancelling timer:
heartbeat-failure-timer
Oct 20 14:37:02 shanknas upssched[542596]: New timer:
heartbeat-failure-timer (660 seconds)
Oct 20 14:37:47 shanknas upsmon[3319457]: UPS shanknas_ups@localhost
battery is low
Oct 20 14:38:07 shanknas upsmon[3319457]: Giving up on the master for UPS
[shanknas_ups@localhost]
Oct 20 14:38:07 shanknas upsmon[3319457]: Executing automatic power-fail
shutdown
Oct 20 14:38:07 shanknas upsmon[3319457]: Auto logout and shutdown
proceeding
Oct 20 14:38:07 shanknas upssched[713933]: Executing command: powerdown
Oct 20 14:38:07 shanknas upssched-cmd[713936]: Calling upssched-cmd
powerdown
Oct 20 14:38:07 shanknas upssched-cmd[713941]: , []:% - we're shutting down!
Oct 20 14:38:09 shanknas upssched[713933]: Executing command: powerdown
Oct 20 14:38:09 shanknas upssched-cmd[713978]: Calling upssched-cmd
powerdown

[Nut-upsuser] APC Modbus support is finally here!

2023-10-21 Thread Jim Klimov via Nut-upsuser
Hello fellow NUTs :)

  It is my great pleasure to get a bit of sunshine from other people's
work, and announce that the initial pull request for `apc_modbus` NUT
driver has recently been merged to the main NUT codebase, so it would be
part of an eventual 2.8.1 release. Great thanks go to Axel Gembe for the
implementation, and to numerous community members for testing as well as
their bug-fix and feature suggestions. It is my understanding that some
thanks also goes to the apcupsd project that served as inspiration for data
points that we can collect from the devices, as well as for its unfortunate
development inactivity that convinced someone to step up and implement a
native NUT driver for the "new" APC devices (~2010+) which all but
deprecated USB HID support. Really, this feature was very long awaited!

  At the moment the driver is "read-only", with commands and variables to
be implemented later (hopefully some coming before 2.8.1 cut-off point). It
can be used with the already published and packaged libmodbus versions for
Serial-port and TCP support (with add-in network management cards). However
please note that USB support is not currently part of the upstream library,
but rather is provided by a fork (PR pending). It is not certain
whether/when exactly such a feature will appear in the upstream library
project - it does not seem very actively maintained at the moment :\

  For more details please see
https://github.com/networkupstools/nut/pull/2063 and
https://github.com/networkupstools/nut/issue/139.

Jim Klimov
___
Nut-upsuser mailing list
Nut-upsuser@alioth-lists.debian.net
https://alioth-lists.debian.net/cgi-bin/mailman/listinfo/nut-upsuser