Total Download and Upload Data Limit

Hello,

I'm pretty new to OpenWrt and I just installed my brand new Netgear R6220 Router with OpenWrt 19.07.4 firmware.

I have the following requirements:

1> Limit the total bytes transferred and received through a particular interface, to say 250 GB. All packets MUST be dropped post reaching this data limit, if the limit is reached within a month. I want this data limit to be reset every month.

2> Limit the Max Speed of Data Transferred on a certain Interface, to say 50 Mbps.

3> It would be great if I can prioritize the traffic in one interface over the other. For example, I want to prioritize Default LAN Interface over another LAN2 Interface by sharing my overall WAN Data Speed in the ratio of 75:25 respectively.

Please let me know how would I go about accomplishing the above requirements. Point 1 & 3 is more important than Point 2 and I would highly appreciate the help provided.

Thanks & Regards,
Cliff!

would you like some dynamic with that?

seriously tho'... do (1) on the router and use a managed switch for (2) and (3)...

done.

Could you please let me know how would you do point 1 on the router?

you would slide your mouse up to the top right of the screen... click the thing that looks like a magnifying glass... search for 'wan limits' ( etc. )... read... read... read... try... test... try... test... and come back with any specific targeted questions that may arise.

1 Like

Sharing the solution I found for at least part of my original request. What's fulfilled?

1> Being able to monitor total usage from a particular subnet and then shutting down the interface it is connected through, once when the data limit has been reached and then resetting the same at 00:00 hrs on 1st of every month in case the interface was shutdown due to the limit being reached.

2> Hourly email backup of the Usage Database in case the db get's corrupted for restore purposes.

First, let's get started with the solution for point 1 above:

a> I installed nlbwmon (NetLink Bandwidth Monitoring) with the following commands:

opkg update
opkg install nlbwmon

b> nlbwmon by default monitors your traffic from the first of every month till the end and then resets the counters on the first of every month. It backups it's data to a database once every 24 hours, however the location of it's db is not persistent as it goes to /tmp on the router, which is lost upon rebooting or shutdown of the router for whatever reason. In order to avoid losing my data, I mounted a USB Pen Drive and starting backing up it's data to the pen drive. I also set my backup frequency to once every 45 seconds instead of once every day. My /etc/config/nlbwmon config file of nlbwmon is as follows:

config nlbwmon
        # The buffer size for receiving netlink conntrack results, in bytes.
        # If the chosen size is too small, accounting information might get
        # lost, leading to skewed traffic counting results
        option netlink_buffer_size 524288

        # Interval at which the temporary in-memory database is committed to
        # the persistent database directory
        option commit_interval 45s

        # Interval at which traffic counters of still established connections
        # are refreshed from netlink information
        option refresh_interval 30s

        # Storage directory for the database files
        option database_directory /mnt/sda1/nlbwmon

        # Amount of database generations to retain. If the limit is reached,
        # the oldest database files are deleted.
        option database_generations 10

        # Accounting period interval; may be either in the format YYYY-MM-DD/NN
        # to start a new accounting period exactly every NN days, beginning at
        # the given date, or a number specifiying the day of month at which to
        # start the next accounting period.
        #option database_interval '2017-01-17/14' # every 14 days, starting at Tue
        #option database_interval '-2' # second last day of month, e.g. 30th in March
        option database_interval '1' # first day of month (default)

        # The maximum amount of entries that should be put into the database,
        # setting the limit to 0 will allow databases to grow indefinitely.
        option database_limit 10000

        # Whether to preallocate the maximum possible database size in memory.
        # This is mainly useful for memory constrained systems which might not
        # be able to satisfy memory allocation after longer uptime periods.
        # Only effective in conjunction with database_limit, ignored otherwise.
        #option database_prealloc 0

        # Whether to gzip compress archive databases. Compressing the database
        # files makes accessing old data slightly slower but helps to reduce
        # storage requirements.
        #option database_compress 1

        # Protocol description file, used to distinguish traffic streams by
        # IP protocol number and port
        option protocol_database /usr/share/nlbwmon/protocols

        # List of local subnets. Only conntrack streams from or to any of these
        # subnets are counted. Logical interface names may be specified to
        # resolve the local subnets on the fly.
        list local_network 'wan'
        list local_network 'LAN3_Guest'
        list local_network 'LAN4_Venu'
        list local_network 'lan'

You must modify the option database_directory to whichever persistent store directory you want, mine is my Pen Drive path. Also modify option commit_interval to a frequency of your choice with the following analogy (s-seconds,m-minutes,h-hour). Finally, in the list of local_network, replace it with the interface name's defined in your /etc/config/network file which you want to monitor. Restart nlbwmon, /etc/init.d/nlbwmon restart. Find more info at https://github.com/jow-/nlbwmon/blob/master/README.md.

c> If you need to know how to mount a Pen Drive and make it so that it auto mounts on reboot or boot up, use the following link:

https://openwrt.org/docs/guide-user/storage/usb-drives-quickstart

