1、查看vmstat的輸出
[root@361way ~]# vmstat 1 2
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 1479452 420588 5600548 0 0 0 3 0 5 0 0 100 0 0
0 0 0 1479444 420588 5600548 0 0 0 0 40 37 0 0 100 0 0
2、為vmstat的輸出結果建表
rrdtool create vmstat.rrd --step 1 \
DS:r:GAUGE:5:U:U \
DS:b:GAUGE:5:U:U \
DS:swpd:GAUGE:5:U:U \
DS:free:GAUGE:5:U:U \
DS:buff:GAUGE:5:U:U \
DS:cache:GAUGE:5:U:U \
DS:si:GAUGE:5:U:U \
DS:so:GAUGE:5:U:U \
DS:bi:GAUGE:5:U:U \
DS:bo:GAUGE:5:U:U \
DS:in:GAUGE:5:U:U \
DS:cs:GAUGE:5:U:U \
DS:us:GAUGE:5:U:U \
DS:sy:GAUGE:5:U:U \
DS:id:GAUGE:5:U:U \
DS:wa:GAUGE:5:U:U \
DS:st:GAUGE:5:U:U \
RRA:AVERAGE:0.5:1:100000
DS:r:GAUGE:5:U:U 中的5表示兩次更新最大心跳時間秒數。第一個U表示最小值,第二個U表示最大值,使用U(ulimit)表示值不限制。
3、將vmstat的輸出各項值都存入vmstat.rrd檔案中
vmstat 1 600 | awk -v time=`date +%s` '/^.[0-9]/{ n++; print "rrdtool update vmstat.rrd "time+n":" $1 ":" $2 ":" $3 ":" $4 ":" $5 ":" $6 ":" $7 ":" $8 ":" $9 ":" $10 ":" $11 ":" $12 ":" $13 ":" $14 ":" $15 ":" $16 ":" $17 }' >vmstat.output
bash <./vmstat.output
收集最近10分鐘的資料並將其導到的vmstat.rrd 庫檔案中。
4、利用rrdtool graph匯圖
rrdtool內建有匯圖參數,可以利用rrdtool graph為剛剛輸入的資料繪圖
#圖1
rrdtool graph physical_consumed.gif \
--title "Physical CPU Consumed" \
--vertical-label "CPUs" \
--height 300 \
--start 1422606034 \
--end 1422606333 \
DEF:st=vmstat.rrd:st:AVERAGE LINE2:st#00FF00:"Physical Consumed"
#圖2
rrdtool graph cpu_util.gif \
--rigid --lower-limit 0 --upper-limit 100 \
--title "CPU Util" \
--vertical-label "Percent Stacked" \
--start 1422606034 \
--end 1422606333 \
--height 300 \
DEF:us=vmstat.rrd:us:AVERAGE AREA:us#00FF00:"User" \
DEF:sy=vmstat.rrd:sy:AVERAGE STACK:sy#0000FF:"System" \
DEF:wa=vmstat.rrd:wa:AVERAGE STACK:wa#FF0000:"Wait" \
DEF:id=vmstat.rrd:id:AVERAGE STACK:id#FFFFFF:"Idle"
註:
1、上需的start和end時間,可以從vmstat.output檔案中的第四列的值中擷取;
2、lower-limit和upper-limit為cpu_util 圖表的縱座標指定了上下限。
產生的效果圖如下:
5、完整的指令碼如下
#!/bin/bash
rrdtool create vmstat.rrd --step 1 \
DS:r:GAUGE:5:U:U \
DS:b:GAUGE:5:U:U \
DS:swpd:GAUGE:5:U:U \
DS:free:GAUGE:5:U:U \
DS:buff:GAUGE:5:U:U \
DS:cache:GAUGE:5:U:U \
DS:si:GAUGE:5:U:U \
DS:so:GAUGE:5:U:U \
DS:bi:GAUGE:5:U:U \
DS:bo:GAUGE:5:U:U \
DS:in:GAUGE:5:U:U \
DS:cs:GAUGE:5:U:U \
DS:us:GAUGE:5:U:U \
DS:sy:GAUGE:5:U:U \
DS:id:GAUGE:5:U:U \
DS:wa:GAUGE:5:U:U \
DS:st:GAUGE:5:U:U \
RRA:AVERAGE:0.5:1:100000
$TIME=`date +%s`
vmstat 1 600 | awk -v time=$TIME '/^.[0-9]/{ n++; print "rrdtool update vmstat.rrd "time+n":" $1 ":" $2 ":" $3 ":" $4 ":" $5 ":" $6 ":" $7 ":" $8 ":" $9 ":" $10 ":" $11 ":" $12 ":" $13 ":" $14 ":" $15 ":" $16 ":" $17 }' >vmstat.output
$ENDTIME=`date +%s`
bash <./vmstat.output
rrdtool graph physical_consumed.gif \
--title "Physical CPU Consumed" \
--vertical-label "CPUs" \
--height 300 \
--start $TIME \
--end $ENDTIME \
DEF:st=vmstat.rrd:st:AVERAGE LINE2:st#00FF00:"Physical Consumed"
rrdtool graph cpu_util.gif \
--rigid --lower-limit 0 --upper-limit 100 \
--title "CPU Util" \
--vertical-label "Percent Stacked" \
--start $TIME \
--end $ENDTIME \
--height 300 \
DEF:us=vmstat.rrd:us:AVERAGE AREA:us#00FF00:"User" \
DEF:sy=vmstat.rrd:sy:AVERAGE STACK:sy#0000FF:"System" \
DEF:wa=vmstat.rrd:wa:AVERAGE STACK:wa#FF0000:"Wait" \
DEF:id=vmstat.rrd:id:AVERAGE STACK:id#FFFFFF:"Idle"