# print usage
function printUsage() {
        local BASENAME=`basename $0`
        echo "Usage: $BASENAME [-V] [-h] [-e TRUE|FALSE] [-w warning threshold] [-c critical threshold] [-s path to socket]"
}

# print version
function printVersion() {
        local VERSION="0.0.1-alpha"
        local BASENAME=`basename $0`
        echo "$BASENAME (nagios-plugins-hosteurope 1.0.0) $VERSION"
        echo "This nagios plugin comes with ABSOLUTELY NO WARRANTY. You may not redistribute
copies of the plugins under the terms of any FOO General Public License.
For more information about these matters, bang your head against the WALL."
}

# print help
function printHelp() {
        printUsage
        local BASENAME=`basename $0`
        echo "This plugin checks the MySQL slave status."
        echo " "
        echo "Example: $BASENAME -e TRUE -w 60 -c 180 -s /var/run/mysqld/mysqld.sock"
        echo " "
        echo "-V	Version"
        echo "-h	Help"
        echo "-e	Enable Plugin, defaults to true. If set to false the Plugin will always return OK."
	echo "-s	Path to MySQL Socket, defaults to /var/lib/mysql/mysql.sock"
        echo "-w	Warning Threshold for replication delay in seconds (only for Seconds_Behind_Master variant)"
        echo "-c	Critical Threshold for replication delay in seconds (only for Seconds_Behind_Master variant)"
        echo " "
	echo "This Plugin needs a configured ini file (~/.my.cnf), the format follows:"
	echo "[mysql]"
	echo "user=nagios"
	echo "password=random"
	echo "socket=/path/to/socket"
	echo "host=localhost"
        echo " "
        exit 0
}

# parse options
while getopts ":Vhs:w:c:e:" Option
do
        case $Option in
        V)
                printVersion
                exit 3
        ;;
        h)
                printHelp
                exit 3
        ;;
        s)
                export SOCKET="$OPTARG"
        ;;
        w)
                export WARNING="$OPTARG"
        ;;
        c)
                export CRITICAL="$OPTARG"
        ;;
        e)
                case "$OPTARG" in 
		0|false|FALSE|f)
			PARAMETER=$(basename $0 | cut -d'_' -f3-)
			echo "SKIPPED Plugin disabled |'$PARAMETER'=${VALUE:-0}[${UOM:-""}];[${WARNING:-0}];[${CRITICAL:-0}];[${MINIMUM:-0}];[${MAXIMUM:-0}]"
			exit 0
		;;
		esac
        ;;
        *)
                printUsage
                exit 3
        ;;
        esac
done
shift $(($OPTIND - 1))

# check for config file
if [ ! -f "./.my.cnf" ]; then
	echo "UNKNOWN Missing default option file."
	exit 3
fi

# check for socket
if [ ! -S ${SOCKET=/var/lib/mysql/mysql.sock} ]; then
	echo "CRITICAL Database Socket ($SOCKET) not available."
	exit 2
fi

BUFFER=$(mysql -e "show slave status \G" | egrep "Slave_|Seconds_Behind_Master"  | sed -e 's!^\ *!!' |perl -e '$var=""; while(<>){chomp $_; $var .= $_ . ",   "; };print "$var\n";' | sed 's!,   $!!')

PARAMETER=$(basename $0 | cut -d'_' -f3-)
case $PARAMETER in 
	Slave_IO_State)
		MESSAGE=`echo $BUFFER | cut -d',' -f1 |cut -d ':' -f2- | sed -e 's/^\ //g'`
		case $MESSAGE in
			"Waiting for master to send event")
				STATE=OK; VALUE=1; RETURNCODE=0
			;;
		esac
	;;
	Slave_IO_Running)
		MESSAGE=`echo $BUFFER | cut -d',' -f2 |cut -d ':' -f2- | sed -e 's/^\ //g'`
		case $MESSAGE in
			Yes)
				STATE=OK; VALUE=1; RETURNCODE=0
			;;
		esac
	;;
	Slave_SQL_Running)
		MESSAGE=`echo $BUFFER | cut -d',' -f3 |cut -d ':' -f2- | sed -e 's/^\ //g'`
		case $MESSAGE in
			Yes)
				STATE=OK; VALUE=1; RETURNCODE=0
			;;
		esac
	;;
	Seconds_Behind_Master)
		VALUE=`echo $BUFFER | cut -d',' -f4 |cut -d ':' -f2- | sed -e 's/^\ //g'`
		UOM=s
                if [ $(echo "if ($VALUE>=${CRITICAL:-999999999999999999}) 1"|bc) ]; then
                	STATE=CRITICAL ; RETURNCODE=2
		elif [ $(echo "if ($VALUE>=${WARNING:-9999999999999999999}) 1"|bc) ]; then
               		STATE=WARNING ; RETURNCODE=1
		else
               		STATE=OK ; RETURNCODE=0;
		fi
                MESSAGE="$VALUE $UOM behind master."
	;;
        Threads_connected)
                VALUE=$(mysqladmin extended-status | grep Threads_connected | cut -d'|' -f 3 |sed -e 's/\ *//g')
                UOM=Threads
                if [ $(echo "if ($VALUE>=${CRITICAL:-999999999999999999}) 1"|bc) ]; then
                        STATE=CRITICAL ; RETURNCODE=2
                elif [ $(echo "if ($VALUE>=${WARNING:-9999999999999999999}) 1"|bc) ]; then
                        STATE=WARNING ; RETURNCODE=1
                else
                        STATE=OK ; RETURNCODE=0;
                fi
        ;;
        *)
                MESSAGE="Parameter $PARAMETER is not defined."
		STATE=UNKNOWN; VALUE=0; RETURNCODE=3
        ;;
esac

echo "${STATE:-UNKNOWN} $MESSAGE |'$PARAMETER'=${VALUE:-0}[${UOM:-""}];[${WARNING:-0}];[${CRITICAL:-0}];[${MINIMUM:-0}];[${MAXIMUM:-0}]"
exit ${RETURNCODE:-2}
