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.
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 »

I think he was trying to emphasis what he added, not what was actually there. But I could be wrong.

@perric,

Can you please post your entire script, without any modification as an attachment, rather than a code../code block?
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoyd
I'm a Nagios Fanatic! • Join our public Nagios Discord Server!
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,

My mistake on the <b>, </b>. It was left off from editing in the forum editor.

I got the script to work after swapping all $1, $2 variables for static values. How do I make use of the following variables (arguments) from the Nagios GUI?

1. IP Address/Host - do I use $HOSTNAME in the script to allow this service check to get the IP address of the host?
2. Community string
3. Link Size - will $1 map to $ARG1$ automatically?
4. WARNING thresholds - $2 will map to $ARG2$, etc.?

David
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 »

$ARGx$ is an internal Nagios variable. It does not map automatically.

However, in Unix shell scripting, $1 is the first parameter, $2 the second, and so on.

So if you called the script from Nagios as: /usr/local/nagios/libexec/plugin "$ARG1$" "$ARG2$" "$ARG3$" and so on, then in your script, ARG1 would be $1 and ARG2 would be $2, etc.

Edit - corrected "ARG2 would be $3" to "ARG2 would be $2" like a normal person would have written.
Last edited by eloyd on Tue Jul 21, 2015 8:01 am, edited 1 time in total.
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoyd
I'm a Nagios Fanatic! • Join our public Nagios Discord Server!
perric
Posts: 161
Joined: Fri Mar 28, 2014 10:37 am

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

Post by perric »

Eloyd,

Thanks.

Can the hostname be mapped directly using the $HOSTNAME variable in the shell script? Or is there a way to pass the IP address automatically as a variable, without having to specify it as an argument ($ARGx$)?

If I change the IP address on the host object, I would like this to carry through to the service objects without having to edit the service objects. I will eventually have hundreds and possibly thousands of these services.

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:Eloyd,

Thanks.

Can the hostname be mapped directly using the $HOSTNAME variable in the shell script? Or is there a way to pass the IP address automatically as a variable, without having to specify it as an argument ($ARGx$)?

If I change the IP address on the host object, I would like this to carry through to the service objects without having to edit the service objects. I will eventually have hundreds and possibly thousands of these services.

David
Its actually $HOSTNAME$ and you reference in the script/plugin by passing it as a commandline variable. You have to get this working from commandline before even thinking about how to set it up in NagiosXI ccm. You had to hard code $1 and $2??? That makes no sense to me, I had it working just great with those, the only one I had worries about was the IP. Just for the time being, try my original version again, but make sure you put 20000000000 in for the first variable and put the IP inside double quotes and try that. If it doesn't work, then let me know and I'll find a switch to actually test it with here.
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
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,

Your script was fine. The static variables were only for my own troubleshooting, because it was not working for me. The cause was a problem with the variables I was using.

The script is working great with the variables ($1, $2, etc.) added back in. See code below. Hope you do not mind, but I modified some of the calculations. And I also modifed the variables as follows:

$1 - Host IP Address
$2 - Community String
$3 - Link Bandwidth in Mbps
$4 - Cisco Device Interface Index

WARNING and CRITICAL threshold variables are still static, but that's easy to resolve.

Code: Select all

#!/bin/bash
inbound_temp="$(/usr/local/nagios/libexec/check_snmp $1 -C $2 -o 1.3.6.1.2.1.2.2.1.10.$4)"
#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 $1 -C $2 -o 1.3.6.1.2.1.2.2.1.16.$4)"
#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=$(( $inbound / ($3 * 10000000)))
outbound_per=$(( $outbound / ($3 * 10000000)))
total=$(( ($inbound_per + $outbound_per)/2 ))

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

perfdata="| 'total'=$total%;80;90;0;100 '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
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 »

Do what ya want, was just helping and wrote that super quick. Glad it's working
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
perric
Posts: 161
Joined: Fri Mar 28, 2014 10:37 am

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

Post by perric »

The script is working fine, but the values are cumulative from the SNMP check, which throws off the "percentage utilization values". As a fix, I can take two readings and then use the difference as the bandwidth usage.

To do this, I have added a sleep command (60 seconds) to the script and then I take a second reading. See below. Does this pose any impact to Nagios in terms of performance, etc.? Is there a better way to do this?

Code: Select all

#!/bin/bash
#### Collect first reading ####
inbound_temp="$(/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_temp="$(/usr/local/nagios/libexec/check_snmp $1 -P 2c -C $2 -o 1.3.6.1.2.1.31.1.1.1.10.$4)"
inbound=`echo $inbound_temp|grep -oP '(?<=\-)(.*?)(?=\|)'`
outbound=`echo $outbound_temp |grep -oP '(?<=\-)(.*?)(?=\|)'`

sleep 60

#### 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 '(?<=\-)(.*?)(?=\|)'`


### Perform Logic here
#echo "$inbound"
#echo "$outbound"
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
Note - I also changed the SNMP to version 2c, so that I could take readings from 64 bit counters, which are required for high speed Cisco interfaces. Values collected are multiplied by 8 to convert octets (bytes) to bits.

David
perric
Posts: 161
Joined: Fri Mar 28, 2014 10:37 am

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

Post by perric »

The "sleep 60" command is giving "(Service check timed out after 60.00 seconds)" errors in Nagios, so I need to come up with another approach.

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:The "sleep 60" command is giving "(Service check timed out after 60.00 seconds)" errors in Nagios, so I need to come up with another approach.

David
You can use it, you just have to tell Nagios it is OK for service checks to take that long. Its a variable in the nagios.cfg file.
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