Page 1 of 2

Writing Custom Plugin with String output

Posted: Wed Dec 07, 2016 11:38 am
by chrisbooth
Here's an example of the command reporting no errors

sh-3.2# /usr/local/aw/bin/nsdchat -s awsock:/admin:Operation8@localhost:9001 -c Job failed 1
<empty>

Here's an example of the check reporting errors
sh-3.2# /usr/local/aw/bin/nsdchat -s awsock:/admin:Operation8@localhost:9001 -c Job failed 90
11305 11306 11353 11371 11382 11389 11390

I'm guessing i need to put this into a script with an 'if statement' that anything other than an <empty> output is critical

can somebody help me with this?

Re: Writing Custom Plugin with String output

Posted: Wed Dec 07, 2016 11:57 am
by chrisbooth

Code: Select all

#!/bin/bash
input=$1
./check_by_ssh -H "$1" -l admin -C '/usr/local/aw/bin/nsdchat -s awsock:/admin:Operation8@localhost:9001 -c Job failed 90' &> /dev/tty
if [ $? == 0 ]; then
   echo "no errors"
   exit 0
else
  echo "errors"
  exit 2
fi
i just need to fix my:
if [ $? == 0 ]
..to match the string <empty>

Re: Writing Custom Plugin with String output

Posted: Wed Dec 07, 2016 12:50 pm
by tmcdonald
You'll want to look into this article:

http://unix.stackexchange.com/questions ... nly-spaces

However, this is not entirely in-scope for this forum since this is a general bash programming question. If you need further assistance with programming tasks, please direct your questions to a forum dedicated to that topic.

Re: Writing Custom Plugin with String output

Posted: Wed Dec 07, 2016 1:57 pm
by gormank
To clarify:

$? contains the integer return status of the previous command, not text.
STDOUT and/or STDERR is/are the text returned. Typically both go to the console so you need to know they aren't the same.

Nagios alerts (or not) on the return value ($?), and adds the STDOUT text to the service status. If there's no STDOUT, Nagios adds text complaining about the lack of output.

So matching $? to an empty string is not correct since it isn't a string.

Re: Writing Custom Plugin with String output

Posted: Wed Dec 07, 2016 2:06 pm
by dwhitfield
@chrisbooth, is @gormank's response helpful for you? Thanks for contributing @gormank!

Re: Writing Custom Plugin with String output

Posted: Wed Dec 07, 2016 4:23 pm
by chrisbooth
thank you so much for you help.
i've got my script working but i think my exit codes must be incorrect
because it returns OK in nagios XI no matter what.
is my exit code syntax ok?

Code: Select all

while [[ $# -gt 1 ]]
do
key="$1"
case $key in
    -H|--hostname)
    HOSTNAME="$2"
    shift # past argument
    ;;
    *)
            # unknown option
    ;;
esac
shift # past argument or value
done
if ./check_by_ssh -H "$HOSTNAME" -l admin -C '/usr/local/aw/bin/nsdchat -s awsock:/admin:Operation8@localhost:9001 -c Job failed 90' | grep -v empty > /dev/tty; then
   echo "Syncs Failed"
   exit 2
else
   echo "OK"
   exit 0
fi

Re: Writing Custom Plugin with String output

Posted: Wed Dec 07, 2016 4:29 pm
by gormank
Try opening the service in CCM, click test, give it a hostname and run it. Then grab the command line it shows and copy it to the clipboard. Open a shell window on the Nagios server host, su - nagios, and paste the command. Once it runs, do an echo $? to get the return value. Try it when it should return ok and when it should create an alert.

Post all that.

Re: Writing Custom Plugin with String output

Posted: Wed Dec 07, 2016 4:40 pm
by chrisbooth
here is the check test command
Screen Shot 2016-12-07 at 21.36.53.png
here's the exact command in the terminal
Screen Shot 2016-12-07 at 21.37.45.png

Re: Writing Custom Plugin with String output

Posted: Wed Dec 07, 2016 4:52 pm
by gormank
One thing about the test in Nagios is, it runs as the apache user, so you can't always trust it. When Nagios runs checks they run as nagios. Just FYI.
From the outputs, it kind of looks like some of what's printed is going to STDERR. You might try redirecting STDERR to STDOUT in the script when you run the command by putting 2>&1 at the end. This is just for the text, not the return value.

I don't really see why it would always be ok in Nagios, since $? is clearly 2, which should be critical. You have it running regularly as a service in Nagios?

Re: Writing Custom Plugin with String output

Posted: Wed Dec 07, 2016 5:08 pm
by dwhitfield
It is possible to reverse the Nagios outputs, but I don't see you doing that (https://assets.nagios.com/downloads/nag ... ios-XI.pdf).

What happens of you change exit 2 to exit 0, and the else to exit 2? Also, what happens if you change the first one to some random out of bounds error like 255?