#!/bin/sh
## $Id: check_apc,v 1.3 2004/05/21 00:05:28 janthony Exp $

## Nagios Plugin for apcupsd APC Smart-UPS
## janthony@negative1.org

## Copyright (C) 2004  Joe Anthony
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


APCA="/sbin/apcaccess"
APCA_ARGS="status"

#APCA="apcsnmp.pl"
#APCA_ARGS="public fs1"

WARN=$2
CRIT=$3

if [ ! -x $APCA ]; then
	echo "$0: Error: $APCA not found!"
	exit 3
fi

dishelp() {
		echo "Usage: $0 <load|bcharge|time> WARN_VALUE CIRT_VALUE "
		echo "       $0 status"
		echo
		echo "load (%): Warn when load is > WARN_VALUE, and CRIT when load is > CRIT_VALUE"
		echo "bcharge (%): Warn when charge is < WARN_VALUE, and CRIT when charge is < CRIT_VALUE"
		echo "time (minutes): Warn when time is < WARN_VALUE, and CRIT when time is < CRIT_VALUE"
		echo "staus: Crit if not online"
		echo
		echo "UNKNOWN UPS State"
		exit 3
}

checkargs() {
	if [ "$WARN" = '' -o "$CRIT" = '' ]; then
		dishelp
		exit 3
	fi
}

case "$1" in 
	load) 
		checkargs
		LOAD=`$APCA $APCA_ARGS | grep LOAD | awk '{print $3}' | sed -e 's/\.[0-9]* *$//g'`
		if [ "$LOAD" -gt "$CRIT" ]; then
			echo "UPS CRITICAL - Load:  ${LOAD}% > ${3}%"
			exit 2
		fi			
		if [ "$LOAD" -gt "$WARN" ]; then
			echo "UPS WARNING - Load:  ${LOAD}% > ${2}%"
			exit 1
		fi			
		echo "UPS OK - LOAD: ${LOAD}%"
		exit 0
		;;
	bcharge)
		checkargs
		CHRG=`$APCA $APCA_ARGS | grep BCHARGE | awk '{print $3}' | sed -e 's/\.[0-9]* *$//g'`
		if [ "$CHRG" -lt "$CRIT" ]; then
			echo "UPS CRITICAL - Battery Charge:  ${CHRG}% < ${3}%"
			exit 2
		fi			
		if [ "$CHRG" -lt "$WARN" ]; then
			echo "UPS WARNING - Battery Charge:  ${CHRG}% < ${2}%"
			exit 1
		fi			
		echo "UPS OK - Battery Charge: ${CHRG}%"
		exit 0
		;;
	time)
		checkargs
		TLEFT=`$APCA $APCA_ARGS | grep TIMELEFT | awk '{print $3}' | sed -e 's/\.[0-9]* *$//g'`
		if [ "$TLEFT" -lt "$CRIT" ]; then
			echo "UPS CRITICAL - Time left:  ${TLEFT} mins < ${3} mins"
			exit 2
		fi			
		if [ "$TLEFT" -lt "$WARN" ]; then
			echo "UPS WARNING - Time Left:  ${TLEFT} mins < ${2} mins"
			exit 1
		fi			
		echo "UPS OK - Time Left: ${TLEFT} mins"
		exit 0
		;;
	status)
		ONLINE=`$APCA $APCA_ARGS | grep STATUS | awk '{print $3}'`
		if [ "$ONLINE" != ONLINE ]; then
			echo "UPS CRITICAL - $ONLINE"
			exit 2
		fi
		echo "UPS OK - $ONLINE"
		exit 0
		;;
	*)
		dishelp
esac



