当我用 Nagios 调用 ps 时,为什么我的 Perl 脚本中只返回一行输出?
我正在运行这个:
if (open(PS_ELF, "/bin/ps -eLf|")) {
while (<PS_ELF>) {
if ($_ =~ m/some regex/) {
# do some stuff
}
}
}
如果在本地调用,则循环运行得很好,对于 ps -eLf
的每个输出行一次。
现在,如果从 Nagios 通过 NRPE, PS_ELF
仅包含一行(ps
输出的第一行)。
这让我很困惑; 可能是什么原因?
也许这根本不限于 Nagios/由 Nagios 引起,我只是为了完整起见将其包括在内。
我使用的是 SUSE Enterprise Linux 10 SP2 和 perl v5.8.8。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然这个问题很老了,但我今天遇到了完全相同的问题。
所以我想我分享我的发现。
问题在于,由 NRPE 守护进程创建的进程(可能)与您作为 NRPE 守护进程用户直接在 shell 中执行的进程具有不同的环境。
我创建了以下 bash 脚本:
这为我提供了当前进程的环境变量 COLUMN,它与父进程(由 NRPE 守护进程分叉的进程)具有相同的环境。
当我以 NRPE 守护程序用户身份执行此脚本时,
它会提供当前 shell 窗口的值。
但是,当我通过 NRPE 执行此脚本时,我得到:
这就是为什么 ps -eaf 输出限制为 80 个字符,除非您使用 ww 参数来实现无限宽度,这会忽略 COLUMNS 环境变量。
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:
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
it gives me the value of my current shell window.
But when I execute this script via NRPE, I get:
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.
我将“ps -eLf”更改为“ps -eLfww”(ww 表示无限输出),这解决了问题,即使我不明白为什么远程调用时会有差异。
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.
与 Perl 本身相比,这可能更多地与 NRPE 插件的工作方式有关。
您的插件是否按照此处(返回代码+输出)的说明工作?
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) ?