Page 6 of 6

Re: Combine 2 SNMP Service Checks into a graph & calculation

Posted: Tue Aug 18, 2015 4:15 pm
by hsmith
eloyd wrote:Look carefully at my sample script for how to do it in a simple case.
Thank you!
perric wrote:Hi All,

I have been away and just got back. Thanks for all replies, etc. I know how to write the data to a file, but I need to figure out how to retrieve the data from a file.

The initial question is resolved.

David
Let us know if eloyd's solution works for you.

Thanks.

Re: Combine 2 SNMP Service Checks into a graph & calculation

Posted: Fri Aug 21, 2015 4:34 pm
by perric
Eloyd's solution is working great. Sorry - somehow I missed that post.

Thank you. Below is my final script.

David

Code: Select all

#!/bin/bash
#### Collect previous reading from temp files ####
inbound=0
[ -r "/tmp/nagios/$1.in.last" ] && inbound=$(</tmp/nagios/$1.in.last)
outbound=0
[ -r "/tmp/nagios/$1.out.last" ] && outbound=$(</tmp/nagios/$1.out.last)

#### Collect 2nd reading ####
inbound_temp2="$(/usr/local/nagios/libexec/check_snmp $1 -P 2c -C $2 -o 1.3.6.1.2.1.31.1.1.1.6.$4)"
outbound_temp2="$(/usr/local/nagios/libexec/check_snmp $1 -P 2c -C $2 -o 1.3.6.1.2.1.31.1.1.1.10.$4)"
inbound2=`echo $inbound_temp2|grep -oP '(?<=\-)(.*?)(?=\|)'`
outbound2=`echo $outbound_temp2 |grep -oP '(?<=\-)(.*?)(?=\|)'`

#### Save new readings to files for next collection
echo "$inbound2" > /tmp/nagios/$1.in.last
echo "$outbound2" > /tmp/nagios/$1.out.last

### Perform Logic here
inbound_per=$(( (($inbound2 - $inbound) * 8) / ($3 * 1000000)))
outbound_per=$(( (($outbound2 - $outbound) * 8) / ($3 * 1000000)))
total=$(( ($inbound_per + $outbound_per)/2 ))

#echo "Inbound Percent = $inbound_per"
#echo "Outbound Percent = $outbound_per"
#echo "Total Percent = $total"

perfdata="| 'inbound'=$inbound_per%;;;0;100 'outbound'=$outbound_per%;;;0;100"

if [ "$total" -ge "90" ];then
        echo "CRITICAL - Total traffic above threshold - $total $perfdata"
        exit 2
elif [ "$total" -ge "80" ];then
        echo "WARNING - Total traffic above threshold - $total $perfdata"
        exit 1
else
        echo "OK - Traffic at good percentage $perfdata"
        exit 0
fi

Re: Combine 2 SNMP Service Checks into a graph & calculation

Posted: Sat Aug 22, 2015 8:48 am
by eloyd
Glad I could help. As it says below, feel free to nominate me for an MVP award if you think I was helpful! :-)

Re: Combine 2 SNMP Service Checks into a graph & calculation

Posted: Sat Aug 22, 2015 7:13 pm
by BanditBBS
eloyd wrote:Glad I could help. As it says below, feel free to nominate me for an MVP award if you think I was helpful! :-)
No no no, nominate the guy who volunteered first and wrote the script :) or....nom us both, lol

Re: Combine 2 SNMP Service Checks into a graph & calculation

Posted: Sat Aug 22, 2015 7:40 pm
by eloyd
Haha! Yah, you can nominate Bandit if you want, too. :P

Re: Combine 2 SNMP Service Checks into a graph & calculation

Posted: Sun Aug 23, 2015 6:53 pm
by tmcdonald
@perric, are we all clear to close this up now?

Re: Combine 2 SNMP Service Checks into a graph & calculation

Posted: Tue Aug 25, 2015 1:22 pm
by perric
Yes, thanks. Good to close.

Zero values (when a device was inaccessible) were causing both negative and positive spikes in the graphs, so I updated the script to accommodate. See below.

David

Code: Select all

#!/bin/bash
#### Collect previous reading from temp files ####
inbound=0
[ -r "/tmp/nagios/$1.in.last" ] && inbound=$(</tmp/nagios/$1.in.last)
outbound=0
[ -r "/tmp/nagios/$1.out.last" ] && outbound=$(</tmp/nagios/$1.out.last)

#### Collect 2nd reading ####
inbound_temp2="$(/usr/local/nagios/libexec/check_snmp $1 -P 2c -C $2 -o 1.3.6.1.2.1.2.2.1.10.$4)"
#1.3.6.1.2.1.31.1.1.1.6.$4)"
outbound_temp2="$(/usr/local/nagios/libexec/check_snmp $1 -P 2c -C $2 -o 1.3.6.1.2.1.2.2.1.16.$4)"
#1.3.6.1.2.1.31.1.1.1.10.$4)"
inbound2=`echo $inbound_temp2|grep -oP '(?<=\-)(.*?)(?=\|)'`
outbound2=`echo $outbound_temp2 |grep -oP '(?<=\-)(.*?)(?=\|)'`

#### Save new readings to files for next collection
echo "$inbound2" > /tmp/nagios/$1.in.last
echo "$outbound2" > /tmp/nagios/$1.out.last

### Perform Logic here
#echo "$inbound"
#echo "$inbound2"
#echo "$outbound"
#echo "$outbound2"

### Avoid spikes caused by zero values, when device is not available
if [ $inbound -eq 0 ] || [ $inbound2 -eq 0 ]
  then
    $inbound_per=0
  else
    inbound_per=$(( (($inbound2 - $inbound) * 8) / ($3 * 1000000)))
  fi
#echo "$inbound_per"

if [ $outbound -eq 0 ] || [ $outbound2 -eq 0 ]
  then
    $outbound_per=0
  else
    outbound_per=$(( (($outbound2 - $outbound) * 8) / ($3 * 1000000)))
  fi
#echo "$outbound_per"

total=$(( ($inbound_per + $outbound_per)/2 ))

#echo "Inbound Percent = $inbound_per"
#echo "Outbound Percent = $outbound_per"
#echo "Total Percent = $total"

perfdata="| 'inbound'=$inbound_per%;;;0;100 'outbound'=$outbound_per%;;;0;100"

if [ "$total" -ge "90" ];then
        echo "CRITICAL - Total traffic above threshold - $total $perfdata"
        exit 2
elif [ "$total" -ge "80" ];then
        echo "WARNING - Total traffic above threshold - $total $perfdata"
        exit 1
else
        echo "OK - Traffic at good percentage $perfdata"
        exit 0
fi