Rafael Reinoso
2022-Dec-22 12:47 UTC
[Nut-upsuser] NUT & UPS, how to shut down client after 2 min on battery?
Hi! Some systems I have take some time to shut down, so I would like them to shut down if the UPS have been on battery for 2 minutes. Internet has told me that SHUTDOWNCMD ?/sbin/shutdown -h +0? in /etc/nut/upsmon.conf will execute when the UPS is both OB (on battery) and LB (Low battery). Like in wiki.ipfire.org - Detailed NUT Configuration<https://wiki.ipfire.org/addons/nut/detailed> where it says ?SHUTDOWNCMD ?/sbin/shutdown -h +0? this command will be triggered if UPS sends the BL (Battery Low) flag.? But I would like to be on the safest side, and not hope ?low battery? is enough time to shut down. So 2 minutes sound much more safe for paranoid me. I do not know much about NUT, but after some Googling, one idea I have is to in /etc/nut/upsmon.conf on client change to this NOTIFYCMD "/etc/nut/shutdown_script.sh" NOTIFYFLAG ONBATT EXEC And then in /etc/nut/shutdown_script.sh have something like this #!/bin/bash poll_interval=5 wait_time=120 counter=0 no_wait_time=60 no_counter=0 while true do output=$(upsc qnapups at 192.168.222.252 ups.status) if echo "$output" | grep -q "OB" then counter=$((counter + poll_interval)) no_counter=0 else counter=0 no_counter=$((no_counter + poll_interval)) fi if [ $counter -ge $wait_time ] then echo "Still outoput OB = On Battery, after 2 min. I will now shut down" # Here I will shutdown the system # "/sbin/shutdown -h +0" break fi if [ $no_counter -ge $no_wait_time ] then echo "Have not seen OB for 1 min, so quiting the script" break fi sleep $poll_interval done But now I am starting to wonder if I am overdoing this. Surely there must already be a ready-made solution for this in NUT somewhere, but I have just searched with wrong words i Google. Have any of you done something similar with NUT, and what is the right way to do it? /raffe -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://alioth-lists.debian.net/pipermail/nut-upsuser/attachments/20221222/8ee9f074/attachment-0001.htm>
Roger Price
2022-Dec-22 17:05 UTC
[Nut-upsuser] NUT & UPS, how to shut down client after 2 min on battery?
On Thu, 22 Dec 2022, Rafael Reinoso via Nut-upsuser wrote:> > Some systems I have take some time to shut down, so I would like them to shut > down if the UPS have been on battery for 2 minutes.Have a look at chapter 7 of the NUT Configuration Examples at https://rogerprice.org/NUT/ConfigExamples.A5.pdf Roger
Matt Kinni
2022-Dec-31 04:51 UTC
[Nut-upsuser] NUT & UPS, how to shut down client after 2 min on battery?
On 2022-12-22 05:47, Rafael Reinoso via Nut-upsuser wrote:> I do not know much about NUT, but after some Googling, one idea I have > is to in /etc/nut/upsmon.conf on client change to this > > |NOTIFYCMD "/etc/nut/shutdown_script.sh" NOTIFYFLAG ONBATT EXEC | > > And then in /etc/nut/shutdown_script.sh have something like this >Hi Raffe, I had a very similar setup to this for a long time until I recently changed it; I will describe what I used to do and why I don't set a timer like this anymore. In /etc/ups/upsmon.conf (my system is Fedora) I captured three events: NOTIFYFLAG ONLINE SYSLOG+EXEC NOTIFYFLAG ONBATT SYSLOG+EXEC NOTIFYFLAG LOWBATT SYSLOG+EXEC NOTIFYCMD /usr/sbin/upssched The key thing here is I am not calling a bash script directly with NOTIFYCMD, but delegating it to the built in upssched process which has timer functionality (and is packaged with nut in Fedora). In /etc/ups/upssched.conf, the key options are: CMDSCRIPT /usr/local/bin/upsmon-execscript.sh AT ONBATT * START-TIMER shutdown_onbatt 600 AT ONLINE * CANCEL-TIMER shutdown_onbatt As you can see it starts a 10 minute timer, and cancels the timer if mains power is restored. The bash script is similar to yours, but benefits from not having to do any time management of its own or count how long it has been running. upssched does that for you, and simply calls your script when the listed events occur and with the timer of your choosing. Here is the problem with this approach in general: - what happens if I boot my server while already on battery, and the battery is low? My script won't do anything for 10 minutes - what happens if the power repeatedly cycles on/off/on/off? 10 more minutes are granted every time, which again leaves you in a dangerous situation My current approach is to have my server turn off when the UPS hits 40% battery. I have redefined the "lowbatt" signal to a very comfortable 40%, so there is plenty of time to shutdown and still plenty of battery left for charging other things like my cellphone etc. The way to accomplish this is in /etc/ups/ups.conf, under your ups configuration put: ignorelb override.battery.charge.low=40 This seems safer to me because it's based on the actual capacity remaining, and not an arbitrary timer. Hope this helps, and happy new years! Matt
Rafael Reinoso
2023-Jan-12 08:20 UTC
[Nut-upsuser] NUT & UPS, how to shut down client after 2 min on battery?
Now I have this in /etc/nut/upsmon.conf (my QNAP NAS wants admin and 123456 or not working) RUN_AS_USER root MONITOR qnapups at localhost 1 admin 123456 master MONITOR qnapups at 192.168.222.252 3493 admin 123456 slave MINSUPPLIES 1 SHUTDOWNCMD "/sbin/shutdown -h +0" POLLFREQ 5 POLLFREQALERT 5 HOSTSYNC 15 DEADTIME 15 POWERDOWNFLAG /etc/killpower NOTIFYFLAG ONLINE SYSLOG+EXEC NOTIFYFLAG ONBATT SYSLOG+EXEC NOTIFYFLAG LOWBATT SYSLOG+EXEC NOTIFYFLAG REPLBATT SYSLOG+EXEC NOTIFYCMD /usr/sbin/upssched RBWARNTIME 43200 NOCOMMWARNTIME 300 FINALDELAY 5 And in /etc/nut/upssched.conf (changed from 2 min to 4 min) CMDSCRIPT /etc/nut/upssched-execscript.sh PIPEFN /etc/nut/upssched.pipe LOCKFN /etc/nut/upssched.lock AT ONBATT * START-TIMER shutdown_onbatt 240 AT ONBATT * EXECUTE info_onbatt AT ONLINE * CANCEL-TIMER shutdown_onbatt AT ONLINE * EXECUTE ups-back-on-power AT LOWBATT * EXECUTE shutdown_lowbatt AT REPLBATT * EXECUTE replace_batt And in /etc/nut/upssched-execscript.sh #! /bin/sh case $1 in shutdown_onbatt) logger -t upsmon[upssched] "shutdown_onbatt): Triggering shutdown after 4 minutes on battery" /sbin/shutdown -h +0 ;; shutdown_lowbatt) logger -t upsmon[upssched] "shutdown_lowbatt): Triggering shutdown when battery.charge.low is under 50%" /sbin/shutdown -h +0 ;; info_onbatt) logger -t upsmon[upssched] "info_onbatt): Now on battery" ;; ups-back-on-power) logger -t upsmon[upssched] "ups-back-on-power): UPS back on power" ;; replace_batt) message="Quick self-test indicates battery requires replacement" logger -t upsmon[upssched] "replace_batt): $message" ;; *) logger -t upsmon[upssched] "*) = Unrecognized command: $1" ;; esac And in /etc/nut/ups.conf [qnapups] driver = usbhid-ups port = auto desc = "UPS" ignorelb override.battery.charge.low=50 So I have these files [root at ipfire2 nut]# pwd /etc/nut [root at ipfire2 nut]# ls -la total 104 drwxr-xr-x 2 root root 4096 Jan 10 09:23 . drwxr-xr-x 50 root root 4096 Jan 10 10:33 .. -rw-r--r-- 1 root root 1542 Jan 10 09:05 nut.conf -rw-r--r-- 1 root root 1538 Dec 18 2020 nut.conf.sample -rw-r--r-- 1 root root 4734 Jan 10 09:10 ups.conf -rw-r--r-- 1 root root 4618 Dec 18 2020 ups.conf.sample -rw-r----- 1 root root 4606 Dec 22 08:44 upsd.conf -rw-r--r-- 1 root root 4578 Dec 18 2020 upsd.conf.sample -rw-r----- 1 root root 2292 Dec 22 07:11 upsd.users -rw-r--r-- 1 root root 2131 Dec 18 2020 upsd.users.sample -rw-r--r-- 1 root root 15569 Jan 10 10:20 upsmon.conf -rw-r--r-- 1 root root 15313 Dec 18 2020 upsmon.conf.sample -rw-r--r-- 1 root root 4173 Jan 10 10:22 upssched.conf -rw-r--r-- 1 root root 3895 Dec 18 2020 upssched.conf.sample -rwxr-xr-x 1 root root 810 Jan 10 10:29 upssched-execscript.sh Do all that seem right to you? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://alioth-lists.debian.net/pipermail/nut-upsuser/attachments/20230112/45cb9cef/attachment.htm>