linux中利用rrdtool為vmstat輸出繪圖

來源:互聯網
上載者:User

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"

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.