Page 2 of 6

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

Posted: Wed Jul 15, 2015 12:53 pm
by ssax
That should work.

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

Posted: Wed Jul 15, 2015 3:44 pm
by BanditBBS
Very ugly script, but here ya go!

Call it whatever you want, for this example, we'll go with "banditisawesome.sh".
Call it like this, ./banditisawesome.sh totalsize warn crit IP, for example: ./banditisawesome.sh 10000000000 80 90 10.197.0.11

Code: Select all

#!/bin/bash
inbound_temp="$(/usr/local/nagios/libexec/check_snmp $4 -C public -o 1.3.6.1.2.1.2.2.1.10.15)"
#inbound_temp="SNMP OK - 3788642629 | iso.3.6.1.2.1.2.2.1.10.15=3788642629c"
outbound_temp="$(/usr/local/nagios/libexec/check_snmp $4 -C public -o 1.3.6.1.2.1.2.2.1.16.15)"
#outbound_temp="SNMP OK - 4243923118 | iso.3.6.1.2.1.2.2.1.16.15=4243923118c"
inbound=`echo $inbound_temp|grep -oP '(?<=\-)(.*?)(?=\|)'`
outbound=`echo $outbound_temp |grep -oP '(?<=\-)(.*?)(?=\|)'`
### Perform Logic here
inbound_per=$(echo $inbound  $1|awk '{ print $1/$2*100 }')
inbound_per=`printf %.0f $inbound_per`
outbound_per=$(echo $outbound  $1|awk '{ print $1/$2*100 }')
outbound_per=`printf %.0f $outbound_per`
total=$(( $inbound_per + $outbound_per ))

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

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

if [ "$total" -ge "$3" ];then
        echo "CRITICAL - Total traffic above threshold - $total $perfdata"
        exit 2
elif [ "$total" -ge "$2" ];then
        echo "WARNING - Total traffic above threshold - $total $perfdata"
        exit 1
else
        echo "OK - Traffic at good percentage $perfdata"
        exit 0
fi
Note: I did not try it in action actually using the $4 for IP. I was testing with your example output, but I think my code is good.

Example output:

Code: Select all

WARNING - Total traffic above threshold - 80 | 'total'=80%;80;99;0;100 'inbound'=38%;;;0;100 'outbound'=42%;;;0;100

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

Posted: Thu Jul 16, 2015 9:04 am
by ssax
Thanks for posting that BanditBBS!

perric, let us know if that will work for you or if you have any other questions.

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

Posted: Mon Jul 20, 2015 11:23 am
by perric
Thanks Bandit. A few questions... sorry, but I am a novice with Nagios.

I understand inbound_temp, inbound, inbound_per, etc., but can you confirm if I am understanding the variables that appear to be "called" or submitted when running the script?

$1 - ?
$2 - Maximum or Total Link speed
$3 - Threshold Level - 80%, for example
$4 - Device IP Address

How does this data appear into a Nagios chart? Is that done via the $perfdata variable that Nagios will receive as the output from this script?

Will Nagios read the WARNING and CRITICAL Values echoed from the script for alerting purposes?

How can I define colors for the chart data, so that my inbound data is one color (ex: red) and outbound is another color (ex: blue)?

David

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

Posted: Mon Jul 20, 2015 12:35 pm
by BanditBBS
perric wrote:Thanks Bandit. A few questions... sorry, but I am a novice with Nagios.

I understand inbound_temp, inbound, inbound_per, etc., but can you confirm if I am understanding the variables that appear to be "called" or submitted when running the script?

$1 - ?
$2 - Maximum or Total Link speed
$3 - Threshold Level - 80%, for example
$4 - Device IP Address

How does this data appear into a Nagios chart? Is that done via the $perfdata variable that Nagios will receive as the output from this script?

Will Nagios read the WARNING and CRITICAL Values echoed from the script for alerting purposes?

