mikemansell Posted May 23, 2011 Report Share Posted May 23, 2011 Here's a pretty simple bash script for controlling bandwidth utilization;#!/bin/bash# This script will help you limit the amount of bandwidth that you consume so that you can predict/budget bandwidth fees# while using services such as the RackSpace Cloud which bill based on bandwidth utilization# Requires "vnstat" and "screen"# Maximum amount of bandwidth (megabytes) that you want to consume in a given month before anti-overage commands are runMAX=10240# Interface that you would like to monitor (typically "eth0")INTERFACE="eth0"function getusage { DATA=`vnstat --dumpdb -i $INTERFACE | grep 'm;0'` INCOMING=`echo $DATA | cut -d\; -f4` OUTGOING=`echo $DATA | cut -d\; -f5` TOTUSAGE=$(expr $INCOMING + $OUTGOING) if [ $TOTUSAGE -ge $MAX ]; then logevent "`echo $TOTUSAGE/$MAX`mb of monthly bandwidth has been used; bandwidth-saving precautions are being run" iptables-restore < /etc/firewall-lockdown.conf else logevent "`echo $TOTUSAGE/$MAX`mb of monthly bandwidth has been used; system is clear for the time being" fi sleep 300 getusage}function logevent { STRINGBASE="`date +%d\ %B\ %Y\ @\ %H:%M:%S` -:-" MESSAGE="$@" echo "$STRINGBASE $MESSAGE" >> aolog.txt}if [ $MAX == "" ]; then logevent "The maximum monthly traffic level (\$MAX) has not been defined. Please define this and restart." exitelif [ $INTERFACE == "" ]; then logevent "You have not defined the interface network (\$INTERFACE) that you want to monitor. Please define this and restart" exitelif [ "`whereis vnstat`" == "vnstat:" ]; then logevent "It appears that you do not have \"vnstat\" installed. Please install this package and restart." exitelif [ "`whereis screen`" == "screen:" ]; then logevent "It appears that you do not have \"screen\" installed. Please install this package and restart." exitfiif [ "$1" == "doscreen" ]; then getusageelse logevent "Starting vnstat interface logging on $INTERFACE" vnstat -u -i $INTERFACE logevent "Initiating screen session to run as a daemon process" screen -d -m $0 doscreenfi Quote Link to post Share on other sites
hakko Posted March 7, 2013 Report Share Posted March 7, 2013 Thanks for this!so min reqs packages are :screen - screen - A program to allow multiple screens on a VT100/ANSI Terminal ? vnstat I place this script in /etc/init.d ? Quote Link to post Share on other sites
mcmansell Posted March 8, 2013 Report Share Posted March 8, 2013 When I used it I just kept it in my home directory and ran it manually.~M Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.