#!/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, date, printf, # msmtp # Last modified: July 6, 2010 ######################################## #################### # Set variables #################### prefix=/usr exec_prefix=/usr apcpid=/var/run/apcupsd.pid apcupsd=/sbin/apcupsd shutdown=/sbin/shutdown scriptshell=/bin/sh scriptdir=/etc/apcupsd echo=/bin/echo wall=/usr/bin/wall hostname=/bin/hostname host=`$hostname -s` date=/bin/date current_day=`$date +%m/%d/%y` current_time=`$date +%H:%M:%S` timestamp="$current_day $current_time" printf=/usr/bin/printf from="Test " recipient="networkalerts@test.com" msmtp=/usr/bin/msmtp msmtp_config=/etc/msmtprc 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) echo "Apccontrol doing: $apcupsd --killpower on UPS ${2}" | $wall sleep 10 $apcupsd --killpower echo "Apccontrol has done: $apcupsd --killpower on UPS ${2}" | $wall ;; commfailure) description="Warning communications lost with UPS ${2}." $echo "$description [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; commok) description="Communications restored with UPS ${2}." $echo "$description [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; # # powerout, onbattery, offbattery, mainsback events occur # in that order. # powerout) ;; onbattery) description="Power failure on UPS ${2}. Running on batteries." $echo "$description [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $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 [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; mainsback) if [ -f /etc/apcupsd/powerfail ] ; then description="Continuing with shutdown." $echo "$description [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $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 [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $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 [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $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 [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $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 [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; doreboot) description="UPS ${2} initiating reboot sequence." $echo "$description [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $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 [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $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 [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $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 [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $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 [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; remotedown) description="Remote shutdown. Beginning shutdown sequence." $echo "$description [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; startselftest) description="Self-test starting." $echo "$description [$timestamp]" | $wall ;; endselftest) description="Self-test completed." $echo "$description [$timestamp]" | $wall ;; battdetach) description="Battery disconnected." $echo "$description [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; battattach) description="Battery reattached." $echo "$description [$timestamp]" | $wall subject="Network Alert: Apcupsd event (${1}) on $host [$timestamp]" body="$timestamp $description" $printf "From: $from\nTo: $recipient\nSubject: $subject\n\n$body" | $msmtp --file=$msmtp_config -t ;; *) 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