Search code examples
linuxperlnagiosnrpe

Why does ps only return one line of output in my Perl script when I call it with Nagios?


I have this running:

if (open(PS_ELF, "/bin/ps -eLf|")) {
  while (<PS_ELF>) {
    if ($_ =~ m/some regex/) {
      # do some stuff
    }
  }
}

If called locally, the loop runs just fine, once for every output line of ps -eLf

Now if the same script is called from Nagios via NRPE, PS_ELF does only contain one line (the first line output by ps).

This puzzles me; what could be the reason?

Maybe this is not limited to/caused by Nagios at all, I just included it for the sake of completeness.

I'm on SUSE Enterprise Linux 10 SP2 and perl v5.8.8.


Solution

  • Although this problem is very old, I experienced the exact same problem today. So I thought I share what I found. The problem is that processes created by the NRPE daemon (can) have a different environment than processes you execute directly in the shell as the NRPE daemon user.

    I created the following bash script:

    #!/bin/bash
    echo `env | grep COLUMNS`
    

    This gives me the environment variable COLUMN of the current process, which has the same environment as the parent process (the process forked by the NRPE daemon).

    When I execute this script as the NRPE daemon user

    $ /tmp/check_env.sh
    COLUMNS=174
    

    it gives me the value of my current shell window. But when I execute this script via NRPE, I get:

    nagios-server $ check_nrpe -H client -c check_env
    COLUMNS=80
    

    Which is why ps -eaf output is limited to 80 characters unless you use the ww parameter for unlimited width, which ignores the COLUMNS environment variable.