Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Network Interface Usage Monitoring in C/C++

I am in a situation where I am extremely bandwidth limited and must devote most of the bandwidth to transferring one type of measurement data. Sometimes I will be sending out lots of this measurement data and other times I will just be waiting for events to occur (all of this is over a TCP socket).

I would like to be able to stream out the full data capture file (different than the measurements) in the background at a speed that is inversely proportional to the amount of measurements that I am sending back.

I am looking for a way to monitor how many bytes are being sent out the network interface in much the same was as the system monitor on Ubuntu. The source code for the system monitor relies on gnome libraries and since my program is on an embedded device, I would like to reduce the number of external libraries that I use. Does anybody know of a way to do this in C/C++ without many additional libraries on a standard Linux distribution?

like image 275
Adam A. Avatar asked Oct 20 '25 15:10

Adam A.


2 Answers

One of the simplest ways is to parse the file: /proc/net/dev

Mine contains:

Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:   44865    1431    0    0    0     0          0         0    44865    1431    0    0    0     0       0          0
  eth0:150117850  313734    0    0    0     0          0         0 34347178  271210    0    0    0     0       0          0
  pan0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0

You could then write a parser that uses nothing other than the C/C++ libraries.

like image 82
hookenz Avatar answered Oct 23 '25 04:10

hookenz


Bytes transmitted and recieved accessable via /sys/class/net/eth0/statistics/tx_bytes and /sys/class/net/eth0/statistics/rx_bytes files.

$ cat /sys/class/net/net1/statistics/rx_bytes 
1055448
like image 31
eri Avatar answered Oct 23 '25 04:10

eri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!