Monday, May 7, 2012

debugging nagios remote nrpe commands

Nagios NRPE debugging steps:
  1. run the command manually on the target host
  2. enable debugging in nrpe.cfg and watch syslog
  3. dig in deeper with debug jobs.
Debugging nagios remote nrpe commands can feel very opaque. Normally I find my issue in step 1 of the debug escalation. Today I had to hit all three steps while debugging a test that wrapped around s3cmd. I eventually found that HOME is not set in the environment used to run nrpe commands.
check_nrpe -H -c check_ui_s3_backup                                 
NRPE: Unable to read output
I run my check remotely and receive the dreaded general error "unable to read output." This means the script failed to run and didn't produce any output to STDOUT. STDERR seems to be ignored, even with logging enabled.

Step 1a: go to the server and verify the command being run by nrpe.

[andrew@ip-10-7-202-92]% grep check_ui_s3_backup /etc/nagios/nrpe.d/herbie.cfg
command[check_ui_s3_backup]=HOME=~postgres /usr/lib/nagios/plugins/herbie/check_ui_s3_backup
Step 1b: run the command manually. Here I find that the script fails if I don't have the config file:
[andrew@ip-10-7-202-92]% /usr/lib/nagios/plugins/herbie/check_ui_s3_backup
ERROR: /home/andrew/.s3cfg: No such file or directory
ERROR: Configuration file not available.
ERROR: Consider using --configure parameter to create one.
That should be a simple fix. Find the user running the nrpe command and give them a .s3cfg. Easy-Peasy.
cp .s3cfg ~nagios/
sudo -u nagios -H /usr/lib/nagios/plugins/herbie/check_ui_s3_backup
OK - Last backup 0 days ago.
Ok, it works locally. Recheck it remotely. It fails?!!?! This is where we start the gnashing of teeth and pulling of hair
[andrew@ip-10-7-203-10]% check_nrpe -H -c check_ui_s3_backup
NRPE: Unable to read output
Step 2: Enable logging in nrpe.cfg, run the remote check and inspect the logs. Surprise, nothing useful.
May  7 19:08:29 ip-10-7-202-92 nrpe[17159]: Connection from port 15286
May  7 19:08:29 ip-10-7-202-92 nrpe[17159]: Host address is in allowed_hosts
May  7 19:08:29 ip-10-7-202-92 nrpe[17159]: Handling the connection...
May  7 19:08:29 ip-10-7-202-92 nrpe[17159]: Host is asking for command 'check_ui_s3_backup' to be run...
May  7 19:08:29 ip-10-7-202-92 nrpe[17159]: Running command: /usr/lib/nagios/plugins/herbie/check_ui_s3_backup
May  7 19:08:29 ip-10-7-202-92 nrpe[17159]: Command completed with return code 3 and output: 
May  7 19:08:29 ip-10-7-202-92 nrpe[17159]: Return Code: 3, Output: NRPE: Unable to read output
May  7 19:08:29 ip-10-7-202-92 nrpe[17159]: Connection from closed.
Step 3: debug jobs (aka printf aka "hail marry" debugging). I create two new nrpe entries and restart nagios-nrpe-server. The first will show me the user running the command and the second will show the environment, using whoami and env respectively.
[andrew@ip-10-7-203-10]% check_nrpe -H -c check_ui_test
[andrew@ip-10-7-203-10]% check_nrpe -H -c check_ui_test2
Yep, the tests are running as the expected user, nagios.

Holy Schmoly! Look at that environment! HOME is not set. Simple enough to fix for my check, but wow was I not expecting that. Also useful to note the minimal PATH and that the working directory is /.

Update check to explicitly set HOME in the environment and restart nrpe:

command[check_ui_s3_backup]=HOME=~nagios /usr/lib/nagios/plugins/herbie/check_ui_s3_backup
Restart nrpe:
[andrew@ip-10-7-202-92]% sudo service nagios-nrpe-server restart
 * Stopping nagios-nrpe nagios-nrpe
 * Starting nagios-nrpe nagios-nrpe
[andrew@ip-10-7-203-10]% check_nrpe -H -c check_ui_s3_backup
OK - Last backup 0 days ago.


Anonymous said...

Man, you just save me, i was going man, i just did not check this f.....g $ENV{"HOME"}, was so sure it was correct.... thx again

Anonymous said...

Man, you just save me, i was going man, i just did not check this f.....g $ENV{"HOME"}, was so sure it was correct.... thx again

Priyesh Gaikwad said...

Hi Im getting same error "NRPE: Unable to read output"

output after adding "command[check_ui_test2]=env" is as follows.

SSH_CLIENT= 59785 22

Andrew Grangaard said...


what is the full nagios command are you are hoping to run ( the one returning "NRPE: Unable to read output" ) ? What happens if you run that command directly? Does it require HOME to be set?

Michael said...

I've a similar problem on RHEL. The environment returned by



# /usr/lib64/nagios/plugins/check_nrpe -H localhost -c check_env

No complex checks (e.g. informix 'onstat', 'dbaccess', ...) are running within the environment.

Any suggestions ?

Michael said...

After hours of recherche, I found out, that SELinux was the reason of that strange behavior. Switching off SELinux solved the problem.

Ed Greenberg said...

The absence of stderr is the most difficult part of this. I wasn't sure it would work, but I put 2>&1 on the end of my command (in nrpe.d/command.cfg) and started getting output when I ran check_nrpe from the nagios server.

After that, the resolution of the problem was simple.

Stefan Freitag said...

Got stuck at a similar problem and spent hours in debugging this NRPE thing.
Thank you for this post - it helped me to solve the problem :)

Danny Howard said...

Holy crud thank you for helping me debug my bacon.

Todd Allis said...

I turned on debug and found "timeout" wasn't installed on this one server. Thanks!