#!/bin/bash
#
# Written By: Pierre SOURDEAU to AMF company [FRANCE]
# Created: 28/10/2008
# Description: Check SNMP v 1, 2c, 3 on Cisco NTP service, and return :
# 	
# Check succesfuly on Cisco 6509 and 6513
#
# License: This nagios plugin comes with ABSOLUTELY NO WARRANTY. You may redistribute copies of
# the plugins under the terms of the GNU General Public License. For more information about these 
# matters, see the GNU General Public License.
# Version: 2.3 (updated 09/04/2009)

############################################################
# Varialbles
############################################################
PROGNAME=$0
#`/bin/basename $0` #no basename on Mac OS X 10.3
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$PROGNAME: 2.3 $' | sed -e 's/[^0-9.]//g'`
SNMPGET="/usr/bin/snmpget";
SNMPWALK="SNMPGET";

. $PROGPATH/utils.sh

USAGE="Usage: check_cisco_ntp.sh -H hostname -V version -M asso [-C|-l|-u|-a|-d|-h]
### PARAMETERS ### 
-H Hostname (IP adresse or DNS name)
-V Version (1|2c|3)
-M Mode (asso)
### OPTIONNAL ###
-C Community (name) *** Use on Version 1|2 ***
-l Login (NoAuthNoPriv | AuthNoPriv | AuthPriv) *** Use on Version 3 ***
-u Username *** Use on Version 3 ***
-a Password *** Use on Version 3 ***
-d Debug mode
-h Help (print command usage, and quit)"

sum_value_Ok=0
sum_value_Warning=0
sum_value_Critical=0
sum_value_Unknwon=0

sysleap_value=([0]=noWarning [1]=addSecond [2]=subtractSecond [3]=alarm)
servermode_value=([0]=unspecified [1]=symmetricActive [2]=symmetricPassive [3]=client [4]=server [5]=broadcast [6]=reservedControl [7]=reservedPrivate)
comm_final=""
walk_param=""

mib_cntpSysLeap=".1.3.6.1.4.1.9.9.168.1.1.1.0"
mib_cntpSysPeer=".1.3.6.1.4.1.9.9.168.1.1.9.0"
mib_cntpPeersConfigured=".1.3.6.1.4.1.9.9.168.1.2.1.1.2"
mib_cntpPeersPeerAddress=".1.3.6.1.4.1.9.9.168.1.2.1.1.3"
mib_cntpPeersPeerPort=".1.3.6.1.4.1.9.9.168.1.2.1.1.4"
mib_cntpPeersMode=".1.3.6.1.4.1.9.9.168.1.2.1.1.8"
mib_cntpPeersReach=".1.3.6.1.4.1.9.9.168.1.2.1.1.21"
mib_cntpSysClock=".1.3.6.1.4.1.9.9.168.1.1.10.0"


############################################################
# Global Functions
############################################################
# Check if value is numeric or not
check_num()
{
if ! let $1 2> /dev/null
then
	if [ $1 != 0 ]
	then
		echo "Error - Not numeric value : $2 = $1"
		exit $STATE_UNKNOWN
	fi
fi
}

# Check Parameters
check_param()
{
if [ "$hostname" == "" ] || [ "$version" == "" ] || [ "$mode" == "" ]
then
	echo "Error - Missing parameters - hostname = $hostname : version = $version : mode = $mode"
	echo "$USAGE"
	exit $STATE_UNKNOWN
fi

case $version in
		1 | 2c)
			if [ "$community" == "" ]
			then
				echo "Error - Missing parameters - community = $community"
				echo "$USAGE"
				exit $STATE_UNKNOWN
			fi
			walk_param="-v $version -c $community $hostname";;
		3) 
			if [ "$login" == "" ] || [ "$user" == "" ] || [ "$password" == "" ]
			then
				echo "Error - Missing parameters - login = $login : user = $user : password = $password"
				echo "$USAGE"
				exit $STATE_UNKNOWN
			fi
			walk_param="-v $version -l $login -u $user -A $password $hostname";;
esac
}


# Command used to debug
debug()
{
echo "*********************************************************************"
echo "### Mode Association ###"
echo "cntpSysLeap (Two-bit code warning) = $cntpSysLeap ($cntpSysLeap_trans)"
echo "cntpPeersConfigured (Peer configured) =$cntpPeersConfigured"
echo "cntpPeersPeerAddress (IP Synch. source) = $cntpPeersPeerAddress"
echo "cntpPeersPeerPort (Port Synch. source) = $cntpPeersPeerPort"
echo "cntpPeersMode (Mode of Synch. source)= $cntpPeersMode ($cntpPeersMode_trans mode)"
echo "cntpPeersReach (Reachability of Synch. source) = $cntpPeersReach"
echo "cntpSysClock (Current time) = $cntpSysClock"
echo "### Global Values ###"
echo "Sum values : Ok = $sum_value_Ok ; Warning = $sum_value_Warning ; Critical = $sum_value_Critical ; Unknown = $sum_value_Unknwon"
echo "Nagios return : Ok = $STATE_OK ; Warning = $STATE_WARNING ; Critical = $STATE_CRITICAL ; Unknown = $STATE_UNKNOWN"
echo "Parameters : Hostname = $hostname ; Version = $version ; Community = $community ; Mode = $mode ; login = $login ; user = $user ; password = $password"
echo "*********************************************************************"
}

