Python自定义nagios脚本,带有漂亮的“喝汤”功能;NRPE:无法读取输出“;
我正在尝试创建一个定制的Python2Nagios脚本,以便能够监视单个起搏器资源。 当从文件中读取输入时,我设法使其工作,但当从cli收集输入时,我无法使其工作 所以它是这样工作的:Python自定义nagios脚本,带有漂亮的“喝汤”功能;NRPE:无法读取输出“;,python,beautifulsoup,nagios,pacemaker,Python,Beautifulsoup,Nagios,Pacemaker,我正在尝试创建一个定制的Python2Nagios脚本,以便能够监视单个起搏器资源。 当从文件中读取输入时,我设法使其工作,但当从cli收集输入时,我无法使其工作 所以它是这样工作的: from __future__ import print_function from bs4 import BeautifulSoup import os,sys with open ("/tmp/crm_output.txt","r") as f: #with os.popen ("/usr/sbin/cr
from __future__ import print_function
from bs4 import BeautifulSoup
import os,sys
with open ("/tmp/crm_output.txt","r") as f:
#with os.popen ("/usr/sbin/crm_mon -r -X") as f:
contents = f.read()
soup = BeautifulSoup(contents, 'lxml')
resource_status = soup.find("resource").attrs["role"]
resource_name = soup.find("resource").attrs["id"]
if resource_status == "Started":
print("The status of " +resource_name + " is " + resource_status)
sys.exit(0)
elif resource_status == "Stopped" or resource_status == "Stopped (disabled)":
print("The status of " +resource_name + " is " + resource_status)
sys.exit(1)
elif resource_status == "Failed":
print("The status of " +resource_name + " is " + resource_status)
sys.exit(2)
else:
print("The status of " +resource_name + " is " + "UNKNOWN")
sys.exit(3)
但如果我取消注释这一行:
以os.popen(“/usr/sbin/crm_mon-r-X”)作为f:
为了让它从cli读取输入,它给了我NRPE:cannotreadoutput
有趣的是,当我在目标服务器上本地运行脚本时,它始终为我提供正确的输出。
像这样:
[root@lb-01 tmp]# /usr/lib64/nagios/plugins/check_pacemaker.py
The status of api-lb-ip is Started
我怀疑我读取命令输出的方式有问题,但无法理解它。有什么建议可以在哪里查找更多详细信息吗?这可能是在远程服务器上执行脚本的远程用户的问题。
默认行为是只有
root
可以运行集群命令。您可以将命令
/usr/sbin/crm\u mon-r-X
添加到/etc/sudoers
中,如下所示:
nrpe ALL=(ALL) NOPASSWD: /usr/sbin/crm_mon -r -X
用远程用户替换nrpe
。
您还需要编辑Python脚本并在命令之前添加sudo
:
with os.popen ("sudo /usr/sbin/crm_mon -r -X") as f:
希望它能有所帮助