Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Live RX and TX rates in linux

Tags:

c++

python

c

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?

like image 708
helloandre Avatar asked Nov 22 '25 21:11

helloandre


2 Answers

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)
like image 165
mrk Avatar answered Nov 25 '25 10:11

mrk


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).

like image 45
mox1 Avatar answered Nov 25 '25 11:11

mox1