Page 2 of 2

Re: Problems with my own plugins

Posted: Thu Jul 31, 2014 1:50 am
by turboscrew
That's probably because the "usage" is printed to stderr instead of stdout.

Re: Problems with my own plugins

Posted: Thu Jul 31, 2014 2:30 am
by turboscrew
I wonder if this relates to the OP's problem?

# su -c "/etc/nagios/check_omat 20% 10% 1>/dev/null" nrpe
# su -c "/etc/nagios/check_omat 20% 10% 2>/dev/null" nrpe
SERVICE STATUS: OK
# su -c "/usr/lib64/nagios/plugins/check_nrpe -H localhost -c check_omat 20% 10% " nrpe
NRPE: Unable to read output
# su -c "/usr/lib64/nagios/plugins/check_nrpe -H localhost -c check_omat\!20%\!10% " nagios
NRPE: Unable to read output

Re: Problems with my own plugins

Posted: Thu Jul 31, 2014 11:33 am
by brunocardoso
tmcdonald i dont understaing what you write about linking with bc. Because with nagios user and root works well the variables.

Re: Problems with my own plugins

Posted: Thu Jul 31, 2014 4:29 pm
by Box293
turboscrew wrote:That's probably because the "usage" is printed to stderr instead of stdout.
Ahh I see, thanks for explaining that, I tried it with a full grep command and it worked via NRPE.

But this is where I get stumped.

Here is the first command in his script (I needed to change f5 to f8):

Code: Select all

#!/bin/bash
usadakb=`top -n1 | grep Mem | cut -d" " -f8 | cut -d"k" -f1`
echo "==="
echo $usadakb
echo "---"
Then when executed as the local nagios user, the following output is produced:

Code: Select all

===
528280
---
When executed via NRPE, the following output is produced:

Code: Select all

===

---
This is where I get confused

Re: Problems with my own plugins

Posted: Fri Aug 01, 2014 7:18 pm
by brunocardoso
yeah my tests is the same like yours.

Re: Problems with my own plugins

Posted: Sat Aug 02, 2014 9:34 pm
by Box293
OK I've worked it out. Basically top was outputting an error and we needed to switch to batch mode using -b in our top command.

Big thanks to turboscrew who put me onto the path of the output being directed to stderr !!!

Here's how I worked it out:

Code: Select all

#!/bin/bash
echo "==="
echo `top -n1 2>&1`
echo "---"
When run via NRPE this outputs:

Code: Select all

===
top: failed tty get
---
A quick google revealed we needed to use the -b option with top. So now:

Code: Select all

#!/bin/bash
echo "==="
echo `top -b -n1 2>&1`
echo "---"
When run via NRPE this outputs:

Code: Select all

===
top - 11:59:15 up 42 min, 4 users, blah blah blah blah
---
SO here's a full running version of your command with the following changes:
  • On the usadakb line I added -b to the top command
    On the usadakb line I needed to change -f5 to -f8 (most likely your OS language outputs slightly differently)
    On the totalkb line I needed to change -f8 to -f9 (most likely your OS language outputs slightly differently)
    I also changed your output message but removing the pipe symbols and putting each one in curtly brackets, also seeing as it was the same message for each state type I turned it into a variable
Enjoy

Code: Select all

#!/bin/bash
usadakb=`top -b -n1 | grep Mem | cut -d" " -f8 | cut -d"k" -f1`
totalkb=`cat /proc/meminfo | grep MemTotal | cut -d " " -f9`
a=`echo "$usadakb * 100"|bc`
usada=`echo "$a / $totalkb"|bc`
livre=`echo "100 - $usada"|bc`
livrekb=`echo "$totalkb - $usadakb"|bc`
message="{Usada: $usadakb KB $usada%} {Livre: $livrekb KB $livre%} {Total: $totalkb KB}"

if [ $usada -lt 90 ]; then
        echo "OK - $message"
        exit 0
fi
if [ $usada -gt 90 ]; then
       	echo "WARNING - $message"
        exit 1
fi
if [ $usada -gt 95 ]; then
        echo "CRITICAL - $message"
        exit 2

Re: Problems with my own plugins

Posted: Mon Aug 04, 2014 11:56 am
by brunocardoso
Yeah works really well with me. I work around grep command. But now i read the batch mode in manual page and understanding this.

Re: Problems with my own plugins

Posted: Mon Aug 04, 2014 12:17 pm
by brunocardoso
wow. i forget to thank you :)

Re: Problems with my own plugins

Posted: Mon Aug 04, 2014 12:33 pm
by tmcdonald
So is it safe to close this thread now?