Story #3826

Updated by Tom Clegg almost 5 years ago

(TC) sysfs provides traffic statistics. I tried this in a docker container, after doing a few pings:

<pre>
# head /sys/devices/virtual/net/*/statistics/*_bytes
==> /sys/devices/virtual/net/eth0/statistics/rx_bytes <==
1116

==> /sys/devices/virtual/net/eth0/statistics/tx_bytes <==
1170

==> /sys/devices/virtual/net/lo/statistics/rx_bytes <==
168

==> /sys/devices/virtual/net/lo/statistics/tx_bytes <==
168
</pre>

In order to read these stats from outside the container, use the "setns" syscall to set your network namespace to match the netns of any process inside the container. Close the filehandle after each use, though: otherwise it will keep the container alive after all processes exit.
* Update: "setns" turns out to be complicated: e.g., threaded programs can't use it. Instead, we settle for parsing @/proc/PID/net/dev@ after choosing an appropriate PID.

Back