This is a simple python script to control data usage by shutting down the machine when a set limit is reached and sends an email alert to the admin.
- vnStat
- Edit
/etc/vnstat.conf
- Change
MonthRotate
on line 10 to start date of billing period (1-28)
- Setup vnStat monitoring
sudo vnstat -u -i eth0
- Replace eth0 with network interface to be monitored, the command
vnstat --iflist
will show all available network interfaces
- Replace eth0 with network interface to be monitored, the command
- Start the vnStat daemon and set to run on boot
sudo vnstatd -d
- Add
vnstat -d
aboveexit 0
line in/etc/rc.local
- Change the number on Line 7 to the usage limit in GB (whole numbers only)
- Add script to
crontab
to run automatically
- Add
5 * * * * /path/to/script.py
to root crontab (sudo crontab -e
)- Replace the 5 with how often you want the script to check usage in minutes
- Replace
/path/to/script.py
with the actual path to the script (obviously)
- Update and addition of conversion code is because vnStat uses GiB (gibibyte) rather than GB (gigabyte) and 1 GiB is more than 1 GB
- Completely changed how script retrieves usage data
- Added conversion from GB to GiB within the script for more accuracy
- Added units checking for MiB and GiB
- Cleanup and new README
- Email capable version of script added
This software is licensed under the MIT License, you can view the license terms at https://jibreil.mit-license.org/ or, in the LICENSE.md
file.