apc BX1400U-FR ups

DIY HOME SERVER 2021 – Software – PROXMOX – UPS Monitoring

1. DIY HOME SERVER - PROXMOX - UPS

When building a home lab server, you’re mostly going to use some kind of Uninterruptible Power Supply (UPS).

It’s important for your home servers to always know the status of the UPS providing the power. This makes it possible to gracefully shutdown all systems before the UPS batterries get drained.

apc BX1400U-FR ups
APC Back-UPS BX1400U-FR

In most cases, home users are running one or multiple Proxmox bare metal servers, each running several VMs and containers. Each virtualisation host can shutdown all of its own clients during a normal shutdown sequence.  But therefore each host has to be aware of the UPS status.

Most UPS have one port to connect them to computers or servers for management purposes. On the budget friendly devices, you’ll find a USB or serial connector. The more expensive devices often have an ethernet port.

My APC Back-UPS BX1400U-FR UPS is just a cheap and simple 700 Watt/1.4 kVA device. It has only 4 230V output sockets, but that’s not a problem because all devices in my network rack are connected to one single surge protected power distribution unit. The UPS has one USB port to communicate with connected devices.

Tip :
Before buying a certain UPS, always first check replacement batteries prices and availability!
I especially chose this UPS because replacement batteries are widely available, afordable and don’t have any exotic dimensions.

2. DIY HOME SERVER - PROXMOX - Network UPS Tools

We’ll use the Open Source Network UPS Tools (NUT) to communicate with the UPS.

Because there’s only one (USB) port, this means that only one computer or server can communicate with the UPS directly. This brings us to the following topologies :

nut

“Simple” configuration :

One UPS, one computer or server. This is also known as a “Standalone” configuration.

This is the configuration that most users will use. You need at least a driver, upsd, and upsmon running.

“Advanced” configuration :

One UPS, multiple computers or servers. Only one of them can actually talk to the UPS directly. That’s where the network comes in. The Master system runs the driver, upsd, and upsmon in master mode. The Slave systems only run upsmon in slave mode.

This is useful when you have a very large UPS that’s capable of running multiple systems simultaneously. There is no longer the need to buy a bunch of individual UPSes or “sharing” hardware, since this software will handle the sharing for you.

In our example, we’ll setup a “simple” configugartion. The UPS USB port is connected directly to an USB port on the Proxmox server.

Shutdown timeline (simple configuration) :

  • Main power failure occurs :
    • UPS device switches power to battery
    • UPS device notifies server with a “On battery” event message
  • USP battery is getting close to depletion :
    • UPS device notifies server with a “Battery low” event message
    • Server waits the set “Final delay” time
    • Server starts his shutdown procedure :
      • Sets the “Kill power” flag
      • Ends all running processes
      • Unmounts all file systems
      • Remounts file systems as read-only
      • Looks for the “Kill power” flag
      • Issues the “Kill power” command to the UPS device
      • Halts the system, but doesn’t power off
    • UPS device receives the “Kill power” command from the server :
      • UPS waits for the “Shutdown delay” time to pass
        This is to give all systems enough time to properly shut down
      • UPS device cuts power on all outlets
      • All connected systems lose power
    • Main power supply has been restored :
      • UPS device starts to reload its battery
      • UPS device waits for the “Startup delay” time to pass
        This is to reload the battery to a safe minimum level
      • UPS device restores power on all outlets
      • All connected systems start up again

If you want to set up an advanced configuration, please consult this excellent post.

3. DIY HOME SERVER - PROXMOX - NUT Setup

Connect the USB port of the UPS to an USB port of your home server.
Log on to your Proxmox server, select your node and click Shell.

List all USB ports :
lsusb
Note the Bus and Device of your UPS device.
List the USB port details :
lsusb -v -s [bus]:[device]
In my case :
lsusb -v -s 2:2

Tip : If the above does not work, replace the USB cable and reboot the system.

Install NUT :
apt install nut -y
Edit /etc/nut/nut.conf :
nano /etc/nut/nut.conf
Replace :
MODE=none
with :
MODE=standalone

Ref : nut.conf(5)

Edit /etc/nut/ups.conf :
nano /etc/nut/ups.conf
Add :
[apc]
# APC Back-UPS BX1400U-FR
driver = usbhid-ups
port = auto
desc = "APC Back-UPS BX1400U-FR"
offdelay = 120
ondelay = 240
After saving the file, you can test it by entering the command :
upsdrvctl start
You should get a return like :
Network UPS Tools - UPS driver controller 2.7.4
Network UPS Tools - Generic HID driver 0.41 (2.7.4)
USB communication driver 0.33
Using subdriver: APC HID 0.96

Ref : ups.conf(5) and usbhid-ups(8) 

Please check the Network UPS Tools – Hardware Compatibility List to identify the correct driver suited for your UPS.

Edit /etc/nut/upsd.conf :
nano /etc/nut/upsd.conf
Add :
LISTEN 0.0.0.0 3493
LISTEN :: 3493

The above configuration is not the default configuration but needed for Proxmox hosts.

Ref : nut.conf(5)

Edit /etc/nut/upsd.users :
nano /etc/nut/upsd.users
Add :
[upsadmin]
# Administrative user
password = ********
# Allow changing values of certain variables in the UPS.
actions = SET
# Allow setting the "Forced Shutdown" flag in the UPS.
actions = FSD
# Allow all instant commands
instcmds = ALL
upsmon master

Ref : nut.conf(5)

Edit /etc/nut/upsmon.conf :
nano /etc/nut/upsmon.conf
Add (at the appropriate place) :
RUN_AS_USER nut

MONITOR apc@localhost 1 upsadmin ******* master

MINSUPPLIES 1

SHUTDOWNCMD "/bin/systemctl halt"

NOTIFYCMD "/sbin/upssched"

POLLFREQ 5

POLLFREQALERT 5

HOSTSYNC 15

DEADTIME 15

POWERDOWNFLAG /etc/killpower

NOTIFYFLAG ONLINE   SYSLOG+WALL+EXEC
NOTIFYFLAG ONBATT   SYSLOG+WALL+EXEC
NOTIFYFLAG LOWBATT  SYSLOG+WALL+EXEC
NOTIFYFLAG FSD      SYSLOG+WALL+EXEC
NOTIFYFLAG COMMOK   SYSLOG+WALL+EXEC
NOTIFYFLAG COMMBAD  SYSLOG+WALL+EXEC
NOTIFYFLAG SHUTDOWN SYSLOG+WALL+EXEC
NOTIFYFLAG REPLBATT SYSLOG+WALL+EXEC
NOTIFYFLAG NOCOMM   SYSLOG+WALL+EXEC
NOTIFYFLAG NOPARENT SYSLOG+WALL

RBWARNTIME 43200

NOCOMMWARNTIME 300

FINALDELAY 5

Ref : nut.conf(5)

The system will now shut down when the UPS runtime or battery charge values fall below their values. By default, those values are way too low on an APC UPS, so we will set this parameter to a higher value to keep enough reserves in the UPS. You can list the editable values :
upsrw apc@localhost
We will set the parameter for minimum runtime to 15 minutes and the minimum charge to 40% :
upsrw -s battery.runtime.low=900 apc@localhost

upsrw -s battery.charge.low=40 apc@localhost

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.