#!/bin/bash
#
# Usage: ./check_peplink_balance_305_wan_status_snmp.bash <hostname> <user> <md5> <des> <wan_name>
#
# Description: 
#		This plugin will check the status of a WAN interface via SNMP the from an Peplink Balance 305.
#
#       Requirements for Peplink:
#       Model: Peplink Balance 305
#		Firmware: 8.1.0
#       Peplink configured with SNMP version 3 with MD5 authentication and DES privacy.
#       
#		Requirements for Nagios server:
#		Nagios: nagioscore 4.4.5
#       OS: CentOS 8.1
#       MIB: WAN.mib
#		
#		Make sure that the script is fetiching the right DATA. You can test it with the following command: 
#
#       snmpwalk -m WAN -v 3 -l authPriv -u <snmp_user> -a MD5 -A "<md5 authentication>" -x DES -X "<des privacy>" -Oq <hostname> WAN::wanName 
#
#       Example of output of test command:
#		WAN::wanName.0 "MY WAN1"
#		WAN::wanName.1 "MY WAN PROVIDER 2"
#		WAN::wanName.2 "ANOTHER WAN PROVIDER"
#		
#		You have to place the WAN.mib file in:		
#		/usr/share/snmp/mibs
#
#		Get the WAN.mib file from:
#		https://download.peplink.com/resources/balance_max_snmp_mib-8.1.0.zip
#
#		SYNTAX: ./check_peplink_balance_305_wan_status_snmp.bash <hostname> <snmp_user> <md5 authentication> <des privacy> <wan_name>
#
#		EXAMPLE ./check_peplink_balance_305_wan_status_snmp.bash 172.31.0.1 "peplinkDC1" "246e410e31c34fdfa" "856c5b77821dde_n" "Comcast"
#
#
# Created:      2021-08-11	Author  : Cesar San
#
#
#
#
##################################################################################

PATH="/usr/bin:/usr/sbin:/bin:/sbin"

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

##################################################################################

PROGNAME=$0

print_help() {
	echo ""
	echo "Usage: $PROGNAME <hostname> <snmp_user> <md5 authentication> <des privacy> <wan_name>"
	echo ""
	echo "This plugin will check the status of a WAN interface via SNMP the from an Peplink Balance 305."
	echo ""
	echo "Requirements for Peplink:"
	echo "Model: Peplink Balance 305"
	echo "Firmware: 8.1.0"
	echo "Peplink configured with SNMP version 3 with MD5 authentication and DES privacy."
	echo ""
	echo "Requirements for Nagios server:"
	echo "Nagios: nagioscore 4.4.5"
	echo "OS: CentOS 8.1"
	echo "You have to place the WAN.mib file in:	"	
	echo "/usr/share/snmp/mibs"
	echo ""
	echo "Get the WAN.mib file from:"
	echo "https://download.peplink.com/resources/balance_max_snmp_mib-8.1.0.zip"
	echo ""
	exit $STATE_CRITICAL
}


HOSTNAME=$1
USER=$2
MD5=$3
DES=$4
WAN_NAME=$5



case "$1" in
	--help)
		print_help
		exit $STATE_CRITICAL
		;;
	-h)
		print_help
		exit $STATE_CRITICAL
		;;
	--version)
   		echo $PROGNAME v1.0
		exit $STATE_CRITICAL
		;;
	-V)
		echo $PROGNAME v1.0
		exit $STATE_CRITICAL
		;;
	*)
	
	#Sanity check
	if [ $# -ne 5 ]; then
			echo "Error: Missing parameter"
			print_help
			exitstatus=$STATE_CRITICAL
			exit $exitstatus 
	fi
	
	# Check for missing parameters
	if [[ -z "$HOSTNAME" ]] || [[ -z "$USER" ]] || [[ -z "$MD5" ]] || [[ -z "$DES" ]] || [[ -z "$WAN_NAME" ]]; then
			echo "Error: Empty parameter"
			print_help
			exitstatus=$STATE_CRITICAL
			exit $exitstatus
	fi

	WAN_ID=$(snmpwalk -m WAN -v 3 -l authPriv -u $USER -a MD5 -A "$MD5"  -x DES -X "$DES" -Oq $HOSTNAME WAN::wanName | grep " \"$WAN_NAME\"" | sed 's/ .*//g' | sed 's/WAN::wanName.//g' 2> /dev/null)

	WAN_STATE=$(snmpwalk -m WAN -v 3 -l authPriv -u $USER -a MD5 -A "$MD5"  -x DES -X "$DES" -Oq $HOSTNAME WAN::wanState.$WAN_ID | sed "s/WAN::wanState.${WAN_ID} //g" 2> /dev/null)

	WAN_HEALTH_CHECK_STATE=$(snmpwalk -m WAN -v 3 -l authPriv -u $USER -a MD5 -A "$MD5"  -x DES -X "$DES" -Oq $HOSTNAME WAN::wanHealthCheckState.$WAN_ID | sed "s/WAN::wanHealthCheckState.${WAN_ID} //g" 2> /dev/null)


	if [ $WAN_STATE != "connected" ]; then
			echo "CRITICAL - WAN $WAN_NAME NOT CONNECTED, STATE=$WAN_STATE"
			exitstatus=$STATE_CRITICAL
	elif [ $WAN_HEALTH_CHECK_STATE != "success" ]; then
			echo "CRITICAL - WAN $WAN_NAME FAILED HEATH CECK, STATE=$WAN_HEALTH_CHECK_STATE"
			exitstatus=$STATE_CRITICAL
	else
			echo "OK"
			exitstatus=$STATE_OK
	fi
	
	exit $exitstatus
	;;
esac
	
