I modified the helloworld in the python modular input example, to poll a website, and calculate the latency.
I don't understand why it is not working when I add a time.sleep, without, it is workint !!?
config = get_input_config()
#TODO , poll for data and print output to STD OUT
while True : try: interval=float(config['interval']) #time.sleep(interval) start_timer = time.time() resp = urllib2.urlopen(config['url']) content = resp.read() latency = time.time() - start_timer print_xml_single_instance_mode( "time=" + str(start_timer) + " latency=" + str(round(latency,2)) + " interval=" + str(interval) ) assert (resp.code == 200), 'Bad HTTP Response' #assert ('Example Web Page' in content), 'Failed Content Verification' except RuntimeError,e: logging.error("Looks like an error: %s" % str(e)) sys.exit(2)
It's because sys.stdout is buffered. In a sleep loop you need to explicitly flush the buffer.With the sleep the script exits and flushes the buffer for you.
Hi in fact the only change in the script is here, when I uncomment #time.sleep... it doesn't work, it is commented, then it works.
The rest ist the normal helloword from the python sdk modular input
Actually you should see what your method print_xml_single_instance_mode() prints to stdout, which should get indexed later. If you like to, you can send me over your script and I'll try to debug it...
Did you try to run the script by hand? For example like:
$SPLUNK_HOME/bin/splunk cmd splunkd print-modinput-config yourmodinputname | $SPLUNK_HOME/bin/splunk cmd python $SPLUNK_HOME/etc/apps/yourappname/bin/yourmodinputname.py
This always helped me to debug the modular inputs since it's the way how splunk will launch them.