When your UPS batteries get low, the operating system needs to be brought down cleanly. Also, the UPS load should be turned off so that all devices that are attached to it are forcibly rebooted.
Here are the steps that occur when a critical power event happens:
- The UPS goes on battery
The UPS reaches low battery (a "critical" UPS), that is to say upsc displays:
ups.status: OB LB
The exact behavior depends on the specific device, and is related to:
- battery.charge and battery.charge.low
- battery.runtime and battery.runtime.low
The upsmon master notices and sets "FSD" - the "forced shutdown" flag to tell all slave systems that it will soon power down the load.
(If you have no slaves, skip to step 6)
upsmon slave systems see "FSD" and:
- generate a NOTIFY_SHUTDOWN event
- wait FINALDELAY seconds - typically 5
- call their SHUTDOWNCMD
- disconnect from upsd
- The upsmon master system waits up to HOSTSYNC seconds (typically 15) for the slaves to disconnect from upsd. If any are connected after this time, upsmon stops waiting and proceeds with the shutdown process.
The upsmon master:
- generates a NOTIFY_SHUTDOWN event
- waits FINALDELAY seconds - typically 5
- creates the POWERDOWNFLAG file - usually /etc/killpower
- calls the SHUTDOWNCMD
- On most systems, init takes over, kills your processes, syncs and unmounts some filesystems, and remounts some read-only.
- init then runs your shutdown script. This checks for the POWERDOWNFLAG, finds it, and tells the UPS driver(s) to power off the load.
- The system loses power.
- Time passes. The power returns, and the UPS switches back on.
- All systems reboot and go back to work.
For this setup, we will be setting up our UPS master on a raspberry pi with slaves setup on a QNAP NAS and a Ubuntu Server.
Master Setup on Raspberry Pi
Connect UPS using USP cable
Configuring the UPS/UPSD
Add the following config
Add the following config:
Configuring UPS Monitor
Add the following line
When running in master mode, upsmon waits this long after sending the NOTIFY_SHUTDOWN to warn the users. After the timer elapses, it then runs your SHUTDOWNCMD. By default this is set to 5 seconds.
If you need to let your users do something in between those events, increase this number. Remember, at this point your UPS battery is almost depleted, so don’t make this too big.
Alternatively, you can set this very low so you don’t wait around when it’s time to shut down. Some UPSes don’t give much warning for low battery and will require a value of 0 here for a safe shutdown.
Verify Hardware Config
Restart the Service
Query the UPS
Client Setup QNAP
From the QNAP UI, open the control panel and navigate to External Sevices → UPS
Select Remote UPS Slave and enter the IP address of the master.
Click apply. The UPS information should now be updated. See screenshots below.
From a shell connection on the QNAP server, you can verify connection via the following command: (not required)
Client Setup (Ubuntu)
Set mode to netclient
On our master we created a slave user called deepthought.
- upsname = qnapups
- user = deepthought
- password = 123456
Setup Timer to Shutdown after 1 Minute
Edit the upssched.conf file
Set the following
Create the Command Script
Add the following content
Restart Nut Client
Restart nut client:
Get Status of Nut Client
|Network UPS Tools User Manual||https://networkupstools.org/docs/user-manual.chunked/index.html|
|UPSMON Man Page||https://networkupstools.org/docs/man/upsmon.conf.html|
NUT Introduction to Network UPS Tools - Configuration Examples
|Raspberry Pi Home Server v2: Network UPS Tools||https://melgrubb.com/2016/12/11/rphs-v2-ups/|