Combine 2 SNMP Service Checks into a graph & calculation

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

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

Post by ssax »

That should work.
User avatar
BanditBBS
Posts: 2474
Joined: Tue May 31, 2011 12:57 pm
Location: Scio, OH
Contact:

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

Post 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
2 of XI5.6.14 Prod/DR/DEV - Nagios LogServer 2 Nodes
See my projects on the Exchange at BanditBBS - Also check out my Nagios stuff on my personal page at Bandit's Home and at github
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

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

Post by ssax »

Thanks for posting that BanditBBS!

perric, let us know if that will work for you or if you have any other questions.
perric
Posts: 161
Joined: Fri Mar 28, 2014 10:37 am

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

Post 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
User avatar
BanditBBS
Posts: 2474
Joined: Tue May 31, 2011 12:57 pm
Location: Scio, OH
Contact:

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

Post 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)
2 of XI5.6.14 Prod/DR/DEV - Nagios LogServer 2 Nodes
See my projects on the Exchange at BanditBBS - Also check out my Nagios stuff on my personal page at Bandit's Home and at github
User avatar
eloyd
Cool Title Here
Posts: 2190
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

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

Post by eloyd »

What complicated grepping. Why not just awk '{print $3}' to grab the bytes instead of grep -oP? :)
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoyd
I'm a Nagios Fanatic! • Join our public Nagios Discord Server!
User avatar
BanditBBS
Posts: 2474
Joined: Tue May 31, 2011 12:57 pm
Location: Scio, OH
Contact:

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

Post 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
2 of XI5.6.14 Prod/DR/DEV - Nagios LogServer 2 Nodes
See my projects on the Exchange at BanditBBS - Also check out my Nagios stuff on my personal page at Bandit's Home and at github
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

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

Post by tmcdonald »

Thanks for the help, Bandit!

@perric, does that answer everything for you? Does the script work as you need?
Former Nagios employee
perric
Posts: 161
Joined: Fri Mar 28, 2014 10:37 am

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

Post 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
You do not have the required permissions to view the files attached to this post.
User avatar
BanditBBS
Posts: 2474
Joined: Tue May 31, 2011 12:57 pm
Location: Scio, OH
Contact:

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

Post 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.
2 of XI5.6.14 Prod/DR/DEV - Nagios LogServer 2 Nodes
See my projects on the Exchange at BanditBBS - Also check out my Nagios stuff on my personal page at Bandit's Home and at github
Locked