Anyone know of a tool/command that can measure how much traffic (mb/s) is being sent to an application?
Anyone know of a tool/command that can measure how much traffic (mb/s) is being sent to an application?
`nethogs` is also useful.
If you'd like to do something more specialized, you could build something with a couple of lines in `bpftrace`, but you'll have to put in more work there if you're not familiar with it.
The book "BPF Performance Tools" by Brendan Gregg is a good introduction there. Of course, for a simple overview, that would be way overkill, though :)
I whipped up a simple shell script for the polling thing.
Usage: ./traffic.sh <pid>
Copy-pastable for your terminal (dollar signs signify your prompt, obviously):
$ cat <<'EOF' > traffic.sh
#!/usr/bin/env bash
pid=$1
set -euo pipefail
while true; do
date +'Time: %H:%M:%S'
sed '/:/!d' /proc/$pid/net/dev
echo
sleep 1
done \
| awk '
BEGIN { MB = 1024*1024.0; }
/^Time/ || /^$/ { print $0; next; }
seen[$1] {
rx_mbps = ($2 - rx[$1]) / MB;
tx_mbps = ($11 - tx[$1]) / MB;
printf("%10s Rx: %8.1f MiB/s, Tx: %8.1f MiB/s\n",
$1, rx_mbps, tx_mbps);
}
{ seen[$1] = 1; rx[$1] = $2; tx[$1] = $11; }
'
EOF
$ chmod +x traffic.sh> TL;DR: systemd now can do per-service IP traffic accounting, as well as access control for IP address ranges.
It can be enabled using:
[Service]
IPAccounting=yes
systemd won't, AFAIK, give you per second values -- just the aggregate totals -- so you'd have to constantly retrieve the updated values and calculate the differences yourself. The totals can be obtained programmatically via the D-Bus APIs. They'll also be shown in the "systemctl status ..." output or you can grab just the value(s) you're interested in with "systemctl show ...".However...
systemd's functionality is basically just a thin wrapper around eBPF (in the kernel, since sometime around 4.8, I think?) so you can do it without systemd too.
In fact, the author of this article has also written a nice introduction to XDP and eBPF [1]! That article includes an example "XDP program" which counts the number of packets that have been blocked from a list of source IP addresses -- it could be modified fairly easily to do what you want, I think.
---
[0]: http://0pointer.net/blog/ip-accounting-and-access-lists-with...
Related: trickle (per application bandwidth shaping) and wondershaper (per interface bandwidth shaping) are great for testing.