#!/bin/bash
# NetLoad monitor in text mode for xterm or similar
# (C) 2002 F.Hauri - Sainte-Croix (CH) - felix@f-hauri.ch
# This program is distributed under the terms of the GNU General Public License:

export Width=${COLUMNS:-80}
export IFace=${1:-ppp0}
export LC_ALL=C
/sbin/ifconfig $IFace >/dev/null || exit 1

export filI=$(dd if=/dev/zero bs=$Width count=1 2>/dev/null | tr \\000 I)
export filO=$(echo -n $filI|tr I O) filB=$(echo -n $filI|tr I B)
export Yel=$'\e[33;40;1m' Blu=$'\e[34;40;1m' Gre=$'\e[32;40;1m'
export Cln=$'\e[0m' Clr=$'\e[K'
export fmtlne="\r$Gre%-${Width}s$Cln"
fmtlne="${fmtlne}\nIn: %12.2f %12.2f %7.2f%%  Out: %12.2f %12.2f %7.2f%%\e[K"
export maxin=1 maxout=1 crt old Fline
export pIn=0 pOut=1 pTime=2

dump() {
    echo $(
        grep ${IFace}: /proc/net/dev |\
	    tr : \ |\
	    awk '{printf "%s %s\n", $2, $10}'
        ) $(
        sed -e s/^\ *// -e s/\ .*// </proc/uptime
        )
}
amount() {
    echo " (${crt[$1]} - ${old[$1]}) / (${crt[$pTime]} - ${old[$pTime]})" |\
        bc -l
}

old=($(dump))
sleep 3
while :;do
    crt=($(dump))
    in=$( amount $pIn )
    out=$( amount $pOut )
    [ ${in%.*}0 -gt ${maxin%.*}0 ] && maxin=$in
    [ ${out%.*}0 -gt ${maxout%.*}0 ] && maxout=$out
    GrIn=$(echo $Width*$in/$maxin | bc)
    GrOut=$(echo $Width*$out/$maxout | bc)
    if [ $GrIn -gt $GrOut ] ;then
        FLine="${filB:0:$GrOut}$Yel${filI:0:$(($GrIn-$GrOut))}"
    else
        FLine="${filB:0:$GrIn}$Blu${filO:0:$(($GrOut-$GrIn))}"
    fi
    printf "$fmtlne" "$FLine$Clr" \
        $in $maxin "$(echo $in/$maxin*100|bc -l)" \
        $out $maxout "$(echo $out/$maxout*100|bc -l)"
        old=(${crt[@]})
    sleep 10
    done
