#!/bin/bash


# declarations
# ============

# exit codes expected by nagios
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3

# component ids
AD=2
ZIMBRA=3
PRINT=5

# db results
TRUE="t"
FALSE="f"


# functions
# =========
usage() {
	echo ""
	echo "Usage: ${0} component_name [-w minutes] [-c minutes]"
	echo ""
	echo "  -w minutes        amount of minutes since last timestamp to cause a warning"
	echo "  -c minutes        amount of minutes since last timestamp to cause error"
	echo ""

	exit ${UNKNOWN}
}

trim() {
	local var=${1}
	var="${var#"${var%%[![:space:]]*}"}"   # remove leading whitespace characters
	var="${var%"${var##*[![:space:]]}"}"   # remove trailing whitespace characters
	echo -n "${var}"
}


# parameter handling
# ==================

# default values
WARNING_MIN=5
CRITICAL_MIN=1440

if [ ${#} -lt 1 -o ${#} -gt 5 ]
then
	usage
else
	COMPONENT=${1}
	shift

#	if [ ${COMPONENT} != ${AD} -a ${COMPONENT} != ${ZIMBRA} -a ${COMPONENT} != ${PRINT} ]
#	then
#		echo "Unknown component id: ${COMPONENT}"
#		exit ${UNKNOWN}
#	fi

	while [ ${#} -gt 1 ]
	do
		PARAM=${1}
		shift

		if [ "${PARAM}" = "-w" ]
		then
			WARNING_MIN=${1}
			shift
		elif [ "${PARAM}" = "-c" ]
		then
			CRITICAL_MIN=${1}
			shift
		else
			echo "Unknown parameter ${PARAM}"
			exit ${UNKNOWN}
		fi
	done
fi


# check if connector is running
# =============================
export PGSSLMODE=require
# ab psql 9.2: PSQL="psql postgresql://db1.kinet.ch/kinet?sslmode=require -U db.icinga -t -c"
PSQL="psql -h db1.kinet.ch -d kinet -U db.icinga -t -c"
CRITICAL_RES=$(${PSQL} "select time > (current_timestamp - interval '"${CRITICAL_MIN}" minutes') from monitor.component_activity where component_code = '"${COMPONENT}"'")
CRITICAL_RES=$(trim ${CRITICAL_RES})
#WARNING_RES=$(${PSQL} "select time > (current_timestamp - interval '"${WARNING_MIN}" minutes') from monitor.component_activity where component_code = '"${COMPONENT}"'")
#WARNING_RES=$(trim ${WARNING_RES})
TIMESTAMP=$(${PSQL} "select time from monitor.component_activity where component_code = '"${COMPONENT}"'")
DATALIB_VERSION=$(${PSQL} "select datalib_version from monitor.component_activity where component_code = '"${COMPONENT}"'")
USERLIB_VERSION=$(${PSQL} "select userlib_version from monitor.component_activity where component_code = '"${COMPONENT}"'")
COMPONENT_VERSION=$(${PSQL} "select component_version from monitor.component_activity where component_code = '"${COMPONENT}"'")
HEALTH_STATUS=$(${PSQL} "select healthy from monitor.component_activity where component_code = '"${COMPONENT}"'")
HEALTH_STATUS=$(trim ${HEALTH_STATUS})


# return status
# =============
#if [ "${WARNING_RES}" = "${TRUE}" ]
#then
#	# run before warning minutes
#	echo "Connector (version: ${COMPONENT_VERSION}, datalib: ${DATALIB_VERSION}, userlib: ${USERLIB_VERSION}) run within the last ${WARNING_MIN} minutes."
#	exit ${OK}
#elif [ "${WARNING_RES}" = "${FALSE}" -a "${CRITICAL_RES}" = "${TRUE}" ]
#then
#	# run within 1 day
#	echo "Connector not running, but run ${TIMESTAMP}"
#	exit ${WARNING}

if [ "${CRITICAL_RES}" = "${TRUE}" ]
then
	if [ "${HEALTH_STATUS}" = "${TRUE}" ]
	then
		echo "Connector (version: ${COMPONENT_VERSION}, datalib: ${DATALIB_VERSION}, userlib: ${USERLIB_VERSION}) is running and healty."
		exit ${OK}
	elif [ "${HEALTH_STATUS}" = "${FALSE}" ]
	then
		echo "Connector (version: ${COMPONENT_VERSION}, datalib: ${DATALIB_VERSION}, userlib: ${USERLIB_VERSION}) is not healty."
		exit ${WARNING}
	else
		echo "Unknown health status: ${HEALTH_STATUS}."
		exit ${UNKNOWN}
	fi
elif [ "${CRITICAL_RES}" = "${FALSE}" ]
then
	echo "Connector not running and not run for quite some time: ${TIMESTAMP}"
	exit ${CRITICAL}
else
	echo "Unknown timestamp: ${TIMESTAMP}"
	exit ${UNKNOWN}
fi

echo "This point should not be reachable..."
exit ${UNKNOWN}
