2

I created a simple script for nagios to check a port on a remote server. Originally, I tried to use lsof.

#!/bin/bash

RESULT=$(lsof -i :8080 | grep LISTEN)

if [ -n "$RESULT" ]
  then
  echo "$RESULT"
exit 0
  else
   echo "Check application port is down"
exit 2
fi

On the local machine, the nagios user could run and execute the script and get the expected output. But remotely, using check_nrpe, I always got the else echo back

Check application port is down

Even though as I said running the script locally it worked properly

The nrpe user had all rights needed to execute the script, SELinux was in permissive mode, but I could never get it to work. So I changed the RESULT line to use netstat.

RESULT=$(netstat -anp | grep ":8080" | grep LISTEN)

And then I could get the expected response back using check_nrpe remotely. So I assume this is some sort of problem with check_nrpe and lsof. But I could never find anything in the logs.

Anyone have any ideas or suggestions to get this to work right with lsof?

3

Non-root users don't see any output from lsof -i. (Confirmed on Debian 8 and Cent 7, without SELinux.)

If you wanted to use this in a nagios check, you'd have to run lsof through sudo.

If sudo is configured to require a tty (might be the default on CentOS), you can do something like this:

Defaults:nagios !requiretty
nagios ALL=NOPASSWD:/usr/bin/lsof

Replace nagios with nrpe if necessary, and modify path to lsof if necessary.

|improve this answer|||||
  • I gave the nagios user sudo for lsof and called sudo in the nrpe.cfg file. I even tried RESULT=$(sudo lsof -i :8080 | grep LISTEN) in the script and it still did not work. I confirmed sudo lsof worked locally with the account, just remotely it does not. – user53029 Apr 25 '16 at 23:49
  • See updated answer – Keith Apr 26 '16 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.