#!/bin/bash
#########################################################################
# Script:       check_confixxlicense                                    #
# Author:       Claudio Kuenzler www.claudiokuenzler.com for            #
#		Nova Company GmbH www.novacompany.ch			#
# Purpose:      Nagios Plugin to check end of Confixx license           #
#									#
# History:                                                              #
# 20100402	Started Script programming				#
# 20100403	Small Bugfixes in Output				#
#########################################################################
# Usage: ./check_confixxlicense -H host [-P SQL-Port] -u SQL-User -p SQL-Password [-d SQL-DB] [-w Warning Days] [-c Critical Days]
#########################################################################
help="check_confixxlicense (c) 2010 Claudio Kuenzler (published under GPL licence)\n
Usage: ./check_confixxlicense -H host [-P SQL-Port] -u SQL-User -p SQL-Password [-d SQL-DB] [-w Warning Days] [-c Critical Days]\n
Options:\n-H Hostname\n-P Port on which SQL Server listens (3306 default) OPTIONAL
-u SQL-Username (needs Select Rights on confixx.register)
-p Password for SQL-User
-d Name of Confixx Database (default is confixx) OPTIONAL
-w How many days before end of license do you want the warning OPTIONAL
-c How many days before end of license do you want the critical OPTIONAL\n
Requirements: mysql, grep, cut"

STATE_OK=0              # define the exit code if status is OK
STATE_WARNING=1         # define the exit code if status is Warning
STATE_CRITICAL=2        # define the exit code if status is Critical
STATE_UNKNOWN=3         # define the exit code if status is Unknown
now=$(date +%s)
PATH=/usr/local/bin:/usr/bin:/bin # Set path

for cmd in mysql grep cut [
do
 if ! `which ${cmd} 1>/dev/null`
 then
 echo "UNKNOWN: ${cmd} does not exist, please check if command exists and PATH is correct"
 exit ${STATE_UNKNOWN}
 fi
done

# Check for people who need help - aren't we all nice ;-)
#########################################################################
if [ "${1}" = "--help" -o "${#}" = "0" ];
       then
       echo -e "${help}";
       exit 1;
fi

# Get user-given variables
#########################################################################
while getopts "H:P:u:p:d:w:c:" Input;
do
       case ${Input} in
       H)      host=${OPTARG};;
       P)      port=${OPTARG};;
       u)      user=${OPTARG};;
       p)      password=${OPTARG};;
       d)      db=${OPTARG};;
       w)      warning=${OPTARG};;
       c)      critical=${OPTARG};;
       \?)     echo -e "${help}"
               exit 1
               ;;
       esac
done


# Check MySQL Connection  
#########################################################################
if [ -n "${port}" ]
then	conncheck=$(mysql -h ${host} -P ${port} -u ${user} --password=${password} -e "\g" 1>/dev/null 2>/dev/null )
else	conncheck=$(mysql -h ${host} -u ${user} --password=${password} -e "\g" 1>/dev/null 2>/dev/null )
fi

if [ $? -gt 0 ]
then echo "CRITICAL Cannot connect to Database"; exit ${STATE_CRITICAL}
fi


# Connect to the DB server and get informations  
#########################################################################
if [ -n "${port}" ] && [ -n "${db}" ]
then
	finaldate=$(mysql -h ${host} -P ${port} -u ${user} --password=${password} -e "select ablauf from ${db}.register\G" | grep ablauf | cut -d: -f2 | cut -d " " -f2)

elif [ -n "${port}" ]
then
	finaldate=$(mysql -h ${host} -P ${port} -u ${user} --password=${password} -e "select ablauf from confixx.register\G" | grep ablauf | cut -d: -f2 | cut -d " " -f2)

elif [ -n "${db}" ]
then
	finaldate=$(mysql -h ${host} -u ${user} --password=${password} -e "select ablauf from ${db}.register\G" | grep ablauf | cut -d: -f2 | cut -d " " -f2)

else
	finaldate=$(mysql -h ${host} -u ${user} --password=${password} -e "select ablauf from confixx.register\G" | grep ablauf | cut -d: -f2 | cut -d " " -f2)
fi


# Calculate days left and exit with status 
#########################################################################
daysleft=$(((${finaldate}-${now})/86400))

if [ ${warning} ] && [ ${critical} ]
then

	# Check for Logical Errors (Warning Threshold smaller than Critical)
	# -------------------------------------------------------------------
	if [ ${warning} -le ${critical} ]
	then echo "ERROR Warning is smaller than Critical"; exit ${STATE_UNKNOWN}
	fi

	if [ ${daysleft} -le ${critical} ] 
	then
		if [ ${now} -ge ${finaldate} ]
                        then echo "CRITICAL Confixx License not valid anymore"; exit ${STATE_CRITICAL}
                	else echo "CRITICAL Confixx License will end in ${daysleft} days"; exit ${STATE_CRITICAL}
                fi
	
	elif [ ${daysleft} -gt ${critical} ] && [ ${daysleft} -le ${warning} ]
	then echo "WARNING Confixx License will end in ${daysleft} days"; exit ${STATE_WARNING}

	else echo "OK Confixx License still valid for ${daysleft} days"; exit ${STATE_OK}
	fi

elif [ ${warning} ] || [ ${critical} ]
then	echo "ERROR Warning and Critical Thresholds must be given"; exit ${STATE_UNKNOWN}

else 
	if [ ${now} -ge ${finaldate} ]
	then echo "CRITICAL Confixx License not valid anymore"; exit ${STATE_CRITICAL}
	else echo "OK Confixx License still valid for ${daysleft} days"; exit ${STATE_OK}
	fi
fi

echo "UNKNOWN"
exit ${STATE_UNKNOWN}
