How to Monitor UPS with Raspberry Pi

I’ve got a couple of LINUX servers and a Synology NAS on my Cyber Power 1350 UPS. I’d like to monitor the battery level and OUTAGES. I can use the little pushbutton on the front to poke through the status OR I can install NUT and monitor my UPS with a cool webpage and much better statistics! I’m going the webpage route!

Setting up NUT on Raspberry

Image an SD card and do the usual set up steps. Check my GUIDE if you have questions. I changed the hostname to “ups” to identify my new server. Plug a USB cable into the UPS and into the Raspberry. Check your connection with “lsusb”, literally “list USB devices”:

pi@ups:~ $ lsusb
Bus 001 Device 004: ID 0764:0501 Cyber Power System, Inc. CP1500 AVR UPS
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. SMC9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
pi@ups:~ $

Looks good. I see my UPS at the top of the list. Now install the monitoring software:

apt-get update && apt-get install nut nut-client nut-server

This is the Network UPS Tools package or “NUT” for short. Nice to have real-world tools in the Raspberry repositories for easy installs! w00t!

NUT Configuration

First, let’s add the connection and the UPS to the config in /etc/nut/ups.conf. I like to add my stuff right at the bottom so I can find it quickly in the future. Add the following “stanza”:

[servers]
driver = usbhid-ups
port = auto
desc = "CyberPower CP1350 AVR UPS"

Hopefully, YOUR UPS is like mine and works well with the usbhid-ups driver. The name can be whatever suits your fancy. Put it in [square] brackets. The desc(ription) is also completely up to you. I would say, try the auto settings–otherwise, you can look up your model here.

Next, we need to setup upsmon. Again, I’m just going to add what I need to the end of the file. The first line shows the format. You can include it as a comment for future reference. :

# MONITOR <system> <powervalue> <user> <password> ("master"|"slave")
MONITOR ups@localhost 1 monuser secret master

The system is the name that we configured in the last bit. Powervalue of 1 is fine for home use. Username and password are at your discretion. (Mine are based on the hardcoded credentials that a Synology NAS uses.) “master” because the UPS is attached directly to this device through the USB cable. The clients will be defined as “slave”.

This same “user” is then added to /etc/nut/upsd.conf. At the very bottom of the file, add:

[monuser]
   password = secret
   upsmon master

Finally, set the mode of how this whole thing will function. Edit /etc/nut/nut.conf and change the last line:

MODE=netserver

The Big Restart

Now, let’s restart these processes and see if we can connect. I’m going to just reboot. That will ensure that all the config files got saved and the unit files are all enabled. Once the Raspberry comes back up, try to connect with UPSC and the name of your UPS:

root@ups:~# upsc servers@localhost
Init SSL without certificate database
battery.charge: 53
battery.charge.low: 10
battery.charge.warning: 20
battery.mfr.date: CPS
battery.runtime: 490
battery.runtime.low: 300
battery.type: PbAcid
battery.voltage: 13.7
battery.voltage.nominal: 12
device.mfr: CPS
device.model: CP 1350C
device.type: ups
driver.name: usbhid-ups
driver.parameter.pollfreq: 30
driver.parameter.pollinterval: 2
driver.parameter.port: auto
driver.parameter.synchronous: no
driver.version: 2.7.4
driver.version.data: CyberPower HID 0.4
driver.version.internal: 0.41
input.transfer.high: 140
input.transfer.low: 90
input.voltage: 123.0
input.voltage.nominal: 120
output.voltage: 123.0
ups.beeper.status: enabled
ups.delay.shutdown: 20
ups.delay.start: 30
ups.load: 33
ups.mfr: CPS
ups.model: CP 1350C
ups.productid: 0501
ups.realpower.nominal: 298
ups.status: OL CHRG
ups.test.result: Done and warning
ups.timer.shutdown: -60
ups.timer.start: 0
ups.vendorid: 0764
root@ups:~#

UPS Web Server

YAY! It works! Now, to install the web component, as promised! First, install the webserver and the NUT web stuff:

apt install apache2 nut-cgi

Edit /etc/nut/hosts.conf to allow CGI scrips access to the monitor. Add one line to the bottom of the file:

MONITOR servers@localhost "Server UPS"

Enable CGI in apache and restart the web server:

root@ups:/etc/nut# a2enmod cgi
Your MPM seems to be threaded. Selecting cgid instead of cgi.
Enabling module cgid.
To activate the new configuration, you need to run:
systemctl restart apache2
root@ups:/etc/nut# systemctl restart apache2
root@ups:/etc/nut#

Now, you just need to convince apache that you really, really want to turn on CGI by uncommenting the following in /etc/nut/upsset.conf:

### I_HAVE_SECURED_MY_CGI_DIRECTORY

…and you should be able to hit the webpage:

http://ups.local/cgi-bin/nut/upsset.cgi

Do you remember the username and password that we configured for upsmon? You’re going to need it here! Once you’re logged in, you can take a look at your status, or actually issue commands to the UPS! (No wonder it’s password protected!) Another page that comes with the package is here:

http://ups.local/cgi-bin/nut/upsstats.cgi
upsstats.cgi page is a nice monitor

Next post, we look at configuring the client or “slave” systems. This allows the upsmon process to send shutdown commands to the servers that are plugged into this UPS and do an orderly shutdown instead of crashing when the lights go out!

Author: John

10 thoughts on “How to Monitor UPS with Raspberry Pi

  1. Thanks. Works like a charm. Never realised my Eaton 3S 850 UPS was able to share this much data. Any idea if this data can be shared with InfluxDB / Grafana? When possible, what would be the route in general?

  2. How would you monitor two UPSes, on different USB ports on the same Raspberry Pi? I can monitor one UPS, but not the other. Both are Tripplite SmartUPS Pro boxes.

    1. Just add a second line to each of the config files for that second UPS:

      hosts.conf:MONITOR ups@localhost “Downstairs UPS”
      hosts.conf:MONITOR 2Dups@localhost “Upstairs UPS

      upsmon.conf:MONITOR ups@localhost 1 monuser secret master
      upsmon.conf:MONITOR 2Dups@localhost monuser secret master

  3. Hi John,
    Where’s that 2nd article, in which you share that info with your servers for an orderly shutdown? That is precisely the problem that led me to find your article. 2 Servers plugged into the same UPS which only has 1 USB cable out. I have several Rpi’s laying about doing nothing, and this sounds like an elegant and useful purpose for one of them!
    Best Regards,
    Robert

  4. Request for followup post – How to send alerts via email.

    Loss of power, power restored, discharging low battery etc would all be real useful.

    Thanks!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.