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"