#!/bin/bash
#
# Check USP Metrics for APC UPS
# Written by Senthil Nathan
# Last Modified: June 26th 2021
#
# Usage: ./check_ups_metrics -w Time Left Warn -c Time Left Critical
#
# Description: Check UPS Metrics and warn on remaining minutes
#
# Output:
#
#
#
#
PROGNAME=`/usr/bin/basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="1.0"
. $PROGPATH/utils.sh
check_root()
{
# make sure script is running as root
if [ `whoami` != root ]; then
echo "UNKNOWN: please make sure script is running as root"
exit $STATE_UNKNOWN
fi
}
print_usage() {
echo "Usage: $PROGNAME -w <warning minutes> -c <critical minutes>"
echo "Usage: $PROGNAME --help"
echo "Usage: $PROGNAME --version"
}
print_revision() {
echo "Program: $PROGNAME"
echo "Version: $REVISION"
}
print_help() {
print_revision
echo ""
print_usage
echo ""
echo "Record UPS Metrics and Alert onf remaining Time"
echo ""
}
# Check user is root
#check_root
# Make sure the correct number of command line
# arguments have been supplied
if [ $# -lt 1 ]; then
print_usage
exit $STATE_UNKNOWN
fi
# Grab the command line arguments
exitstatus=$STATE_WARNING #default
while test -n "$1"; do
case "$1" in
--help)
print_help
exit $STATE_OK
;;
-h)
print_help
exit $STATE_OK
;;
--version)
print_revision
exit $STATE_OK
;;
-V)
print_revision
exit $STATE_OK
;;
--warning)
thewarn=$2
shift
;;
-w)
thewarn=$2
shift
;;
--critical)
thecrit=$2
shift
;;
-c)
thecrit=$2
shift
;;
*)
echo "Unknown argument: $1"
print_usage
exit $STATE_UNKNOWN
;;
esac
shift
done
# Validate arguments
if [ -z $thecrit ]; then
print_usage
exit $STATE_UNKNOWN
fi
if [ -z $thewarn ]; then
print_usage
exit $STATE_UNKNOWN
fi
# Check begins here
#
UPS_STATUS=`/sbin/apcaccess status`
if [ $? -eq 1 ]; then
echo "UPS Status check error!"
exit $STATE_UNKNOWN
fi
#
UPS_INFO=
UPS_INFO1=
UPS_M_BATTV="V;22;20;0;30"
UPS_M_LINEV="V;130;140;0;150"
UPS_M_LOADPCT="%;80;90;0;100"
UPS_M_BCHARGE="%;;;0;100"
UPS_M_TIMELEFT="M;20;10;0;60"
UPS_M_MBATTCHG="%;20;10;0;100"
#
for UPS_METRIC in BATTV LINEV LOADPCT BCHARGE TIMELEFT MBATTCHG
do
UPS_METRIC_VAL=`echo "$UPS_STATUS"|grep "^$UPS_METRIC"|awk '{print $3}'`
#echo "$UPS_METRIC=$UPS_METRIC_VAL"
declare $UPS_METRIC=$UPS_METRIC_VAL
UPS_INFO="$UPS_INFO $UPS_METRIC=$UPS_METRIC_VAL"
UPS_M_VAR=UPS_M_$UPS_METRIC
UPS_INFO1="$UPS_INFO1 $UPS_METRIC=$UPS_METRIC_VAL${!UPS_M_VAR}"
done
if (( $(echo "$TIMELEFT < $thecrit" | bc -l) )); then
UPS_STATE="UPS Critical :"
EXIT_STATUS=$STATE_CRITICAL
elif (( $(echo "$TIMELEFT < $thewarn" | bc -l) )); then
UPS_STATE="UPS Warning :"
EXIT_STATUS=$STATE_WARNING
else
UPS_STATE="UPS OK :"
EXIT_STATUS=$STATE_OK
fi
UPS_INFO="$UPS_STATE $UPS_INFO | $UPS_INFO1"
echo $UPS_INFO
exit $EXIT_STATUS