I have this script which is run by nagios which checks the provider API if mitigation is enabled and reports back. I just copied an example from Nagios, I have no Python knowledge whatsoever. The problem is that sometimes the scripts needs 10 seconds to run and python just continues, so I need it to wait for execution.

I found some examples using subprocess which were successful but I don't know how to add the .readline and .strip to the command.

This is the original script:

#!/usr/bin/python
import os, sys
mitigation_enabled=os.popen("/usr/local/nagios/libexec/check_mitigation.py 
| grep auto | awk '{print $2}'").readline().strip()

if mitigation_enabled == "false":
    print "OK - Mitigation disabled." .format (mitigation_enabled)
    sys.exit(0)
elif mitigation_enabled == "true":
    print "WARNING - Mitigation enabled." .format (mitigation_enabled)
    sys.exit(1)
else:
    print "UKNOWN - mitigation status unknown." .format (mitigation_enabled)
    sys.exit(2)

So how do I make this with subprocess, wait for the execution of the external script and add the .readline and .strip values?

Short question, how to make this work :)

Thanks!

1 Answers

0
J_H On

You are complaining that the ancient historic API permits "short reads" of zero bytes. Yes, that is correct, it is working as designed.

Recommend you use subprocess directly.

Also, two nits about awk '{print $2}':

  1. Delete the grep by invoking awk '/auto/ {print $2}'.
  2. Delete the overhead of the awk child by using split().