From 93896927315a641827c4b1ca5eac4b2c36957106 Mon Sep 17 00:00:00 2001 From: Thomas Perl Date: Sat, 3 Nov 2012 13:11:33 +0100 Subject: [PATCH] Update information on conntrack accounting --- README | 13 +++++++++++-- bwmon/monitor.py | 17 +++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/README b/README index 5ddbf52..9fc6441 100644 --- a/README +++ b/README @@ -25,8 +25,17 @@ a replacement, bwmon now supports the "conntrack" command-line utility from the "conntrack-utils" package. - If you get "KeyError: 'bytes'", this is a known problem, see: - http://marc.info/?l=netfilter&m=135194333402321&w=2 + Make sure to run this command to enable accounting for conntrack + (otherwise you might get a "KeyError: 'bytes'" traceback): + + sysctl -w net.netfilter.nf_conntrack_acct=1 + + Sometimes you have to wait a bit after setting this option to + make sure old connections are removed from conntrack's output. + + Related netfilter mailing list thread: + + http://marc.info/?t=135194346800001&r=1&w=2 -- More information -- diff --git a/bwmon/monitor.py b/bwmon/monitor.py index ff9d08e..2cfd3a3 100644 --- a/bwmon/monitor.py +++ b/bwmon/monitor.py @@ -155,10 +155,19 @@ class Monitor(object): for direction in ('in', 'out'): k = keys[direction] if k in self.conntrack: - if key_in in self.last_conntrack: - new_byte[direction] = int(self.conntrack[k]['bytes']) - int(self.last_conntrack[k]['bytes']) - else: - new_byte[direction] = int(self.conntrack[k]['bytes']) + try: + if key_in in self.last_conntrack: + diff = (int(self.conntrack[k]['bytes']) - + int(self.last_conntrack[k]['bytes'])) + else: + diff = int(self.conntrack[k]['bytes']) + except KeyError: + print >>sys.stderr, ("WARNING: No 'bytes' field in output (use '" + "sysctl -w net.netfilter.nf_conntrack_acct=1" + "' to enable accounting.") + sys.exit(1) + + new_byte[direction] = diff current_in, current_out = entries[process['cmd']] new_in, new_out = (new_byte['in'], new_byte['out']) -- 2.11.4.GIT