Setting up an APC UPS on Linux with apcupsd

In the time that I’ve been living in my current home, there have been a couple of power interruptions, but nothing that was more than a blip until the past week. For quite some time, I’ve used an APC SmartUPS as a line conditioner to feed power to my network gear. It wasn’t until I experienced an outage that would have outlasted the batteries that I turned to making the device work as a trigger to shut down the network.

Most of the shut-down is conceptually simple. All of the network equipment (firewall, AP, switch, hubs, DSL modem, ATAs) is solid-state and can be powered down abruptly. The main piece of equipment that I need to take some care on is the Asterisk server. Fortunately, there’s apcupsd, a software package that will speak to the APC in it’s so-called smart mode. When the power goes out and the battery drains to a pre-set level, apcupsd can initiate a system shutdown.

Most APC devices these days interface with the computer over USB, though a somewhat strange protocol. To speak it, you need to enable USB support in Linux for Human Interface Devices (HIDs), but with a special “hidden” mode (the CONFIG_USB_HIDDEV kernel option).

As a basic first step for USB support, you’ll need to build support for your host controller. Most controllers these days follow the Open Host Controller Interface (OHCI), though a Intel and Via chips use the alternative Universal Host Controller Interface (UHCI) instead. To find out which one you have, probe your PCI bus to see the controller. It will helpfully tell you which one to use:

root@ups:~ # lspci -v | grep USB
00:13.0 USB Controller: ATI Technologies Inc IXP SB400 USB Host Controller (prog-if 10 [OHCI])
00:13.1 USB Controller: ATI Technologies Inc IXP SB400 USB Host Controller (prog-if 10 [OHCI])
00:13.2 USB Controller: ATI Technologies Inc IXP SB400 USB2 Host Controller (prog-if 20 [EHCI])

The final controller is the Extended Host Controller Interface (EHCI), better known as a USB 2.0 controller. UPSes don’t operate at high speeds, so all you need are the host controller and the human interface drivers:

root@ups:~# modprobe ohci-hcd
root@ups:~# modprobe usbhid

Now, plug in the UPS. If your distribution is set up right, the udev system will create the /dev/usb/hiddevX device interface to talk to the UPS. You can see it by looking at the kernel messages:

usb 2-2: new low speed USB device using ohci_hcd and address 2
hiddev96: USB HID v1.10 Device [American Power Conversion Smart-UPS 750 XL FW:630.3.D USB FW:1.4] on usb-0000:00:13.1-2

At this point, I could follow the standard gentoo path of emerge apcupsd and do the basic configuration. The halt scripts on Gentoo are even configured to automatically turn off the UPS after shutdown, so I didn’t have to do anything special to power down my rack once the shutdown completed.

One Response to “Setting up an APC UPS on Linux with apcupsd”

  1. Canuck says:

    Nice. Disregard my other comment, this is a far more elegant solution. Also, sorry about cluttering up your comments section. Cheers.

Leave a Reply