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:

import os, sys
| grep auto | awk '{print $2}'").readline().strip()

if mitigation_enabled == "false":
    print "OK - Mitigation disabled." .format (mitigation_enabled)
elif mitigation_enabled == "true":
    print "WARNING - Mitigation enabled." .format (mitigation_enabled)
    print "UKNOWN - mitigation status unknown." .format (mitigation_enabled)

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 :)


1 Answers

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().