Differences between version 7 and predecessor to the previous major change of KnowledgeBase/Scripts/diskstats.sh.
Other diffs: Previous Revision, Previous Author
Newer page: | version 7 | Last edited on Tuesday, 23 February 2010 5:23:20 | by CyberLeo | Revert |
Older page: | version 6 | Last edited on Tuesday, 20 October 2009 8:22:14 | by CyberLeo | Revert |
@@ -1,33 +1,51 @@
<code brush="bash">
#!/bin/bash
+# Digest a single number into a pretty number
pretty(){
- # Digest a single number into a pretty number
+ # Initialization
num=${1:-0}
zero=0
+ tera_mag=1099511627776
+ giga_mag=1073741824
+ mega_mag=1048576
+ kilo_mag=1024
+ byte_mag=1
- tera=$(( $num / 1099511627776
))
- num=$(( $num % 1099511627776
))
- giga=$(( $num / 1073741824
))
- num=$(( $num % 1073741824
))
- mega=$(( $num / 1048576
))
- num=$(( $num % 1048576
))
- kilo=$(( $num / 1024
))
- num=$(( $num % 1024
))
+ # Scale down to appropriate magnitude
+
tera=$(( $num / ${tera_mag}
))
+ num=$(( $num % ${tera_mag}
))
+ giga=$(( $num / ${giga_mag}
))
+ num=$(( $num % ${giga_mag}
))
+ mega=$(( $num / ${mega_mag}
))
+ num=$(( $num % ${mega_mag}
))
+ kilo=$(( $num / ${kilo_mag}
))
+ num=$(( $num % ${kilo_mag}
))
byte=${num:-0}
+ # Select proper magnitude and suffix
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
+ [ $kilo -gt 0 ] && one=kilo && two=byte && suffix
=kB
+ [ $mega -gt 0 ] && one=mega && two=kilo && suffix
=MB
+ [ $giga -gt 0 ] && one=giga && two=mega && suffix
=GB
+ [ $tera -gt 0 ] && one=tera && two=giga && suffix
=TB
+ # Compute float
+ whole=$(eval echo \$$one)
+ part=$(eval echo \$$two)
+ part_mag=$(eval echo \$${two}_mag)
+ part=$(( ( ${part:-0} * 100 ) / $(eval echo \$${two}_mag) ))
+ num=$(printf "%s.%s" "${whole}" "${part}")
+
+ # Compute printf format
fmt="%0.2f%s\n"
- [ "${sign
}" = "B"
] && fmt="%0d
%s\n"
- num=
$(eval echo \$$one
.\$$two)
- [ "$two" = "zero" ] && num
=$(eval echo
\$$one)
- printf "${fmt}" ${num} $sign
+ [ "${whole
}" -gt 9
] && fmt="%0.1f
%s\n"
+ [ "
${whole}" -gt 99 ] && fmt="%0
.0f%s
\n"
+ [ "${
two}
" = "zero" ] && fmt
="%0.0f%s
\n"
+
+ # Generate result
+ printf "${fmt}"
"${num}" "
${suffix}"
}
digest(){
dev="${1}"
version 7
#!/bin/bash # Digest a single number into a pretty number pretty(){ # Initialization num=${1:-0} zero=0 tera_mag=1099511627776 giga_mag=1073741824 mega_mag=1048576 kilo_mag=1024 byte_mag=1 # Scale down to appropriate magnitude tera=$(( $num / ${tera_mag} )) num=$(( $num % ${tera_mag} )) giga=$(( $num / ${giga_mag} )) num=$(( $num % ${giga_mag} )) mega=$(( $num / ${mega_mag} )) num=$(( $num % ${mega_mag} )) kilo=$(( $num / ${kilo_mag} )) num=$(( $num % ${kilo_mag} )) byte=${num:-0} # Select proper magnitude and suffix one=byte && two=zero && sign=B [ $kilo -gt 0 ] && one=kilo && two=byte && suffix=kB [ $mega -gt 0 ] && one=mega && two=kilo && suffix=MB [ $giga -gt 0 ] && one=giga && two=mega && suffix=GB [ $tera -gt 0 ] && one=tera && two=giga && suffix=TB # Compute float whole=$(eval echo \$$one) part=$(eval echo \$$two) part_mag=$(eval echo \$${two}_mag) part=$(( ( ${part:-0} * 100 ) / $(eval echo \$${two}_mag) )) num=$(printf "%s.%s" "${whole}" "${part}") # Compute printf format fmt="%0.2f%s\n" [ "${whole}" -gt 9 ] && fmt="%0.1f%s\n" [ "${whole}" -gt 99 ] && fmt="%0.0f%s\n" [ "${two}" = "zero" ] && fmt="%0.0f%s\n" # Generate result printf "${fmt}" "${num}" "${suffix}" } digest(){ dev="${1}" if [ ! -d "/sys/block/${dev}" ] then return 1 fi eval $(printf 'old_r=$old_r_%s old_w=$old_w_%s' "${dev}" "${dev}") [ -z "${old_r}" ] && old_r=0 [ -z "${old_w}" ] && old_w=0 eval $(awk '{print "cur_r=" $3 " cur_w=" $7}' /sys/block/${dev}/stat) dif_r=$(( (${cur_r} - ${old_r}) * 512 )) dif_w=$(( (${cur_w} - ${old_w}) * 512 )) eval $(printf "old_r_%s=%u old_w_%s=%u" "${dev}" "${cur_r}" "${dev}" "${cur_w}") printf "%s: R: %s W: %s \033[K\n" "${dev}" "$(pretty ${dif_r})" "$(pretty ${dif_w})" } while true do printf "\033[0;0H" # Cursor to upper left for I in /sys/block/sd? do digest "$(basename "${I}")" done printf "\033[0J" # Clear the rest of the page sleep 1 done