d> In order to actually monitor the subnet I was interested in monitoring, I ran a cron job every min to check for usage and shutdown the interface if the limit exceeded, the script I used to check is as follows, I wanted to monitor the 192.168.10.* subnet for total usage not exceeding 250 GB in a month, if the usage exceeded this limit, shutdown the interface, if it is not already down. You'll need to install bash and awk for this script to work, if it is not already installed. The name of the interface in my case is LAN4_Venu, you can find your interface name in /etc/config/network for the subnet you want to monitor.

root@OpenWrt_Netgear_R6220:~# cat /mnt/sda1/bin/scripts/calculateTotalUsage.sh
#!/bin/bash

totalUsage=$(nlbw -c csv -o -rx_bytes | grep -i "192.168.10.*" | awk '{ DOWN += $7; UP += $9 } END { if (((DOWN + UP) / 1024 / 1024 / 1024) > 250) print "Yes"; else print "No" }')
if [[ $totalUsage = "Yes" ]]
then
        interface=$(ifconfig -a eth0.10 2> /dev/null)
        if [[ $? -eq 0 ]]
        then
                interface=$(ifdown LAN4_Venu 2> /dev/null)
        fi
fi
root@OpenWrt_Netgear_R6220:~#

Cron Job entry to check every min:

* * * * * /mnt/sda1/bin/scripts/calculateTotalUsage.sh

Script and cron job to bring the interface back up on 1st of every month in case it was shutdown due to it's limit being reached:

root@OpenWrt_Netgear_R6220:~# cat /mnt/sda1/bin/scripts/resetvenuinterface.sh
#!/bin/bash

interface=$(ifconfig -a eth0.10 2> /dev/null)
if [[ $? -ne 0 ]]
then
        interface=$(ifup LAN4_Venu 2> /dev/null)
fi
root@OpenWrt_Netgear_R6220:~#
* * 1 * * /mnt/sda1/bin/scripts/resetvenuinterface.sh

Finally, let's discuss the second point above, hourly email backups of the nlbwmon db.

a> I installed mailsend with the following commands, I chose this program due to 2 reasons, first was because it supported SSL for encryption which was required by my SMTP Server and second, it supported sending attachments. There are other options available, you can refer to the following to install the package of your choice.

https://openwrt.org/docs/guide-user/services/email/smtp.client

opkg update
opkg install mailsend

b> As I had set my nlbwmon db to commit it's db to an external pen drive, my db location might be different from your's, modify the script accordingly. Here is the script I put into place and it's respective cron job to run it hourly. In order for this to work correctly, ensure that you have your Date and Time set correctly in your router, if you have internet, your router defaults should get you the right Date and Time through NTP Clients already configured.

Modify the dbfilename variable to point to the fullpath of your db file. This script will first stop the nlbwmon process and then send an email with the db file as the attachment, you'll also be notified by email if anything fails. After the backup, the nlbwmon process is started back up.

I created my own custom mime format in my home directory for application/database with the file name .mime.types, this is a hidden file to be kept in the user's home directory who would be initiating the mailsend command.

Also, you'll need an app password from your SMTP Provider, regular login password's won't work, especially if you have 2FA's enabled on your email account.

root@OpenWrt_Netgear_R6220:~# cat /mnt/sda1/bin/scripts/emailnlbwmondbbackup.sh
#!/bin/bash

/etc/init.d/nlbwmon stop 2> /dev/null
if [[ $? -eq 0 ]]
then
        dbfilename="/mnt/sda1/nlbwmon/$(date +"%Y%m01").db.gz"
        mailsend -f "xyz@xyz.com" -t "xyz@xyz.com" -smtp "smtp.gmail.com" -starttls -auth -user "xyz@xyz.com" -pass "abcdefghijklmnop" -sub "nlbwmon Database Backup from OpenWrt" -M "nlbwmon Database Backup from OpenWrt" -attach "${dbfilename}","application/database",a > /dev/null 2>&1
        processcount=$(/etc/init.d/nlbwmon start 2> /dev/null && ps | grep "/usr/sbin/nlbwmon" | wc -l)
        if [[ $? -ne 0 || $processcount -ne 2 ]]
        then
                mailsend -f "xyz@xyz.com" -t "xyz@xyz.com" -smtp "smtp.gmail.com" -starttls -auth -user "xyz@xyz.com" -pass "abcdefghijklmnop" -sub "UNABLE TO START nlbwmon back up After Backup" -M "UNABLE TO START nlbwmon back up After Backup" > /dev/null 2>&1
        fi
else
        mailsend -f "xyz@xyz.com" -t "xyz@xyz.com" -smtp "smtp.gmail.com" -starttls -auth -user "xyz@xyz.com" -pass "abcdefghijklmnop" -sub "UNABLE TO STOP nlbwmon for Backup" -M "UNABLE TO STOP nlbwmon for Backup" > /dev/null 2>&1
fi
root@OpenWrt_Netgear_R6220:~#

Custom mime types file .mime.types in /root:

root@OpenWrt_Netgear_R6220:~# cat .mime.types
application/database                    db mdf accdb nsf fp7
root@OpenWrt_Netgear_R6220:~#

Hourly Cron Job:

0 * * * * /mnt/sda1/bin/scripts/emailnlbwmondbbackup.sh

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.