I have a very strong preferences about services, whatever its kind, that provide some sort of spending limit. For that reason I prefer Vultr over DigitalOcean, but I wanted to use a marketplace app available in DigitalOcean and not in Vultr. Yes, I know in a true business pulling the plug is not an option, but for my projects it's acceptable, though I would be shocked if I reached 1000GB of bandwidth usage over a month.
Enter vnstat, a wonderful tool that updates the usage every five minutes based on kernel information, without packet sniffing. On my Ubuntu VPS I installed it:
apt install vnstat
Then I created a the following script as
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin [ $(vnstat --oneline | cut -d \; -f 11 | sed '/[kKM]iB/s,.*,0,;s,\..*,,') -gt 910 ] && poweroff
sed substitution replaces MiB by 0, since in my case I started caring with GiB. Just in case I also did the same for KiB and kiB, I haven't seen them in the wild. More than 910GiB of bandwidth usage should pull the plug in a five-minute window, just don't become a trending topic during those five minutes!
PATH is a has some unnecessary values, but they come in the right order and admittedly it was a copy/paste operation. This file must be made executable
chmod +x /root/bwcheck.sh
Finally, I added the following to the crontab file with
*/5 * * * * /root/bwcheck.sh
As usual, use this at your own risk, it's not very elegant to just shut down if it's a real business, and so on. Cheers!