work-work.work-logo: A man with brains and a shovel

work-work.work

A blog about goals and obstacles, motivation and procrastination, life's random events and getting things done.

APC Back UPS 700 on Ubuntu 1804

Having an uninterruped power supply for my server is important. I use an inexpensive one: The APC Back-UPS BX 700 and it works well. If I had to buy a new one, I'd use the one with the proper Schuko-connection for Germany. The so called "Kaltgeräte-Ausgänge - IEC 320 C16" / "Appliance couplers" are a little flimsy and tend to disconnect.

apcupsd not in Ubuntu 18.04

Your server needs to know the status of the UPS in order to do a nice shutdown before the battery is empty. apcupsd is the daemon of choice for ubuntu. I was disappointed to see that this package does not exist any more. (UPDATE: The reason it did not exist was that I did not have universe repository installed. sudo add-apt-repository universe would have provided the old apcupsd.)

nut is the successor

The new package is nut and it looks complicated on first sight. Nut will act as a server for multiple clients that can monitor the current state of the UPS, which sounds like a bit of overkill at first, but thinking about it, …, well I guess this is the proper unix-way of doing it. The instructions I found were a little outdated and after some trial an error I found a nice configuration that works for me. It is surprisingly easy.

At first, I become root and create a btrfs-snapshot of my system just in case I mess up and need to roll back.

$ sudo -i
$ btrfs subvolume snapshot / /snapshots/beforenut

Then I install the package

$ apt install nut

This will install both, the nut-client and the nut-server.

/etc/nut/nut.conf

MODE=standalone

/etc/nut/ups.conf

[apc700]
	driver = usbhid-ups
	port = auto
	desc = "APC Back-UPs 700"

I can test this setting to see if it works.

$ service nut-server restart
Init SSL without certificate database
battery.charge: 100
battery.charge.low: 10
battery.charge.warning: 50
battery.date: 2001/09/25
battery.mfr.date: 2015/11/01
battery.runtime: 788
battery.runtime.low: 120
battery.type: PbAc
battery.voltage: 13.7
battery.voltage.nominal: 12.0
device.mfr: American Power Conversion
device.model: Back-UPS XS 700U  
device.serial: 3B1544X17148  
device.type: ups
driver.name: usbhid-ups
driver.parameter.pollfreq: 30
driver.parameter.pollinterval: 2
driver.parameter.port: auto
driver.parameter.synchronous: no
driver.version: 2.7.4
driver.version.data: APC HID 0.96
driver.version.internal: 0.41
input.sensitivity: medium
input.transfer.high: 300
input.transfer.low: 140
input.transfer.reason: input voltage out of range
input.voltage: 234.0
input.voltage.nominal: 230
ups.beeper.status: enabled
ups.delay.shutdown: 20
ups.firmware: 924.Z3 .I
ups.firmware.aux: Z3
ups.load: 36
ups.mfr: American Power Conversion
ups.mfr.date: 2015/11/01
ups.model: Back-UPS XS 700U  
ups.productid: 0002
ups.realpower.nominal: 390
ups.serial: 3B1544X17148  
ups.status: OL
ups.test.result: No test initiated
ups.timer.reboot: 0
ups.timer.shutdown: -1
ups.vendorid: 051d

Now I need to configure the client

/etc/nut/upsd.users:

[upsmon]
  password  = pass
  upsmon master

/etc/nut/upsmon.conf:

RUN_AS_USER nut
MONITOR apc700@localhost 1 upsmon pass master

And restart the services.

$ service nut-server restart
$ service nut-client restart

$ service nut-client status

You can see it's active and running. This is pretty much it. Events get logged in /var/log/syslog and the server will be shutdown when the battery is low.

Bonus: E-Mail notification

As a little extra, I like to get notified with an email message when events occur.

I create a script that does that. Prequisite: Have a mail transfer agent installed. I use postfix.

$ cd /bin/
$ emacs /notifyme
#!/bin/bash
echo -e "Subject:UPS system event on my server \n\n $*" | sendmail myname@mydomain.com

Make the file executable

$ chmod ug+x notifyme
$ chown root:nut notifyme

You can try if you really receive an email by calling

$ /bin/notifyme "This is a test"

Now I can add the +EXEC flag to the events I want to get an email for.

upsmon.conf:

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+EXEC

Don't forget to remove the # at the beginning of the lines.

Now restart the services and unplug the UPS to see if it works.

$ service nut-server restart
$ service nut-client restart

I am aware that this email is only of limited use. If there is a outage, my switch and internet service will most likely be down, too. However it's good to get notified when a cable disconnects or my battery needs to be replaced.