Return to Snippet

Revision: 53214
at November 16, 2011 18:32 by mattsn0w


Updated Code
#!/bin/bash

function nets {
    TX_TOTAL=0
    RX_TOTAL=0
    TX_GAUGE="B"   # Bytes
    RX_GAUGE="B"   # Bytes
    INTERFACE="venet0"
    for ((i=0;i<10;i++)); do    
        RX_BYTES=$(cat /sys/class/net/${INTERFACE}/statistics/rx_bytes)
        TX_BYTES=$(cat /sys/class/net/${INTERFACE}/statistics/tx_bytes)
        LAST_RX_BYTES=${RX_BYTES}
        LAST_TX_BYTES=${TX_BYTES}
        sleep .10
        RX_BYTES=$(cat /sys/class/net/${INTERFACE}/statistics/rx_bytes)
        TX_BYTES=$(cat /sys/class/net/${INTERFACE}/statistics/tx_bytes)
        TX_DIFF=$[ ${TX_BYTES} - ${LAST_TX_BYTES} ]
        RX_DIFF=$[ ${RX_BYTES} - ${LAST_RX_BYTES} ]
        TX_TOTAL=$[ ${TX_TOTAL} + ${TX_DIFF} ]
        RX_TOTAL=$[ ${RX_TOTAL} + ${RX_DIFF} ]
        if [ ${RX_DIFF} -gt 1024 ]; then
            RX_GAUGE="KB"
            RX_DIFF=$[ ${RX_DIFF} / 1024 ]
            if [ ${RX_DIFF} -gt 1024 ]; then
                RX_GAUGE="MB"
                RX_DIFF=$[ ${RX_DIFF} / 1024 ]
            fi
        fi
        if [ ${TX_DIFF} -gt 1024 ]; then
            TX_GAUGE="KB"
            TX_DIFF=$[ ${TX_DIFF} / 1024 ]
            if [ ${TX_DIFF} -gt 1024 ]; then
                TX_GAUGE="MB"
                TX_DIFF=$[ ${TX_DIFF} / 1024 ]
            fi
        fi    
        if [[ ${TX_DIFF} -gt 0 || ${RX_DIFF} -gt 0 ]]; then
            printf "${i}00ms  - TX: ${TX_DIFF}${TX_GAUGE}\tRX: ${RX_DIFF}${TX_GAUGE}\n"
        fi
    done
    if [ ${RX_TOTAL} -gt 1024 ]; then
        RX_GAUGE="KB"
        RX_TOTAL=$[ ${RX_TOTAL} / 1024 ]
        if [ ${RX_TOTAL} -gt 1024 ]; then
            RX_GAUGE="MB"
            RX_TOTAL=$[ ${RX_TOTAL} / 1024 ]
        fi
    fi
    if [ ${TX_TOTAL} -gt 1024 ]; then
        TX_GAUGE="KB"
        TX_TOTAL=$[ ${TX_TOTAL} / 1024 ]
        if [ ${TX_TOTAL} -gt 1024 ]; then
            TX_GAUGE="MB"
            TX_TOTAL=$[ ${TX_TOTAL} / 1024 ]
        fi
    fi 
    printf "1000ms - TX: ${TX_TOTAL}${TX_GAUGE}\tRX: ${RX_TOTAL}${RX_GAUGE}\n"
}

while :; do nets && date "+%S"; done

Revision: 53213
at November 16, 2011 12:43 by mattsn0w


Updated Code
#!/bin/bash

