Uncategorized

Use NUT to watch your UPS

A client of mine today wanted to be able to shut down their linux server if their UPS goes off line and reaches low battery. They currently have the CyberPower 1500 AVR UPS, which states that 2008 will have linux support. After googlin’ around a bit I came across this project, Network UPS Tools or NUT. This project rules, and they have a lot of UPS drivers available. On top of that, it’s in portage!

Basically, you can configure a machine (the master) to connect to the UPS. In this case, it needs to connect via serial cable. The master will have a ups daemon where monitoring daemons can connect to. What’s cool with this, is that other linux boxes can connect to this upsd as ‘slaves’. In my scenario I have two Gentoo boxes, a file server and firewall. I configured the daemon and monitor to exists on the fileserver as well as a monitor on the firewall. I’ll list the procedure below.

A client of mine today wanted to be able to shut down their linux server if their UPS goes off line and reaches low battery. They currently have the CyberPower 1500 AVR UPS, which states that 2008 will have linux support. After googlin’ around a bit I came across this project, Network UPS Tools or NUT. This project rules, and they have a lot of UPS drivers available. On top of that, it’s in portage!

Basically, you can configure a machine (the master) to connect to the UPS. In this case, it needs to connect via serial cable. The master will have a ups daemon where monitoring daemons can connect to. What’s cool with this, is that other linux boxes can connect to this upsd as ‘slaves’. In my scenario I have two Gentoo boxes, a file server and firewall. I configured the daemon and monitor to exists on the fileserver as well as a monitor on the firewall. I’ll list the procedure below.


emerge -pv sys-power/nut

Configure ups.conf


[server]
driver = cyberpower
port = /dev/ttyS0

The driver option are the available nut drivers available. To see what driver will work for you, go here. Port can be set to auto, but I knew what serial interface is was listening on. You may need to build serial support in your kernel and enable it in your BIOS. Also, some UPS use USB, make sure you have HID support built into the kernel.

Next, configure your upsd.conf. Basically, we need to set up ACLs and we’ll be using those names in other areas..


ACL all 0.0.0.0/0
ACL localhost 127.0.0.1/32
ACL firewall 10.0.1.1/32
ACL backup1 10.0.1.20/32
ACCEPT localhost
ACCEPT firewall
ACCEPT fileserver
REJECT all

Now, we need to create users that can connect to the monitor, we do this by editing upsd.users.


[admin]
password = yourpassword
actions = SET
instcmds = ALL

[monuser]
password = yourpassword
allowfrom = localhost fileserver
upsmon master

[monslave]
password = yourpassword
allowfrom = firewall
upsmon slave

As you can see, we reference the names from our ACL here.

Finally, we configure the upsmon.conf. Here we will put our MONITOR statement and access. This example is for the ‘master’, bascially the machine that is directly connected to the UPS.


MINSUPPLIES 1
SHUTDOWNCMD "/sbin/shutdown -h +0"
POLLFREQ 5
POLLFREQALERT 5
HOSTSYNC 15
DEADTIME 15
POWERDOWNFLAG /etc/killpower
RBWARNTIME 43200
NOCOMMWARNTIME 300
FINALDELAY 5
RUN_AS_USER root
MONITOR server@localhost 1 monuser yourpassword master

Make sure you start and add these services to the runlevel


/etc/init.d/upsd start
/etc/init.d/upsmon start
rc-update add upsd default
rc-update add upsmon default

Test to make sure you can talk to the UPS.


upsc server@localhost

You should see something similar to this:


nut # upsc server@localhost
battery.charge: 100
driver.name: cyberpower
driver.parameter.port: /dev/ttyS0
driver.version: 2.0.5
driver.version.internal: 1.00
input.frequency: 60.0
input.voltage: 120
ups.firmware: 5.100
ups.load: 020
ups.mfr: CyberPower
ups.model: Unknown model - 15
ups.status: OL
ups.temperature: 44.0

To configure the ‘slave’, the firewall in my case, emerge sys-power/nut there. Only edit the upsmon.conf and add the following:


MONITOR server@10.0.1.20 1 monslave yourpassword slave

Start and add upsmon


/etc/init.d/upsmon start
rc-update add upsmon default

Test from the slave


upsc server@10.0.1.20
battery.charge: 100
driver.name: cyberpower
driver.parameter.port: /dev/ttyS0
driver.version: 2.0.5
driver.version.internal: 1.00
input.frequency: 60.0
input.voltage: 120
ups.firmware: 5.100
ups.load: 020
ups.mfr: CyberPower
ups.model: Unknown model - 15
ups.status: OL
ups.temperature: 44.0

How nut works is it waits for ON BATTERY and LOW BATTERY indications before issuing the shutdown, you can configure your BIOS to reboot when power returns. I’m still working on that issue on the boxes. You can also graph the UPS via cacti and have nut send alerts when it’s offline, etc. Overall, this is a really cool project!!!