Search code examples
pythonbeautifulsoupnagiospacemaker

Python custom nagios script with beautiful soup - getting "NRPE: Unable to read output"


I am trying to create a custom python 2 nagios script in order to be able to monitor individual pacemaker resources. I managed to have it working when its reading the input from a file but I cannot manage to make it work when the input is being collected from the cli.

So like this it works:


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)

but if I uncomment this line:

with os.popen ("/usr/sbin/crm_mon -r -X") as f:

to have it read the input from the cli,it gives me the NRPE:unable to read output

The interesting thing is that when I run the script locally in the target server its giving me the correct ouput all the time. Like this:

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

I suspect there is something wrong with how I read the output from the command but not being able to figure it out. Any recommendations where to look for more details?


Solution

  • This can be problem with your remote user which execute your script on remote server.
    Default behavior is that only root can run cluster commands.
    You can add your command /usr/sbin/crm_mon -r -X to /etc/sudoers something like:

    nrpe            ALL=(ALL)       NOPASSWD: /usr/sbin/crm_mon -r -X
    

    Replace nrpe with your remote user. You also need edit your Python script and add sudo before your command:

    with os.popen ("sudo /usr/sbin/crm_mon -r -X") as f:
    

    Hope that it helps.