My home server is protected by an MGE Ellipse 750 UPS for years. I bought it for several reasons: it’s affordable, has good capacity and is Ubuntu certified.

I also read back then rumors implying that Nut’s maintainer was employed by MGE. Having a hardware manufacturer employing a fellow open-source hacker has certainly influenced my purchase decision.

MGE is no more and has been merged with EATON. But my UPS is still supported, and the release of Debian Squeeze is a good opportunity to consolidate my knowledge in the form of this tutorial.

So here is how I setup Nut on Debian Squeeze to monitor my UPS.

First things first, we have to install the main package and its USB driver:

1 $ aptitude install nut nut-usb

Now let’s configure Nut and run it:

 1 $ sed -i 's/MODE=none/MODE=standalone/g' /etc/nut/nut.conf
 2 $ echo '
 3 [MGE-Ellipse750]
 4 driver = usbhid-ups
 5 port = auto
 6 desc = "MGE UPS Systems"
 7 ' >> /etc/nut/ups.conf
 8 $ sed -i 's/# LISTEN 127\.0\.0\.1 3493/LISTEN 127\.0\.0\.1/g' /etc/nut/upsd.conf
 9 $ echo '
10 [kevin]
11 password = badpassword
12 upsmon master
13 ' >> /etc/nut/upsd.users
14 $ sed -i 's/# NOTIFYCMD \/usr\/local\/ups\/bin\/notifyme/NOTIFYCMD \/sbin\/upssched/g' /etc/nut/upsmon.conf
15 $ echo '
16 MONITOR MGE-Ellipse750@localhost 1 kevin badpassword master
17 NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC
18 NOTIFYFLAG ONLINE SYSLOG+WALL+EXEC
19 ' >> /etc/nut/upsmon.conf
20 $ sed -i 's/CMDSCRIPT \/upssched-cmd/CMDSCRIPT \/etc\/nut\/upssched-cmd/g' /etc/nut/upssched.conf
21 $ sed -i 's/# PIPEFN \/var\/run\/nut\/upssched\/upssched.pipe/PIPEFN \/var\/run\/nut\/upssched.pipe/g' /etc/nut/upssched.conf
22 $ sed -i 's/# LOCKFN \/var\/run\/nut\/upssched\/upssched.lock/LOCKFN \/var\/run\/nut\/upssched.lock/g' /etc/nut/upssched.conf
23 $ echo '
24 AT ONBATT * START-TIMER onbatt 30
25 AT ONLINE * CANCEL-TIMER onbatt
26 ' >> /etc/nut/upssched.conf
27 $ echo '
28 #!/bin/sh
29 exit 0
30 ' > /etc/nut/upssched-cmd
31 $ /etc/init.d/nut restart

As you can see you have lots of stuff to configure before Nut can do what it was designed for. But after all of these commands, you should have a working UPS.

You can now test that your system works by using the command below, which list statistics of a given UPS:

1 $ upsc MGE-Ellipse750@localhost

But in some rare cases, your UPS will not be recognized and you’ll have like me the following messages in your /var/log/syslog:

May  5 16:12:36 paris-server upsmon[10773]: Poll UPS [[email protected]] failed - Driver not connected

In this case, you should run Nut’s driver in debug mode:

 1 $ /lib/nut/usbhid-ups -DDD -a MGE-Ellipse750
 2 Network UPS Tools - Generic HID driver 0.34 (2.4.3)
 3 USB communication driver 0.31
 4     0.000000     debug level is '3'
 5     0.013911     upsdrv_initups...
 6     0.189541     Checking device (0463/FFFF) (005/003)
 7     0.189705     - VendorID: 0463
 8     0.189741     - ProductID: ffff
 9     0.189767     - Manufacturer: unknown
