首先:我知道已经有关于这个问题的问题,但是即使使用他们的答案,我仍然不知道为什么我的命令不起作用。
我想要做的是使用NRPE在远程主机上调用脚本。 但是,这个脚本要做什么可能只能由特定用户执行(我们称他为userA )。
现在,我的NRPE命令如下所示:
command[debug_now]=/usr/local/bin/debug_now
我的sudoers有条目nagios ALL=(tomcat) NOPASSWD: /usr/local/bin/debug_now因为我注意到用户调用nrpe.cfg的脚本叫做nagios 。
脚本/usr/local/bin/debug_now看起来像:
#!/bin/sh whoami echo "Debug..." sudo -u tomcat whoami echo "Debug finished"
上面的脚本具有与其他脚本可以由用户nagios执行相同的权限。 更重要的是,当它在本地执行它相当打印:
nagios Debug... tomcat Debug finished
它一般按照预期工作。 但是,从pipe理主机中调用时,只能打印
nagios Debug... Debug finished
这可能是由NRPE: Cannot read output引起的NRPE: Cannot read output ,每当我尝试从pipe理主机中获取任何内容时都会显示。 我已经尝试添加到sudoers项目Defaults !requiretty没有成功。 我使用Debian 6.0.7。 当从pipe理主机中调用脚本时,我能做些什么来使脚本工作?
编辑。
首先,目录/etc/sudoers.d/只包含文件README。
userA真实姓名应该是tomcat让我们来看看visudo输出然后:
Defaults env_reset Defaults !requiretty root ALL=(ALL) ALL nagios ALL=(tomcat) NOPASSWD: /usr/bin/whoami %www ALL=(ALL) NOPASSWD: /bin/su - www %tomcat ALL=(ALL) NOPASSWD: /bin/su - tomcat %tomcat ALL=(ALL) NOPASSWD: /etc/init.d/tomcat stop %tomcat ALL=(ALL) NOPASSWD: /etc/init.d/tomcat start %tomcat ALL=(ALL) NOPASSWD: /etc/init.d/tomcat restart
请注意我甚至尝试了nagios ALL=(tomcat) NOPASSWD: ALL和nagios ALL=(ALL) NOPASSWD: ALL 。 我也删除了像username ALL=(ALL) NOPASSWD: ALL几个用户条目。
NRPE
$ tail -n 1 /etc/nagios/nrpe.cfg command[debug_now]=/usr/local/bin/debug_now
$ ls -l /usr/local/bin/debug_now -rwxr-xr-x 1 root staff 573 Dec 2 09:06 /usr/local/bin/debug_now $ ls -lh /usr/local | grep bin drwxrwsr-x 2 root staff 4.0K Dec 2 09:12 bin $ ls -lh /usr | grep local drwxrwsr-x 14 root staff 4.0K Oct 22 2010 local
从另一个主机
$ /usr/lib/nagios/plugins/check_nrpe -H <ip address> -c debug_now nagios Debug... Debug finished
您的sudoers条目指定了运行/usr/local/bin/debug_now ,但您的脚本指出sudo -u userA whoami 。 这不行; 无论您想要使用sudo权限运行的sudo ,都必须出现在sudoers文件中。
我试图在Debian Squeeze(6.0.10)框中复制你的问题,但我没有任何问题。 如果你设置完全像这样( 使用正确的sudo设置 ,就像MadHatter指出的那样),包括权限和模式是正确的,它将按照你的预期行事。
# ls -l /etc/sudoers.d/nagios-test -r--r----- 1 root root 44 Dec 1 11:36 /etc/sudoers.d/nagios-test # cat /etc/sudoers.d/nagios-test nagios ALL=(keith) NOPASSWD:/usr/bin/whoami # tail -n 1 /etc/nagios/nrpe.cfg command[debug]=/usr/local/bin/debug # ls -l /usr/local/bin/debug -rwxr-xr-x 1 root staff 75 Dec 1 11:37 /usr/local/bin/debug # cat /usr/local/bin/debug #!/bin/sh whoami echo "Debug..." sudo -u keith whoami echo "Debug finished"
从另一个主机:
$ ./check_nrpe -H squeeze -c debug nagios Debug... keith Debug finished
哦,上帝,我现在感到很惭愧。
一切安好。 更换sudoers后需要重新启动 nagios-nrpe-server ,重新加载是不够的。 如果只更改nrpe.cfg或其他configuration文件,则重新加载就可以了。
@MatHatter,@Keith – 谢谢你们的帮助!