一、安装
centos或者redhat默认的yum包中是不包括rrdtool的
cd /etc/yum.repos.d/
vim dag.repo
[dag] name=Dag RPM Repository for Red Hat Enterprise Linux baseurl=http://apt.sw.be/redhat/el5/en/$basearch/dag gpgcheck=0 gpgkey=http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt enabled=1
yum install rrdtool
which rrdtool #可以看一下安装的运行目录
这样就安装完成了
二、使用rrdtool工具创建图形
先创建一个常用变量的配置文件
vim rrd.conf
#!/bin/bash RRDTOOL='/usr/bin/rrdtool' FILE='/var/www/rrdtool/rrdtool.rrd' #存储文件 HOST='192.168.0.1' OUTPUT='/var/www/rrdtool/rrdtool.png' #存储图片
创建生产rrd文件脚本
vim createrrd.sh
#!/bin/bash . rrd.conf $RRDTOOL create $FILE -s 60 \ #每60秒一个点 DS:ping:GAUGE:120:0:65535 \ #120:表示到120秒的时候传进来的点有效,65535:表示这个点的最大值 RRA:AVERAGE:0.5:1:2880 #1表示不压缩点,总共2880个点 RRA:AVERAGE:0.5:5:2880 #5表示5个点进行压缩,比如上面设置1分钟一个点,这里5就表示5分钟一个点
执行了这个脚本后,就会生成rrdtool.rrd文件,这个就是图形点的数据
vim updaterrd.sh #写数据进rrd文件
#!/bin/bash . rrd.conf UPDATECMD=$(ping -c 3 -w 6 $HOST | grep rtt | awk -F "/" '{ print $5 }' ) $RRDTOOL update $FILE N:$UPDATECMD #测试数据
上面的语句执行后,就在rrd中生成数据了,可以通过以下命令来查看
rrdtool fetch rrdtool.rrd AVERAGE
创建生成图形的脚本
vim createimg.sh
#!/bin/bash . rrd.conf $RRDTOOL graph $OUTPUT \ -t "WAN Ping" -v "Time in ms" \ --start="now-1d" \ --end="now" \ --height="120" \ --width="440" \ -c "BACK#000000" \ -c "SHADEA#000000" \ -c "SHADEB#000000" \ -c "FONT#DDDDDD" \ -c "CANVAS#202020" \ -c "GRID#666666" \ -c "MGRID#AAAAAA" \ -c "FRAME#202020" \ -c "ARROW#FFFFFF" \ "DEF:ping_time=$FILE:ping:AVERAGE" \ "CDEF:shading2=ping_time,0.98,*" "AREA:shading2#F90000:$HOST" \ "GPRINT:ping_time:LAST:Last\: %5.2lf ms" \ "GPRINT:ping_time:MIN:Min\: %5.2lf ms" \ "GPRINT:ping_time:MAX:Max\: %5.2lf ms" \ "GPRINT:ping_time:AVERAGE:Avg\: %5.2lf ms" \ "CDEF:shading10=ping_time,0.90,*" "AREA:shading10#E10000" \ "CDEF:shading15=ping_time,0.85,*" "AREA:shading15#D20000" \ "CDEF:shading20=ping_time,0.80,*" "AREA:shading20#C30000" \ "CDEF:shading25=ping_time,0.75,*" "AREA:shading25#B40000" \ "CDEF:shading30=ping_time,0.70,*" "AREA:shading30#A50000" \ "CDEF:shading35=ping_time,0.65,*" "AREA:shading35#960000" \ "CDEF:shading40=ping_time,0.60,*" "AREA:shading40#870000" \ "CDEF:shading45=ping_time,0.55,*" "AREA:shading45#780000" \ "CDEF:shading50=ping_time,0.50,*" "AREA:shading50#690000" \ "CDEF:shading55=ping_time,0.45,*" "AREA:shading55#5A0000" \ "CDEF:shading60=ping_time,0.40,*" "AREA:shading60#4B0000" \ "CDEF:shading65=ping_time,0.35,*" "AREA:shading65#3C0000" \ "CDEF:shading70=ping_time,0.30,*" "AREA:shading70#2D0000" \ "CDEF:shading75=ping_time,0.25,*" "AREA:shading75#180000" \ "CDEF:shading80=ping_time,0.20,*" "AREA:shading80#0F0000" \ "CDEF:shading85=ping_time,0.15,*" "AREA:shading85#000000" >/dev/null
创建一个html页面
vim rrdtool.html
<html> <body> <img src=\'#\'" /rrdtool.png" /> <body> </html>
OK,到这里就完成了整个rrdtool的简单创建过程。
设置环境变量export LANG=en_US.UTF-8 #可以解决编辑html文件输入中文问题
推荐看的文章:http://www.study-area.org/tips/rrdtool/rrdtool.html