Note: You are viewing an old version of this page. View the current version.

#!/bin/bash

sdb_r=0
sdb_w=0
sdb_r_old=0
sdb_w_old=0
sdc_r=0
sdc_w=0
sdc_r_old=0
sdc_w_old=0

pretty(){
        # Digest a single number into a pretty number
        num=$1
        zero=000

        tera=$(( $num / 1099511627776 ))
        num=$(( $num % 1099511627776 ))
        giga=$(( $num / 1073741824 ))
        num=$(( $num % 1073741824 ))
        mega=$(( $num / 1048576 ))
        num=$(( $num % 1048576 ))
        kilo=$(( $num / 1024 ))
        num=$(( $num % 1024 ))
        byte=${num:-0}

        one=byte && two=zero && sign=B
        [ $kilo -gt 0 ] && one=kilo && two=byte && sign=kB
        [ $mega -gt 0 ] && one=mega && two=kilo && sign=MB
        [ $giga -gt 0 ] && one=giga && two=mega && sign=GB
        [ $tera -gt 0 ] && one=tera && two=giga && sign=TB

        printf "%0.2f %s\n" $(eval echo \$$one.\$$two) $sign
}

while true
do
        # Get new values
        eval $(printf 'sdb_r=%u sdb_w=%u' $(cat /sys/block/sdb/stat |tr -s ' ' | cut -d' ' -f4,8))
        eval $(printf 'sdc_r=%u sdc_w=%u' $(cat /sys/block/sdc/stat |tr -s ' ' | cut -d' ' -f4,8))

        # Compute diffs
        sdb_r_diff=$(( ($sdb_r - $sdb_r_old) * 512))
        sdb_w_diff=$(( ($sdb_w - $sdb_w_old) * 512))
        sdc_r_diff=$(( ($sdc_r - $sdc_r_old) * 512))
        sdc_w_diff=$(( ($sdc_w - $sdc_w_old) * 512))

        # Print the display
        printf "\r sdb: r %s w %s - sdc: r: %s w %s           " \
                "$(pretty $sdb_r_diff)" "$(pretty $sdb_w_diff)" \
                "$(pretty $sdc_r_diff)" "$(pretty $sdc_w_diff)"

        # Save values
        sdb_r_old=$sdb_r
        sdb_w_old=$sdb_w
        sdc_r_old=$sdc_r
        sdc_w_old=$sdc_w

        # Sleep until the next cycle
        sleep 1
done