Home Forums OS X Server and Client Discussion Questions and Answers How to harness UPS state change?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #369150
    PhillyMJS
    Participant

    I know I can edit /usr/libexec/upsshutdown to meet my needs and, for example, send a notification email that the computer is shutting down.

    However, I’d also like to be able to trigger a script when the UPS switches to battery power, and when electrical power is restored before any shutdown conditions are reached. This should be possible since the system pops up a warning dialog when the UPS switches to battery power, but I can’t seem to find anything about how to detect that to trigger my own stuff.

    TIA,

    ~Philly

    #369159
    PhillyMJS
    Participant

    Well, I have a tentative solution– now I need a fresh set of eyes to check out my work and maybe provide a nudge or two in the right direction. I also need to test and make sure this doesn’t interfere with the operation of the UPS and automatic shutdowns.

    I found a key in scutil that reports information about the UPS, including whether it’s running on AC power or battery. I then added to Kicker.xml, so it triggers a script when the UPS state changes.

    The addition to /System/Library/SystemConfiguration/Kicker.bundle/Contents/Resources/Kicker.xml:
    ———-
    <dict>
    <key>execCommand</key>
    <string>/Library/Management/reportpowersourcechange.sh</string>
    <key>execUID</key>
    <integer>0</integer>
    <key>keys</key>
    <array>
    <string>State:/IOKit/PowerSources/UPS0</string>
    </array>
    <key>name</key>
    <string>Power Source State</string>
    </dict>
    ———-

    The triggered script, reportpowersourcechange.sh:
    ———-
    #!/bin/bash
    # Returns “AC” or “Battery”
    currentpowersource=`echo “show State:/IOKit/PowerSources/UPS0” | scutil | grep “Power Source State” | awk ‘{ print $5 }’`

    #Compare the current power source with the one returned at last check, only act if it has changed
    if [ -f /Library/Management/lastpowersource ]
    then
    lastpowersource=`cat /Library/Management/lastpowersource`
    else
    lastpowersource=”AC”
    fi

    # If the power source has changed, log it.
    if [ $currentpowersource != $lastpowersource ]
    then
    message=”Power source is now running on $currentpowersource power.”
    logger -p local0.notice -i -t UPS $message
    #add code to send an email notification here
    echo $currentpowersource > /Library/Management/lastpowersource
    fi
    ———-

    The log entries:
    ———-
    May 28 22:10:13 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:10:14 server UPS[312]: Power source is now running on Battery power.
    May 28 22:10:18 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:10:24 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:10:28 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:10:58 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:11:03 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:11:08 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:11:13 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:11:14 server UPS[369]: Power source is now running on AC power.
    May 28 22:11:18 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:11:23 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:11:33 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:11:43 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:12:03 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:12:33 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    May 28 22:13:28 server configd[60]: executing /Library/Management/reportpowersourcechange.sh
    ———-

    Assuming this doesn’t mess up the ability of the UPS to shut the machine down when needed, the only thing I have left to figure out is how do I get the thing to stop filling my logs? The kicker apparently reports *every* change in UPS information, which is not what I want. All the entries you see above are most likely due to changes in the charge level of the UPS battery, as it drained slightly when I unplugged it, and as it charged back to 100% after I reconnected it to AC power. If it drains all the way, the logs will have a ton of entries, and I’d like to avoid that. I don’t see a way to use Kicker to monitor only one value in a dictionary instead of all of them.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Comments are closed