Getting Data In

time.sleep not working in modular input ?

Builder

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 !!?

def do_run():
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) 
0 Karma
1 Solution

Ultra Champion

Try adding a sys.stdout.flush() after the print_xml_single_instance_mode line.

View solution in original post

Ultra Champion

Try adding a sys.stdout.flush() after the print_xml_single_instance_mode line.

View solution in original post

Ultra Champion

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.

0 Karma

Builder

could you explain me why this is needed ?

0 Karma

Builder

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
http://dev.splunk.com/view/python-sdk/SP-CAAAEBB

0 Karma

Contributor

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

0 Karma

Builder

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 ?

0 Karma

Contributor

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.

Builder

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

0 Karma

Contributor

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?

Cheers,
Simon

0 Karma