Re: [dev] up/down KB/s retrieval?

From: Kris Maglione <maglione.k_AT_gmail.com>
Date: Fri, 9 Oct 2009 03:58:14 -0400

On Fri, Oct 09, 2009 at 01:52:16AM -0400, Guy wrote:
>I'd like the status bar to also indicate the current transfer rates,
>something along the lines of:
>0.22 | 763M | 137.2 KB/s Up - 344.8 KB/s Down | 10/09 0147

There are a few solutions. Your best bet is probably to poll the
TX/RX statistics and take a sliding window average. But just how
you do it will vary from system to system.

Let's assume python and modern-ish Linux for now.

#!/usr/bin/env python
import io, operator, sys, time

dir = "/sys/class/net/%s/statistics" % sys.argv[1]
txfile = io.open("%s/tx_bytes" % dir, "r")
rxfile = io.open("%s/rx_bytes" % dir, "r")
def read(file):
     file.seek(0)
     return int(file.read())

WIN = 10
tx_val = [0 for i in range(0, WIN)]
rx_val = [0 for i in range(0, WIN)]
times = [time.time() for i in range(0, WIN)]
i = 0
while True:
     i += 1
     times[i % WIN] = time.time()
     tx_val[i % WIN] = read(txfile)
     rx_val[i % WIN] = read(rxfile)
     diff = lambda ary: ary[i % WIN] - ary[(i - 1) % WIN]
     avg = lambda ary: diff(ary) / diff(times)
     print '%d %d' % (avg(tx_val), avg(rx_val))
     time.sleep(1)

That will give you the average TX/RX in bytes for the past ten
seconds. Or you could drop the sliding average and do it in
shell and expr. Whatever works.

>I prefer avoiding anything like 'conky' if possible.

Come on, all the script kiddies are using conky these days.
You're just not 1337 enough.

-- 
Kris Maglione
Long hair minimizes the need for barbers; socks can be done without;
one leather jacket solves the coat problem for many years; suspenders
are superfluous.
	--Albert Einstein
Received on Fri Oct 09 2009 - 07:58:14 UTC

This archive was generated by hypermail 2.2.0 : Fri Oct 09 2009 - 08:00:01 UTC