Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 357
Node Avatar asked Dec 31 '25 22:12

Node


2 Answers

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.

like image 99
xorpaul Avatar answered Jan 02 '26 14:01

xorpaul


I changed 'ps -eLf' to 'ps -eLfww' (ww for unlimited output) and this fixed the problem even if I don't understand why there is a difference when called remotely.

like image 20
Node Avatar answered Jan 02 '26 15:01

Node



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!