2

I am trying to write a small script to gather network statistics on my Linux system. Is there a way I can do this?

What I'm after is live statistics on the current network throughput. I have done some reading and found that /proc/net/dev is useful, and I can parse in the content of this to work out current network speed. Is this a viable/reliable way of doing this? How do packages like iptraf and iftop calculate speed up and down?

2
  • BTW: Once you get to 15 rep (won't take long if you stick around), you'll be able to up-vote answers that you find helpful. You can also—at any rep level—pick one answer as the one that answers your question, if you feel any of them do (that's the checkmark to the left of each answer).
    – derobert
    Commented Aug 6, 2014 at 12:32
  • @derobert yep, i found both answers good and right. Thanks I'm new to the site so I can't do much yet :)
    – W Khan
    Commented Aug 6, 2014 at 13:11

3 Answers 3

4

Yes, /proc/net/dev is the normal way of doing this. You can also use the files in /sys/class/net/eth0/statistics if you find that easier.

Alternatively, moreutils contains an ifdata script that'll get this information for you. For example, to print number of bytes in (-sib) and out (-sob), you'd do:

$ ifdata -sib -sob eth0
48115944587
71982675360

Output is in the same order as the flags, so that's 48… in and 71… out.

It'll also calculate the bits/sec over the last second for you (by doing a sleep 1, basically):

$ ifdata -bips -bops eth0
1148
1755
2
  • you have a typo: second sib should be sob in line #6
    – Elad Katz
    Commented Dec 9, 2017 at 0:39
  • @EladKatz Thank you for pointing that typo out, I fixed it.
    – derobert
    Commented Dec 13, 2017 at 19:04
3

Are you familiar with sar?

You can try sar -n ALL to get all possible network statistics, or if you want rx and tx statistics per network device, every second - try this:

 sar -n DEV 1

For a 5 second avarage of rx and tx for eth0 (for example) do:

sar -n DEV 1 5 | grep -i eth0 | tail -n1 | awk '{print $5, $6}'
1

In the past I have used the output of ifconfig to gather statistics on throughput and write it to graphite. Each interface has a RX and TX counter which counts the bytes of throughput. You only need to write a script that periodically polls ifconfig and writes the difference between the current value and the previous value to screen or a file.

eth0      Link encap:Ethernet  HWaddr 00:0c:29:cf:12:d3  
          inet addr:10.100.3.26  Bcast:10.100.3.31  Mask:255.255.255.248
          inet6 addr: fe80::20c:29ff:fecf:12d3/64 Scope:Link
          inet6 addr: 2001:xxx:7927:3::26/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:160523475 errors:0 dropped:921 overruns:0 frame:0
          TX packets:106097000 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2912933876 (2.7 GiB)  TX bytes:3734512667 (3.4 GiB)

eth1      Link encap:Ethernet  HWaddr 00:0c:29:cf:12:dd  
          inet addr:10.100.0.1  Bcast:10.100.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fecf:12dd/64 Scope:Link
          inet6 addr: 2001:xxx:7927::1/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:92858590 errors:0 dropped:0 overruns:0 frame:0
          TX packets:142257564 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1849495529 (1.7 GiB)  TX bytes:389856127 (371.7 MiB)

eth2      Link encap:Ethernet  HWaddr 00:0c:29:cf:12:e7  
          inet addr:10.100.4.1  Bcast:10.100.4.255  Mask:255.255.255.0
          inet6 addr: 2001:xxx:7927:4::1/64 Scope:Global
          inet6 addr: fe80::20c:29ff:fecf:12e7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:10951337 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16448597 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1437098401 (1.3 GiB)  TX bytes:1634328371 (1.5 GiB)

The script is at https://gist.github.com/MerijntjeTak/1cddb08d191045e66a9c, perhaps you can draw some inspiration from that.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .