#!/bin/sh
# PROGRAM NEEDS WGET VERSION 1.10 !


PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

PROGNAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$Revision: 1.1 $' | sed -e 's/[^0-9.]//g'`
COOKIESFILE="/usr/local/nagios/wgetcookies"
LINEMONFILE="/tmp/nagiosstingray.$$"
STINGRAYHOST=$1
STINGRAYPORT=$2
STINGRAYUSER=$3
STINGRAYPASS=$4

. $PROGPATH/utils.sh

 print_usage() {
        echo "Usage: $PROGNAME HOST PORT USER PASS LINE[1-6] [-v]"
        echo "Parameter order must be identical!"
}

print_help() {
        print_revision $PROGNAME $REVISION
        echo ""
        print_usage
        echo ""
        echo "This plugin checks line status of a STINGRAY Interface Line."
        echo ""
        support
        exit 0
}

case "$1" in
        --help)
                print_help
                exit 0
                ;;
        -h)
                print_help
                exit 0
                ;;
        --version)
                print_revision $PROGNAME $REVISION
                exit 0
                ;;
        -V)
                print_revision $PROGNAME $REVISION
                exit 0
                ;;
        *)
                cd /tmp
                # Log in
                wget -q --save-cookies $COOKIESFILE \
                        --keep-session-cookies \
                        --timeout=7 \
                        --load-cookies $COOKIESFILE \
                        --cookies=on \
                        --delete-after \
                        http://$STINGRAYHOST:$STINGRAYPORT/verify_login.jsp?form_username=$STINGRAYUSER\&form_password=$STINGRAYPASS

                # Now grab the line monitor stuff
                wget -q --save-cookies $COOKIESFILE \
                        --keep-session-cookies \
                        --timeout=7 \
                        --load-cookies $COOKIESFILE \
                        --cookies=on \
                        -O $LINEMONFILE \
                        http://$STINGRAYHOST:$STINGRAYPORT/hw_details_isdn.jsp?form_action=details\&form_hw_param=$5
                WGETSTATUS=$?

                #LogOut Again
                wget -q --save-cookies $COOKIESFILE \
                        --keep-session-cookies \
                        --timeout=7 \
                        --load-cookies $COOKIESFILE \
                        --cookies=on \
                        --delete-after \
                        http://$STINGRAYHOST:$STINGRAYPORT/log_out.jsp


                # Figure out if we have any alarms
                export ALARMVAL=`cat $LINEMONFILE | grep -c "led_red.gif"`

                LINENAME=`cat $LINEMONFILE | grep form_name -A 1 | grep value | awk -F \" '{ print $2 }'`

                # No more data needed, remove our temporary file
                rm $LINEMONFILE

                if test "$6" = "-v" -o "$6" = "--verbose"; then
                        echo "${LINENAME}"
                        echo "${ALARMVAL}"
                fi
                if test ${WGETSTATUS} -eq 1; then
                        echo "Unable to get status from http://${STINGRAYHOST}:${STINGRAYPORT}/ - check Host and PORT + PASS settings in $0?"
                        exit -1
                fi
                if test ${ALARMVAL} -eq 0; then
                        echo "STINGRAY LINE $5 OK - $LINENAME"
                        exit 0
                else
                        echo "STINGRAY LINE $5 CRITICAL - $LINENAME"
                        exit 2
                fi
                ;;
esac

