NagiosEnterprises/nrdp

send_nrdp.py not working with arguments when used as an obsessive compulsive command

box293 opened this issue · 1 comments

In send_nrdp.py this if statement is being used to determine if STDIN is being used:

if not sys.stdin.isatty():

For the situations when you are piping to the script ... however issue #16 reports this does not work.

I was configuring the script as an obsessive compulsive command for hosts and services. This was on a Nagios Core server sending results back to a central Nagios XI server. I was using the script in argument mode, passing all the arguments instead of piping them. When I do this, the xml variable in the script does not get populated with data and the script fails. You can see this in /var/log/messages with:

stderr line 01: syntax error: line 1, column 0
stdout line 01: Cannot connect to url.

Here are the command definitions:

define command{
	command_name	send_nrdp_host
	command_line	$USER1$/send_nrdp.py -u https://10.25.5.17/nrdp/ -t XXXXX -H '$HOSTNAME$' -S $HOSTSTATEID$ -o '$HOSTOUTPUT$'
	}

define command{
	command_name	send_nrdp_service
	command_line	$USER1$/send_nrdp.py -u https://10.25.5.17/nrdp/ -t XXXXX -H "$HOSTNAME$" -s "$SERVICEDESC$" -S $SERVICESTATEID$ -o "$SERVICEOUTPUT$"
	}

Here is nagios.cfg

obsess_over_services=1
ocsp_command=send_nrdp_service
obsess_over_hosts=1
ochp_command=send_nrdp_host

Here is how you can reproduce / troubleshoot.
Make the following changes to the script:
On line 114 (before self.post_data(options.url, options.token, xml)) add this:
print "xml variable: {0}" . format(xml)

Now if you test from the command line:

/usr/local/nagios/libexec/send_nrdp.py -u https://10.25.5.17/nrdp/ -t XXXXX -H "host" -s "service" -S 0 -o "test"

The output will be something like:

xml variable: <?xml version='1.0'?>
<checkresults>
<checkresult type='service' checktype='1'><hostname>host</hostname><servicename>service</servicename><state>0</state><output>test</output></checkresult></checkresults>

This shows valid data in the xml variable.

To see it fail when Nagios Core uses it, execute this command:
tail -f /var/log/messages

Now go to a service object and force a check, this will cause the obsessive command to be executed, the output will be something like:

Aug 15 17:43:37 core-015 nagios: wproc: OCSP job 45 from worker Core Worker 5429 is a non-check helper but exited with return code 1
Aug 15 17:43:37 core-015 nagios: wproc:   host=DNS1; service=PING; contact=(none)
Aug 15 17:43:37 core-015 nagios: wproc:   early_timeout=0; exited_ok=1; wait_status=256; error_code=0;
Aug 15 17:43:37 core-015 nagios: wproc:   stderr line 01: syntax error: line 1, column 0
Aug 15 17:43:37 core-015 nagios: wproc:   stdout line 01: xml variable: <?xml version='1.0'?>
Aug 15 17:43:37 core-015 nagios: wproc:   stdout line 02: <checkresults>
Aug 15 17:43:37 core-015 nagios: wproc:   stdout line 03:
Aug 15 17:43:37 core-015 nagios: wproc:   stdout line 04: Cannot connect to url.

You can see that stdout line 01 - 03 shows the content of the xml variable, it has not been correctly populated.

Tested on CentOS 7.x, Nagios Core 4.3.2, send_nrdp.py from master.

Fixed in #22