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,308
  • 3
  • 23
  • 38
HoisZyrian
  • 57
  • 4
  • 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

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
  • 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 08 '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 08 '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 08 '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 07:01
  • I'll write you a script, but sysstat does all this and more. – Gerard H. Pille Jun 10 '18 at 07:14
  • I concocted something, see my answer. – Gerard H. Pille Jun 10 '18 at 09:52
  • @GerardH.Pille thanks a lot for the script. I'm having trouble getting it to write to file with Cron. Do you see an error in 1 0 * * * /home/pi/nettraffic.sh >> /home/pi/nettraffic.log (nettrafic.log has already been created) – HoisZyrian Jun 11 '18 at 07:21
  • Who's cron is this, and who created /home/pi/nettraffic.log? I'd add a " 2>&1" to the end of the line, so that possible errors would also go in the log. Is cron actually running? – Gerard H. Pille Jun 11 '18 at 09:02
  • @GerardH.Pille the user pi created the cron entry and created /home/pi/nettraffic.log. How can I tell if cron is running? I set it up with crontab -e – HoisZyrian Jun 14 '18 at 06:04
  • Also to confirm my understanding is correct, ifconfig reports back the total amount of network traffic since the last system reboot? – HoisZyrian Jun 14 '18 at 06:06
  • Yes, I believe you can't reset it without rebooting. Is it running now? – Gerard H. Pille Jun 14 '18 at 06:39
  • @GerardH.Pille thanks for continued help. I had to reinstall Rasbian. I'm busy with my paid job and haven't had time to look into this. I'll try to update this when I get it working. – HoisZyrian Jun 20 '18 at 06:31
  • You're letting a paid job come before the raspberry? Where are your priorities? – Gerard H. Pille Jun 20 '18 at 07:07