Monitoring your UPS with a Pi

I have a need to monitor a couple of APC UPS’ that are supplying power for my Home Theater system.  I currently don’t have a computer close by but want to monitor those UPS’… A Raspberry Pi and NUT to the rescue.  It seems like a perfect match.

 NUT is a great program for monitoring and network enabling just about any UPS.  Once you have NUT monitoring your UPS, you can then run the NUT client on your computers and they will be able to monitor the UPS and shutdown properly if needed.  If you haven’t had a chance ot look at NUT, and you have a UPS, you really should check it out.

The server side of NUT runs on a computer and has to connect directly to the UPS in order ot monitor it.  My problem is that I don’t have a computer close my UPS (it is protecting a computer) to run NUT on but I would like to monitor it.  Craming a full workstation in behind my Home Theater setup is ideal either.  I decided to let a Raspberry Pi do the server side.  It is small, quiet, and I can put it right on the back on the UPS.  Problem solved.

Here is the documentation to setup a Raspberry Pi with NUT and get it all working.

Installing the software

The first thing to do is install the NUT server and client software on the Raspberry Pi.  I am using a Paspberry Pi 2 with Raspian running on it.  If you are using another distribution then some of the command may be different.

# alt-get install nut-client nut-server
# apt-get install usbutils

If you receive some warnings on install, don’t worry about it, we will fix this up later.

Finding the port

NOTE: My APC-SMART UPS is an older one that has a 9 pin serial connection.  Some of the newer ones have a direct USP port but this documentation will be for the serial port version.

The next step is to find out what USB port your serial connection is on.  I am using a serial to USB cable plugged into a USB port on the Raspberry PI. This looks just like a serial port so that when it is plugged in it created the tty port in the /dev directory.  To find out what port it is on, unplug the USB cables from the Raspberry Pi and list the tty devices in the /dev directory. 

NOTE: If this is the only USB serial port on your device, then you will not see any when you try and list them without anything plugged in.

# ls -al /dev/*ttyUSB*
ls: cannot access /dev/*ttyUSB*: No such file or directory
#

Plug the USB port back in and then check it.

# ls -al /dev/*ttyUSB*
crw-rw---- 1 root dialout 188, 0 May  2 13:17 /dev/ttyUSB0
#

As you can see.  I only have one USB serial port plugged in and it is /dev/ttyUSB0.

Configure NUT

All the configuration files for NUT are in the directory /etc/nut. Change to that directory and then edit the file ups.conf

# cd /etc/nut
# vi ups.conf

This file needs to be changed to reflect your type of UPS.  For my APC-SMART UPS with a serial port, this is what the file should look like

[officeups]
        driver = apcsmart
        port = /dev/ttyUSB0
        cable = 940-0024C
        desc = "APC Smart-UPS 1000 upstairs office"

The UPS name is contained in the square brackets.  Yuo can name your UPS anything you like. The name is used to identify the UPS and for clients to use to connect to it. The next few lines describe the type of UPS, cable, and what driver to use. See the driver documentation on the offical NUT site for a explination if you are not using an APC with a serial port.

On the Raspian OS you may need to also create the /var/run/nut directory so the driver can write the pid file there. 

# mkdir -p /var/run/nut
# chown root:nut /var/run/nut
# chmod 770 /var/run/nut

Now we can test the driver.  Try to start the driver upas follows:

# upsdrvctl start
Network UPS Tools - UPS driver controller 2.7.2
Network UPS Tools - APC Smart protocol driver 3.1 (2.7.2)
APC command table version 3.1
[7] unrecognized
#

NOTE: Do not worry about the “[7] unrecognized” command if you you see this.  This happens with some APC firmwares but can be ignored.

If all goes well Your driver is now working and we just need to finish setting up the server ‘upsd’ and client ‘upsmon’.  In NUT the client and server are separate.  If that was only a client machine (one without the UPS attached to it) then we would only have to setup upsmon. It is upsmon that talks to the server process ‘upsd’ and shuts down the system if needed. Since this is the server machine, then we will be setting up both.

In this documentation we are going to assume that you have other network clients that want to monitor this UPS so we will be setting it up for full network access.  If this UPS is only for this Raspberry Pi then you can lock it down more by allowing the server to only listen on the loopback address (127.0.0.1).  Start by editing the upsd.conf file.

# cd /etc/nut
# vi upsd.conf

Add one listen directive for each interface that you want the server process to listen on.

LISTEN 127.0.0.1 3493
LISTEN 172.31.14.77 3493

The rest of the defaults in the file should be fine.

Now lets setup the security and create some user names.  This is very important since some of the command can allow remote client to set paramaters in your UPS or even shut it down.  We will create two type of users.  An admin user that can run remote commands and a monitor user.  The monitor user is what we will use in remote clients that are sharing this UPS and need to shutdown when required but otherwise will not be ‘administering’ the UPS.  The users are contained in the file upsd.users. Go ahead andedit this.

# cd /etc/nut
# vi upsd.users

The file should contain the following (change the password to anyting you like).

[admin]
        password = adminpasword
        actions = SET
        instcmds = ALL
[monlocal]
        password = mypassword
        upsmon master
[monuser]
        password = mypassword
        upsmon slave

OK so we have setup the password and access for the server ‘upsd’, now lets go ahead and setup the connection for the upsmon so that is can connect to the server.  Edit the upsmon.conf file.

# cd /etc/nut
# vi upsmon.conf

There is only one line the has to be there (the ther stuff is optional and the defaults are good).  The MONITOR lines should look something like this:

MONITOR officeups@localhost 1 monlocal mypassword master

The final step is to edit the nut.conf file and change the Mode lines from none to netserver.  This is what tells nut how to startup and what mode to work in.

# cd /etc/nut
# vi nut.conf
MODE=netserver

Testing and Starting the services

You are now ready to try and startup NUT.


# service nut-server start
# service nut-client start

Make sure that the server and client will start automatically after a reboot.

# update-rc.d nut-server enable
# update-rc.d nut-client enable

You can noew check the NUT UPS setup by quering the UPSD server.  Enter the folling command.

NOTE: Your output will be different for eavh UPS and model. WE just want to make sure it can communicate.

# upsc officeups@localhost
Init SSL without certificate database
battery.alarm.threshold: L
battery.charge: 100.0
battery.charge.restart: 00
battery.date: 07/30/04
battery.packs: 000
battery.runtime: 2280
battery.runtime.low: 120
battery.voltage: 27.33
battery.voltage.nominal: 024
device.mfr: APC
device.model: SMART-UPS 1000
device.serial: WS9938003976
device.type: ups
driver.name: apcsmart
driver.parameter.cable: 940-0024C
driver.parameter.pollinterval: 2
driver.parameter.port: /dev/ttyUSB0
driver.version: 2.7.2
driver.version.internal: 3.1
input.frequency: 60.00
input.quality: FF
input.sensitivity: H
input.transfer.high: 132
input.transfer.low: 103
input.transfer.reason: simulated power failure or UPS test
input.voltage: 122.2
input.voltage.maximum: 122.8
input.voltage.minimum: 121.5
output.voltage: 122.2
output.voltage.nominal: 115
ups.delay.shutdown: 180
ups.delay.start: 000
ups.firmware: 60.11.D
ups.id: SMRT1000
ups.load: 008.3
ups.mfr: APC
ups.mfr.date: 09/14/99
ups.model: SMART-UPS 1000
ups.serial: WS9938003976
ups.status: OL
ups.temperature: 034.6
ups.test.interval: 1209600
ups.test.result: NO


Congratulations, you now have a NUT server all setup and working.

Leave a Comment

Your email address will not be published. Required fields are marked *