Multicolour Network monitor

The following is code to display Network bandwidth usage with the multicolour LedBorg
The code looks at the Rx and Tx bytes counter and takes the difference over time.
This gives the number of bytes per second which is displayed in colour ranges, evenly split between Zero and a HeadLimit.

The code:
First two non-coment lines of script set the interface (iface=) and maximum bytes (maxBytes=) for the levels

download as text file here
Save file as
To change permissions and execute:
chmod +x

The range is from 0% (Blue) to 100% (Red)


# Choose which network interface to poll and bytes per second at which to represent 100%

# Setup our levels list
level=("002" "012" "022" "021" "020" "120" "220" "210" "200")

# Work out how many levels we have (level list length)

# Get some starting values and wait to gather data
echo "000" > /dev/ledborg
lastRx=`cat /sys/class/net/${iface}/statistics/rx_bytes`
lastTx=`cat /sys/class/net/${iface}/statistics/tx_bytes`
sleep 1

# Loop indefinitely
while [ 1 ]; do
    # Read network statistics
    rx=`cat /sys/class/net/${iface}/statistics/rx_bytes`
    tx=`cat /sys/class/net/${iface}/statistics/tx_bytes`
    bytes=$((($rx - $lastRx) + ($tx - $lastTx)))

    # Save starting values

    # Work out utilisation level
    util=$((($bytes * $levels) / $maxBytes))
    if [ $util -ge $levels ]; then
        util=$(($levels - 1))

    # Set colour based on utilisation
    echo ${level[$util]} > /dev/ledborg

    # Wait for next gather
    sleep 1