Rob de Jonge

Q: Scripting UPS status

I'm looking to update some of the bulky and convoluted scripts I wrote to monitor my UPS and report on events. The existing scripts use frequent execution to poll status, temporary files to communicate several valuables between one another, etc. With a fresh new battery just installed, and some time on my hands, I'm now looking to update the situation.

 

Here is what I am looking for ...

 

  1. A command-line tool that can tell me what the current power source is
  2. A command-line tool which can tell me the configured values to trigger shutdown
  3. A command-line tool that can tell me the status of the UPS: charge level, minutes left at current load, etc.
  4. A way of knowing when there is a change in power source, that does not require polling

 

Now, apcupsd provides all of the above. However, I found it to be an unreliable piece of software that was prone to crashing. After a few weeks of trying to understand and predict what triggered crashes, I gave up and removed it from my system.

 

There is a tool pmset which can help me with 1-3 above. I have no idea how resource-intensive it would be for me to run this and process the output it provides, but given that it ships with OS X I guess it is the best I can get. Are there better alternatives?

 

The most interesting for me is to be notified when power goes down. This is where apcupsd shined, as it triggers customisable scripts upon a power event. Unfortunately, well, see above. So the only option I have right now is to keep running commands like pmset -g | grep \* | awk '{ print $1 }' every 10 or 30 seconds to get an idea of what the current power source is, and triggering an alert if that was different from the previous run of the script. Not a very elegant solution. Hoping somebody has a better alternative!

 

Much appreciate any feedback.

Mac mini, Mac OS X (10.7), 1.83GHz Core2Duo, 3GB RAM, 80GB HDD

Posted on Apr 21, 2016 8:21 AM

Close

Q: Scripting UPS status

  • All replies
  • Helpful answers

  • by Hiroto,

    Hiroto Hiroto May 6, 2016 3:19 AM in response to Rob de Jonge
    Level 5 (7,281 points)
    May 6, 2016 3:19 AM in response to Rob de Jonge

    Hello

     

    It's been a while but there's a simple way to observe power source changes by means of pmset's built-in facility, that is:

     

     

    pmset -g pslog
    

     

     

     

    You may execute the command in Terminal and see what it does.

     

    Here's a sample bash script to use this facility via fifo, as a proof of concept. You may change code in while loop to whatever you see fit.

     

    To use it, save the script as, e.g., ps_observer.sh and run it in Terminal. It will run in background and create ~/Desktop/pmset_temp directory and create fifo there. When "pmset -g pslog" prints something, "while read" loop reads the output and process it. To stop the script, kill pmset running in background subshell and the fifo will be closed and removed and "while read" loop will exit.

     

     

    #!/bin/bash
    
    DIR=~/Desktop/pmset_temp
    LOG="$DIR"/pmset_log.txt
    
    mkdir -p "$DIR" && cd "$DIR" || exit
    [[ -p fifo ]] || mkfifo fifo || exit
    
    while read t
    do
        echo "$t" >> "$LOG"
        [[ $t =~ ^Currently\ drawing\ from\ \'(.*)\' ]] || continue
        ps=${BASH_REMATCH[1]}
        if [[ $ps == 'AC Power' ]]
        then
            echo 'AC'
        elif [[ $ps == 'Battery Power' ]]
        then
            echo 'Battery'
        elif [[ $ps == 'UPS Power' ]]
        then
            echo 'UPS'
        else
            echo $ps
        fi
        [[ $ps != $ps1 ]] && printf 'Power source changed: %s => %s\n' "$ps1" "$ps"
        ps1=$ps
    done <fifo &
    
    (
        exec 3>fifo
        pmset -g pslog >&3
        exec 3>&-
        rm fifo
    ) &
    

     

     

     

    Briefly tested under OS X 10.6.8.

     

    * The text 'UPS Power' may not be correct, for I have no UPS and cannot test it.

     

    Good luck,

    H