Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / operating-systems / Linux

Monitoring a UPS with Nut and lighttpd on Linux - Part 1

5.00/5 (2 votes)
13 Jun 2022CPOL3 min read 5.7K  
How to monitor a UPS with Nut and lighttpd on Linux
This post covers the basic setup of server and client for Linux to monitor a UPS with nut and lighttpd.

Image 1

UPDATE: An astute reader posted the following comment for anyone who may be attempting to implement this solution on a Raspberry Pi running with the pi-hole setup (see https://pi-hole.net/). The full comment:

I needed to use the info here: https://www.reddit.com/r/pihole/comments/hauphd/adding_apcupsd_ups_status_util_to_rpi_with/ in order to get it fully working. For some reason, it wasn't able to launch the .cgi file until I aliased the nut directory. My lighttpd.conf has these differences:
server.modules = ( "modaccess", "modaccesslog", "modauth", "modalias", "modexpire", 
                   "modredirect", "modrewrite", "modfastcgi", "modsetenv", "modstatus" )

alias.url = ( "/nut" => "/usr/share/nut/www/") 
and now nut works with lighttpd + pihole

Thanks Rob!

We all know the importance of having a battery backup solution for your servers and network equipment; in a small company environment where cost is a factor, the purpose of the battery backup is somewhat different. Typically, the goal isn't all day (or even multi-hour) run time, but rather 20 - 30 minutes - long enough to cover short outages and notify and gracefully shut down the equipment in a longer outage. Again, YMMV of course depending on the needs of your organization. The purpose of these articles is to walk through configuring the nut-server service on a Proxmox host server and the nut-client service on a Linux guest, including email notification and graceful server shutdown for all VM's.

Recently, I had to replace an aging UPS with a new unit and based on cost, user reviews, and technical capability, I selected the Tripp Lite SMART1500RM2U. For our purposes, we'll assume that our UPS unit is connected via USB to our Proxmox host server named VMSERVER.

  1. Before plugging it in, let's start a persistent tail of /var/log/messages to ensure that the UPS gets picked up by the operating system. Open a new SSH session to the host and we'll tail the log file, watching for the USB entries. We should see output similar to the following:
    root@vmserver [~]# tail -f /var/log/messages
    Oct 26 09:32:03 vmserver kernel: [149701.226307] 
           usb 1-11: SerialNumber: 3122CVYSM88EC01815
    Oct 26 09:32:03 vmserver kernel: [149701.275704] 
           hid-generic 0003:09AE:3015.270B: hiddev0,hidraw0: USB HID v1.10 
           Device [Tripp Lite  TRIPP LITE SMART1500RM2U ] on usb-0000:00:14.0-11/input0
    Oct 26 09:32:17 vmserver kernel: [149715.705236] 
           usb 1-11: USB disconnect, device number 36
    Oct 26 09:32:18 vmserver kernel: [149716.051727] 
           usb 1-11: new low-speed USB device number 37 using xhci_hcd
    Oct 26 09:32:18 vmserver kernel: [149716.209951] 
           usb 1-11: New USB device found, idVendor=09ae, idProduct=3015, bcdDevice= 2.0a
    Oct 26 09:32:18 vmserver kernel: [149716.209960] 
           usb 1-11: New USB device strings: Mfr=2, Product=3, SerialNumber=4
    Oct 26 09:32:18 vmserver kernel: [149716.209965] 
           usb 1-11: Product: TRIPP LITE SMART1500RM2U
    Oct 26 09:32:18 vmserver kernel: [149716.209969] 
           usb 1-11: Manufacturer: Tripp Lite
    Oct 26 09:32:18 vmserver kernel: [149716.209972] 
           usb 1-11: SerialNumber: 3122CVYSM88EC01815
    Oct 26 09:32:18 vmserver kernel: [149716.259635] 
           hid-generic 0003:09AE:3015.270C: hiddev0,hidraw0: USB HID v1.10 
           Device [Tripp Lite  TRIPP LITE SMART1500RM2U ] on usb-0000:00:14.0-11/input0 

    From our log entries above, we can see that the UPS has been recognized by the operating system so we can now install and configure nut-server and nut-client.

  2. Install nut, nut-client, nut-server, nut-cgi, and lighttpd using apt-get:
    root@vmserver [~]# apt-get install nut nut-client nut-server nut-cgi lighttpd
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    The following additional packages will be installed:
      libltdl7 libnutscan1 libupsclient4 libusb-0.1-4 lighttpd-mod-deflate 
      lighttpd-mod-openssl spawn-fcgi
    Suggested packages:
      rrdtool php-cgi php-fpm apache2-utils lighttpd-doc lighttpd-mod-authn-gssapi 
      lighttpd-mod-authn-pam lighttpd-mod-authn-sasl lighttpd-mod-geoip 
      lighttpd-mod-maxminddb lighttpd-mod-trigger-b4-dl lighttpd-mod-vhostdb-pgsql
      lighttpd-mod-webdav lighttpd-modules-dbi lighttpd-modules-ldap 
      lighttpd-modules-lua lighttpd-modules-mysql nut-monitor nut-ipmi nut-snmp nut-xml
    The following NEW packages will be installed:
      libltdl7 libnutscan1 libupsclient4 libusb-0.1-4 lighttpd 
      lighttpd-mod-deflate lighttpd-mod-openssl nut nut-cgi nut-client 
      nut-server spawn-fcgi
    0 upgraded, 12 newly installed, 0 to remove and 65 not upgraded.
    Need to get 2,360 kB/2,786 kB of archives.
    After this operation, 9,075 kB of additional disk space will be used.
    Do you want to continue? [Y/n] 
  3. I configured nut using the steps in this article.
    NOTE: If you're not rebooting the server, you'll need to restart udev like so: systemctl restart udev (it's not mentioned in the article).
  4. Load the CGI module in the lighttpd configuration and restart lighttpd:
    root@vmserver [/etc/nut]# lighttpd-enable-mod cgi
    Enabling cgi: ok
    Run "service lighttpd force-reload" to enable changes
    
    root@vmserver [/etc/nut]# service lighttpd force-reload 
  5. Check the UPS status by visiting http://<server name/IP>/cgi-bin/upsstats.cgi in your browser. You should see a page similar to the following:

    Image 2

    UPS Status - HTML Page
  6. Now that the nut server is functional, you can install the nut client on the Proxmox guest systems. On Linux, you can install the client with apt-get:

    root@linuxappserver [~]# apt-get install nut-client
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    The following additional packages will be installed:
      bash-completion libupsclient4
    Suggested packages:
      nut-monitor
    The following NEW packages will be installed:
      bash-completion libupsclient4 nut-client
    0 upgraded, 3 newly installed, 0 to remove and 28 not upgraded.
    Need to get 673 kB of archives.
    After this operation, 2,348 kB of additional disk space will be used.
    Do you want to continue? [Y/n] 
  7. Check the nut server with the upsc command:

    upsc tripplite@vmserver ups.status

    You should get output similar to the following:

    root@linuxappserver [/var/log]# upsc tripplite@vmserver ups.status
    Init SSL without certificate database
    OL 
  8. Modify the nut configuration files:

    nut.conf:

    MODE=netclient

    upsmon.conf:

    RUN_AS_USER root
    
    MONITOR tripplite@vmserver.wwf-mgt.com 1 admin secret slave
    
    MINSUPPLIES 1
    SHUTDOWNCMD "/sbin/shutdown -h now"
    NOTIFYCMD /usr/sbin/upssched
    POLLFREQ 2
    POLLFREQALERT 1
    HOSTSYNC 15
    DEADTIME 15
    POWERDOWNFLAG /etc/killpower
    
    NOTIFYMSG ONLINE    "UPS %s on line power"
    NOTIFYMSG ONBATT    "UPS %s on battery"
    NOTIFYMSG LOWBATT   "UPS %s battery is low"
    NOTIFYMSG FSD       "UPS %s: forced shutdown in progress"
    NOTIFYMSG COMMOK    "Communications with UPS %s established"
    NOTIFYMSG COMMBAD   "Communications with UPS %s lost"
    NOTIFYMSG SHUTDOWN  "Auto logout and shutdown proceeding"
    NOTIFYMSG REPLBATT  "UPS %s battery needs to be replaced"
    NOTIFYMSG NOCOMM    "UPS %s is unavailable"
    NOTIFYMSG NOPARENT  "upsmon parent process died - shutdown impossible"
    
    NOTIFYFLAG ONLINE   SYSLOG+WALL+EXEC
    NOTIFYFLAG ONBATT   SYSLOG+WALL+EXEC
    NOTIFYFLAG LOWBATT  SYSLOG+WALL
    NOTIFYFLAG FSD      SYSLOG+WALL+EXEC
    NOTIFYFLAG COMMOK   SYSLOG+WALL+EXEC
    NOTIFYFLAG COMMBAD  SYSLOG+WALL+EXEC
    NOTIFYFLAG SHUTDOWN SYSLOG+WALL+EXEC
    NOTIFYFLAG REPLBATT SYSLOG+WALL
    NOTIFYFLAG NOCOMM   SYSLOG+WALL+EXEC
    NOTIFYFLAG NOPARENT SYSLOG+WALL
    
    RBWARNTIME 43200
    
    NOCOMMWARNTIME 600
    
    FINALDELAY 5 

That covers the basic setup of server and client for Linux. I will finish addressing the Promox-specific pieces - including email notification and guest shutdown - in Part 2 of this article.

If you have questions about the article or if you're trying to follow along and having problems, I'd be happy to help as best I can. Please leave a comment below.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)