function nets {
    TX_TOTAL=0
    RX_TOTAL=0
    TX_GAUGE="B"   # Bytes
    RX_GAUGE="B"   # Bytes
    for ((i=0;i<10;i++)); do    
        RX_BYTES=$(cat /sys/class/net/eth0/statistics/rx_bytes)
        TX_BYTES=$(cat /sys/class/net/eth0/statistics/tx_bytes)
        LAST_RX_BYTES=${RX_BYTES}
        LAST_TX_BYTES=${TX_BYTES}
        sleep .10
        RX_BYTES=$(cat /sys/class/net/eth0/statistics/rx_bytes)
        TX_BYTES=$(cat /sys/class/net/eth0/statistics/tx_bytes)
        TX_DIFF=$[ ${TX_BYTES} - ${LAST_TX_BYTES} ]
        RX_DIFF=$[ ${RX_BYTES} - ${LAST_RX_BYTES} ]
        TX_TOTAL=$[ ${TX_TOTAL} + ${TX_DIFF} ]
        RX_TOTAL=$[ ${RX_TOTAL} + ${RX_DIFF} ]
        if [ ${RX_DIFF} -gt 1024 ]; then
            RX_GAUGE="KB"
            RX_DIFF=$[ ${RX_DIFF} / 1024 ]
            if [ ${RX_DIFF} -gt 1024 ]; then
                RX_GAUGE="MB"
                RX_DIFF=$[ ${RX_DIFF} / 1024 ]
            fi
        fi
        if [ ${TX_DIFF} -gt 1024 ]; then
            TX_GAUGE="KB"
            TX_DIFF=$[ ${TX_DIFF} / 1024 ]
            if [ ${TX_DIFF} -gt 1024 ]; then
                TX_GAUGE="MB"
                TX_DIFF=$[ ${TX_DIFF} / 1024 ]
            fi
        fi    
        if [[ ${TX_DIFF} -gt 0 || ${RX_DIFF} -gt 0 ]]; then
            printf "${i}00ms  - TX: ${TX_DIFF}${TX_GAUGE}\tRX: ${RX_DIFF}${TX_GAUGE}\n"
        fi
    done
    if [ ${RX_TOTAL} -gt 1024 ]; then
        RX_GAUGE="KB"
        RX_TOTAL=$[ ${RX_TOTAL} / 1024 ]
        if [ ${RX_TOTAL} -gt 1024 ]; then
            RX_GAUGE="MB"
            RX_TOTAL=$[ ${RX_TOTAL} / 1024 ]
        fi
    fi
    if [ ${TX_TOTAL} -gt 1024 ]; then
        TX_GAUGE="KB"
        TX_TOTAL=$[ ${TX_TOTAL} / 1024 ]
        if [ ${TX_TOTAL} -gt 1024 ]; then
            TX_GAUGE="MB"
            TX_TOTAL=$[ ${TX_TOTAL} / 1024 ]
        fi
    fi 
    printf "1000ms - TX: ${TX_TOTAL}${TX_GAUGE}\tRX: ${RX_TOTAL}${RX_GAUGE}\n"
}

while :; do nets && date "+%S"; done

Revision: 53212
at November 16, 2011 12:42 by mattsn0w


Initial Code
function nets {
    TX_TOTAL=0
    RX_TOTAL=0
    TX_GAUGE="B"   # Bytes
    RX_GAUGE="B"   # Bytes
    for ((i=0;i<10;i++)); do    
        RX_BYTES=$(cat /sys/class/net/eth0/statistics/rx_bytes)
        TX_BYTES=$(cat /sys/class/net/eth0/statistics/tx_bytes)
        LAST_RX_BYTES=${RX_BYTES}
        LAST_TX_BYTES=${TX_BYTES}
        sleep .10
        RX_BYTES=$(cat /sys/class/net/eth0/statistics/rx_bytes)
        TX_BYTES=$(cat /sys/class/net/eth0/statistics/tx_bytes)
        TX_DIFF=$[ ${TX_BYTES} - ${LAST_TX_BYTES} ]
        RX_DIFF=$[ ${RX_BYTES} - ${LAST_RX_BYTES} ]
        TX_TOTAL=$[ ${TX_TOTAL} + ${TX_DIFF} ]
        RX_TOTAL=$[ ${RX_TOTAL} + ${RX_DIFF} ]
        if [ ${RX_DIFF} -gt 1024 ]; then
            RX_GAUGE="KB"
            RX_DIFF=$[ ${RX_DIFF} / 1024 ]
            if [ ${RX_DIFF} -gt 1024 ]; then
                RX_GAUGE="MB"
                RX_DIFF=$[ ${RX_DIFF} / 1024 ]
            fi
        fi
        if [ ${TX_DIFF} -gt 1024 ]; then
            TX_GAUGE="KB"
            TX_DIFF=$[ ${TX_DIFF} / 1024 ]
            if [ ${TX_DIFF} -gt 1024 ]; then
                TX_GAUGE="MB"
                TX_DIFF=$[ ${TX_DIFF} / 1024 ]
            fi
        fi    
        if [[ ${TX_DIFF} -gt 0 || ${RX_DIFF} -gt 0 ]]; then
            printf "${i}00ms  - TX: ${TX_DIFF}${TX_GAUGE}\tRX: ${RX_DIFF}${TX_GAUGE}\n"
        fi
    done
    if [ ${RX_TOTAL} -gt 1024 ]; then
        RX_GAUGE="KB"
        RX_TOTAL=$[ ${RX_TOTAL} / 1024 ]
        if [ ${RX_TOTAL} -gt 1024 ]; then
            RX_GAUGE="MB"
            RX_TOTAL=$[ ${RX_TOTAL} / 1024 ]
        fi
    fi
    if [ ${TX_TOTAL} -gt 1024 ]; then
        TX_GAUGE="KB"
        TX_TOTAL=$[ ${TX_TOTAL} / 1024 ]
        if [ ${TX_TOTAL} -gt 1024 ]; then
            TX_GAUGE="MB"
            TX_TOTAL=$[ ${TX_TOTAL} / 1024 ]
        fi
    fi 
    printf "1000ms - TX: ${TX_TOTAL}${TX_GAUGE}\tRX: ${RX_TOTAL}${RX_GAUGE}\n"
}

Initial URL

                                

Initial Description
Bytes per second isn't granular enough to get a real idea of how your network is really being used.

Initial Title
Monitoring Network Microbursts

Initial Tags
linux

Initial Language
Bash