Nagios Bash Script Help

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: Nagios Bash Script Help

Post by Box293 »

So the crux of your issue I believe is because it's all done in a one liner and it all ties back to the exit code.
spyder13337 wrote:i ran this

/usr/local/nagios/libexec/check_snmp -H xx.xx.xx.xx -C XXXXX -o .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1 -w one -c two


and got this
SNMP OK - 67 | iso.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1=67;one;two

then i ran echo $?

and got this

0
Remember us getting you to test the exit code? This is what a plugin returns back to bash when it runs.
Every time a command is run, it outputs an exit code, for example:

Code: Select all

/usr/local/nagios/libexec/check_dummy 2 'my exit message, yo'
CRITICAL: my exit message, yo

echo $?
2

echo $?
0
In that example, check dummy was used to produce an exit code, 2.
Then we echo $? and it shows us 2
Then we echo $? again but this time it shows us 0 .... what the ???

To go back to what I said earlier, every time a command is run, it outputs an exit code.
"echo $?" is a command, and that command completed successfully so it outputted an exit code of 0.

How does this relate to your problem?
spyder13337 wrote:

Code: Select all

/usr/local/nagios/libexec/check_snmp -H xx.xx.xx.xx -C xxxxx -o .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1 -w $1 -c $2 | awk -F'|' '{split($1,array_left,"-");} {split($2,array_right,"=");} {print array_left[1]"- "array_left[2]/10"|"array_right[1]"="array_right[2]/1$
The check_snmp program runs and then pipes it's output to the awk program. So you are getting the exit code from the awk program.

So you're going to need to record the exit code from check_snmp and use it when you exit.

Code: Select all

#!/bin/bash
snmp_output=$(/usr/local/nagios/libexec/check_snmp -H xx.xx.xx.xx -C XXXXX -o .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1 -w $1 -c $2)
snmp_exit_code=$?
echo "$snmp_output" | awk -F'|' '{split($1,array_left,"-");} {split($2,array_right,"=");} {print array_left[1]"- "array_left[2]/10"|"array_right[1]"="array_right[2]/1$
exit $snmp_exit_code
I haven't tested this, but it should work.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
spyder13337
Posts: 68
Joined: Tue Oct 06, 2015 9:50 pm

Re: Nagios Bash Script Help

Post by spyder13337 »

so from my understanding i need a exit from my AWK command ok so do i need to create a separate wrapper script by it self or do i need to include this in the original script because if i run this by it self i get error msg

"test: line 5: unexpected EOF while looking for matching `''
test: line 7: syntax error: unexpected end of file
"
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Nagios Bash Script Help

Post by rkennedy »

The script @Box293 wrote does that, by preserving $? as a variable after the command runs -

Code: Select all

#!/bin/bash
snmp_output=$(/usr/local/nagios/libexec/check_snmp -H xx.xx.xx.xx -C XXXXX -o .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1 -w $1 -c $2)
snmp_exit_code=$?
echo "$snmp_output" | awk -F'|' '{split($1,array_left,"-");} {split($2,array_right,"=");} {print array_left[1]"- "array_left[2]/10"|"array_right[1]"="array_right[2]/1$
exit $snmp_exit_code
Former Nagios Employee
spyder13337
Posts: 68
Joined: Tue Oct 06, 2015 9:50 pm

Re: Nagios Bash Script Help

Post by spyder13337 »

