Skip to content

Commit

Permalink
Make STDOUT flushing configurable in udp_bridge
Browse files Browse the repository at this point in the history
See #155.
To enable, create a flush_delay() function in etc/udp_bridge_conf.py
that returns an integer representing seconds (defaults to 60s).
A flush_delay <= 0 will be near to per-packet flushing of STDOUT.
  • Loading branch information
hynd committed Oct 1, 2014
1 parent f6c4ca9 commit ef41a85
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
28 changes: 20 additions & 8 deletions collectors/0/udp_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import socket
import sys
import time
from collectors.lib import utils

try:
Expand All @@ -31,19 +32,25 @@ def main():
sys.exit(13)
utils.drop_privileges()

def removePut(line):
if line.startswith('put '):
return line[4:]
else:
return line

try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((HOST, PORT))
except socket.error, msg:
sys.stderr.write('could not open socket: %s\n' % msg)
utils.err('could not open socket: %s' % msg)
sys.exit(1)

def removePut(line):
if line.startswith('put '):
return line[4:]
else:
return line
try:
flush_delay = udp_bridge_conf.flush_delay()
except AttributeError:
flush_delay = 60

flush_timeout = int(time.time())
try:
try:
while 1:
Expand All @@ -52,11 +59,16 @@ def removePut(line):
lines = data.splitlines()
data = '\n'.join(map(removePut, lines))
if not data:
sys.stderr.write("invalid data\n")
utils.err("invalid data")
break
print data
now = int(time.time())
if now > flush_timeout:
sys.stdout.flush()
flush_timeout = now + flush_delay

except KeyboardInterrupt:
sys.stderr.write("keyboard interrupt, exiting\n")
utils.err("keyboard interrupt, exiting")
finally:
sock.close()

Expand Down
5 changes: 4 additions & 1 deletion collectors/etc/udp_bridge_conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/bin/env python

def enabled():
return False
return True

def flush_delay():
return 60

0 comments on commit ef41a85

Please sign in to comment.