From ef41a852da9a3d20b4e426ac676e242ab10a83ed Mon Sep 17 00:00:00 2001 From: Kieren Hynd Date: Wed, 1 Oct 2014 15:59:00 +0100 Subject: [PATCH] Make STDOUT flushing configurable in udp_bridge 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. --- collectors/0/udp_bridge.py | 28 ++++++++++++++++++++-------- collectors/etc/udp_bridge_conf.py | 5 ++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/collectors/0/udp_bridge.py b/collectors/0/udp_bridge.py index 20f77d76..2c8b0b0e 100755 --- a/collectors/0/udp_bridge.py +++ b/collectors/0/udp_bridge.py @@ -15,6 +15,7 @@ import socket import sys +import time from collectors.lib import utils try: @@ -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: @@ -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() diff --git a/collectors/etc/udp_bridge_conf.py b/collectors/etc/udp_bridge_conf.py index c5930f7a..b9c24475 100644 --- a/collectors/etc/udp_bridge_conf.py +++ b/collectors/etc/udp_bridge_conf.py @@ -1,4 +1,7 @@ #!/usr/bin/env python def enabled(): - return False + return True + +def flush_delay(): + return 60