# Results sent to Nagios
function_exit()
{
if [ $sum_value_Unknwon != 0 ]
then
	echo "Unknown $comm_final"
	exit $STATE_UNKNOWN
fi

if [ $sum_value_Critical != 0 ]
then
	echo "Critical $comm_final"
	exit $STATE_CRITICAL
fi

if [ $sum_value_Warning != 0 ]
then
	echo "Warning $comm_final"
	exit $STATE_WARNING
fi

if [ $sum_value_Ok != 0 ]
then
	echo "OK $comm_final"
	exit $STATE_OK
fi
}


############################################################
# Mode Association Functions
############################################################
# Two-bit code warning	function_cntpSysLeap()
function_cntpSysLeap()
{
	cntpSysLeap=`$SNMPGET $walk_param $mib_cntpSysLeap | cut -d' ' -f4`
	check_num $cntpSysLeap cntpSysLeap
	cntpSysLeap_trans=${sysleap_value[$"${cntpSysLeap}"]}
}

# Source synchronisation
function_sourceSynch()
{
	cntpSysPeer=`$SNMPGET $walk_param $mib_cntpSysPeer | cut -d' ' -f4`
	check_num $cntpSysPeer cntpSysPeer

	cntpPeersConfigured=`$SNMPGET $walk_param $mib_cntpPeersConfigured.$cntpSysPeer | cut -d' ' -f4`
	check_num $cntpPeersConfigured cntpPeersConfigured

	cntpPeersPeerAddress=`$SNMPGET $walk_param $mib_cntpPeersPeerAddress.$cntpSysPeer | cut -d' ' -f4`

	cntpPeersPeerPort=`$SNMPGET $walk_param $mib_cntpPeersPeerPort.$cntpSysPeer | cut -d' ' -f4`
	check_num $cntpPeersPeerPort cntpPeersPeerPort

	cntpPeersMode=`$SNMPGET $walk_param $mib_cntpPeersMode.$cntpSysPeer | cut -d' ' -f4`
	check_num $cntpPeersMode cntpPeersMode
	cntpPeersMode_trans=${servermode_value[$"${cntpPeersMode}"]}

	cntpPeersReach=`$SNMPGET $walk_param $mib_cntpPeersReach.$cntpSysPeer | cut -d' ' -f4`
	check_num $cntpPeersReach cntpPeersReach
}

# Curent time
function_cntpSysClock()
{
	cntpSysClock=`$SNMPGET $walk_param $mib_cntpSysClock | cut -c 56-78`
}

# Translate SNMP status return by host to Nagios status - part 1
function_check_status1()
{
	case $cntpSysLeap in
	0)	sum_value_Ok=$(( ${sum_value_Ok} + 1 ))
		comm_final="$comm_final - Node status : $cntpSysLeap_trans"
		;;
	1)	sum_value_Warning=$(( ${sum_value_Warning} + 1 ))
		comm_final="$comm_final - Node status : $cntpSysLeap_trans"
		;;
	2)	sum_value_Warning=$(( ${sum_value_Warning} + 1 ))
		comm_final="$comm_final - Node status : $cntpSysLeap_trans"
		;;
	3)	sum_value_Critical=$(( ${sum_value_Critical} + 1 ))
		comm_final="$comm_final - Node status : $cntpSysLeap_trans | ok=0"
		;;
	*)	sum_value_Unknwon=$(( ${sum_value_Unknwon} + 1 ))
		comm_final="$comm_final - Node status : $cntpSysLeap_trans | ok=0"
	esac
}

# Translate SNMP status return by host to Nagios status - part 2
function_check_status2()
{
	if [ "$sum_value_Unknwon" == 0 ] && [ "$sum_value_Critical" == 0 ]
	then
		if [ "$cntpPeersPeerAddress" != "127.127.7.1" ]
		then
			if [ "$cntpPeersReach" == "0" ]
			then
				sum_value_Critical=$(( ${sum_value_Critical} + 1 ))
				comm_final="$comm_final - Source status : $cntpPeersPeerAddress:$cntpPeersPeerPort ($cntpPeersMode_trans mode) : unreachable | ok=0"
			else
				sum_value_Ok=$(( ${sum_value_Ok} + 1 ))
				comm_final="$comm_final - Source status : $cntpPeersPeerAddress:$cntpPeersPeerPort ($cntpPeersMode_trans mode) : OK | ok=1"
			fi
		else
			sum_value_Critical=$(( ${sum_value_Critical} + 1 ))
			comm_final="$comm_final - Source status : $cntpPeersPeerAddress:$cntpPeersPeerPort ($cntpPeersMode_trans mode) : Local Clock | ok=0"
		fi
	fi
}


############################################################
# Main Method
############################################################
main()
{
	# Get Options and check parameters
	while getopts H:V:C:M:l:u:a:dh option;
	do
		case $option in
			H) hostname=$OPTARG;;
			V) version=$OPTARG;;
			C) community=$OPTARG;;
			M) mode=$OPTARG;;
			l) login=$OPTARG;;
			u) user=$OPTARG;;
			a) password=$OPTARG;;
			d) debug=1;;
			h) echo "$USAGE"
			exit $STATE_UNKNOWN;;
		esac
	done

	check_param


	### Mode 1 - Association ### 
	if [ "$mode" == "asso" ]
	then
		# Functions lunch
		function_cntpSysLeap
		function_sourceSynch
		function_check_status1
		function_check_status2
	fi


	if [ "$debug" == 1 ]
	then
		debug
	fi

	function_exit
}

main $*

# Default exit
echo "Unknown - No result sent to nagios - End of script"
exit $STATE_UNKNOWN
