#!/bin/bash
# Nagios plugin to check for a TS3 console
# 
# expect is required
#
# TODO: GPL
#
# TODO: enable login to TS3 console to get acces to more info than just the version
#
# written by Thomas Wagner (wagner-thomas@gmx.at)


usage()
{
cat << EOF
usage:
This skript checks for the presence of a teamspeak 3 console and reads out the version of the teamspeak server.
OPTIONS:
 -h	Show this message
 -p	port (default 10011)
 -H	hostname (default localhost)
 -v	verbose
EOF
}

# NAGIOS' wanted return values
NAG_OK=0
NAG_WARN=1
NAG_CRIT=2
NAG_UNKNOWN=3



HOST=localhost
PORT=10011
VERBOSE=0

while getopts ":hH:p:v" OPTION
do
  case $OPTION in 
  	h) usage
  	   exit $NAG_CRIT
  	   ;;
  	H) HOST=$OPTARG
  	   ;;
  	p) PORT=$OPTARG
  	   ;;
  	v) VERBOSE=1
  	   ;;
  	?) usage
  	   exit $NAG_UNKNOWN
  	   ;;
  esac
done

[ $PORT -ge 0 ] 2>/dev/null
if [ $? -ge 1 ]; then
	echo "Port must be a positve integer"
fi


TMP_FILE=`mktemp`
echo "start" > $TMP_FILE

expect  &> $TMP_FILE << END 
set timeout 5
spawn telnet $HOST $PORT
expect "command."
send "version\n"
expect "error id=0 msg=ok"
send "quit\n"
expect eof
END

EXPECT_RET=$?

# output expect's transmission protocoll to console
if [ $VERBOSE -gt 0 ]; then
	cat $TMP_FILE
fi

if [ $EXPECT_RET -eq 0 ]; then
	echo "OK - " $(grep "version=" $TMP_FILE)
	RET_VAL=$NAG_OK
else
	echo "CRITICAL - check TS3 manually or use -v for more information"
	RET_VAL=$NAG_CRIT
fi

rm $TMP_FILE
exit $RET_VAL

