rrdtool作图查看ping的响应时间
为了测试到远程一台机器的ping的响应时间,要每隔5分钟ping一次,每次发送10个数据包。然后看min max avg这三个值,为了更直观的表示,现将每次执行后ping返回的数据输入到rrdtool的数据库中,最终生成图片。可以一目了然的看到变化曲线。
下面贴下我的shell吧。
本文假定你已经安装rrdtool,(/usr/local/rrdtool)
第一步:建立数据库
rrdtool=/usr/local/rrdtool/bin
RRD_DB_FILE=/home/ldc_user/ping-ms.rrd
下面贴下我的shell吧。
本文假定你已经安装rrdtool,(/usr/local/rrdtool)
第一步:建立数据库
rrdtool=/usr/local/rrdtool/bin
RRD_DB_FILE=/home/ldc_user/ping-ms.rrd
NOW=`date +%s`
MONTH_IN_SEC=2629743
MONTH_AGO=$(($NOW-$MONTH_IN_SEC))
MONTH_IN_SEC=2629743
MONTH_AGO=$(($NOW-$MONTH_IN_SEC))
if [ -e "$RRD_DB_FILE" ]
then
echo “file exists: $RRD_DB_FILE”
else
$rrdtool/rrdtool create $RRD_DB_FILE –step 300 \
DS:min_ms:GAUGE:600:0:500 \
DS:avg_ms:GAUGE:600:0:500 \
DS:max_ms:GAUGE:600:0:500 \
RRA:AVERAGE:0.5:1:600 \
RRA:AVERAGE:0.5:6:700 \
RRA:AVERAGE:0.5:24:775 \
RRA:AVERAGE:0.5:288:797 \
RRA:MAX:0.5:1:600 \
RRA:MAX:0.5:6:700 \
RRA:MAX:0.5:24:775 \
RRA:MAX:0.5:288:797 \
then
echo “file exists: $RRD_DB_FILE”
else
$rrdtool/rrdtool create $RRD_DB_FILE –step 300 \
DS:min_ms:GAUGE:600:0:500 \
DS:avg_ms:GAUGE:600:0:500 \
DS:max_ms:GAUGE:600:0:500 \
RRA:AVERAGE:0.5:1:600 \
RRA:AVERAGE:0.5:6:700 \
RRA:AVERAGE:0.5:24:775 \
RRA:AVERAGE:0.5:288:797 \
RRA:MAX:0.5:1:600 \
RRA:MAX:0.5:6:700 \
RRA:MAX:0.5:24:775 \
RRA:MAX:0.5:288:797 \
echo “file created: $RRD_DB_FILE”
fi
第二步
为数据库更新数据
rrdtool=/usr/local/rrdtool/bin
RRD_DB_FILE=/home/ldc_user/ping-ms.rrd
VALUE=`tail -1 request |cut -c 5-|cut -d= -f2|cut -c 2-|awk -F/ ‘{print $1″:”$2″:”$3}’`
#VALUE1=`tail -1 request |cut -c 5-|cut -d= -f2|awk -F/ ‘{print $1}’`
$rrdtool/rrdtool update $RRD_DB_FILE N:$VALUE
fi
第二步
为数据库更新数据
rrdtool=/usr/local/rrdtool/bin
RRD_DB_FILE=/home/ldc_user/ping-ms.rrd
VALUE=`tail -1 request |cut -c 5-|cut -d= -f2|cut -c 2-|awk -F/ ‘{print $1″:”$2″:”$3}’`
#VALUE1=`tail -1 request |cut -c 5-|cut -d= -f2|awk -F/ ‘{print $1}’`
$rrdtool/rrdtool update $RRD_DB_FILE N:$VALUE
第三步:
做图
#!/bin/bash
rrdtool=/usr/local/rrdtool/bin
RRD_DB_DIR=/home/ldc_user/
RRD_DB_FILE=/home/ldc_user/ping-ms.rrd
ONE_DAY_AGO=$(($NOW-86400))
ONE_WEEK_AGO=$(($NOW-604800))
ONE_MONTH_AGO=$(($NOW-2629743))
ONE_YEAR_AGO=$(($NOW-31556926))
做图
#!/bin/bash
rrdtool=/usr/local/rrdtool/bin
RRD_DB_DIR=/home/ldc_user/
RRD_DB_FILE=/home/ldc_user/ping-ms.rrd
ONE_DAY_AGO=$(($NOW-86400))
ONE_WEEK_AGO=$(($NOW-604800))
ONE_MONTH_AGO=$(($NOW-2629743))
ONE_YEAR_AGO=$(($NOW-31556926))
# $1 -> period (secondes)
# $2 -> period (text)
do_rrdgraph() {
$rrdtool/rrdtool graph “request-ping_$2.png” -s $1 -h 120 -w 500 -a PNG -t “Ping of request (last $2)” \
-v ms \
–base=1000 \
–alt-autoscale-max \
–lower-limit=0 \
–rigid \
–units-exponent 0 \
–x-grid MINUTE:10:HOUR:1:HOUR:4:0:%X \
DEF:a=”$RRD_DB_DIR/ping-ms.rrd”:min_ms:AVERAGE \
DEF:b=”$RRD_DB_DIR/ping-ms.rrd”:avg_ms:AVERAGE \
DEF:c=”$RRD_DB_DIR/ping-ms.rrd”:max_ms:AVERAGE \
LINE1:a#003300:”min_ms. \: ” \
GPRINT:a:AVERAGE:”%.2lf” \
LINE1:b#9900FF:”avg_ms. \: ” \
GPRINT:b:AVERAGE:”%.2lf” \
LINE1:c#FF0000:”max_ms. \: ” \
GPRINT:c:AVERAGE:”%.2lf”
}
# Launch
do_rrdgraph $ONE_DAY_AGO day
do_rrdgraph $ONE_WEEK_AGO week
do_rrdgraph $ONE_MONTH_AGO month
do_rrdgraph $ONE_YEAR_AGO year
# $2 -> period (text)
do_rrdgraph() {
$rrdtool/rrdtool graph “request-ping_$2.png” -s $1 -h 120 -w 500 -a PNG -t “Ping of request (last $2)” \
-v ms \
–base=1000 \
–alt-autoscale-max \
–lower-limit=0 \
–rigid \
–units-exponent 0 \
–x-grid MINUTE:10:HOUR:1:HOUR:4:0:%X \
DEF:a=”$RRD_DB_DIR/ping-ms.rrd”:min_ms:AVERAGE \
DEF:b=”$RRD_DB_DIR/ping-ms.rrd”:avg_ms:AVERAGE \
DEF:c=”$RRD_DB_DIR/ping-ms.rrd”:max_ms:AVERAGE \
LINE1:a#003300:”min_ms. \: ” \
GPRINT:a:AVERAGE:”%.2lf” \
LINE1:b#9900FF:”avg_ms. \: ” \
GPRINT:b:AVERAGE:”%.2lf” \
LINE1:c#FF0000:”max_ms. \: ” \
GPRINT:c:AVERAGE:”%.2lf”
}
# Launch
do_rrdgraph $ONE_DAY_AGO day
do_rrdgraph $ONE_WEEK_AGO week
do_rrdgraph $ONE_MONTH_AGO month
do_rrdgraph $ONE_YEAR_AGO year
本文来自: Notycn( www.selfcai.com.cn) 详细出处参考: http://www.selfcai.com.cn/index.php/2009/05/rrdtool-ping/
根据上面文章改进的脚本:
#!/bin/sh
rrdtool=/usr/bin
RRD_DB_DIR=/opt/rrd_db/
RRD_DB_FILE=/opt/rrd_db/ping-ms.rrd
RRD_DB_PIC=/opt/rrd_pic/
NOW=`date +%s`
MONTH_IN_SEC=2629743
MONTH_AGO=$(($NOW-$MONTH_IN_SEC))
ONE_QUARTER_AGO=$(($NOW-900))
ONE_HOUR_AGO=$(($NOW-3600))
ONE_DAY_AGO=$(($NOW-86400))
ONE_WEEK_AGO=$(($NOW-604800))
ONE_MONTH_AGO=$(($NOW-2629743))
ONE_YEAR_AGO=$(($NOW-31556926))
do_rrdgraph(){
if [ $2 = "quarter" ]
then
echo quarter
flag=MINUTE:1:MINUTE:3:MINUTE:3:0:"%H:%M"
elif [ $2 = "hour" ]
then
echo hour
flag=MINUTE:3:MINUTE:6:MINUTE:6:0:"%H:%M"
elif [ $2 = "day" ]
then
echo day
flag=MINUTE:30:HOUR:1:HOUR:2:0:"%H:%M"
elif [ $2 = "week" ]
then
echo week
flag=DAY:1:DAY:2:DAY:1:0:"%m/%d"
elif [ $2 = "month" ]
then
echo month
flag=DAY:1:DAY:3:DAY:3:0:"%m/%d"
elif [ $2 = "year" ]
then
echo year
flag=MONTH:1:MONTH:2:MONTH:2:0:"%Y/%m"
fi
if [[ $3 != "" ]]
then
echo "resolution=$3"
resolution="-S $3"
else
echo "resolution=300"
resolution="-S 300"
fi
$rrdtool/rrdtool graph "${RRD_DB_PIC}request-ping_$2.png" -s $1 -h 120 -w 500 -a PNG ${resolution} -t "Ping of request (last $2)" \
-v ms \
--lower-limit=0 \
--units-exponent 0 \
--rigid \
--x-grid ${flag} \
DEF:a="$RRD_DB_DIR/ping-ms.rrd":min_ms:AVERAGE \
DEF:b="$RRD_DB_DIR/ping-ms.rrd":avg_ms:AVERAGE \
DEF:c="$RRD_DB_DIR/ping-ms.rrd":max_ms:AVERAGE \
LINE1:a#003300:"min_ms. \: " \
GPRINT:a:AVERAGE:"%.2lf" \
LINE1:b#9900FF:"avg_ms. \:" \
GPRINT:b:AVERAGE:"%.2lf" \
LINE1:c#FF0000:"max_ms. \:" \
GPRINT:c:AVERAGE:"%.2lf"
}
if [ -e "$RRD_DB_FILE" ]
then
echo "file exits: $RRD_DB_FILE"
VALUE=`ping 163.com -c 2|tail -1|cut -d= -f2|cut -c 2-|awk -F/ '{print $1":"$2":"$3}'`
echo $VALUE
$rrdtool/rrdtool updatev $RRD_DB_FILE N:$VALUE
#exit
do_rrdgraph $ONE_QUARTER_AGO quarter
do_rrdgraph $ONE_HOUR_AGO hour
do_rrdgraph $ONE_DAY_AGO day 600
do_rrdgraph $ONE_WEEK_AGO week 600
do_rrdgraph $ONE_MONTH_AGO month 600
do_rrdgraph $ONE_YEAR_AGO year 1800
else
$rrdtool/rrdtool create $RRD_DB_FILE --step 300 \
DS:min_ms:GAUGE:600:0:500 \
DS:avg_ms:GAUGE:600:0:500 \
DS:max_ms:GAUGE:600:0:500 \
RRA:AVERAGE:0.5:1:600 \
RRA:AVERAGE:0.5:6:700 \
RRA:AVERAGE:0.5:24:775 \
RRA:AVERAGE:0.5:288:797 \
RRA:MAX:0.5:1:600 \
RRA:MAX:0.5:6:700 \
RRA:MAX:0.5:24:775 \
RRA:MAX:0.5:288:797
echo "file created:$RRD_DB_FILE"
fi
rrdtool=/usr/bin
RRD_DB_DIR=/opt/rrd_db/
RRD_DB_FILE=/opt/rrd_db/ping-ms.rrd
RRD_DB_PIC=/opt/rrd_pic/
NOW=`date +%s`
MONTH_IN_SEC=2629743
MONTH_AGO=$(($NOW-$MONTH_IN_SEC))
ONE_QUARTER_AGO=$(($NOW-900))
ONE_HOUR_AGO=$(($NOW-3600))
ONE_DAY_AGO=$(($NOW-86400))
ONE_WEEK_AGO=$(($NOW-604800))
ONE_MONTH_AGO=$(($NOW-2629743))
ONE_YEAR_AGO=$(($NOW-31556926))
do_rrdgraph(){
if [ $2 = "quarter" ]
then
echo quarter
flag=MINUTE:1:MINUTE:3:MINUTE:3:0:"%H:%M"
elif [ $2 = "hour" ]
then
echo hour
flag=MINUTE:3:MINUTE:6:MINUTE:6:0:"%H:%M"
elif [ $2 = "day" ]
then
echo day
flag=MINUTE:30:HOUR:1:HOUR:2:0:"%H:%M"
elif [ $2 = "week" ]
then
echo week
flag=DAY:1:DAY:2:DAY:1:0:"%m/%d"
elif [ $2 = "month" ]
then
echo month
flag=DAY:1:DAY:3:DAY:3:0:"%m/%d"
elif [ $2 = "year" ]
then
echo year
flag=MONTH:1:MONTH:2:MONTH:2:0:"%Y/%m"
fi
if [[ $3 != "" ]]
then
echo "resolution=$3"
resolution="-S $3"
else
echo "resolution=300"
resolution="-S 300"
fi
$rrdtool/rrdtool graph "${RRD_DB_PIC}request-ping_$2.png" -s $1 -h 120 -w 500 -a PNG ${resolution} -t "Ping of request (last $2)" \
-v ms \
--lower-limit=0 \
--units-exponent 0 \
--rigid \
--x-grid ${flag} \
DEF:a="$RRD_DB_DIR/ping-ms.rrd":min_ms:AVERAGE \
DEF:b="$RRD_DB_DIR/ping-ms.rrd":avg_ms:AVERAGE \
DEF:c="$RRD_DB_DIR/ping-ms.rrd":max_ms:AVERAGE \
LINE1:a#003300:"min_ms. \: " \
GPRINT:a:AVERAGE:"%.2lf" \
LINE1:b#9900FF:"avg_ms. \:" \
GPRINT:b:AVERAGE:"%.2lf" \
LINE1:c#FF0000:"max_ms. \:" \
GPRINT:c:AVERAGE:"%.2lf"
}
if [ -e "$RRD_DB_FILE" ]
then
echo "file exits: $RRD_DB_FILE"
VALUE=`ping 163.com -c 2|tail -1|cut -d= -f2|cut -c 2-|awk -F/ '{print $1":"$2":"$3}'`
echo $VALUE
$rrdtool/rrdtool updatev $RRD_DB_FILE N:$VALUE
#exit
do_rrdgraph $ONE_QUARTER_AGO quarter
do_rrdgraph $ONE_HOUR_AGO hour
do_rrdgraph $ONE_DAY_AGO day 600
do_rrdgraph $ONE_WEEK_AGO week 600
do_rrdgraph $ONE_MONTH_AGO month 600
do_rrdgraph $ONE_YEAR_AGO year 1800
else
$rrdtool/rrdtool create $RRD_DB_FILE --step 300 \
DS:min_ms:GAUGE:600:0:500 \
DS:avg_ms:GAUGE:600:0:500 \
DS:max_ms:GAUGE:600:0:500 \
RRA:AVERAGE:0.5:1:600 \
RRA:AVERAGE:0.5:6:700 \
RRA:AVERAGE:0.5:24:775 \
RRA:AVERAGE:0.5:288:797 \
RRA:MAX:0.5:1:600 \
RRA:MAX:0.5:6:700 \
RRA:MAX:0.5:24:775 \
RRA:MAX:0.5:288:797
echo "file created:$RRD_DB_FILE"
fi
rrdtool安装,需要依赖不少东西。一般的的源还没他。
[root@yoebuntu yum.repos.d]# cat dag.repo
[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
gpgkey=http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt
enabled=1
[root@yoebuntu yum.repos.d]# yum install rrdtool
[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
gpgkey=http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt
enabled=1
[root@yoebuntu yum.repos.d]# yum install rrdtool