How can I define colors for the chart data, so that my inbound data is one color (ex: red) and outbound is another color (ex: blue)?

David
$1 - Link speed
$2 - Warning threshold for total used speed
$3 - Critical threshold for total used speed
$4 - Device IP address

Yes, $perfdata variable is all the data for the charts to be created. the | designates the end of results, beginning of performance data so nagios can properly parse the data. Nagios doesn't assign WARNING or CRITICAL, that is the job of plugins and nagios just reads that result. The script I wrote adds both inbound and outbound percentage together and alerts based on the $2 and $3 variables.

NagiosXI will auto assign colors to the three charted items(total, in, out)

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

Posted: Mon Jul 20, 2015 12:40 pm
by eloyd
What complicated grepping. Why not just awk '{print $3}' to grab the bytes instead of grep -oP? :)

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

Posted: Mon Jul 20, 2015 12:55 pm
by BanditBBS
eloyd wrote:What complicated grepping. Why not just awk '{print $3}' to grab the bytes instead of grep -oP? :)
Oh hush, it works! LOL

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

Posted: Mon Jul 20, 2015 4:02 pm
by tmcdonald
Thanks for the help, Bandit!

@perric, does that answer everything for you? Does the script work as you need?

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

Posted: Mon Jul 20, 2015 4:24 pm
by perric
Bandit,

Thanks. I am not getting output so far. I added the script into Nagios as a command. I am assuming that the $1, $2, $3, etc. are arguments that can be passed from Nagios. I also tried changing the variables $1, $2, etc. to $ARG1$, $ARG2$, etc., but no luck.

Here is a screenshot of my service check screen.
check_cisco_bw.png
When I do a check from the GUI, I get the following:

COMMAND: /usr/local/nagios/libexec/check_cisco_bw.sh 20 50 80 10.192.0.11 15
OUTPUT:

I made one change to the script by adding $5, so I can specify an OID option as an argument.

Code: Select all

#!/bin/bash
inbound_temp="$(/usr/local/nagios/libexec/check_snmp $4 -C public -o 1.3.6.1.2.1.2.2.1.10.[b]$5[/b])"
#inbound_temp="SNMP OK - 3788642629 | iso.3.6.1.2.1.2.2.1.10.15=3788642629c"
outbound_temp="$(/usr/local/nagios/libexec/check_snmp $4 -C public -o 1.3.6.1.2.1.2.2.1.16.[b]$5[/b])"
#outbound_temp="SNMP OK - 4243923118 | iso.3.6.1.2.1.2.2.1.16.15=4243923118c"
From CLI, I get the following:

[root@nag1 libexec]# check_cisco_bw.sh 20 0 0 10.192.0.11 15
awk: (FILENAME=- FNR=1) fatal: division by zero attempted
awk: (FILENAME=- FNR=1) fatal: division by zero attempted
CRITICAL - Total traffic above threshold - 0 | 'total'=0%;0;0;0;100 'inbound'=0%;;;0;100 'outbound'=0%;;;0;100

David

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

Posted: Mon Jul 20, 2015 4:32 pm
by BanditBBS
Whats with the

Code: Select all

[b]
and

Code: Select all

[/b]
bolding commands around your $5? Also, according to your example outputs you gave me, the numbers were 3788642629 and 4243923118 so not sure how the first CLI argument you are supplying is only 20. You sure it shouldn't be 20000000000 ?

Try and hardcode the IP address and see if it works also, thats the one thing I changed as I was just using your example outputs and wasn't actually running the check_snmp queries. If that works, let me know and I'll fix whatever I screwed up :)

I would try it from command line before worrying if it works in Nagios. You could remove the # from some of the echo commands too, just to see what its seeing in parts of the script, maybe add at line 5

Code: Select all

echo "inbound_temp = $inbound_temp"
echo "outbound_temp = $outbound_temp"
That will show you on the CLI what it is getting back from each check_snmp line.