3

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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Node
  • 21,706
  • 2
  • 31
  • 35
  • Try piping the output into a file to make sure that 'ps' is running as expected, inside the script. Maybe something like `/bin/ps -eLf > /tmp/psout.txt`; – codelogic Feb 04 '09 at 22:49
  • Thanks! :) I tried this and funnily enough every line of "ps -eLf" is cut after 80 chars. But just when I run the script via nrpe/remote. :-/ – Node Feb 04 '09 at 22:56
  • Hm, ok I found the error.. I changed "ps -eLf" to "ps -eLfww" and everything works fine. But I still can't explain why there is a difference between lokal and remote execution. Maybe some env variables. – Node Feb 04 '09 at 23:05

3 Answers3

2

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.

Node
  • 21,706
  • 2
  • 31
  • 35
2

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.

xorpaul
  • 207
  • 3
  • 10
1

It's probably more something to do with how NRPE plugins work than Perl itself.

Your plugin is working like explained here (return code + output) ?

sebthebert
  • 12,196
  • 2
  • 26
  • 37
  • Yes, output and return code are set as documented. :-/ I've uploaded the complete script, may this helps to understand my problem: http://home.arcor.de/reik/check_threads – Node Feb 04 '09 at 22:22