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.

Aurora0001
  • 6,357
  • 3
  • 25
  • 39
HoisZyrian
  • 57
  • 4

1 Answers1

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
Gerard H. Pille
  • 461
  • 2
  • 5