Writing Custom Plugin with String output

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
chrisbooth
Posts: 43
Joined: Thu Dec 01, 2016 10:09 am

Writing Custom Plugin with String output

Post 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?
chrisbooth
Posts: 43
Joined: Thu Dec 01, 2016 10:09 am

Re: Writing Custom Plugin with String output

Post 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>
Last edited by tmcdonald on Wed Dec 07, 2016 12:48 pm, edited 1 time in total.
Reason: Please use [code][/code] tags around code output
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: Writing Custom Plugin with String output

Post 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.
Former Nagios employee
gormank
Posts: 1114
Joined: Tue Dec 02, 2014 12:00 pm

Re: Writing Custom Plugin with String output

Post 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.
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Writing Custom Plugin with String output

Post by dwhitfield »

@chrisbooth, is @gormank's response helpful for you? Thanks for contributing @gormank!
chrisbooth
Posts: 43
Joined: Thu Dec 01, 2016 10:09 am

Re: Writing Custom Plugin with String output

Post 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
gormank
Posts: 1114
Joined: Tue Dec 02, 2014 12:00 pm

Re: Writing Custom Plugin with String output

Post 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.
chrisbooth
Posts: 43
Joined: Thu Dec 01, 2016 10:09 am

Re: Writing Custom Plugin with String output

Post 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
You do not have the required permissions to view the files attached to this post.
gormank
Posts: 1114
Joined: Tue Dec 02, 2014 12:00 pm

Re: Writing Custom Plugin with String output

Post 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?
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Writing Custom Plugin with String output

Post 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?
Locked