From 565b041c23761dda44b8ac2cf8336d12fe88d579 Mon Sep 17 00:00:00 2001 From: Stefan Koegl Date: Sun, 30 May 2010 10:15:36 +0200 Subject: [PATCH] various bugfixes --- bwmon/__init__.py | 1 + bwmon/aggregator.py | 18 +++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/bwmon/__init__.py b/bwmon/__init__.py index 4adc108..8520541 100644 --- a/bwmon/__init__.py +++ b/bwmon/__init__.py @@ -67,6 +67,7 @@ class Monitor(object): if len(cmd) > 60: cmd = cmd[:57] + '...' print '%10d / %10d -- %s' % (bytes[0], bytes[1], cmd) + sys.stdout.flush() def loop(self): while True: diff --git a/bwmon/aggregator.py b/bwmon/aggregator.py index 8aa32a1..dfb83b9 100644 --- a/bwmon/aggregator.py +++ b/bwmon/aggregator.py @@ -16,8 +16,7 @@ class Aggregator(): records = collections.defaultdict(list) for line in proc.stdout: - - record = self.get_record(line) + record, process = self.get_record(line) if not record: continue @@ -27,22 +26,24 @@ class Aggregator(): if len(records[process]) > 1: prev_record = records[process][-2] bw = self.get_bandwidth(prev_record, record) - # process bandwidth + + if bw: + print '%10d / %10d B/s -- %s' % (bw[0], bw[1], process) def get_record(self, line): match = self.line_regex.match(line) if not match: - return None + return None, None process = match.group('proc').strip() bytes_in = int(match.group('in')) bytes_out = int(match.group('out')) record = {'timestamp': datetime.now(), 'in': bytes_in, 'out': bytes_out} + return record, process - - def get_bandwidth(rec1, rec2): + def get_bandwidth(self, rec1, rec2): """ returns the mean incoming and outgoing bandwidth used between the two given records. @@ -50,9 +51,12 @@ class Aggregator(): rec1 represents the earlier, rec2 the later record """ date_diff = rec2['timestamp'] - rec1['timestamp'] - in_diff = rec2['in'] - rec1['diff'] + in_diff = rec2['in'] - rec1['in'] out_diff = rec2['out'] - rec1['out'] + if not date_diff.seconds: + return None + in_bw = in_diff / date_diff.seconds out_bw = out_diff / date_diff.seconds -- 2.11.4.GIT