Page 3 of 5

Re: Nagios Bash Script Help

Posted: Fri May 13, 2016 12:04 am
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.

Re: Nagios Bash Script Help

Posted: Fri May 13, 2016 9:15 am
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
"

Re: Nagios Bash Script Help

Posted: Fri May 13, 2016 1:22 pm
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

Re: Nagios Bash Script Help

Posted: Mon May 16, 2016 12:57 pm
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

Re: Nagios Bash Script Help

Posted: Mon May 16, 2016 5:11 pm
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

Re: Nagios Bash Script Help

Posted: Tue May 17, 2016 10:39 am
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
}

Re: Nagios Bash Script Help

Posted: Tue May 17, 2016 3:31 pm
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.

Re: Nagios Bash Script Help

Posted: Tue May 17, 2016 4:56 pm
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;}'

Re: Nagios Bash Script Help

Posted: Tue May 17, 2016 5:19 pm
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?

Re: Nagios Bash Script Help

Posted: Wed May 18, 2016 10:04 am
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;}'