#!/bin/ksh
#
# $Id: apcupsd.ksh,v 1.3 2008/06/03 02:04:54 mike Exp $
#
# Hobbit Monitor Client apcupsd Test
# - Allows the Hobbit client to test the APC UPSd service on the host.
# - Test via "DEBUG=y bbcmd apcupsd.ksh"
#
# On the Hobbit SERVER:
# Add to columndoc.csv:
# power;The power column shows the status of the UPS.;
#
#
# On the Hobbit CLIENT:
# Add to clientlaunch.cfg:
# [apcupsd]
# ENVFILE $HOBBITCLIENTHOME/etc/hobbitclient.cfg
# CMD $HOBBITCLIENTHOME/ext/apcupsd.ksh
# LOGFILE $HOBBITCLIENTHOME/logs/apcupsd.log
# INTERVAL 5m
#
# EXIT CODE:
# 0 = success
# 1 = print_help function (or incorrect commandline)
# 2 = ERROR: Must be root.
# 9 = ERROR: Missing Hobbit environment. Please use bbcmd.
#
AUTHOR="Mike Arnold "
VERSION=20080602
LOCATION="http://www.razorsedge.org/~mike/software/apcupsd.ksh"
#
if [ $DEBUG ]; then set -x; fi
#
##### START CONFIG ###################################################
# Thresholds for UPS load (in %).
upsLoadOutWARN=70
upsLoadOutPANIC=90
# Threshold for battery runtime (in minutes).
upsMinsRunTimePANIC=15
# Threshold for time on battery (in seconds).
upsSecsOnBattPANIC=300
# Thresholds for line voltage (in volts).
upsVoltageInWARNLOW=110
upsVoltageInWARNHIGH=127
upsVoltageInPANIC=135
# Thresholds for internal temperature (in °F).
upsTempInWARN=99
upsTempInPANIC=105
##### STOP CONFIG ####################################################
PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin:/usr/local/sbin:${PATH}
COLUMN=power
COLOR=clear
# Function to print the help screen.
print_help () {
print "Usage: $1"
print " $1 [-h|--help]"
print " $1 [-v|--version]"
print " ex. $1"
exit 1
}
# Function to check for root priviledges.
check_root () {
if [[ `$ID | $AWK -F= '{print $2}' | $AWK -F"(" '{print $1}' 2>/dev/null` -ne 0 ]]; then
print "You must have root priviledges to run this program."
exit 2
fi
}
# If the variable DEBUG is set, then turn on tracing.
# http://www.research.att.com/lists/ast-users/2003/05/msg00009.html
if [ $DEBUG ]; then
# This will turn on the ksh xtrace option for mainline code
set -x
# This will turn on the ksh xtrace option for all functions
typeset +f |
while read F junk
do
typeset -ft $F
done
unset F junk
fi
# Process arguments.
while [[ $1 = -* ]]; do
case $1 in
-h|--help)
print_help "$(basename $0)"
;;
-v|--version)
print "\tHobbit Monitor Client apcupsd Test"
print "\t$LOCATION"
print "\tVersion: $VERSION"
print "\tWritten by: $AUTHOR"
exit 0
;;
*)
print_help "$(basename $0)"
;;
esac
shift
done
# Check to see if we have no parameters.
#if [[ ! $# -ge 1 ]]; then print_help "$(basename $0)"; fi
# Lets not bother continuing unless we have the privs to do something.
#check_root
# main
# If we are running outside of the Hobbit environment, then complain.
if [ -z "$HOBBITCLIENTHOME" ]; then
echo "ERROR: Missing Hobbit environment. Please use bbcmd."
echo ' Test via "DEBUG=y bbcmd apcupsd.ksh"'
exit 9
fi
APCOUT=$(apcaccess status)
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
upsModel=`echo "$APCOUT" | $AWK -F: '$1~/^MODEL/{print $2}' | $SED -e 's|^ *||'`
upsOutStat=`echo "$APCOUT" | $AWK -F: '$1~/^STATUS/{print $2}' | $SED -e 's|^ *||'`
upsBattStat=unknown
upsMinsRunTime=`echo "$APCOUT" | $AWK -F: '$1~/^TIMELEFT/{print $2}' | $SED -e 's|^ *||'`
upsBattCap=`echo "$APCOUT" | $AWK -F: '$1~/^BCHARGE/{print $2}' | $SED -e 's|^ *||'`
upsLoadOut=`echo "$APCOUT" | $AWK -F: '$1~/^LOADPCT/{print $2}' | $SED -e 's|^ *||' -e 's| Load Capacity||'`
upsVoltageIn=`echo "$APCOUT" | $AWK -F: '$1~/^LINEV/{print $2}' | $SED -e 's|^ *||'`
upsVoltageOut=`echo "$APCOUT" | $AWK -F: '$1~/^OUTPUTV/{print $2}' | $SED -e 's|^ *||'`
if [ -z "$upsVoltageOut" ]; then upsVoltageOut=unknown; fi
upsFailCause=`echo "$APCOUT" | $AWK -F: '$1~/^LASTXFER/{print $2}' | $SED -e 's|^ *||'`
upsSecsOnBatt=`echo "$APCOUT" | $AWK -F: '$1~/^TONBATT/{print $2}' | $SED -e 's|^ *||'`
upsTempIn=`echo "$APCOUT" | $AWK -F: '$1~/^ITEMP/{print $2}' | $SED -e 's|^ *||'i -e 's| Internal||'`
DEG=`echo $upsTempIn | $AWK '{print $2}'`
upsTempIn=`echo $upsTempIn | $AWK '{print $1}'`
if [ -z "$DEG" ]; then DEG=F; fi
if [ -z "$upsTempIn" ]; then upsTempIn=0; fi
#BATTV=`echo "$APCOUT" | $AWK -F: '$1~/^BATTV/{print $2}' | $SED -e 's|^ *||'i -e 's| Volts||'`
#NOMBATTV=`echo "$APCOUT" | $AWK -F: '$1~/^NOMBATTV/{print $2}' | $SED -e 's|^ *||'i -e 's| Volts||'`
# Convert C to F.
if [ "$DEG" == C ]; then
upsTempIn=`echo $upsTempIn 1.8 \* 32 + p | dc`
DEG=F
fi
# Set COLOR based on UPS status.
case $upsOutStat in
ONLINE*)
COLOR=green ;;
ONBATT*)
COLOR=red ;;
*)
COLOR=red ;;
esac
# Turn red if runtime is less than upsMinsRunTimePANIC.
if [ `echo $upsMinsRunTime | $AWK -F. '{print $1}'` -le "$upsMinsRunTimePANIC" ]; then
COLOR=red
fi
# Turn red if time on battery is more than upsSecsOnBattPANIC.
if [ `echo $upsSecsOnBatt | $AWK '{print $1}'` -ge "$upsSecsOnBattPANIC" ]; then
COLOR=red
fi
TMP=`echo $upsLoadOut | $AWK -F. '{print $1}'`
# Turn red if ups load is more than upsLoadOutPANIC.
if [ "$TMP" -ge "$upsLoadOutPANIC" ]; then
COLOR=red
# Turn yellow if ups load is more than upsLoadOutWARN.
elif [ "$TMP" -ge "$upsLoadOutWARN" ]; then
[ $COLOR != red ] && COLOR=yellow
fi
# Turn red if incomming voltage is more than upsVoltageInPANIC.
TMP=`echo $upsVoltageIn | $AWK -F. '{print $1}'`
if [ "$TMP" -ge "$upsVoltageInPANIC" ]; then
COLOR=red
# Turn yellow if incomming voltage is more than upsVoltageInWARNHIGH or less
# than upsVoltageInWARNLOW.
elif [ "$TMP" -ge "$upsVoltageInWARNHIGH" -o "$TMP" -le "$upsVoltageInWARNLOW" ]; then
[ $COLOR != red ] && COLOR=yellow
fi
TMP=`echo $upsTempIn | $AWK -F. '{print $1}'`
# Turn red if ups temperature is more than upsTempInPANIC.
if [ "$TMP" -ge "$upsTempInPANIC" ]; then
COLOR=red
# Turn yellow if ups temperature is more than upsTempInWARN.
elif [ "$TMP" -ge "$upsTempInWARN" ]; then
[ $COLOR != red ] && COLOR=yellow
fi
MSG="
UPS status:
Vendor: apc
Model: $upsModel
UPS Status: $upsOutStat
Battery Status: $upsBattStat
Runtime Remaining: $upsMinsRunTime
Battery Capacity: $upsBattCap
UPS Load: $upsLoadOut
Voltage in: $upsVoltageIn
Voltage out: $upsVoltageOut
Last failure due to: $upsFailCause
Time on battery: $upsSecsOnBatt
UPS Temperature: $upsTempIn °${DEG}
"
else
MSG="
apcupsd NIS server NOT available!
"
fi
# Send the message to the Hobbit server.
if [ $DEBUG ]; then
echo $BB $BBDISP "\"status $MACHINE.$COLUMN $COLOR $($DATE) $MSG"\"
else
$BB $BBDISP "status $MACHINE.$COLUMN $COLOR $($DATE) $MSG"
fi
exit 0