Quick and Dirty bash script to check bandwidth usage
1(20%) 1 votes

I need it to shutdown my home server when it’s idle for a long time. Nothing very elegant, a bit of grep on ifconfig output but it seems to work fine. Have fun!

Donwload bwusage

#! /bin/bash

# BandWidth Usage
#   Federico Zanco 


IFACE=eth0
TIMEOUT=10
RX="1"
TX="1"
TRAFFIC="FALSE"
UNIT="KiB"
VERBOSE=


show_help () {
	echo
	echo "usage: bwusage [-h|--help] [-i|--interface ] [--rx-only]"
	echo "               [--tx-only] [--traffic] [-t|--timeout ]"
	echo "               [-u|--unit  [-v|--verbose]"
	echo
}


change_unit () {
	case "$UNIT" in
	
		"KiB")
			[ "$1" = "RX" ] && RX=`echo "$RX"|awk '{printf "%0.4f", $1 / 1024}'`
			[ "$1" = "TX" ] && TX=`echo "$TX"|awk '{printf "%0.4f", $1 / 1024}'`
			;;
			
		"MiB")
			[ "$1" = "RX" ] && RX=`echo "$RX"|awk '{printf "%0.4f", $1 / 1048576}'`
			[ "$1" = "TX" ] && TX=`echo "$TX"|awk '{printf "%0.4f", $1 / 1048576}'`
			;;

	esac
}


TEMP=`getopt -q -o hi:t:u:v --long help,interface,rx-only,tx-only,traffic,timeout,unit,verbose -- "$@"`

eval set -- "$TEMP"

while true ; do
	case "$1" in

	   	-h|--help)
	    	show_help
			exit 0
			;;
			
		-i|--interface)
			IFACE=$2
			shift 2
			;;

	   	--rx-only)
	    	TX=
	    	shift
			;;
	
		--tx-only)
	    	RX=
	    	shift
			;;
			
		--traffic)
	    	TRAFFIC="TRUE"
	    	shift
			;;

		-t|--timeout)
			TIMEOUT=$2
			shift 2
			;;
		
		-u|--unit)
			[ "$2" != "B" -a "$2" != "KiB" -a "$2" != "MiB" ] && echo && echo "ERROR: unit $2 not valid" && show_help && exit 1
			UNIT=$2
			shift 2
			;;
		
	   	-v|--verbose)
	    	VERBOSE="1"
	    	shift
			;;
		
		*)
			# [ "--" != "$1" ] && echo && echo "ERROR: $1 unknown option" && show_help && exit 1
			break
			;;
	esac
done

[ -z "$(ifconfig -s |grep $IFACE)" ] && echo && echo "ERROR: $IFACE not up or not valid" && exit 1

[ -n "$VERBOSE" ] && echo 
[ -n "$VERBOSE" ] && echo "IFACE=$IFACE"
[ -n "$VERBOSE" ] && echo "TIMEOUT=$TIMEOUT s"
[ -n "$VERBOSE" ] && echo "TRAFFIC=$TRAFFIC"
[ -n "$VERBOSE" ] && echo "UNIT=$UNIT"
[ -n "$VERBOSE" ] && echo
[ -n "$VERBOSE" ] && echo "... please wait $TIMEOUT seconds ..."
[ -n "$VERBOSE" ] && echo

[ -n "$RX" ] && rx1=`cat /sys/class/net/$IFACE/statistics/rx_bytes`
[ -n "$TX" ] && tx1=`cat /sys/class/net/$IFACE/statistics/tx_bytes`

sleep $TIMEOUT

[ -n "$RX" ] && rx2=`cat /sys/class/net/$IFACE/statistics/rx_bytes`
[ -n "$TX" ] && tx2=`cat /sys/class/net/$IFACE/statistics/tx_bytes`

if [ -n "$RX" ] 
then
	RX=`echo "$rx2 - $rx1"|bc`
	[ "$TRAFFIC" = "FALSE" ] && RX=`echo "$RX $TIMEOUT"|awk '{printf "%0.4f", $1 / $2 }'`
	change_unit "RX"
	[ -n "$VERBOSE" ] && echo -n "RX: "
	echo "$RX"|awk '{printf "%0.2f", $1 }'
	[ -n "$VERBOSE" ] && echo -n " $UNIT" && [ "$TRAFFIC" = "FALSE" ] && echo -n "/s"
fi

[ -n "$RX" ] && [ -n "$TX" ] && echo -n "    "

if [ -n "$TX" ] 
then
	TX=`echo "$tx2 - $tx1"|bc`
	[ "$TRAFFIC" = "FALSE" ] && TX=`echo "$TX $TIMEOUT"|awk '{printf "%0.4f", $1 / $2 }'`
	change_unit "TX"
	[ -n "$VERBOSE" ] && echo -n "TX: "
	echo "$TX"| awk '{printf "%0.2f", $1 }'
	[ -n "$VERBOSE" ] && echo -n " $UNIT" && [ "$TRAFFIC" = "FALSE" ] && echo -n "/s" 
fi

echo
Quick and Dirty bash script to check bandwidth usage
Tagged on:         

3 thoughts on “Quick and Dirty bash script to check bandwidth usage

  • Pingback: SiorarinaNet » Blog Archive » Quick and Dirty bash script to check … | Linux Blog

  • August 22, 2011 at 10:10 pm
    Permalink

    hello how are you?
    My name is Cleyton, I am a student and researcher in information technology by the Federal Rural University of Pernambuco in Brazil. I am collecting information from machines on the network and would like to use this script to collect your bandwidth consumed by the machine.

    I would like your permission to use your script, we’ll readjust it but it will be used for the most part. The proposed solution is a software under the GPL.

    Any problems?
    hugs!

    Reply
  • September 20, 2011 at 8:08 pm
    Permalink

    Hi Cleyton

    use it freely.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *