tags:

views:

194

answers:

2

I'm looking for a way to programatically (whether calling a library, or a standalone program) monitor live ip traffic in linux. I don't want totals, i want the current bandwidth that is being used. I'm looking for a tool similar (but non-graphical) to OS X's istat menu's network traffic monitor.

I'm fairly certain something like this exists, but I'm not sure where to look, and i'd rather not have to reinvent the wheel.

Is it as simple as monitoring a socket? Or do I need a utility that handles alot of overhead for me?

+7  A: 

We have byte and packet counters in /proc/net/dev, so:

import time

last={}

def diff(col): return counters[col] - last[iface][col]

while True:
  print "\n%10s: %10s %10s %10s %10s"%("interface","bytes recv","bytes sent", "pkts recv", "pkts sent")
  for line in open('/proc/net/dev').readlines()[2:]:
    iface, counters = line.split(':')
    counters = map(int,counters.split())
    if iface in last:
      print "%10s: %10d %10d %10d %10d"%(iface,diff(0), diff(8), diff(1), diff(9))

    last[iface] = counters

  time.sleep(1)
mrk
And on a 2.6 kernel, there's more detailed per-device statistics in `/sys/class/net/$dev/statistics`
ephemient
perfect! thanks so much
contagious
A: 

I use a little program known as dstat It combines a lot "stat" like functions into 1 quick output. Very customizable. It will give you current network throughput as well as much more.

In linux the program netstat will give you raw network statistics. You could parse these stats yourself to produce meaningful output (which is what dstat does).

James