Mag Gam wrote:
Is there a tool to measure network traffic? I am using ifstat but its
reporting wrong statistics. I am trying to get something similar to
eth0 , 16Mb/sec
eth1, 10Mb/sec
etc..
I need something simple :-)
I needed something like that, and I finally ended up writing a small
python script (see attachment) that reads from
/sys/class/net/<iface>/statistics and computes the rates. Works for me.
YMMV.
HTH,
Raj Kiran
--
If you can't explain it simply, you don't understand it well enough.
-- Albert Einstein
#!/usr/bin/python
# Copyright © 2008 Raj Kiran Grandhi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys, time, os, signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
class Status: pass
def get_status(interface):
s = Status()
path = "/sys/class/net/%s/statistics/" % interface
file = open(path + "tx_bytes")
s.tx_bytes = int(file.readline().strip())
file.close()
file = open(path + "rx_bytes")
s.rx_bytes = int(file.readline().strip())
file.close()
file = open(path + "tx_packets")
s.tx_packets = int(file.readline().strip())
file.close()
file = open(path + "rx_packets")
s.rx_packets = int(file.readline().strip())
file.close()
return s
def human(rate):
if rate < 1024:
return "%d B/s" % rate
if rate < 1024*1024:
return "%5.1f kB/s" % (rate/1024.0)
if rate < 1024*1024*1024:
return "%5.1f MB/s" % (rate/(1024.0*1024.0))
else:
return "%5.1f GB/s" % (rate/(1024*1024*1024.0))
devices = sorted(os.listdir("/sys/class/net"))
if len(sys.argv) > 1:
devices = sys.argv[1:]
status_queue = {}
t_start = time.time()
for dev in devices:
status_queue[dev] = []
status_queue[dev].append(get_status(dev))
interval = 1
num_run = 5
print "".center(80,'-')
print "Device".center(20) + "Download(curr/avg)".center(24) +"".center(8)+
"Upload(curr/avg)".center(24)
print "".center(20)+"Current".center(12)+"/"+"5sec
Avg".center(12)+"".center(8)+"Current".center(12)+"/"+"5sec Avg".center(12)
print "".center(80,'-')
time.sleep(interval)
while True:
t_current = time.time()
for dev in devices:
status_queue[dev].append(get_status(dev))
s_old = status_queue[dev][-2]
s_curr = status_queue[dev][-1]
s_start = status_queue[dev][0]
s_first = status_queue[dev][1]
rate_in = s_curr.rx_bytes - s_old.rx_bytes + 0.0
rate_out = s_curr.tx_bytes - s_old.tx_bytes + 0.0
rate_in_avg = (s_curr.rx_bytes - s_start.rx_bytes)/(t_current -
t_start)
rate_out_avg = (s_curr.tx_bytes - s_start.tx_bytes)/(t_current
- t_start)
delta = len(status_queue[dev])-2
if delta >= num_run:
rate_in_run = float(s_curr.rx_bytes -
s_first.rx_bytes)/delta
rate_out_run = float(s_curr.tx_bytes -
s_first.tx_bytes)/delta
else:
rate_in_run = rate_in_avg
rate_out_run = rate_out_avg
# sys.stdout.write("%16.2f%16.2f%16.2f\n" % (rate_in,
rate_in_avg, rate_in_run))
sys.stdout.write(dev.center(20)+human(rate_in).center(12)+"/"+
human(rate_in_run).center(12)+"".center(8)+
human(rate_out).center(12)+"/"+
human(rate_out_run).center(12)+"\n")
sys.stdout.flush()
if len(status_queue[dev]) > num_run+1:
del status_queue[dev][1]
# print "%s: in:%8.2f kbps out: %8.2f kbps" % (dev,
rate_in/1024.0, rate_out/1024.0)
time.sleep(interval)
sys.stdout.write("[%dA" % len(devices))