#!/bin/sh ########################### # Description: This script is used to handle the different events for the # apcupsd service on server.test.com. # Required files: apcupsd, shutdown, sh, echo, wall, hostname, cut, printf, # msmtp # Last modified: November 21, 2009 ########################### ########################### # Set variables ########################### prefix=/usr/local exec_prefix=${prefix} apcpid=/var/run/apcupsd.pid apcupsd=/usr/local/sbin/apcupsd shutdown=/sbin/shutdown scriptshell=/bin/sh scriptdir=/usr/local/etc/apcupsd echo=/bin/echo wall=/usr/bin/wall hostname=/bin/hostname cut=/usr/bin/cut host=`$hostname | $cut -d . -f 1` printf=/usr/bin/printf from="Test " recipient="networkalerts@test.com" msmtp=/usr/local/bin/msmtp msmtp_config=/var/etc/msmtp.conf if [ -f $scriptdir/${1} -a -x $scriptdir/${1} ] then $scriptdir/${1} ${2} ${3} ${4} # exit code 99 means he does not want us to do default action if [ $? = 99 ] ; then exit 0 fi fi case "$1" in killpower) printf "UPS now committed to shut down" | wall # echo "Apccontrol doing: $apcupsd --killpower" # sleep 10 # $apcupsd --killpower ;; commfailure) description="Warning communications lost with UPS ${2}." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; commok) description="Communications restored with UPS ${2}." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; powerout) ;; onbattery) description="Power failure on UPS ${2}. Running on batteries." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; offbattery) description="Power has returned on UPS ${2}." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; mainsback) if [ -f /var/run/powerfail ] ; then description="Continuing with shutdown." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t fi ;; failing) description="Battery power exhausted on UPS ${2}. Doing shutdown." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; timeout) description="Battery time limit exceeded on UPS ${2}. Doing shutdown." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; loadlimit) description="Remaining battery charge below limit on UPS ${2}. Doing shutdown." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; runlimit) description="Remaining battery runtime below limit on UPS ${2}. Doing shutdown." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; doreboot) description="UPS ${2} initiating reboot sequence." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t $shutdown -r now "apcupsd UPS ${2} initiated reboot" ;; doshutdown) description="UPS ${2} initiated shutdown sequence." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t $shutdown -h now "apcupsd UPS ${2} initiated shutdown" ;; annoyme) description="Power problems with UPS ${2}. Please logoff." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; emergency) description="Emergency shutdown. Possible battery failure on UPS ${2}." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; changeme) description="Emergency! Batteries have failed on UPS ${2}. Change them now!" $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; remotedown) description="Remote shutdown. Beginning shutdown sequence." $echo $description | $wall body="$description" subject="Network Alert: Apcupsd event (${1}) on $host" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; startselftest) ;; endselftest) ;; battdetach) ;; battattach) ;; *) echo "Usage: ${0##*/} command" echo " warning: this script is intended to be launched by" echo " apcupsd and should never be launched by users." exit 1 ;; esac