so i ran this script and i get
test: line 5: unexpected EOF while looking for matching `''
test: line 8: syntax error: unexpected end of file"

#!/bin/bash

snmp_output=$(/usr/local/nagios/libexec/check_snmp -H xx.xx.xx.xx -C XXXXX -o .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1 -w $1 -c $2)
snmp_exit_code=$?
echo "$snmp_output" | awk -F'|' '{split($1,array_left,"-");} {split($2,array_right,"=");} {print array_left[1]"- "array_left[2]/10"|"array_right[1]"="array_right[2]/1$
exit $snmp_exit_code
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: Nagios Bash Script Help

Post by Box293 »

Try this:

Code: Select all

#!/bin/bash
snmp_output=$(/usr/local/nagios/libexec/check_snmp -H xx.xx.xx.xx -C XXXXX -o .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1 -w $1 -c $2)
snmp_exit_code=$?
final_output=$(echo "$snmp_output" | awk -F'|' '{split($1,array_left,"-");} {split($2,array_right,"=");} {print array_left[1]"- "array_left[2]/10"|"array_right[1]"="array_right[2]/10;}')
echo "$final_output"
exit $snmp_exit_code
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
spyder13337
Posts: 68
Joined: Tue Oct 06, 2015 9:50 pm

Re: Nagios Bash Script Help

Post by spyder13337 »

this is my output from the script you wanted to run


SNMP WARNING - 0| iso.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1=6.8


this is my script (which has the same result from your script becuase that what i was running)




#!/bin/bash
snmp_output=$(/usr/local/nagios/libexec/check_snmp -H xx.xx.xx.xx -C XXXXX -o .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1 -w $1 -c $2)
snmp_exit_code=$?
final_output=$(echo "$snmp_output" | awk -F'|' '{split($1,array_left,"-");} {split($2,array_right,"=");} {print array_left[1]"- "array_left[2]/10"|"array_right[1]"="array_right[2]/10;}')
echo "$final_output"
exit $snmp_exit_code

this is my service.cfg

define service{
use generic-service,srv-pnp
host_name Metered Rack PDU 241
service_description APC Metered Total
check_command 241_total_load

my command.cfg

# '241_total_load' command definition
define command{
command_name 241_total_load
command_line $USER1$/241_total_load
}
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Nagios Bash Script Help

Post by rkennedy »

You need to pass warning / critical values to $1 and $2. You could do this using $ARG1$ values, or hard coding it in the command definition.
Former Nagios Employee
spyder13337
Posts: 68
Joined: Tue Oct 06, 2015 9:50 pm

Re: Nagios Bash Script Help

Post by spyder13337 »

i think issue lies in the script it self if i just run the script with the AWK command it works just fine...if i add -w $1 -c $2 then it only give me snmp critical and stay green

so at this point i update the service .cfg file

use generic-service,srv-pnp
host_name Metered Rack PDU 241
service_description APC Metered Total
check_command 241_total_load!5!6

my command file

# '241_total_load' command definition
define command{
command_name 241_total_load
command_line $USER1$/241_total_load -w $ARG1$ -c $ARG1$
}


and my script is this

/usr/local/nagios/libexec/check_snmp -H xx.xx.xx.xx -C xxxxxx -o .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1 -w $1 -c $2 | awk -F'|' '{split($1,array_left,"-");} {split($2,array_right,"=");} {print array_left[1]"- "array_left[2]/10"|"array_right[1]"="array_right[2]/10;}'
Last edited by spyder13337 on Wed May 18, 2016 9:29 am, edited 1 time in total.
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: Nagios Bash Script Help

Post by Box293 »

Code: Select all

# '241_total_load' command definition
define command{
        command_name    241_total_load
        command_line    $USER1$/241_total_load -w $ARG1$ -c $ARG1$
}
Two different things need correcting here.

First, the second $ARG1$ needs to be $ARG2$.

But that all needs to be changed because:

This is the command in the script:

Code: Select all

/usr/local/nagios/libexec/check_snmp -H 12.34.228.241 -C univoice -o .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1 -w $1 -c $2
$1 is the first value passed to the script, $2 is the second value passed to the script.

Using the corrected command definition above:

Code: Select all

check_command 241_total_load!5!6
Nagios creates the command:

Code: Select all

/usr/local/nagios/libexec/check_snmp -H 12.34.228.241 -C univoice -o .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1 -w -w -c 5
This is because in your command definition you are defining -w and -c.
Your script is not looking for specific argument flags called -w and -c.
In a bash script,
$1 = the first value passed to it
$2 = the second value passed to it

Hence you are passing:
-w as the first value
the value of $ARG1$ the second value

So your command definition needs to be:

Code: Select all

# '241_total_load' command definition
define command{
        command_name    241_total_load
        command_line    $USER1$/241_total_load $ARG1$ $ARG2$
}
Your service definition is correct.

If you wanted to test at the command line you would use:

Code: Select all

/usr/local/nagios/libexec/241_total_load 5 6
Does this make sense?
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
spyder13337
Posts: 68
Joined: Tue Oct 06, 2015 9:50 pm

Re: Nagios Bash Script Help

Post by spyder13337 »

it really makes perfect sense and i cant see any reason it not working but...it not working when i run the cript it give me this msg

/usr/local/nagios/libexec/241_total_load 5 6
SNMP CRITICAL - 0| iso.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1=6.6

/usr/local/nagios/libexec/241_total_load
SNMP WARNING - 0| iso.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1=6.6

/usr/local/nagios/libexec/241_total_load 120 150 (was not expecting these results)
SNMP OK - 6.5| iso.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1=6.5

my scrpt

/usr/local/nagios/libexec/check_snmp -H xx.xx.xx.xx -C xxxxx -o .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1 -w $1 -c $2 | awk -F'|' '{split($1,array_left,"-");} {split($2,array_right,"=");} {print array_left[1]"- "array_left[2]/10"|"array_right[1]"="array_ri$ray_right[2]/10;}'
Locked