# Limit Bandwidth Used Per Day / Daily Bandwidth



## ejr2122 (Jul 11, 2013)

Hi *a*ll,

Everyone's input here is welcome. I'm looking for a creative solution for a problem imposed by a constraint my ISP has as part of our annual contract. I live out in the woods and have just one option for Internet connectivity, a wireless ISP. The highest consumer service plan this ISP offers is 2[]GB of traffic per day. After that point, the ISP throttles our connection to 5-14 KBps, sends angry emails, etc. It is a big headache and I can't move to a place with more connectivity any time soon.

That said, I'm looking for a way to limit the traffic generated by my network. It would be nice if a machine could monitor a network interface, measure the total KB/MB/GB transported across it, and then do _something_ to prevent my ISP from getting upset. That _something_ could be, for example, sending me an email letting me know when I've gone close to a present limit, maybe throttling connection speed after another preset limit and sending me an email to inform me that my connection was automatically throttled.

I don't have much experience doing anything like this, so I'm hoping someone who does can reach out and give just a little guidance. I'm familiar with unix basics and some programming, but not with writing sophisticated shell scripts (especially not on FreeBSD) I'd really like to get this solution in place because I'll be a lot more comfortable playing with and developing on FreeBSD at home on the weekends.

Thanks!


----------



## ejr2122 (Jul 11, 2013)

Whoops. Some corrections to what I said earlier:

The contract is monthly, not annual.
The monitoring/throttling would be reset every 24 hours.


----------



## SirDice (Jul 11, 2013)

ejr2122 said:
			
		

> The highest consumer service plan this ISP offers is 2GB of traffic per day.


Per day? My UMTS/3G contract has a limit of 1 GB per _month_!


----------



## ejr2122 (Jul 11, 2013)

Yes, I'm fortunate compared to other wireless ISPs.

After some googling, it appears that incoming traffic can't be slowed. Perhaps I'll create a script that will close specific ports after the interface limit is reached. The script should send various alerts out before it does this. Messages should appear on terminal screens, emails should be sent out. It should be noisy.


----------



## SirDice (Jul 11, 2013)

ejr2122 said:
			
		

> After some googling, it appears that incoming traffic can't be slowed.


That is correct, you can only throttle outgoing traffic. That said, incoming traffic is usually a result of some action on your side. Be it a download or perhaps P2P. You won't get massive amounts of incoming traffic if you don't request it. Unless you are running some public facing service you probably won't get incoming traffic at all.


----------



## ejr2122 (Jul 11, 2013)

I'm getting closer. Now I'm thinking the script should run on a machine that operates a bridge between my server toys and the rest of the house network.

I found the following guide on how to measure the amount of data carried through an interface. Some of the commands may need to be changed because the Apple flavor of pf may be different.
http://superuser.com/questions/392952/how-can-i-monitor-total-transferred-network-traffic-on-a-given-interface

The script could run every minute, gathering the value and saving to a variable. For logging purposes, the variable's value could be appended as a new line to a file, with a timestamp. The file should be associated with the current day and the specific interface. If said file doesn't exist, it will be created. In addition to saving the value to the file, this script should also execute additional commands depending on the value itself.

A separate job could be run every day to check the directory containing the different log files, deleting the _(N+1)_th file in the directory. Here, _N_ is equal to the period for which we want to keep these files around. Special logic for processing days / dates will need to be employed.

I'll think about this some more and return. Right now it's pretty theoretical and it will be some time before I can get around to implementation.


----------



## Abriel (Jul 11, 2013)

Try to use net-mgmt/pmacct with databases/mysql51-server. It works nice with ipfw.


----------



## ejr2122 (Jul 12, 2013)

Abriel, 

pmacct looks brilliant. I'm pleased to see that it will work with MongoDB as well.


----------

