# A simple way to count traffic on the interface



## ogogon (Aug 20, 2021)

Colleagues, please tell me how I can most easily, but correctly, solve my problem!

I have a router that is connected to an Internet service provider by its port.
The provider provides me with a channel with a certain bandwidth, for example 50 Mbit/s.
I want to monitor the actual load of the port in order to understand the situation - or raise the issue of expanding the channel or calm users with excessive traffic.

At this stage, I do not want to analyze the traffic structure, as long as I just want to know the real data transfer rate in the port. At the input and at the output.
Further, for example, I can save these values in a file or database and use them as analytical information.

Tell me, pls, what is the best solution for this?

Ogogon.


----------



## cmoerz (Aug 20, 2021)

I'm using net/vnstat to keep an eye on throughput. It allows you to generate html reports, similar to net-mgmt/mrtg. Either of those two should allow you to track bandwidth use. 

Not sure if that qualifies as the "best" but they should both do the trick as a general purpose bandwidth tracker.


----------



## Geezer (Aug 20, 2021)

Or even net-mgmt/net-snmp with net-mgmt/mrtg.


----------



## SirDice (Aug 20, 2021)

Does that modem/router even support SNMP? A lot of these home cable/xDSL modem/routers don't support SNMP. My cable modem certainly doesn't.


----------



## hardworkingnewbie (Aug 20, 2021)

As already mentioned vnstat does just that. Even better, it stores the measured values in a Sqlite3 file, so you can access it later any way you want. 

If you want to have some pretty nitty graphs regarding your machine try Munin. It generates a lot of them, including traffic.


----------



## Geezer (Aug 20, 2021)

SirDice said:


> Does that modem/router even support SNMP? A lot of these home cable/xDSL modem/routers don't support SNMP. My cable modem certainly doesn't.


Does not matter.  Use `bsnmpd` and find the traffic right through your own interface.


----------



## SirDice (Aug 20, 2021)

Numbers will be skewed if there are more devices connected to the modem. You're only measuring the traffic from your own machine and don't measure the traffic that's generated by the other devices that are also connected to the internet.


----------



## ogogon (Aug 23, 2021)

cmoerz said:


> I'm using net/vnstat to keep an eye on throughput. It allows you to generate html reports, similar to net-mgmt/mrtg. Either of those two should allow you to track bandwidth use.
> 
> Not sure if that qualifies as the "best" but they should both do the trick as a general purpose bandwidth tracker.


Thanks! I have installed vnstat. Overall, it does what I need it to do.
Although there are imperfections in it - the minimum time interval is 5 minutes, i.e. it does not record short peaks. And it unloads information in all sorts of pretentious formats, but simple and understandable CSV does not support.


----------



## ogogon (Aug 23, 2021)

Geezer said:


> Or even net-mgmt/net-snmp with net-mgmt/mrtg.


Thanks! The doggy is cool!


----------



## covacat (Aug 23, 2021)

netstat -I iface -finet -b


----------



## gpw928 (Aug 23, 2021)

My ISP charges extortionate data fees if I go over my agreed limit.  So I log the byte counts every 5 minutes from cron, to monitor the usage.

This is the FreeBSD version of the data collection script:
	
	



```
#!/bin/sh

# FreeBSD: Extract the accumulated Ibytes and Obytes for a network link
#
# For the interface nominated, prints:
#
#    RX bytes 1183781219 TX bytes 1050467164

Interface=${1:-ppp0}
TAB=`echo | tr '\n' '\t'`
W="[ $TAB]"                     # white space
F='[^@]*@'                      # "@" terminated field
netstat -b -I $Interface | grep Link | \
  sed -e "s/$W$W*/@/g" \
      -e "s/$F$F$F$F$F$F$F\([^@]*\)@$F$F\([^@]*\)@.*/RX bytes \1 TX bytes \2/"
```


----------

