2

I would simply like to know how many MB the Pi sends/receives in a day. What's the easiest way to do this? I'm using the built in Ethernet adapter. I've tried searching but surprisingly haven't found anything.

  • I like ntop - simple to install via apt-get but after that somewhat complex. All sorts of good info by host, etc. – ivanivan Jun 10 '18 at 18:21
4

Every day, at a fixed time, run "ifconfig" and store the results. A little math on TX and RX lines will provide the information you need.

There is a little caveat: don't reboot.

Sysstat's sar will do the math for you. My VM during a lunch break:

11:08:23 AM     IFACE   rxerr/s   txerr/s    coll/s  rxdrop/s  txdrop/s  txcarr/s  rxfram/s  rxfifo/s  txfifo/s
Average:      docker0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:        vnet0      0.00      0.50      0.00      0.03      0.00      0.00      0.00      0.00
Average:         eth0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:           lo      0.22      0.22      0.07      0.07      0.00      0.00      0.00      0.00
Average:         eth1      7.97      2.39      9.95      0.19      0.00      0.00      0.00      0.00
Average:    virbr0-nic      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:       virbr0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

I've got this script, called nettraffic.sh, which you could run once a day:

mv /tmp/nettraffic.txt /tmp/nettraffic.old 2>/dev/null
/sbin/ifconfig \
| awk '
  /^[a-z0-9]*: / {
    NDEV=$1
    ND[NDEV]=1
  }
  $1 ~ /^[RT]X$/ && $2 == "packets" {
    NTAP[NDEV "=" $1] = $3
    NTAB[NDEV "=" $1] = $5
  }
  END {
    for (NDEV in ND) {
      print NDEV, "TX packets", NTAP[NDEV "=TX"], "bytes", NTAB[NDEV "=TX"], \
                  "RX packets", NTAP[NDEV "=RX"], "bytes", NTAB[NDEV "=RX"]
    }
  }
' > /tmp/nettraffic.txt
if [ -f /tmp/nettraffic.old ]
then
  awk 'BEGIN {
    while (getline <"/tmp/nettraffic.old" ) {
      NTAPT[$1]=$4
      NTABT[$1]=$6
      NTAPR[$1]=$9
      NTABR[$1]=$11
    }
    while (getline <"/tmp/nettraffic.txt" ) {
      print $1, "TX packets", ($4 - NTAPT[$1]), "bytes", ($6 - NTABT[$1]), \
                "RX packets", ($9 - NTAPR[$1]), "bytes", ($11 - NTABR[$1])
    }
  }'
fi

If you run it, you get something like:

$ /tmp/nettraffic.sh   
eth0: TX packets 125 bytes 19217 RX packets 160 bytes 76617
lo: TX packets 18 bytes 6168 RX packets 18 bytes 6168
  • I agree 100% with this good answer, just want to add: one could use Munin to automatically collect, store and visualize the data like described here – Fabian Jun 8 '18 at 10:18
  • Or sysstat, if you can't be bothered to do the math yourself or to cater for the reboots. See the "sar" I added to my answer. – Gerard H. Pille Jun 8 '18 at 11:33
  • 1
    To use sar you will have to install the systat package (apt install sysstat) and configure system data sampling (see man sysstat and man sadc). – goldilocks Jun 8 '18 at 11:59
  • I'm new at Linux and could use another hint. I can setup chron to run ifconfig at a scheduled time each day, and append it to the same log file with >>. This could get large quickly and I was wondering how just the relevant lines could be extracted? This does seem a bit complex so I'm open to better ideas? – HoisZyrian Jun 10 '18 at 7:01
  • I'll write you a script, but sysstat does all this and more. – Gerard H. Pille Jun 10 '18 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.