1、查看vmstat的输出

1[root@361way ~]# vmstat 1 2
2procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
3 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
4 0  0      0 1479452 420588 5600548    0    0     0     3    0    5  0  0 100  0  0
5 0  0      0 1479444 420588 5600548    0    0     0     0   40   37  0  0 100  0  0

2、为vmstat的输出结果建表

 1rrdtool create vmstat.rrd --step 1  \
 2DS:r:GAUGE:5:U:U \
 3DS:b:GAUGE:5:U:U \
 4DS:swpd:GAUGE:5:U:U \
 5DS:free:GAUGE:5:U:U \
 6DS:buff:GAUGE:5:U:U \
 7DS:cache:GAUGE:5:U:U \
 8DS:si:GAUGE:5:U:U \
 9DS:so:GAUGE:5:U:U \
10DS:bi:GAUGE:5:U:U \
11DS:bo:GAUGE:5:U:U \
12DS:in:GAUGE:5:U:U \
13DS:cs:GAUGE:5:U:U \
14DS:us:GAUGE:5:U:U \
15DS:sy:GAUGE:5:U:U \
16DS:id:GAUGE:5:U:U \
17DS:wa:GAUGE:5:U:U \
18DS:st:GAUGE:5:U:U \
19RRA:AVERAGE:0.5:1:100000

DS:r:GAUGE:5:U:U 中的5表示两次更新最大心跳时间秒数。第一个U表示最小值,第二个U表示最大值,使用U(ulimit)表示值不限制。

3、将vmstat的输出各项值都存入vmstat.rrd文件中

1vmstat 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

收集最近10分钟的数据并将其导到的vmstat.rrd 库文件中。

4、利用rrdtool graph汇图

rrdtool自带有汇图参数,可以利用rrdtool graph为刚刚输入的数据绘图

 1#图1
 2rrdtool graph physical_consumed.gif \
 3--title "Physical CPU Consumed" \
 4--vertical-label "CPUs" \
 5--height 300 \
 6--start 1422606034 \
 7--end 1422606333 \
 8DEF:st=vmstat.rrd:st:AVERAGE LINE2:st#00FF00:"Physical Consumed"
 9#图2
10rrdtool graph cpu_util.gif \
11--rigid --lower-limit 0 --upper-limit 100 \
12--title "CPU Util" \
13--vertical-label "Percent Stacked" \
14--start 1422606034 \
15--end 1422606333 \
16--height 300 \
17DEF:us=vmstat.rrd:us:AVERAGE AREA:us#00FF00:"User" \
18DEF:sy=vmstat.rrd:sy:AVERAGE STACK:sy#0000FF:"System" \
19DEF:wa=vmstat.rrd:wa:AVERAGE STACK:wa#FF0000:"Wait" \
20DEF:id=vmstat.rrd:id:AVERAGE STACK:id#FFFFFF:"Idle"

注:

1、上需的start和end时间,可以从vmstat.output文件中的第四列的值中获取; 2、lower-limit和upper-limit为cpu_util 图表的纵坐标指定了上下限。

生成的效果图如下:

cpu-con

cpu-util

5、完整的脚本如下

 1#!/bin/bash
 2rrdtool create vmstat.rrd --step 1  \
 3DS:r:GAUGE:5:U:U \
 4DS:b:GAUGE:5:U:U \
 5DS:swpd:GAUGE:5:U:U \
 6DS:free:GAUGE:5:U:U \
 7DS:buff:GAUGE:5:U:U \
 8DS:cache:GAUGE:5:U:U \
 9DS:si:GAUGE:5:U:U \
10DS:so:GAUGE:5:U:U \
11DS:bi:GAUGE:5:U:U \
12DS:bo:GAUGE:5:U:U \
13DS:in:GAUGE:5:U:U \
14DS:cs:GAUGE:5:U:U \
15DS:us:GAUGE:5:U:U \
16DS:sy:GAUGE:5:U:U \
17DS:id:GAUGE:5:U:U \
18DS:wa:GAUGE:5:U:U \
19DS:st:GAUGE:5:U:U \
20RRA:AVERAGE:0.5:1:100000
21$TIME=`date +%s`
22vmstat 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
23$ENDTIME=`date +%s`
24bash <. area:us="" consumed="" cpu="" cpu_util.gif="" def:id="vmstat.rrd:id:AVERAGE" def:st="vmstat.rrd:st:AVERAGE" def:sy="vmstat.rrd:sy:AVERAGE" def:us="vmstat.rrd:us:AVERAGE" def:wa="vmstat.rrd:wa:AVERAGE" graph="" line2:st="" physical_consumed.gif="" rrdtool="" stack:id="" stack:sy="" stack:wa="" stacked="" util="">