drbd的相关数据可以cat /proc/drbd 获得,输出的结果类似:
sles1:~ # cat /proc/drbd version: 8.3.11 (api:88/proto:86-96) GIT-hash: 0de839cee13a4160eed6037c4bddd066645e23c5 build by root@sles1, 2011-08-10 17:30:07 0: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate A r----- ns:2 nr:0 dw:2 dr:1061 al:1 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0 1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate A r----- ns:113884 nr:0 dw:113884 dr:2538 al:37 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
写个脚本分析之:
INTERVAL=10 KEEP=no ns_value () { c=0 for i in `sed -n -e 's/^.*ns:\([[:digit:]]*\).*/\1/p' /proc/drbd`; do ((c=c+i)) done echo $c } nr_value () { c=0 for i in `sed -n -e 's/^.*nr:\([[:digit:]]*\).*/\1/p' /proc/drbd`; do ((c=c+i)) done echo $c } usage () { echo "usage ${0#*/} [-i interval] {send|read}" } if [[ $# -lt 1 ]]; then usage exit 1 fi while getopts ":i:" opt do case $opt in i) INTERVAL=$OPTARG KEEP=yes shift 2 ;; *) usage exit 2 esac done sw=$1 if [[ $sw == "send" ]]; then act="Send" func=ns_value else act="Read" func=nr_value fi p=`$func` while true; do sleep $INTERVAL c=`$func` v=$(($c-$p)) a=$(($v/$INTERVAL)) echo -e "$act $v kb in $INTERVAL second, average $a kbps" if [[ $KEEP != "yes" ]];then break; fi p=$c done
输出结果
sles1:~/log # ./netio.sh -i 5 send Send 165568 kb in 5 second, average 33113 kbps Send 184440 kb in 5 second, average 36888 kbps Send 145592 kb in 5 second, average 29118 kbps
目前只监控了network send 和 network send,扩展一下可以监控更多的信息。其中$act和$func的初始化似乎可以优化成关联数组的形式,一直对shell的数组不熟,先这样吧……
脚本语言让我感到神奇的一点是,可以用一个字符串来进行函数调用……上面的func=ns_value,有点像c的函数指针,我也不知道这是什么语法,反正它是工作了……