带有漂亮汤的 Python 自定义 nagios 脚本 - 获取 "NRPE: Unable to read output"

标签 python beautifulsoup nagios pacemaker

我正在尝试创建自定义 python 2 nagios 脚本,以便能够监控单个起搏器资源。 当它从文件中读取输入时,我设法让它工作,但当从 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/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:unable to read output

有趣的是,当我在目标服务器本地运行脚本时,它总是给我正确的输出。 像这样:

[root@lb-01 tmp]# /usr/lib64/nagios/plugins/check_pacemaker.py
The status of api-lb-ip is Started

我怀疑我读取命令输出的方式有问题,但无法弄清楚。有什么建议可以在哪里查找更多详细信息?

最佳答案

这可能是您在远程服务器上执行脚本的远程用户的问题。
默认行为是只有 root 可以运行集群命令。
您可以将命令 /usr/sbin/crm_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:

希望对你有帮助。

关于带有漂亮汤的 Python 自定义 nagios 脚本 - 获取 "NRPE: Unable to read output",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57430832/

相关文章:

python - 无法使用 BeautifulSoup Python 从 NBA 统计网站找到 <div ng-view>

nagios - 如何将 nagios 默认 url 更改为自定义 url?

python - 列表操作,跟踪旧列表

python - 如何将 .ost 文件解析为单独的电子邮件(最好使用 Python)

python - CNTK create_trainer 方程

linux - check_snmp_mem.pl 中的内存使用值是如何得出的?

java - 以编程方式访问 JMX 控制台的简单方法

python - DynamoDB 扫描函数返回正确的结果,但出现 DynamoDBResponseError

python - 如何使用 Beautifulsoup 访问没有任何名称的 HTML 表格

python - BeautifulSoup 如何选择带有空格的 <a href> 和 <td> 元素