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 = getinput_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)
I'm doing exactly the same in several modular inputs. It should work with no issue. Can you describe more precisely what happens when you run it?
If I run without the time.sleep, everything goes fine, i get a lot of answers...
If I run with, I have no errors in the splunk.d logs, and nothing being indexed...
Did you try to run the script by hand? For example like:
$SPLUNKHOME/bin/splunk cmd splunkd print-modinput-config yourmodinputname | $SPLUNKHOME/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.
I tried on windows, put I only see a black new terminal, without anyting... no output, is there something I have to set to verbose ?
Actually you should see what your method printxmlsingleinstancemode() 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...
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
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.