#!/bin/bash
 ##################################################################################
#      MikroTik voltage sensor plugin for Nagios3                                  #
#             Michal Lasisz <michal.lasisz@gmail.com>                              #
#             I am usig it for almost 2 years with no problems                     #
#             use at Your own risk                                                 #
 ##################################################################################

PROGNAME=`basename $0`

# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

# Helper functions
usage() {
    echo -e "Usage: $PROGNAME -H <ip_address> [-w <number>] [-c <number>] [-C <community>]"
    echo -e "\nOptions:"
    echo -e " --help\n    Print detailed help screen"
    echo -e " -H ADDRESS\n    Host name od IP address"
    echo -e " -w INTEGER\n    Warning treshold   (deviation in percent -default 20)"
    echo -e " -c INTEGER\n    Critical treshold  (deviation in percent -default 30)"
    echo -e " -C STRING\n     Community string (default public)"
    echo ""
    echo "This plugin uses the 'snmpget' command included with the NET-SNMP package and awk. Make sure you have it in your system."
    echo "Michal Lasisz <michal.lasisz@gmail.com>"
}

help() {
    echo ""
    echo "This plugin reads voltage value from MikroTik Routerboard."
    echo "I am using percentage deviation because of differences in voltage reading among devices on same tower"
    usage
    echo ""
}
cleanref() {
    echo ""
    echo "Cleaning reference files from /tmp"
    echo ""
    rm -f /tmp/mt-ref-volt-*
    echo "Done"
}

# Main
critical=30
warning=20
protocol="2c"
community="public"
Host=""
#Process parameters
while test -n "$1"; do
	case "$1" in
			--help)
			    help
			    exit $STATE_OK
			    ;;
			-w)
			    warning=$2
			    shift
			    ;;
			-c)
			    critical=$2
			    shift
			    ;;
			--clean)
			    cleanref
			    exit $STATE_OK
			    ;;
			-H)
			    Host="$2"
			    shift
			    ;;
			-C)
			    community="$2"
			    shift
			    ;;
			*)
			    echo "Unknown argument: $1"
			    echo "Usage: $PROGNAME --help"
			    exit $STATE_UNKNOWN
			    ;;
	esac
	shift
done

if [ "$Host" == "" ]; then
    echo "STATE UNKNOWN - I need host parameter to be specified"
    exit $STATE_UNKNOWN
fi

if [ "$community" == "" ]; then
    echo "STATE UNKNOWN - I need community parameter to be specified"
    exit $STATE_UNKNOWN
fi

voltage=$(snmpget -c $community -v $protocol -OEqv $Host  1.3.6.1.4.1.14988.1.1.3.8.0 2>/dev/null)

# voltage length is 0, bad ip, snmp off, not rb capable of voltage measuring etc...
if [ -z $voltage ] ; then
    echo "STATE UNKNOWN"
    exit $STATE_UNKNOWN
fi

#check if  reference voltage is present if not create reference voltage file, if exists read
if [ -f /tmp/mt-ref-volt-$Host ]
then
    reference_voltage=$(cat /tmp/mt-ref-volt-$Host)
else
    echo $voltage  > /tmp/mt-ref-volt-$Host
    reference_voltage=$voltage
fi

res=$(awk -v volt=$voltage -v ref_volt=$reference_voltage -v crit=$critical -v warn=$warning 'BEGIN {perc=(100-((volt/ref_volt)*100));if (perc < 0) perc=(perc * (-1));if (perc >crit) print "CRITICAL"; else if (perc > warn) print "WARNING"; else print "OK"; }')

#For debug purposes
#echo "Reference voltage: $reference_voltage"
#echo "Current voltage reading: $voltage"

# Non-zero return code, something happend
if [ $? -ne "0" ] ; then
    echo "STATE UNKNOWN"
    exit $STATE_UNKNOWN
fi

# Check if we met critical or warning threshold of $voltage and deal with it accordingly
if [ $res == "CRITICAL" ];then
    echo "REF:$reference_voltage STATE CRITICAL - $voltage"
    exit $STATE_CRITICAL
elif [ $res == "WARNING" ];then
    echo "REF:$reference_voltage STATE WARNING - $voltage"
    exit $STATE_WARNING
elif [ $res == "OK" ];then
    echo "STATE OK - $voltage"
    exit $STATE_OK
else
    echo "REF:$reference_voltage STATE UNKNOWN - $voltage"
    exit $STATE_UNKNOWN
fi
