Custom SNMP based memory utilization bash script for Nagios
We identified a need to monitor memory on a remote server with nagios and were unable to find a existing perl script to fit the requirement. Prior to the week i didn't have much in the way of experience with SNMP or bash scripting. 3 days into the project to solve the problem i have a working solution based on google searches and self education that is effectively reporting memory totals, usage, free, percentage used, and performing logic on the used percentage to create ok|warn|critical states in nagios for reporting purposes.
for impact, i'm sharing the script. I hope it helps you!
################################################################################
#
#Check Memory by SNMP of remote host
#By Dan Eakin - Last edit 3/1/2018
#
#This script is to acquire and math via SNMP the memory usage of a specific
#index. Based on passed variables it'll return context and state to nagios
#for use in monitoring memory on a specific server.
################################################################################
###
#Passed variables:
#$1 = host ip
#$2 = index of memory
#$3 = warn - if usage is above this value pass a warning state
#$4 = crit - if usage is above this value pass a critical state
###
#Get total
STORAGE=$(snmpget -Le -t 10 -r 5 -m ALL -v 2c -c YOURCOMMUNITYSTRINGHERE $1:161 HOST-RESOURCES-MIB::hrStorageSize.$2 | cut -d' ' -f4)
#get units
UNITS=$(snmpget -Le -t 10 -r 5 -m ALL -v 2c -c YOURCOMMUNITYSTRINGHERE $1:161 HOST-RESOURCES-MIB::hrStorageAllocationUnits.$2 | cut -d' ' -f4)
#TOTAL CALC
TOTALGB=$(bc <<< "scale=2;(($STORAGE * $UNITS / 1024 ^ 3))")
#GET USED
USED=$(snmpget -Le -t 10 -r 5 -m ALL -v 2c -c YOURCOMMUNITYSTRINGHERE $1:161 HOST-RESOURCES-MIB::hrStorageUsed.$2 | cut -d' ' -f4)
#USED CALC
USEDGB=$(bc <<< "scale=2;(($USED * $UNITS / 1024 ^ 3))")
#FREE CALC
FREE=$(bc <<< "scale=2;(($TOTALGB - $USEDGB))")
#math differences in percentages
PERCENTAGEUSED=$(bc <<< "scale=2;($USEDGB * 100 / $TOTALGB)")
#THRESHOLDS
if (( $(echo "$PERCENTAGEUSED $4" | awk '{print ($1 > $2)}') ));then
echo "CRITICAL- T: $TOTALGB U: $USEDGB F: $FREE P: $PERCENTAGEUSED"
exit 2
elif (( $(echo "$PERCENTAGEUSED $3" | awk '{print ($1 > $2)}') ));then
echo "WARNING- T: $TOTALGB U: $USEDGB F: $FREE P: $PERCENTAGEUSED"
exit 1
else
echo "OK- T: $TOTALGB U: $USEDGB F: $FREE P: $PERCENTAGEUSED"
exit 0
fi
Pretty happy with the process entirely so I'm going to say there's not much required.