Linux – Socket based byte counts

bandwidthbandwidth-measuringlinuxsockettcp

I see a lot of tools to get the RX and TX byte counts for transmission on interfaces, is there any tool/program that I can use to get a total transmitted byte count for a single specified socket?

I'm trying to limit users' use of the service, i.e. user number 23 has purchased 20 GiB of transfer, and they just downloaded a 1 GiB linux .iso. Now they should have 1 GiB less of remaining transfer.

The problem is I'm using a cloud based solution… I have a shared interface that each user would use and I don't know if I can use SNMP because I don't know the hardware specifics. (I also don't quite know what SNMP is or if it needs specific hardware)

I'm just looking to track transfers per user, maybe that's per socket or address, or not possible. Is there anything close to what I'm looking for?

Best Answer

There are no per-socket counters that you can query, especially since once a socket is closed, there is nothing to query anymore.

One thing you can do is install a bunch of iptables rules with different --uid-owner options and the -j ACCEPT target. These rules won't block anything but the counters on each rule (visible with iptables -L -n -v) will count packets and bytes on a per-user basis. Note that this will count only outgoing traffic, though (because incoming traffic doesn't come from a user, it comes from the network).