ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Pi as a UPS monitor

    IT Discussion
    raspberry pi ups apc eaton nut
    8
    114
    33.9k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JaredBuschJ
      JaredBusch
      last edited by

      First up is nut.conf. There is only a single option to set in this file, but it is important.

      Assuming you are going to have only one device, or if multiple devices, that they will report in on their own, the simplest configuration is to choose standalone

      MODE=standalone
      

      Here are the various meanings from the conf file.

      # - none: NUT is not configured, or use the Integrated Power Management, or use
      #   some external system to startup NUT components. So nothing is to be started.
      # - standalone: This mode address a local only configuration, with 1 UPS
      #   protecting the local system. This implies to start the 3 NUT layers (driver,
      #   upsd and upsmon) and the matching configuration files. This mode can also
      #   address UPS redundancy.
      # - netserver: same as for the standalone configuration, but also need
      #   some more network access controls (firewall, tcp-wrappers) and possibly a
      #   specific LISTEN directive in upsd.conf.
      #   Since this MODE is opened to the network, a special care should be applied
      #   to security concerns.
      # - netclient: this mode only requires upsmon.
      
      1 Reply Last reply Reply Quote 0
      • JaredBuschJ
        JaredBusch
        last edited by

        Next up is ups.conf, again read the config file for more information, it is fairly thorough.

        In this case I am going to connect it to the APC unit pictured at the beginning of this thread.

        [bnajaredrouter]
        driver = usbhid-ups
        port = auto
        desc = "Jared Router UPS"
        
        1 Reply Last reply Reply Quote 0
        • JaredBuschJ
          JaredBusch
          last edited by

          The file upsd.conf does not need modified for a typical standalone setup.

          1 Reply Last reply Reply Quote 0
          • JaredBuschJ
            JaredBusch
            last edited by

            The file upsd.users needs modified to have the authentication that upsmon will use added to the end of the file.

            [bnaupsmon]
            password = AGoodPassword
            upsmon master
            
            1 Reply Last reply Reply Quote 0
            • JaredBuschJ
              JaredBusch
              last edited by

              The file upsmon.conf starts to get into the nuts and bolts of setting things up to do things for you.

              First up will be to uncomment the run as user setting.

              RUN_AS_USER nut
              

              Next is creating the monitor line to tell it what system to monitor. This uses the settings from ups.conf and upsd.users that you previously configured.

              MONITOR bnajaredrouter@localhost 1 bnaupsmon AGoodPassword master
              

              continuing down the file, the next thing to do is to set notifycmd to point to the upssched program

              NOTIFYCMD /sbin/upssched
              

              The final part of this file is to uncomment all of the notify flags and add the EXEC flag to the ones you want to fire the above upssched application. Leaving WALL in while testing is useful, but pretty pointless later for me since the goal is a remote alert.

              NOTIFYFLAG ONLINE       SYSLOG+WALL+EXEC
              NOTIFYFLAG ONBATT       SYSLOG+WALL+EXEC
              NOTIFYFLAG LOWBATT      SYSLOG+WALL
              NOTIFYFLAG FSD          SYSLOG+WALL
              NOTIFYFLAG COMMOK       SYSLOG+WALL
              NOTIFYFLAG COMMBAD      SYSLOG+WALL
              NOTIFYFLAG SHUTDOWN     SYSLOG+WALL
              NOTIFYFLAG REPLBATT     SYSLOG+WALL
              NOTIFYFLAG NOCOMM       SYSLOG+WALL
              NOTIFYFLAG NOPARENT     SYSLOG+WALL
              
              1 Reply Last reply Reply Quote 0
              • JaredBuschJ
                JaredBusch
                last edited by JaredBusch

                If you never set any of the flags to EXEC in the previous section, there is nothing to do here, you can stop. But what would be the point of a remote alerting device that does not alert.

                So now we come to the last conf file upssched.conf. This one is a bit more annoying because the developers intentionally chose to ship this broken to 'force' you to set up a file yourself.
                By default, PIPEFN and LOCKFN are commented out and point to the /var/run/nut/upssched directory that does not exist.

                # PIPEFN /var/run/nut/upssched/upssched.pipe
                # LOCKFN /var/run/nut/upssched/upssched.lock
                

                So we make a directory and the pipe file. You are instructed to not make the lock file in the comments.

                sudo mkdir /etc/nut/upssched
                sudo chown nut:nut /etc/nut/upssched
                sudo touch /etc/nut/upssched/upssched.pipe
                sudo chown nut:nut /etc/nut/upssched/upssched.pipe
                

                Editing this file, note the location the shell script that will be called. This is the default, and there is an example script already there, ready to be modified.

                CMDSCRIPT /bin/upssched-cmd
                

                Uncomment and update the PIPE and LOCK lines.

                PIPEFN /etc/nut/upssched/upssched.pipe
                LOCKFN /etc/nut/upssched/upssched.lock
                

                Now comes the part that does work, the AT commads. You will want to read the comments and likely the documentation to make full use of this. But here are a couple examples of AT commands to get you going.
                When the unit goes on battery, call the shell script to start a 30 second timer named 'onbattwarn'. When the power comes back online, cancel the timer.

                AT ONBATT * START-TIMER onbattwarn 30
                AT ONLINE * CANCEL-TIMER onbattwarn
                AT ONLINE * EXECUTE ongrid
                
                1 Reply Last reply Reply Quote 0
                • JaredBuschJ
                  JaredBusch
                  last edited by

                  Now the final bit of editing, and the one a lot of you will have to simply copy examples for. Editing the shell script that does the actual work.

                  You can look at the default script and see that because there was no default AT command with 'upsgone' as an named trigger, nothing would ever happen. Let's make it useful.

                  sudo nano /bin/upssched-cmd

                  #! /bin/sh
                  case $1 in
                          ongrid)
                                  logger -t upssched-cmd "The UPS is now on grid power."
                                  ;;
                          onbattwarn)
                                  logger -t upssched-cmd "The UPS has been on battery power for 30 seconds."
                                  ;;
                          *)
                                  logger -t upssched-cmd "Unrecognized command: $1"
                                  ;;
                  esac
                  
                  1 Reply Last reply Reply Quote 0
                  • JaredBuschJ
                    JaredBusch
                    last edited by

                    And something is broke, but since I followed the directions I had previously wrote in October, that I means I left out something I did.

                    pi@bna-pwr-pi-01:/etc/nut $ tail -f /var/log/syslog
                    Feb  2 04:20:42 bna-pwr-pi-01 systemd[1]: Starting Network UPS Tools - power device monitor and shutdown controller...
                    Feb  2 04:20:42 bna-pwr-pi-01 upsmon[1284]: fopen /var/run/nut/upsmon.pid: No such file or directory
                    Feb  2 04:20:42 bna-pwr-pi-01 upsmon[1284]: UPS: bnajaredrouter@localhost (master) (power value 1)
                    Feb  2 04:20:42 bna-pwr-pi-01 upsmon[1284]: Using power down flag file /etc/killpower
                    Feb  2 04:20:42 bna-pwr-pi-01 upsmon[1286]: Startup successful
                    Feb  2 04:20:42 bna-pwr-pi-01 upsmon[1287]: Init SSL without certificate database
                    Feb  2 04:20:42 bna-pwr-pi-01 systemd[1]: nut-monitor.service: Supervising process 1287 which is not our child. We'll most likely not notice when it exits.
                    Feb  2 04:20:42 bna-pwr-pi-01 systemd[1]: Started Network UPS Tools - power device monitor and shutdown controller.
                    Feb  2 04:20:42 bna-pwr-pi-01 upsmon[1287]: UPS [bnajaredrouter@localhost]: connect failed: Connection failure: Connection refused
                    Feb  2 04:20:42 bna-pwr-pi-01 upsmon[1287]: Communications with UPS bnajaredrouter@localhost lost
                    Feb  2 04:20:47 bna-pwr-pi-01 upsmon[1287]: UPS [bnajaredrouter@localhost]: connect failed: Connection failure: Connection refused
                    Feb  2 04:20:47 bna-pwr-pi-01 upsmon[1287]: UPS bnajaredrouter@localhost is unavailable
                                                                                                   
                    Broadcast message from nut@bna-pwr-pi-01 (somewhere) (Thu Feb  2 04:20:47 2017)
                                                                                                   
                    UPS bnajaredrouter@localhost is unavailable                                    
                                                                                                   
                    Feb  2 04:20:52 bna-pwr-pi-01 upsmon[1287]: UPS [bnajaredrouter@localhost]: connect failed: Connection failure: Connection refused
                    Feb  2 04:20:57 bna-pwr-pi-01 upsmon[1287]: UPS [bnajaredrouter@localhost]: connect failed: Connection failure: Connection refused
                    Feb  2 04:21:02 bna-pwr-pi-01 upsmon[1287]: UPS [bnajaredrouter@localhost]: connect failed: Connection failure: Connection refused
                    Feb  2 04:21:07 bna-pwr-pi-01 upsmon[1287]: UPS [bnajaredrouter@localhost]: connect failed: Connection failure: Connection refused
                    
                    1 Reply Last reply Reply Quote 0
                    • JaredBuschJ
                      JaredBusch
                      last edited by JaredBusch

                      Manually executed sudo start upsd and got a driver error. Fixed typo, rebooted, and it is online.

                      pi@bna-pwr-pi-01:~ $ tail -f /var/log/syslog
                      Feb  2 04:27:24 bna-pwr-pi-01 systemd[1]: Reached target Graphical Interface.
                      Feb  2 04:27:24 bna-pwr-pi-01 systemd[1]: Starting Update UTMP about System Runlevel Changes...
                      Feb  2 04:27:24 bna-pwr-pi-01 upsmon[737]: Init SSL without certificate database
                      Feb  2 04:27:24 bna-pwr-pi-01 systemd[1]: Started Update UTMP about System Runlevel Changes.
                      Feb  2 04:27:24 bna-pwr-pi-01 systemd[1]: Startup finished in 2.052s (kernel) + 10.336s (userspace) = 12.389s.
                      Feb  2 04:27:24 bna-pwr-pi-01 upsd[733]: User bnaupsmon@::1 logged into UPS [bnajaredrouter]
                      Feb  2 04:27:31 bna-pwr-pi-01 dhcpcd[698]: wlan0: no IPv6 Routers available
                      Feb  2 04:27:51 bna-pwr-pi-01 systemd[1]: Time has been changed
                      Feb  2 04:27:52 bna-pwr-pi-01 upsd[733]: Data for UPS [bnajaredrouter] is stale - check driver
                      Feb  2 04:27:52 bna-pwr-pi-01 upsd[733]: UPS [bnajaredrouter] data is no longer stale
                      
                      pi@bna-pwr-pi-01:~ $ sudo upsc bnajaredrouter
                      Init SSL without certificate database
                      battery.charge: 100
                      battery.charge.low: 10
                      battery.charge.warning: 50
                      battery.date: 2001/09/25
                      battery.mfr.date: 2010/12/15
                      battery.runtime: 14100
                      battery.runtime.low: 120
                      battery.type: PbAc
                      battery.voltage: 27.3
                      battery.voltage.nominal: 24.0
                      device.mfr: American Power Conversion
                      device.model: Back-UPS BR1000G
                      device.serial: 3B1051X20349  
                      device.type: ups
                      driver.name: usbhid-ups
                      driver.parameter.pollfreq: 30
                      driver.parameter.pollinterval: 2
                      driver.parameter.port: auto
                      driver.version: 2.7.2
                      driver.version.data: APC HID 0.95
                      driver.version.internal: 0.38
                      input.sensitivity: medium
                      input.transfer.high: 147
                      input.transfer.low: 88
                      input.voltage: 126.0
                      input.voltage.nominal: 120
                      ups.beeper.status: disabled
                      ups.delay.shutdown: 20
                      ups.firmware: 868.L1 .D
                      ups.firmware.aux: L1  
                      ups.load: 3
                      ups.mfr: American Power Conversion
                      ups.mfr.date: 2010/12/15
                      ups.model: Back-UPS BR1000G
                      ups.productid: 0002
                      ups.realpower.nominal: 600
                      ups.serial: 3B1051X20349  
                      ups.status: OL
                      ups.test.result: No test initiated
                      ups.timer.reboot: 0
                      ups.timer.shutdown: -1
                      ups.vendorid: 051d
                      pi@bna-pwr-pi-01:~ $ 
                      
                      1 Reply Last reply Reply Quote 0
                      • JaredBuschJ
                        JaredBusch
                        last edited by

                        Unplugged the power and had some good news, some bad.

                        The WALL commands spammed me, so I know things fired.

                        The syslog though showed an error about permissions for PIPE/LOCK. See the message about failed to connect to parent.

                        Feb  2 04:32:33 bna-pwr-pi-01 upsmon[737]: UPS bnajaredrouter@localhost on battery
                        Feb  2 04:32:41 bna-pwr-pi-01 upssched[805]: Failed to connect to parent and failed to create parent: No such file or directory
                        Feb  2 04:33:03 bna-pwr-pi-01 upsmon[737]: UPS bnajaredrouter@localhost on line power
                        Feb  2 04:33:03 bna-pwr-pi-01 upssched[811]: Executing command: ongrid
                        Feb  2 04:33:03 bna-pwr-pi-01 upssched-cmd: The UPS is now on grid power.
                        

                        Time to check the permissions.

                        1 Reply Last reply Reply Quote 0
                        • JaredBuschJ
                          JaredBusch
                          last edited by

                          Well that would be a problem. Where did the file I made go? I guess the default directory was a bad choice?

                          pi@bna-pwr-pi-01:~ $ sudo ls -l /var/run/nut/
                          total 12
                          -rw-r--r-- 1 nut  nut  4 Feb  2 04:27 upsd.pid
                          -rw-r--r-- 1 root root 4 Feb  2 04:27 upsmon.pid
                          srw-rw---- 1 nut  nut  0 Feb  2 04:27 usbhid-ups-bnajaredrouter
                          -rw-r--r-- 1 nut  nut  4 Feb  2 04:27 usbhid-ups-bnajaredrouter.pid
                          pi@bna-pwr-pi-01:~ $ 
                          
                          JaredBuschJ travisdh1T 2 Replies Last reply Reply Quote 0
                          • JaredBuschJ
                            JaredBusch
                            last edited by JaredBusch

                            and there there we go.. changed directory to /etc/nut/upssched for PIPE/LOCK (already corrected instructions above)

                            WALL spam...

                            Broadcast message from nut@bna-pwr-pi-01 (somewhere) (Thu Feb  2 04:44:19 2017)
                                                                                                           
                            UPS bnajaredrouter@localhost on battery                                        
                                                                                       
                            Broadcast message from nut@bna-pwr-pi-01 (somewhere) (Thu Feb  2 04:45:19 2017)
                                                                                                           
                            UPS bnajaredrouter@localhost on line power                   
                            

                            and the SYSLOG showing the trigger and the command from the shell script.

                            Feb  2 04:43:24 bna-pwr-pi-01 upsmon[917]: Startup successful
                            Feb  2 04:43:24 bna-pwr-pi-01 upsmon[918]: Init SSL without certificate database
                            Feb  2 04:43:24 bna-pwr-pi-01 systemd[1]: nut-monitor.service: Supervising process 918 which is not our child. We'll most likely not notice when it exits.
                            Feb  2 04:43:24 bna-pwr-pi-01 systemd[1]: Started Network UPS Tools - power device monitor and shutdown controller.
                            Feb  2 04:43:24 bna-pwr-pi-01 upsd[733]: User bnaupsmon@::1 logged into UPS [bnajaredrouter]
                            Feb  2 04:44:19 bna-pwr-pi-01 upsmon[918]: UPS bnajaredrouter@localhost on battery
                            Feb  2 04:44:19 bna-pwr-pi-01 upssched[929]: Timer daemon started
                            Feb  2 04:44:19 bna-pwr-pi-01 upssched[929]: New timer: onbattwarn (30 seconds)
                            Feb  2 04:44:49 bna-pwr-pi-01 upssched[929]: Event: onbattwarn
                            Feb  2 04:44:49 bna-pwr-pi-01 upssched-cmd: The UPS has been on battery power for 30 seconds.
                            Feb  2 04:45:04 bna-pwr-pi-01 upssched[929]: Timer queue empty, exiting
                            Feb  2 04:45:19 bna-pwr-pi-01 upsmon[918]: UPS bnajaredrouter@localhost on line power
                            Feb  2 04:45:19 bna-pwr-pi-01 upssched[937]: Executing command: ongrid
                            Feb  2 04:45:19 bna-pwr-pi-01 upssched-cmd: The UPS is now on grid power.
                            
                            1 Reply Last reply Reply Quote 0
                            • JaredBuschJ
                              JaredBusch
                              last edited by JaredBusch

                              No able to test email right now because there is no way to send SMTP port 25 from my house. I have a VPN to the colo up, and there is a mail relay running there, but it will not accept from outside its LAN.

                              So I will have to set that up later.

                              1 Reply Last reply Reply Quote 0
                              • JaredBuschJ
                                JaredBusch
                                last edited by

                                I'll rewrite this as an actual how to in the next few days.

                                1 Reply Last reply Reply Quote 1
                                • JaredBuschJ
                                  JaredBusch @JaredBusch
                                  last edited by

                                  @JaredBusch said in Pi as a UPS monitor:

                                  Well that would be a problem. Where did the file I made go? I guess the default directory was a bad choice?

                                  pi@bna-pwr-pi-01:~ $ sudo ls -l /var/run/nut/
                                  total 12
                                  -rw-r--r-- 1 nut  nut  4 Feb  2 04:27 upsd.pid
                                  -rw-r--r-- 1 root root 4 Feb  2 04:27 upsmon.pid
                                  srw-rw---- 1 nut  nut  0 Feb  2 04:27 usbhid-ups-bnajaredrouter
                                  -rw-r--r-- 1 nut  nut  4 Feb  2 04:27 usbhid-ups-bnajaredrouter.pid
                                  pi@bna-pwr-pi-01:~ $ 
                                  

                                  @scottalanmiller what would be the 'proper' place for these files?

                                  JaredBuschJ 1 Reply Last reply Reply Quote 0
                                  • travisdh1T
                                    travisdh1 @JaredBusch
                                    last edited by

                                    @JaredBusch said in Pi as a UPS monitor:

                                    Well that would be a problem. Where did the file I made go? I guess the default directory was a bad choice?

                                    pi@bna-pwr-pi-01:~ $ sudo ls -l /var/run/nut/
                                    total 12
                                    -rw-r--r-- 1 nut  nut  4 Feb  2 04:27 upsd.pid
                                    -rw-r--r-- 1 root root 4 Feb  2 04:27 upsmon.pid
                                    srw-rw---- 1 nut  nut  0 Feb  2 04:27 usbhid-ups-bnajaredrouter
                                    -rw-r--r-- 1 nut  nut  4 Feb  2 04:27 usbhid-ups-bnajaredrouter.pid
                                    pi@bna-pwr-pi-01:~ $ 
                                    

                                    If it's me, that's normally forgetting to add the sudo before my favorite text editor. Anything in /dev or /etc requires root privilege. Besides that, dunno. Nano complains at you saying "read only" when you try to save.... yeah, seen that a few more times than I can count.

                                    1 Reply Last reply Reply Quote 0
                                    • JaredBuschJ
                                      JaredBusch @JaredBusch
                                      last edited by

                                      @JaredBusch said in Pi as a UPS monitor:

                                      @JaredBusch said in Pi as a UPS monitor:

                                      Well that would be a problem. Where did the file I made go? I guess the default directory was a bad choice?

                                      pi@bna-pwr-pi-01:~ $ sudo ls -l /var/run/nut/
                                      total 12
                                      -rw-r--r-- 1 nut  nut  4 Feb  2 04:27 upsd.pid
                                      -rw-r--r-- 1 root root 4 Feb  2 04:27 upsmon.pid
                                      srw-rw---- 1 nut  nut  0 Feb  2 04:27 usbhid-ups-bnajaredrouter
                                      -rw-r--r-- 1 nut  nut  4 Feb  2 04:27 usbhid-ups-bnajaredrouter.pid
                                      pi@bna-pwr-pi-01:~ $ 
                                      

                                      @scottalanmiller what would be the 'proper' place for these files?

                                      Hey @scottalanmiller you never answered this one. What is 'proper' for this kinda thing.

                                      travisdh1T 1 Reply Last reply Reply Quote 0
                                      • travisdh1T
                                        travisdh1 @JaredBusch
                                        last edited by

                                        @JaredBusch said in Pi as a UPS monitor:

                                        @JaredBusch said in Pi as a UPS monitor:

                                        @JaredBusch said in Pi as a UPS monitor:

                                        Well that would be a problem. Where did the file I made go? I guess the default directory was a bad choice?

                                        pi@bna-pwr-pi-01:~ $ sudo ls -l /var/run/nut/
                                        total 12
                                        -rw-r--r-- 1 nut  nut  4 Feb  2 04:27 upsd.pid
                                        -rw-r--r-- 1 root root 4 Feb  2 04:27 upsmon.pid
                                        srw-rw---- 1 nut  nut  0 Feb  2 04:27 usbhid-ups-bnajaredrouter
                                        -rw-r--r-- 1 nut  nut  4 Feb  2 04:27 usbhid-ups-bnajaredrouter.pid
                                        pi@bna-pwr-pi-01:~ $ 
                                        

                                        @scottalanmiller what would be the 'proper' place for these files?

                                        Hey @scottalanmiller you never answered this one. What is 'proper' for this kinda thing.

                                        Should be /etc/nut. The standard is to put config files in /etc. /dev is generally hardware devices.

                                        1 Reply Last reply Reply Quote 1
                                        • gjacobseG
                                          gjacobse
                                          last edited by

                                          @JaredBusch

                                          You could add this little display on your Pi for local status-

                                          https://www.adafruit.com/product/3527?utm_source=youtube&utm_medium=videodescrip&utm_campaign=newproducts

                                          https://cdn-shop.adafruit.com/970x728/3527-04.jpg

                                          scottalanmillerS travisdh1T 2 Replies Last reply Reply Quote 0
                                          • scottalanmillerS
                                            scottalanmiller @gjacobse
                                            last edited by

                                            @gjacobse I like that, it's cute.

                                            1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 3
                                            • 4
                                            • 5
                                            • 6
                                            • 2 / 6
                                            • First post
                                              Last post