5 ms·
This is very timely. Anyone know of a tool/command that can measure how much traffic (mb/s) is being sent to an application?
by victor106 6y ago
This is very timely.
Anyone know of a tool/command that can measure how much traffic (mb/s) is being sent to an application?
- l3s2d 6y agoI like to use nethogs for quickly looking at per-application traffic. Requires root. Related: trickle (per application bandwidth shaping) and wondershaper (per interface bandwidth shaping) are great for testing.
- btschaegg 6y agoAs a simple solution, you could poll `/proc/<pid>/net/dev` and calculate it yourself (from the difference of the total received bytes between two polls). `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 :)
- btschaegg 6y agoAddendum: Since I'm somewhat bored, anyway :) 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
- victor106 6y agoThank you. Very helpful
- jlgaddis 6y agosystemd [0], which you're probably already using, can do IP Accounting: > 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-systemd.html http://0pointer.net/blog/ip-accounting-and-access-lists-with... [1]: https://jvns.ca/blog/2017/04/07/xdp-bpf-tutorial/ https://jvns.ca/blog/2017/04/07/xdp-bpf-tutorial/