10     0.189794     - Product: unknown
11     0.189819     - Serial Number: unknown
12     0.189842     - Bus: 005
13     0.189862     Trying to match device
14     0.189906     Device matches
15     0.189954     failed to claim USB device: could not claim interface 0: Operation not permitted
16     0.189995     failed to detach kernel driver from USB device: could not detach kernel driver from interface 0: Operation not permitted
17     0.190033     failed to claim USB device: could not claim interface 0: Operation not permitted
18     0.190070     failed to detach kernel driver from USB device: could not detach kernel driver from interface 0: Operation not permitted
19     0.190108     failed to claim USB device: could not claim interface 0: Operation not permitted
20     0.190145     failed to detach kernel driver from USB device: could not detach kernel driver from interface 0: Operation not permitted
21     0.190181     failed to claim USB device: could not claim interface 0: Operation not permitted
22     0.190217     failed to detach kernel driver from USB device: could not detach kernel driver from interface 0: Operation not permitted
23     0.190252     Can't claim USB device [0463:ffff]: could not detach kernel driver from interface 0: Operation not permitted

As you can see in messages above, Nut can’t see my UPS. By chance, forcing nut to use the root user let it see my UPS:

 1 $ /lib/nut/usbhid-ups -DDD -u root -a MGE-Ellipse750
 2 Network UPS Tools - Generic HID driver 0.34 (2.4.3)
 3 USB communication driver 0.31
 4     0.000000     debug level is '3'
 5     0.001678     upsdrv_initups...
 6     0.172877     Checking device (0463/FFFF) (005/003)
 7     1.112408     - VendorID: 0463
 8     1.112464     - ProductID: ffff
 9     1.112489     - Manufacturer: MGE OPS SYSTEMS
10     1.112516     - Product: ELLIPSE
11     1.112542     - Serial Number: BDCJ3800Q
12     1.112569     - Bus: 005
13     1.112595     Trying to match device
14     1.112647     Device matches
15     1.112726     failed to claim USB device: could not claim interface 0: Device or resource busy
16     1.113239     detached kernel driver from USB device...
17     1.251394     HID descriptor, method 1: (9 bytes) => 09 21 00 01 21 01 22 01 03
18     1.251460     HID descriptor, method 2: (9 bytes) => 09 21 00 01 21 01 22 01 03
19     1.251491     HID descriptor length 769
20     1.351379     Report Descriptor size = 769
21     1.351456     Report Descriptor: (769 bytes) => 05 84 09 04 a1 01 09 24 a1 00 09 02 a1 00
22     1.351509      55 00 65 00 85 01 75 01 95 05 15 00 25 01 05 85 09 d0 09 44 09 45 09 42 0b
23 (...)
24 

So the issue is now clear and is related to permissions. I was able to fix this issue by changing the permissions on the USB device corresponding to my UPS:

1 $ chmod 0666 /dev/bus/usb/005/003

Another working way to fix this is to change the group of the device to nut:

1 $ chown :nut /dev/bus/usb/005/003

BTW, to get the bus number (005 here) and device number (003 in my case) of your UPS, run lsudb:

1 $ lsusb
2 Bus 005 Device 003: ID 0463:ffff MGE UPS Systems UPS
3 Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
4 Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
5 Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
6 Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
7 Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Of course this fix is absolutely temporary, as you’ll need to perform the change above after every reboot. This is far from practical. In fact, as describe in this Fedora 10 bug report, but also in some other Debian bug report, this issue is directly tied to conflicting Udev rules.

Based on clues from these bug reports you can fix Udev using different strategies. As I can’t decide which one is the cleanest, I just did something that is quite brutal, but works. It consist of replacing in /lib/udev/rules.d/91-permissions.rules the line setting rights for USBfs-like devices:

--- /lib/udev/rules.d/91-permissions.rules-orig 2011-05-05 18:49:08.015538434 +0200
+++ /lib/udev/rules.d/91-permissions.rules      2011-05-05 18:49:16.663537978 +0200
@@ -33,7 +33,7 @@

 # usbfs-like devices
 SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", \
-                               MODE="0664"
+                               MODE="0666"

 # serial devices
 SUBSYSTEM=="tty",

Now all you have to do is to unplug the power cord and wait until your machine gracefully shut down as soon as batteries are low! :)