#!/bin/bash
#
# Check ifoperstatus() without calling the perl routine, this script uses
# snmpwalk to get the info.
#
# $1: Interface ; $@ snmpwalk/snmpget args for snmp v3
#
# by nAAs <nalvarezs@elo.utfsm.cl>
# Edits by Nagios Enterprises, LLC

LIBEXEC=$(dirname $0)
. $LIBEXEC/utils.sh

# make sure iface is pos int, else print usage and exit.
if test -z "$1" || test -z "$2" || test -z "$3"; then
	cat << EOF
Usage $0: <interface index> <snmpget options>
  Check_ifoperstatus requires that the first argument be the interface index that this interface can be found at under the remote devices snmp tree.
  This should always be a positive integer, or zero.
  All options beyond the first, are arguments that must follow the snmpget command line parameters.
EOF
	exit $STATE_UNKNOWN
fi

hostname=$1
type=$2
port_id=$3

shift
shift
shift

if [ ! -d $LIBEXEC/check_ifoperstatnag_by_variable_cache ]; then
	mkdir -p $LIBEXEC/check_ifoperstatnag_by_variable_cache
fi

cached_iface_path=$LIBEXEC/check_ifoperstatnag_by_variable_cache/"$hostname"_"$type"_"$port_id"
if [ -f $cached_iface_path ]; then
	cached_iface=$(cat $cached_iface_path)
fi

if [ ! -z $cached_iface ]; then
	# clear the cached interface index if it's not valid...
	candidate_id=$(snmpget $@ $type.$cached_iface | cut -d '.' -f 2 | cut -d ' ' -f 1)
	if [ "$candidate_id" != "$port_id" ]; then
		cached_iface=
	fi
fi

if [ ! -z $cached_iface ]; then
	iface=$cached_iface
else
	iface=$(snmpwalk $@ $type | grep \\b"$port_id"\$ | cut -d '.' -f 2 | cut -d ' ' -f 1)
fi

if [ -z $iface ]; then
	echo "UNKNOWN - could not find interface with $type of value $port_id"
	exit 3
fi

# Add the interface number to the cache.
# TODO maybe we should populate the whole cache at once so that we're not walking the m_switch 50 times after it restarts :/
echo $iface > $cached_iface_path

# Find the interface name and put it into an array, if possible
name=$(snmpget $@ ifDescr.$iface)
name=($name)
if [ -z $name ]; then
	ifacename=$iface
else
	ifacename="${name[3]} (index $iface)"
fi

# Get and process admin down first.
adminstatus=$(snmpget $@ ifAdminStatus.$iface | awk -F: '{print $4}' | awk -F\( '{print $2}' | awk -F\) '{print $1}')
if [ $adminstatus -eq 2 ]; then
	echo "CRITICAL - Interface $ifacename is administratively down."
	exit $STATE_CRITICAL
fi

# Now that we are not admin down, check and process operstatus
comm=$(snmpget $@ ifOperStatus.$iface | awk -F: '{print $4}' | awk -F\( '{print $2}' | awk -F\) '{print $1}')
if [ -z $comm ]; then
	echo "UNKNOWN - No info is being retrieved."
	exit $STATE_UNKNOWN
elif [ $comm -eq 1 ]; then
	echo "OK - Interface $ifacename is up."
	exit $STATE_OK
elif [ $comm -eq 2 ]; then
	echo "CRITICAL - Interface $ifacename is down"
	exit $STATE_CRITICAL
elif [ $comm -eq 5 ]; then
        echo "CRITICAL - Interface $ifacename is dormant"
        exit $STATE_CRITICAL
elif [ $comm -eq 6 ]; then
        echo "CRITICAL - Interface $ifacename is not present - possible hotswap in progress."
        exit $STATE_CRITICAL
elif [ $comm -eq 7 ]; then
        echo "CRITICAL - Interface $ifacename is down due to a lower layer being down."
        exit $STATE_CRITICAL
else
	echo "UNKNOWN - No info is being retrieved"
	exit $STATE_UNKNOWN
fi

