Linux – Measure network consumption per program and month

bandwidthlinuxnetworking

I'm looking for a permanently-running monitor which can emit a monthly (or daily, etc) bandwidth report, in a per-program manner.
Example desired output:

Bandwidth consumption: last 30 days
==============
Program     Downloaded   Uploaded
/usr/bin/ssh  30MB       100MB
/usr/bin/java 9000MB     3000MB

(it also could be per-process instead of per-program, but then I'd have to consolidate entries manually)

After significative research (including many similar questions over Stack Exchange), I still haven't found such a monitor. Maybe this is not possible under Linux. Or maybe this is bit of an unusual need.

Best Answer

Use nethogs -t to capture per-process usage and parse the output. From https://boopathi.in/blog/capturing-per-process-bandwidth-usage-using-nethogs/

sh -ic "{ /usr/sbin/nethogs -t eth1 &> output; \
kill 0; } | { sleep $TIMEOUT; \
kill 0; }" 3>&1 2>/dev/null

The output fields are:

Process / Process id / User id    sent kbps    recv kbps

and parse the output using https://github.com/boopathi/nethogs-parser

Another solution from SO: https://stackoverflow.com/a/20705080/3812704

But I like the first solution better.

Related Question