Nagios2 own developed plugin

Hello Forum!
I wrote a liitle nagios2 plugin to check processess on remote host using ssh.

[code]#!/bin/bash

Define exit codes

SERVER="${1}"
STATE_OK=0
STATE_CRITICAL=2

function usage(){
echo "$0 "
exit
}
if ${SERVER} == “” ]]
then
usage
fi
K2=“ssh -q -i /etc/ssh/ssh_host_dsa_key ${SERVER} \"exit \ps awfx|grep -v grep|grep apache2|wc -l|awk ‘{print $1}’`”`$?"
echo “exit_status=$? K2=$K2”

if “$K2” == “15” ]]
then
echo "$? OK - ${K2} K2 Services are running"
else
echo "$? CRITICAL - system found ${K2} K2 daemons! Please check configuration."
fi
[/code]

Problem: each time nagios runs this script it gives values:

but if you run it manually you will see proper values of exit code and number of daemons (taken from remote host). What am I doing wrong here? Why nagios keep printing 255 values. My guess is that nagios see those as exit codes which aren’t defined and for this output it is printing 255. If so, how it can be fixed?
Thanks in advance!

Hi!

I’m not sure that it will work better, but as a rule, you should always use full paths to the programs you’re calling (like ssh, ps, grep …etc), because nagios may not have the same env variables as yours.

Try it; maybe it will solve your problem (or not …) :slight_smile:

Thanks for your reply Loose.
I changed binary paths but it did not solve my problem. Still getting annoying 255 code in plugin output. Any other ideas? Did you write any similar script - maybe I could take a look at it?
BTW - funny thing is that nagios is showing this service as OK (STATUS OK). Weird, isn’t it?

PS. Similar script = getting remote stats using ssh only :wink:

Why have you got that awk at the end of your K2 command? Does your “wc -l” output more than one column?
Have you tried running the script as your nagios user? Your nagios user might need ssh key validation before it can run that ssh command.

Also, you aren’t exiting that script with any exit code aside from default (0 = OK), which is why nagios thinks that the service is still OK. You’ll need this for nagios to go critical when it should:


echo "$? CRITICAL - system found ${K2} K2 daemons! Please check configuration."
exit 2
fi

MP: awk just fix the output for me. If you use only wc -l command you will get 4-5 spaces between actual value which looks a little odd with sentence: “system found ${K2} K2 daemons” …
I haven’t tried (yet) to run my plugin as nagios user. However, I used little debugging script to see what is going on and one time I saw nagios having problems with ssh auth and after that I added -i switch to point to private key - message gone. I will try to run nagios as usual, with changed private key to nagios user.

Exit codes - yes, I used them. First version of script exits with 0 when K2 = 15 and with code 2 when K2 != 15… Then I realized it was useless because nagios seems to depend only on exit code from K2 command:

No luck though…
Could you please try to modify my script to follow your thoughts? I have run out of ideas …