0

I need to track the amount of data that my Raspberry pi is sending each month via network (WiFi and Ethernet).

Any ideas how can I get that?

5

To get a statistic about your interfaces you just can use:

rpi ~$ ip -statistics link

It will show something like:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    RX: bytes  packets  errors  dropped overrun mcast
    534        6        0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    534        6        0       0       0       0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether b8:27:eb:53:bd:de brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    80237      1007     0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    142796     1697     0       0       0       0
3: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
    link/ether b8:27:eb:06:e8:8b brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast
    0          0        0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    0          0        0       0       0       0

It is up to you to save this output before rebooting, maybe with a script, because it isn't persistent.

3

netstat -s and ip -statistics will give you a lot of raw data, but you may need to parse it, and accumulate it yourself. You might want to consider one of the many utilities designed to handle the parsing and accumulation for you.

One such tool is vnstat. It's available for RPi, it's reasonably easy to use and it can be run as a regular user (i.e. no sudo required). Install it as follows:

$ sudo apt-get update 
$ sudo apt-get upgrade
$ sudo apt-get install vnstat 

Once installed, you'll be able to generate monthly summaries similar to this one (ref) with a simple command:

$ vnstat -m

eth0 / monthly

   month        rx      |     tx      |    total    |   avg. rate
 ------------------------+-------------+-------------+---------------
   2017-11       1.05 GB |     4.32 GB |     5.37 GB |   17.78 kbit/s
   2017-12     893.89 MB |     3.68 GB |     4.55 GB |   14.60 kbit/s
   2018-01       1.14 GB |     5.10 GB |     6.24 GB |   20.01 kbit/s
   2018-02       1.21 GB |     4.93 GB |     6.14 GB |   21.80 kbit/s
   2018-03       1.23 GB |     5.64 GB |     6.87 GB |   22.03 kbit/s
   2018-04       1.15 GB |     5.22 GB |     6.37 GB |   21.11 kbit/s
   2018-05       1.23 GB |     4.39 GB |     5.62 GB |   18.04 kbit/s
   2018-06       2.68 GB |     7.01 GB |     9.69 GB |   32.10 kbit/s
   2018-07       1.61 GB |     6.94 GB |     8.55 GB |   27.41 kbit/s
   2018-08       1.76 GB |     7.65 GB |     9.41 GB |   30.19 kbit/s
   2018-09       1.82 GB |     7.99 GB |     9.81 GB |   32.52 kbit/s
   2018-10       1.71 GB |     7.64 GB |     9.35 GB |   44.80 kbit/s
 ------------------------+-------------+-------------+---------------
 estimated       2.56 GB |    11.41 GB |    13.97 GB |

Refer to man vnstat for the system manual, and man vnstat.conf for the configuration file. From the command line, vnstat --help will show a summary list of options you're most likely to use.

Additional information can be found at the vnStat website, and vnstat GitHub repository.

The install script creates a simple configuration file at /etc/vnstat.conf, as well as a service definition file for use by systemd at /lib/systemd/system/vnstat.service for starting vnstatd.

Although vnstat is easy to use, you can find tutorials online to help with some of its more advanced features. When searching for help online, do pay attention to the vintage of the information; vnstat has been around for a while, and some tutorials are quite old, and poorly maintained. A couple of tutorials that seem fairly current are this one for Ubuntu, and this one for Linux systems in general.

Finally: If you feel vnstat doesn't meet your needs, there are numerous other utilities that provide network statistics. Here's a couple of Q&A on other SE sites that list several with brief summaries:Ubuntu, Unix&Linux

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.