Quick and Dirty bash script to check bandwidth usage

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 -- "[email protected]"`

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

Leave a Reply

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