There are two different ways to monitor a UPS with nut: either expose the UPS hardware to GNOME Power Manager through hal or monitor the UPS directly with upsmon and upsd. There are trade-offs with each approach. Let’s take a look at both.
Use GNOME Power Manager and hal
On Debian or Ubuntu systems, install the nut-hal-drivers package.
$ sudo apt-get install nut-hal-drivers
GNOME Power Manager should already be running and should automatically detect the UPS. If it’s not running, you can start it from the command line or set it to start automatically in each session with the Sessions system preference. An icon will appear in the system tray if everything is working.
Clicking the system tray icon and selecting UPS from the menu will give detailed information.
GNOME Power Manager can be configured to shutdown or hibernate the computer when the UPS battery level drops below certain thresholds. There are also gconf keys to change which thresholds are used, percentage or time left, and what those thresholds are.
This method of monitoring a UPS is useful for a workstation, but there are three significant disadvantages:
- GNOME Power Manager is not running when you aren’t logged in. If your machine loses power at the login screen, it will not shutdown automatically.
- Currently, the nut-hal-drivers package only supports USB-based UPS’s. Many older and higher-end UPS’s are serial-based.
- GNOME Power Manager’s UPS support is buggy. While it displays notifications of low battery and critically low battery, it doesn’t take any actions. See Debian bug #482431 and GNOME bug #539843.
Update on 2009-01-06:
Arnaud Quette, the nut project leader, informed me of two other significant disadvantages with using nut-hal-drivers:
- gnome-power-manager will not power off the UPS at the end of a shutdown from power failure. This means that either the UPS will be fully drained, or worse, power will be restored before the battery is fully drained and the computer will not automatically start up again.
- No further work will be done on nut-hal-drivers. HAL is going to be replaced with DeviceKit and nut-hal-drivers will be replaced with DeviceKit-power.
Conclusion: Don’t use nut-hal-drivers unless you really, really want a fancy notification icon.
Use nutd and upsmon
Install the nut package. This should conflict with nut-hal-drivers if it’s already installed.
$ sudo apt-get install nut
Installing the nut package, should add the nut user and group. If it doesn’t, you have to add those manually. Next, create or edit /etc/nut/ups.conf:
# /etc/nut/ups.conf [apc] driver = usbhid-ups port = auto
The label in the square brackets can be anything. It identifies the UPS to upsd and upsmon. Find your UPS model on the NUT hardware compatibility list and substitute the corresponding driver for usbhid-ups above. If your UPS isn’t listed, find similar ones. They probably use the same driver. If you have a USB-based UPS, you can leave the port as auto; it’s ignored. Otherwise, the port will usually be /dev/ttyS0, the first serial port, or /dev/ttyS1, the second. If your UPS is connected via the serial port, you’ll need to modify the permissions on the serial port device so that nut can access it. Create the file /etc/udev/rules.d/99_nut-serialups.rules:
# /etc/udev/rules.d/99_nut-serialups.rules KERNEL=="ttyS0", GROUP="nut"
Replace ttyS0 with ttyS1 if your UPS is connected to the second serial port. Next, issue the following two commands to force udev to make the necessary permissions change:
$ sudo udevadm control --reload_rules
$ sudo udevadm control trigger
Note that the preceding two steps are unnecessary if you have USB-based UPS. Finally, we can make sure that nut properly detects the UPS:
$ sudo upsdrvctl start
You should see something like the following:
Network UPS Tools - UPS driver controller 2.2.2 Network UPS Tools: 0.29 USB communication driver - core 0.33 (2.2.2) Using subdriver: APC HID 0.92
Now, we need to configure upsd and upsmon. upsd communicates with the UPS driver that we just started. upsmon communicates with upsd and actually shuts down the machine in the event of a power failure. Why the extra level of indirection? Multiple instances of upsmon can be started on different machines. They can all share the same physical UPS. Create the file /etc/nut/upsd.conf as follows:
# /etc/nut/upsd.conf ACL all 0.0.0.0/0 ACL localhost 127.0.0.1/32 ACCEPT localhost REJECT all
This rejects all connections not originating from the local machine. If you need to monitor from multiple machines, see the man page for upsd.conf. Next, create /etc/nut/upsd.users:
# /etc/nut/upsd.users [local_mon] password = PASSWORD_HERE allowfrom = localhost upsmon master
Replace PASSWORD_HERE with a real password. You should add multiple users if you are monitoring from multiple machines. See the man page for upsd.users for more information. Now we need to configure upsmon. Create /etc/nut/upsmon.conf as follows:
# /etc/nut/upsmon.conf MONITOR apc@localhost 1 local_mon PASSWORD_HERE master POWERDOWNFLAG /etc/killpower SHUTDOWNCMD "/sbin/shutdown -h now"
Replace apc with the name you have your UPS in /etc/nut/ups.conf and PASSWORD_HERE with the password you set in /etc/nut/upsd.users. Since these files contain sensitive data, we should fix the permissions:
$ sudo chown root:nut /etc/nut/*
$ sudo chmod 640 /etc/nut/*
Finally, edit /etc/default/nut so that upsd and upsmon will start at system boot:
# /etc/default/nut START_UPSD=yes START_UPSMON=yes
The moment of truth:
$ sudo /etc/init.d/nut start
Check the end of /var/log/daemon.log to make sure everything started up. Also, you can run the following command which should print out some statistics:
$ upsc apc
Replace apc with whatever you named your UPS. What’s left? Save your work, unplug the UPS, and make sure your machine shuts down cleanly. This may take a while. By default, upsmon waits until the battery is at a critically low level before initiating a shutdown. What’s the point of having 20 minutes of battery life if you aren’t going to use it?
As you can see, this second method is quite a bit more difficult to configure. That said, using upsd and upsmon, as opposed to nut-hal-drivers, is probably the more reliable method of monitoring a UPS. It’s also the only option available if you need to monitor one UPS from multiple machines.