Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rrd latency test tool
rrdtool create latency_db.rrd \
--step 180 \
DS:pl:GAUGE:540:0:100 \
DS:rtt:GAUGE:540:0:10000000 \
RRA:MAX:0.5:1:500\
#!/bin/bash
#
### set the paths
command="/bin/ping -q -n -c 3"
gawk="/usr/bin/gawk"
rrdtool="/usr/bin/rrdtool"
hosttoping="45.76.102.155"
### data collection routine
get_data() {
local output=$($command $1 2>&1)
local method=$(echo "$output" | $gawk '
BEGIN {pl=100; rtt=0.1}
/packets transmitted/ {
match($0, /([0-9]+)% packet loss/, datapl)
pl=datapl[1]
}
/min\/avg\/max/ {
match($4, /(.*)\/(.*)\/(.*)\/(.*)/, datartt)
rtt=datartt[2]
}
END {print pl ":" rtt}
')
RETURN_DATA=$method
}
### change to the script directory
cd /root/latency
### collect the data
get_data $hosttoping
### update the database
$rrdtool update latency_db.rrd --template pl:rtt N:$RETURN_DATA
#!/bin/bash
#
## change directory to the rrdtool script dir
cd /root/latency/
## Graph for last 24 hours
/usr/bin/rrdtool graph latency_graph.png \
-w 785 -h 120 -a PNG \
--slope-mode \
--start -86400 --end now \
--font DEFAULT:7: \
--title "ping aliyun ap" \
--watermark "`date`" \
--vertical-label "latency(ms)" \
--right-axis-label "latency(ms)" \
--lower-limit 0 \
--right-axis 1:0 \
--x-grid MINUTE:10:HOUR:1:MINUTE:120:0:%R \
--alt-y-grid --rigid \
DEF:roundtrip=latency_db.rrd:rtt:MAX \
DEF:packetloss=latency_db.rrd:pl:MAX \
CDEF:PLNone=packetloss,0,0,LIMIT,UN,UNKN,INF,IF \
CDEF:PL10=packetloss,1,10,LIMIT,UN,UNKN,INF,IF \
CDEF:PL25=packetloss,10,25,LIMIT,UN,UNKN,INF,IF \
CDEF:PL50=packetloss,25,50,LIMIT,UN,UNKN,INF,IF \
CDEF:PL100=packetloss,50,100,LIMIT,UN,UNKN,INF,IF \
LINE1:roundtrip#0000FF:"latency(ms)" \
GPRINT:roundtrip:LAST:"Cur\: %5.2lf" \
GPRINT:roundtrip:AVERAGE:"Avg\: %5.2lf" \
GPRINT:roundtrip:MAX:"Max\: %5.2lf" \
GPRINT:roundtrip:MIN:"Min\: %5.2lf\t\t\t" \
COMMENT:"pkt loss\:" \
AREA:PLNone#FFFFFF:"0%":STACK \
AREA:PL10#FFFF00:"1-10%":STACK \
AREA:PL25#FFCC00:"10-25%":STACK \
AREA:PL50#FF8000:"25-50%":STACK \
AREA:PL100#FF0000:"50-100%":STACK
*/3 * * * * /root/latency/update.sh
*/15 * * * * /root/latency/graph.sh >> /dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment