UPS shutdown script

No, this is not CakePHP code snippet! 🙂

So, I had experience with an UPS Inform GUARD Standby SERIES 1500AP recently. It took me a while to understand that this is not the right model for the server ups solution, but anyway, this give me opportunity to understand something for this part of the hardware which I haven’t had change to touch.

The whole problem is that this ups doesn’t have Auto restart functionality, so if the power is down I can shutdown it, but on the power-on it doesn’t start up automatically, so the next power crash the server is doomed.

According with this I couldn’t manage to find single package which could work properly with this UPS. The nut package clam to support it, but in fact the driver recommended as working was not working at all.

Finally I managed to get signals from the ups with powstatd which is quite low level software. Ok, there are some additional files, but they cannot work with the upstart in Ubuntu. What I managed to get from it was status change in a file with following states: OK, FAIL and LOW.

So, in order to turn these states in actions I just wrote a small bash script, which doing different things on each state change.

on FAIL – if there is timeout variable set, execute delayed shutdown and send mail to the admin for power fail. If there is no timeout just inform the admin.
on LOW – start immediate procedure for shutdown and sending mail to the admin for the low battery state
on OK – if the power is on, stop the shutdown procedure /if any/ and send mail to inform admins that everything seems to be ok.
Of course this is executed on every 60 seconds so you could set it as daemon easily.

I don’t know how handy is that script, but here it is:

#!/bin/bash

#setting don't touch
#getting the machine IP /if you manage more than one server/
IP=`/sbin/ifconfig | grep addr: | grep -v 127.0.0.1 | cut -f2 -d: | awk '{print $1}'`
#Time when the event occurred
NOW=`date +%Y-%m-%d\ %k:%M:%S`

#File settings
#Please don't change these except if it's required.
FILE=/etc/powerstatus
FILECHECK=/etc/powerstatus_check

#change these settings
#mail settings
MAIL=your@mail.com
MAILSUBJECT="status report for "$IP
MAILSTATUSOK="Power is back at "$NOW
MAILSTATUSFAIL="Power fail at "$NOW
MAILSTATUSLOW="Server is going down at "$NOW

# Set timeout if power fail and execute halt after this timeout
# note that if timeout is 0 halt is not executed and will be done when the power is low.
TIMEOUT=2

# end of settings

while [ 1 ]
do
    if [ ! -f "$FILE" ]; then
        echo 'stay cool'
    else
        ############################################
        #                                          #
        # reading content of the status file       #
        #                                          #
        ############################################
        STATUS=$(cat $FILE)

        ############################################
        #                                          #
        # reading content of the status check file #
        #                                          #
        ############################################
        if [ ! -f "$FILECHECK" ]; then 
            CHECKCONTENT="MISSING"
        else
            CHECKCONTENT=$(cat $FILECHECK)
        fi
   
        ############################################
        #                                          #
        # checking if the state is different       #
        #                                          #
        ############################################
        if [ $STATUS == $CHECKCONTENT ];
        then
            NEWSTATUS=false
        else
            NEWSTATUS=true
        fi
   
        ############################################
        #                                      #
        # writing the new status                   #
        #                                          #
        ############################################
        cp $FILE $FILECHECK
   
        if [ $STATUS = "OK" ]; then
            # Return to OK
            # cancel shutdown
            # delete $FILE
            if [ $NEWSTATUS = "true" ]; then
                # send mail
                echo "$MAILSTATUSOK" | /usr/bin/mail -s "$MAILSUBJECT" "$MAIL"
                /sbin/powstatd.ok
                # Delete status file and check file
                /bin/rm $FILE
                /bin/rm $FILECHECK
            fi
        elif [ $STATUS = "FAIL" ]; then
            # Power is down
            # send mail to admin, but don't do anything before TIMEOUT
            if [ $NEWSTATUS = true ]; then
                # send mail
                echo $MAILSTATUSFAIL | /usr/bin/mail -s "$MAILSUBJECT" "$MAIL"
                # check if TIMEOUT is set
                if [ $TIMEOUT -gt 0 ]; then
                    /sbin/powstatd.fail $TIMEOUT
                fi

            fi
        elif [ $STATUS = "LOW" ]; then
            # Execute immediate shutdown
            # send mail
            echo $MAILSTATUSLOW | /usr/bin/mail -s "$MAILSUBJECT" "$MAIL"
            /sbin/powstatd.low
        fi
    fi
   
    #loop for every 60 sec
        sleep 60
done

Here are the other related files, one of which is modified for me, in order to set proper timeout depending from settings in the main script.
powstatd.low

#!/bin/sh
/sbin/shutdown -c "Battery failure: rescheduling shutdown."
/sbin/shutdown -f -h now "Battery failure: no juice left!"
#!/bin/sh
/sbin/shutdown -c "Power restored: going off battery power."

powstatd.fail – modified by me

#!/bin/sh
/sbin/shutdown -f -h +$1 "Power failure: system operating on batteries." &

Hope it could help someone.

Share it:
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Yahoo! Buzz
  • Add to favorites
  • Identi.ca

One thought on “UPS shutdown script

  1. which driver have you tried?
    The one mentioned for “Inform AP” is powercom. But the fact is that rebranded units can change provider quite easily. Thus moving from Powercom to Centralion.

    My hint would be to try blazer_ser and report back to the NUT users list.

    cheers,
    Arnaud

Leave a Reply

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