#! /bin/sh
#
# Nexsan ATAboy raid enclosure monitoring plugin for Nagios
# Written by Birger Wathne (birger@birger.sh)
# Last Modified: 20050117
#
# Usage: ./check_ataboy -H <host_address>
#
# Description:
#
# This plugin will fetch the file http://<host_address>/confdmp2.asp
# from the ataboy. This file is then parsed for error messages.
# Nexsan won't say anything about the syntax of this file, except that
# any errors on the system should appear between 'Current Errors' and
# the next header.
#
# Output:
#
# 
# Examples:
#

# Paths to commands used in this script.  These
# may have to be modified to match your system setup.

PATH=""

GETOPT="/usr/bin/getopt"
WGET="/usr/bin/wget"
RM="/bin/rm"
EGREP="/bin/egrep"
SED="/bin/sed"
EXPR="/usr/bin/expr"

#ECHO="/bin/echo"
#DIFF="/usr/bin/diff"
#TAIL="/usr/bin/tail"
#CAT="/bin/cat"

PROGNAME=`/bin/basename $0`
PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$Revision: 1.0 $' | /bin/sed -e 's/[^0-9.]//g'`

. $PROGPATH/utils.sh

print_usage() {
    echo "Usage: $PROGNAME -H <host_address> [-u <username>] [-p <password]"
    echo "       [-t <timeout>]"
    echo "       $PROGNAME (-h | --help) for detailed help"
    echo "       $PROGNAME (-V | --version) for version information"
    echo "       $PROGNAME (-u | --user) username on ataboy (USER)"
    echo "       $PROGNAME (-p | --password) password on ataboy (PASSWORD)"
    echo "       $PROGNAME (-t | --timeout) timeout while getting data (10 sec)"
    echo ""
}

print_help() {
    print_revision $PROGNAME $REVISION
    echo ""
    echo "Nexsan ATAboy raid enclosure monitoring plugin for Nagios"
    echo ""
    print_usage
    echo ""
    echo "Options:"
    echo " -h, --help"
    echo "    Print detailed help screen"
    echo " -V, --version"
    echo "    Print version information"
    echo " -H, --hostname=HOST"
    echo "    host to monitor"
    echo " -t, --timeout=INTEGER"
    echo "    Seconds before connection times out (default: 10)"
    echo " -u, --user=STRING"
    echo "    Username on ATAboy"
    echo " -p, --password=STRING"
    echo "    Password on ATAboy"
    echo ""
    support
}

TEMP=`$GETOPT -o hVH:t:u:p: --long help,version,hostname:,timeout:,user:,password: -n "$PROGNAME" -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

eval set -- "$TEMP"

#default values
hostname=""
timeout=10
user="USER"
passwd="PASSWORD"
tmpfile="/tmp/${PROGNAME}.tmp.$$"

while true ; do
        case "$1" in
		-h|--help)
			print_help
			shift
			exit $STATE_OK
			;;
		-V|--version)
			print_revision $PROGNAME $VERSION
			shift
			exit $STATE_OK
			;;
		-H|--hostname)
			hostname=$2
			shift 2
			;;
		-t|--timeout)
			hostname=$2
			shift 2
			;;
		-u|--user)
			user=$2
			shift 2
			;;
		-p|--password)
			passwd=$2
			shift 2
			;;
		--)
			shift
			break
			;;

		*)
			echo "Unknown argument: $1"
			print_usage
			exit $STATE_UNKNOWN
			;;
	esac
done

if [ $# -ne 0 ] ; then # extra arguments
	print_usage
	exit $STATE_UNKNOWN
fi

if [ "x${hostname}" == "x" ] ; then # no hostname given
	print_usage
	exit $STATE_UNKNOWN
fi

trap "$RM -f $tmpfile" EXIT

$RM -f $tmpfile
CRIT=`$WGET -nv -t 1 -T $timeout --http-user=${user} --http-pass=${passwd} -O $tmpfile http://${hostname}/confdmp2.asp 2>&1`
if [ $? -ne 0 ] ; then # wget command failed
	echo "Could not fetch status page from ATAboy device ${hostname} - $CRIT"
	exit $STATE_UNKNOWN
fi

# collect anything getween 'Current Problems' and the next header, removing empty lines.
CRIT=`$SED -e '1,/^\**Current Problems\**<br>$/d' -e '/^\**<br>$/d' -e '/^\*\**[^*]*\*\**<br>$/,$d' -e 's/<br>//' < $tmpfile`

echo "$CRIT"

if [ "$CRIT" == "There are no problems with this system" ] ; then
	exit $STATE_OK
fi

exit $STATE_CRITICAL
