Monitoring USB UPS on Ubuntu
So, I have a few UPSs laying around. Well, they’re actually in use. Today I decided to set them up to actually cleanly shut down machines connected to them if the power’s off that long. Because it’s not trivial (but also not difficult), I’ll document it here. I’m using nut to manage this all – mainly because it works over the network. One UPS powers two machines in one case, and I want to monitor them all from one workstation, so networkability is important.
First, install the nut packages:
sudo apt-get install nut nut-snmp
I also installed knutclient on my workstation, since I use KUbuntu. It has pretty pictures and stuff. On the workstation – midnight – I have a USB-capable APC UPS. So, I want to call that ups midnightups. The first thing, then, is to define the ups by creating ups.conf. Here’s what it looks like:
sauer@midnight:~$ cat /etc/nut/ups.conf [midnightups] driver = usbhid-ups port = auto
The part in square brackets is the name of the UPS. The other two indicate to use the USB driver, and that the port is automatic. If you have multiple USB UPSes on your system, you can use device and vendor identifiers, channels, etc. Look at the man page for usbhid-ups for details. Most people with one UPS on a system don’t need to worry about that.
Next, I need to set up the system to actually run nut. That’s done in /etc/nt/nut.conf on Ubuntu. All of my systems are going to be network servers, even though several could technically be standalone. I just like common configs, and I have a trusted LAN. To make this happen, open up /etc/nut/nut.conf and make the last line read MODE=netserver. After that, you need to crete users in upsd.users:
sauer@midnight:~$ cat /etc/nut/upsd.users [mon_user] password = ups upsmon master
And, you’ve gotta edit upsmon.conf to monitor something. I copied the sample file over, and added a MONITOR line. Here’s what that looked like:
sauer@midnight:~$ sudo cp /etc/nut/upsmon.conf.sample /etc/nut/upsmon.conf sauer@midnight:~$ sudo vim /etc/nut/upsmon.conf sauer@midnight:~$ sed -n 's/#.*$//;/^[[:space:]]*$/!p' /etc/nut/upsmon.conf MONITOR midnightups@localhost 1 mon_user ups master MINSUPPLIES 1 SHUTDOWNCMD "/sbin/shutdown -h +0" POLLFREQ 5 POLLFREQALERT 5 HOSTSYNC 15 DEADTIME 15 POWERDOWNFLAG /etc/killpower RBWARNTIME 43200 NOCOMMWARNTIME 300 FINALDELAY 5
That sed line prunes comments, and then prints lines that aren’t just whitespace. Cute, eh? Anyway, the MONITOR line is the one I added with vim. Note that I specified the user (the thing in brackets) and password from the upsd.users file, and specified that this is the master sysstem (that’d say “slave” on a second system hooked to the same UPS). Now seems like a good time to set the permissions on those files so they make sense, and to put the sample upsd.conf (which is all comments on my version, but the file needs to exist) into place:
sauer@midnight:~$ sudo cp /etc/nut/upsd.conf.sample /etc/nut/upsd.conf sauer@midnight:~$ sudo chown -R root:nut /etc/nut/ sauer@midnight:~$ sudo chmod -R g=u,g-w,o-rwx /etc/nut/ sauer@midnight:~$ sudo chmod o=u,o-w /etc/nut/
note that the last chmod grants group read and execute to the directory, mainly so sudo tab-completion will work because everyone can read the dir contents. Anyway, this should all be fine now.. And so, we restart nut:
sudo /etc/init.d/nut restart
Hopefully, that’ll be ok, and you’ll see things like this in /var/log/daemon.log:
Aug 28 23:29:53 192.168.0.24 usbhid-ups[11580]: Startup successful Aug 28 23:29:53 192.168.0.24 upsd[11581]: listening on 127.0.0.1 port 3493 Aug 28 23:29:53 192.168.0.24 upsd[11581]: listening on ::1 port 3493 Aug 28 23:29:53 192.168.0.24 upsd[11581]: Connected to UPS [midnightups]: usbhid-ups-midnightups Aug 28 23:29:53 192.168.0.24 upsd[11582]: Startup successful Aug 28 23:29:53 192.168.0.24 upsmon[11584]: Startup successful Aug 28 23:29:53 192.168.0.24 upsd[11582]: User mon_user@::1 logged into UPS [midnightups]
Whee. If it all worked, you should be able to run upsc midnightups@localhost on the machine, and you’ll get some output about voltage, load, and similar.