#!/bin/sh
#----------------------------------------
# autoIT timer check, to run a start time test of a program remotely.
#
#----------------------------------------

print_usage(){
    echo "Usage: $0 -H <host> -p <port> -c <command> -w <warn> -c <crit>"
}


if [ $# -lt 10 ]; then
	print_usage
	exit 3
fi


host=$2
port=$4
cmd=$6
warn=$8
crit=${10}


#echo "HOST: $host"
#echo "PORT: $port"
#echo "CMD: $cmd"
#echo "WARN: $warn"
#echo "CRIT: $crit"



val=`/usr/local/nagios/libexec/check_nrpe -H $host $ -p $port -c $cmd -t 30`
#val=`echo 3232.44563`
rc=$?
if [ $rc -ne 0 ]; then
	echo "ERROR: Woops! The Script returned with an error code of $rc"
	exit 2
fi

#echo "RETURN CODE=$rc"
#echo "RETURN VALUE=$val"

# Drop any decimal places
fval=`echo $val | cut -d . -f 1`
#echo "FRIENDLY VALUE=$fval"

# Turn milliseconds into seconds
fsec=`echo $fval / 1000 | bc -l`
# Whole number
sec1=`echo $fsec | cut -d . -f 1`
# Four decimal points
sec2=`echo $fsec | cut -d . -f 2 | cut -c -4`

#echo "FSEC: $fsec"
#echo "SEC1: $sec1"
#echo "SEC2: $sec2"

# Friendly, printable value
sec=${sec1}.${sec2}


if [ $fval -ge $crit ]; then
	echo "CRITICAL: The Program took $sec seconds to load; time=${fval}ms;$warn;$crit;0"
	exit 2
elif [ $fval -ge $warn ]; then
	echo "WARNING: The Program took $sec seconds to load; time=${fval}ms;$warn;$crit;0"
	exit 1
else
	echo "OK: The 
	Program took $sec seconds to load; time=${fval}ms;$warn;$crit;0"
	exit 0
fi

# Plugin variable description
PROGNAME=$(basename $0)
RELEASE="Version 1.0"
AUTHOR="(c) 2012 Samuel Lansing (slansing@nagios.com)"


# Functions plugin usage
print_release() {
    echo "$RELEASE $AUTHOR"
}

print_usage() {
	echo ""
	echo "$PROGNAME $RELEASE - Check to run an autoIT timer script on a remote windows Host and return the time to Nagios"
	echo ""
	echo "Usage: check_programloadtime.sh"
	echo ""
	echo "		-h  Show help page"
	echo ""
    echo "Usage: $PROGNAME"
    echo "Usage: $PROGNAME --help"
    echo ""
}

print_help() {
		print_usage
        echo ""
        print_release $PROGNAME $RELEASE
        echo ""
        echo "This plugin will run an autoIT timer script on a remote windows Host"
		echo "-h is for opening this help list"
        echo ""
		exit 0
}

# Make sure the correct number of command line arguments have been supplied
#if [ $# -lt 1 ]; then
#    print_usage
#    exit $STATE_UNKNOWN
#fi

#Help

while [ $# -gt 0 ]; do
    case "$1" in
        -h | --help)
            print_help
            exit $STATE_OK
            ;;
        -v | --version)
                print_release
                exit $STATE_OK
                ;;
		*)  echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
        esac
shift
done