#!/bin/bash
# (c) 2005 maik.aussendorf (at) dass-it.de
# $Id: check_mysql_innodb,v 1.2 2005/12/15 13:04:46 maik Exp $

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

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.2 $' | sed -e 's/[^0-9.]//g'`

DEFAULT_MYSQLHOST=localhost
DEFAULT_USER=nagios
DEFAULT_PASSWORD=nagios
# units are kb
DEFAULT_WARNING=10000
DEFAULT_CRITICAL=1000

OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3

## helper variable
DBLISTFILE=`mktemp`
TMPFILE=`mktemp`
RESULTFILE=`mktemp`

print_usage() {
	echo "Usage: $PROGNAME -h hostname -u user -p password"
	echo "Options:"
	echo "-H STRING hostname [Default: $DEFAULT_MYSQLHOST]"
	echo "-u STRING mysql user [Default: $DEFAULT_USER]"
	echo "-p STRING mysql users password [Default: $DEFAULT_PASSWORD]"
	echo "-w INT warning threshold [Default: $DEFAULT_WARNING]"
        echo "-c INT critical threshold [Default: $DEFAULT_CRITICAL]"
}

print_help() {
        echo $PROGNAME $REVISION
        echo ""
        print_usage
        echo "Will check free space of innodbs."
        echo ""
        echo "contact info@dass-it.de for further information"
        exit $UNKNOWN
}

cleanup() {
rm -f $DBLISTFILE
rm -f $TMPFILE
rm -f $RESULTFILE
}

# $1: error message
# $2: exit code
error_exit() {
echo Error: $1
exit $2
}

# Calls the mysqlshow command with all given options with appropriate error handling
mysql_show() {
mysqlshow $@
MYSQLRETURN=$?
if [ $MYSQLRETURN -ne 0 ]; then
  error_exit "mysql connect error. return code: $MYSQLRETURN" $UNKNOWN
fi
}

# Figures out which databases exist
# $1: Filename for results. Writes all found databases into $1
get_databases() {
mysql_show -h ${mysql_host} -u ${mysql_user} --password=${mysql_password} > $TMPFILE

# remove schnodder
cat $TMPFILE | tail +4 | head -n -1 | sed -e s/^\|//g -e s/\|\$//g > $1
echo "" >  $TMPFILE
}

# Calls mysqlshow for each databases and writes free InnoDB Space to result file
# $1: File with databaselist
# $2: Output file for results
check_databases(){
for i in `cat $1`
do
FREESPACE=`mysql_show -i -h ${mysql_host} -u ${mysql_user} --password=${mysql_password} $i | grep InnoDB | sed s/'^.*InnoDB free: '//g | cut -d " " -f 1 | sort | head -n 1`
[ x$FREESPACE != x ]  && echo ${FREESPACE} $i >> $2
done
}

# Evaluates results and determines nagios states
# $1 Resultfile
evaluate_results() {
NUMBERINNODBS=`cat $1 | wc -l`
MINIMUMLINE=`cat $1 | sort | head -n 1`
MINIMUMDB=`echo $MINIMUMLINE | cut -d " " -f 2`
MINIMUMFREE=`echo $MINIMUMLINE | cut -d " " -f 1`

if [ $NUMBERINNODBS -eq 0 ]; then
 EXITCODE=$UNKNOWN
 EXITTEXT="UNKNOWN - no Tables with engine type  innodb found"
else
  if [ $MINIMUMFREE -gt $warning_threshold ] ; then # everything is fine
    EXITCODE=$OK
    EXITTEXT="OK - found $NUMBERINNODBS databases with InnoDB tables. $MINIMUMDB has $MINIMUMFREE kb free space left."
  else
    if [ $MINIMUMFREE -gt $critical_threshold ] ; then # we are at warning stage
     EXITCODE=$WARNING
     EXITTEXT="WARNING - found $NUMBERINNODBS databases with InnoDB tables. $MINIMUMDB has $MINIMUMFREE kb free space left. (less than $warning_threshold)"
    else # this is critical
	EXITCODE=$CRITICAL
	EXITTEXT="CRITICAL - found $NUMBERINNODBS databases with InnoDB tables. $MINIMUMDB has $MINIMUMFREE kb free space left. (less than $critical_threshold)"
    fi
 fi
fi
}


### Parse Arguments

while getopts ":hV:H:u:p:w:c:" Option; do
	case $Option in
		h)
			print_help
			exit 0
			;;
		V)
			print_revision $PROGNAME $REVISION
			exit 0
			;;
		H)
			mysql_host=${OPTARG}
			;;
		u)
			mysql_user=${OPTARG}
			;;
		p)
			mysql_password=${OPTARG}
			;;
                w)
                        warning_threshold=${OPTARG}
                        ;;
                c)
                        critical_threshold=${OPTARG}
                        ;;
		*)
			print_help
			exit 0
			;;
	esac
done
shift $(($OPTIND - 1))

# Set defaults, if options are missing
if [ "${mysql_host}" == '' ]; then
	mysql_host=$DEFAULT_MYSQLHOST
fi
if [ "${mysql_user}" == '' ]; then
	mysql_user=$DEFAULT_USER
fi
if [ "${mysql_password}" == '' ]; then
	mysql_password=$DEFAULT_PASSWORD
fi
if [ "${warning_threshold}" == '' ]; then
	warning_threshold=$DEFAULT_WARNING
fi
if [ "${critical_threshold}" == '' ]; then
        critical_threshold=$DEFAULT_CRITICAL
fi

### Main Program

get_databases $DBLISTFILE
check_databases $DBLISTFILE $RESULTFILE
evaluate_results $RESULTFILE
cleanup
echo $EXITTEXT
exit $EXITCODE