• Pull Request

    br: neruthes/refactor

    #!/bin/bash
    
    IFNAME=$1
    if [ ! -d "/sys/class/net/$IFNAME/statistics" ]; then
    	echo "sysfs dir not exists: /sys/class/net/$IFNAME/statistics"
    	exit 1
    fi
    
    IFSPEED=$2
    IFSPEED_PRE=""
    
    if [ -z $IFSPEED ]; then
    	IFSPEED="100"
    	echo "[INFO] Assuming scale 100 Mbps."
    fi
    
    function logline() {
    	# 1: CURRENTIN; 2:LASTIN  3: CURRENTOUT 4:LASTOUT
    	local BUSY=0
    
            local IN=$(($1 - $2))
    	BUSY=$(($BUSY + $IN))
    	local U_IN="KB/s"
    	if [ 1024 -lt "$IN" ]; then IN=$(($IN / 1024)); U_IN="MB/s"; fi
    
    	local OUT=$(($3 - $4))
    	BUSY=$(($BUSY + $OUT))
    	local U_OUT="KB/s"
    	if [ 1024 -lt "$OUT" ]; then OUT=$(($OUT / 1024)); U_OUT="MB/s"; fi
    
    	BUSY=$(($BUSY * 8 / 10 / $IFSPEED))
    	if [ 100 -lt $BUSY ]; then BUSY=100; fi
    
    	# BUSY_BAR_LEN = $COLUMNS - "IN " + "%4d %4s" + " - OUT " + "%4d %4s " + "- %3d%% "
    	BUSY_BAR_LEN=$(($COLUMNS - 3 - 9 - 7 - 10 - 7))
    	local BUSY_BAR
    	local counter=$(($BUSY * $BUSY_BAR_LEN / 100))
    	while [ $counter -gt 0 ]; do BUSY_BAR="${BUSY_BAR}|"; counter=$(($counter - 1)); done
    	if [ -z "$BUSY_BAR" ]; then BUSY_BAR="|"; fi
    
    	if [[ "$NOHISTORY" != y* ]]; then
    		logline_with_history \
    			"$(printf "\rIN %4d %s | OUT %4d %s | %3d%% %s" $IN "$U_IN" $OUT "$U_OUT" $BUSY "$BUSY_BAR")" \
    			"$(printf "\nIN %4d %s | OUT %4d %s | Interface: $IFNAME" $IN "$U_IN" $OUT "$U_OUT")"
    	else
    		logline_without_history \
    			"$(printf "\rIN %4d %s | OUT %4d %s | Interface: $IFNAME" $IN "$U_IN" $OUT "$U_OUT")"
    	fi
    }
    
    function logline_with_history() {
    	local HISTORY=$1
    	local STRING=$2
    	printf "%-${COLUMNS}s" "$HISTORY"
    	printf "%-${COLUMNS}s" "$STRING"
    }
    function logline_without_history() {
    	local STRING=$1
    	printf "%-${COLUMNS}s" "$STRING"
    }
    
    function getbytesin() {
    	# 1: ifname
    	cat /sys/class/net/$IFNAME/statistics/rx_bytes
    }
    
    function getbytesout() {
    	#1: ifname
    	cat /sys/class/net/$IFNAME/statistics/tx_bytes
    }
    
    STARTBIN=$(getbytesin)
    STARTBOUT=$(getbytesout)
    
    while(true); do
    	LASTIN=$(($(getbytesin) / 1024))
    	LASTOUT=$(($(getbytesout) / 1024))
    	sleep 1
    	CURRENTIN=$(($(getbytesin) / 1024))
    	CURRENTOUT=$(($(getbytesout) / 1024))
    	logline $CURRENTIN $LASTIN $CURRENTOUT $LASTOUT
    done
    
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment