在FreeBSD用RRD监控网络流量
○、写在前面
忙完了考研、找工作,还有那可怕的《体系结构》考试,终于有时间写点什么东西了。
2002年的暑假,进入了学校的网络中心帮忙做一些力所能及的事情。对于网络流量的监控,自然在校园信息化进程中起到了相当的作用。通过它,我们可以对网络的负载情况、网络链路故障进行一些分析。
根据一些其他高校的经验,在2002年,我们也采用了MRTG对网络流量进行监控。在经历了一系列包括采集不准确,服务器采集负担过重以及千兆端口采集流量不准的震荡后,2005年的2月份,我终于下定决心在FreeBSD平台上,搭一个基于RRDTool的流量监控。
感谢FJNUNIC的Chen Song老师、netstorm一直以来无微不至的支持和关怀。
参考资料和书籍:
1.《rrdtool 教學》 作者: abelyang abelyang@twnic.net.tw
唯一的一篇,至少是我所看到的,中文的RRDTool教学的文章。尽管是繁体字版的,但是确实给了很多的启发和思考。相当好的一篇文章。
2.官方文档 http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/
无论怎么样,英文也好,这个永远是最权威的文档。有不明白的可以查这里。
3.《Unix Shell Programming》第三版
尽管abelyang在《rrdtool 教學》中认为Shell编程是用好RRD的基础,但是我并不这样认为。但是用好Shell,确实对配置有很大帮助。这本书确实很好,入门教材。
一、废话和下载
出于尊重作者Tobi Oetiker的目的,哈哈,我们还是要简要介绍一下这个RRD的。
MRTG和RRD是同一个家族的产品。都是在产生 time-series (时间连续)的图文件(如流量,负载,温度,人数.....)。我们这里用它来产生网络流量图。
MRTG功能比较少,我们在日常使用中也希望对采集时间和采集数据进行更加灵活的表现。所以作者又开发了RRDTool。RRD的全称(Acronym for Round Robin Database)。
一起来看一下作者是如何解释的(可以跳过):
It stores the data in a very compact way that will not expand over time, and it presents useful graphs by processing the data to enforce a certain data density. It can be used either via simple wrapper scripts (from shell or Perl) or via frontends that poll network devices and put a friendly user interface on it.
我们再一起看看,用这两个工具对同一个网络端口流量检测图的对比:
MRTG检测的网络流量
RRD检测的网络流量
官方下载页:http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/download.html
我们这里使用的是:rrdtool-1.0.49.tar.gz
[注]在2005年5月份,RRD已经推出1.2版本,该版本支持Turetype字体,因此中文相关问题可以得到解决,这里咱们暂时先用早先版本进行讨论,代码和原理是基本相同的。
二、在FreeBSD下的安装
1.解压tar包
$ tar zxvf rrdtool-1.0.49.tar.gz // 解压到当前目录下
2.编译和安装
$ ./configure --prefix=/usr/local // 设定RRDTool的安装目录
$ make
$ make install
3.安装完毕后,可以看到在/usr/local中添加了一个rrd目录
4.进入到/usr/local/rrd/bin 中,敲入:
$ ./rrdtool
RRDtool 1.0.49 Copyright 1997-2004 by Tobias Oetiker
<tobi@oetiker.ch>
Usage: rrdtool [options] command command_options
Valid commands: create, update, graph, dump, restore,
last, info, fetch, tune, resize, xport
RRDtool is distributed under the Terms of the GNU General
Public License Version 2. (www.gnu.org/copyleft/gpl.html)
For more information read the RRD manpages
如果可以看到上述提示,恭喜喽,安装成功,可以进入下一步了。
三、RRD的运作流程
参考了很多文档,包括官方文档都没有讲到RRD整体程序的流程,偶觉得这个是很有必要的。对于了解RRD的工作原理是很有帮助的。我们以监控网络流量为例。
① 通过Shell脚本,配置我们监控的端口变量,并生成.rrd的数据文档。(具体操作和含义后面会具体讲)(也有人称之为log文档)
② 由数据更新脚本(NIC_7609.sh),通过调用snmpwalk更新.rrd数据文档。
③ 数据更新脚本(NIC_7609.sh)调用绘图脚本(NIC_7609_Graph.sh)根据.rrd数据文档,重新绘图。
④ 休息300S,继续 ②
这样,整个流程就说清楚了,下面就根据流程逐步展开。累……
注:因为官方文档是英文的,翻译过程毕竟存在语言差异,所以在一些特别是概念性很强的地方,偶会给出中文的解释和原官方文档的对照。也许英文原文还更好理解。
四、建立RRD数据文档(.rrd文件)
1.rrdtool 建档语法
rrdtool create filename
[--start|-b start time]
[--step|-s step]
[DS:ds-name:DST:heartbeat:min:max]
[RRA:CF:xff:steps:rows]
用一个官方的例子说明(以下都用这个例子说明参数的使用):
rrdtool create target.rrd
--start 1023654125
--step 300
DS:mem:GAUGE:600:0:671744
RRA:AVERAGE:0.5:12:24
RRA:AVERAGE:0.5:288:31
简单的先说吧:
Create 很容易理解喽,即建档
filename习惯上会以 .rrd 结尾,记住这个数据文档的名字。
--start 这个 filename 的数据记录起始日期 ,以 1970 年至今的秒数
(如果不设置的话,默认是现在)
--step 采集数据的间格时间,习惯上我们会设 300 (秒),当然可以调整,这才是RRD的优势所在。但是采集周期不应该过短,否则会造成服务器负载过重。
原文相关对照:
This example creates a database named target.rrd. Start time (1023654125) is specified in total number of seconds since epoch (time in seconds since 01-01-1970). While updating the database, update time is also specified. This update time MUST occur after start time and MUST be in seconds since epoch.
The step of 300 seconds indicates that database expects new values every 300 seconds. The wrapper script should be scheduled to run every step seconds so that it updates the database every step seconds.
后面两个参数是比较大条的,呵呵,所以要慢慢理解喽……
2.DS (Data Source 申明数据源的意思)
DS是用来申明数据源的,也可以理解为申明数据变量,也就是你要检测的端口对应的变量名,这个参数在画图的时候还要使用的。
官方英文解释是这样的:
DS (Data Source) is the actual variable which relates to the parameter on the device that has to be monitored.
语法:DS : variable_name : DST : heartbeat : min : max
① DS是关键字。
② variable_name 是在数据文档中记录对应的变量名。当每一个刷新周期到来的时候,数据文档中各变量对应的值就会被更新。这个变量对应的值在官方文档中也叫做主要数据点――PDP(Primary Data Point)。
③ DST:DS的类型,有四个可选项:COUNTER, DERIVE, ABSOLUTE, GAUGE
我们用的最多的就是GAUGE了,它的中文解释是:测量。在这里它表示实际的值。而COUNTER表示的是经过一个刷新周期的变化率。剩下两个用的不过,我们也不介绍了。同样的给出一个官方的解释:
COUNTER will save the rate of change of the value over a step period. This assumes that the value is always increasing (difference between last two values is more than 0). Traffic counters on a router is an ideal candidate for using COUNTER as DST.
DERIVE is same as COUNTER but it allows negative values as well. If you want to see the rate of change in free diskspace on your server, then you might want to use the DERIVE data type.
ABSOLUTE also saves the rate of change but it assumes that previous value is set to 0. The difference between current and previous value is always equal to the current value. So, it stores the current value divided by step interval (300 seconds in our example).
GAUGE does not save the rate of change. It saves the actual value itself. There are no divisions/calculations. Memory consumption in a server is an ideal example of gauge.
下面这个例子很好说明了这个问题:
Values = 300, 600, 900, 1200 实际值
Step = 300 seconds 刷新周期
COUNTER DS = 1, 1, 1, 1 COUNTER定义的DS的值
DERIVE DS = 1, 1, 1, 1 DERIVE定义的DS的值
ABSOLUTE DS = 1, 2, 3, 4 ABSOLUTE定义的DS的值
GAUGE DS = 300, 600, 900, 1200 GAUGE定义的DS的值
④ heartbeat 心跳有效期
比如在例子中,我们定义了心跳有效时间是600秒,也就是两个刷新周期。如果,在两个刷新周期内,都没有接收到数据更新,那么这个时候,必须往数据文档中写入一个(UN)UNKNOWN值。这是RRDTool的一个特别的地方。要知道MRTG在处理网络中断的时候,记录的是0值。这个0和UN还是有一定区别的。
官方原文如下:
In our example, heartbeat is 600 seconds. If database does not get a new PDP within 300 seconds, it will wait for another 300 seconds (total 600 seconds). If it doesnt receive any PDP with in 600 seconds, it will save an UNKNOWN value into database. This UNKNOWN value is a special feature of RRDTool - it is much better than to assume a missing value was 0 (zero). For example, the traffic flow counter on a router keeps on increasing. Lets say, a value is missed for an interval and 0 is stored instead of UNKNOWN. Now when next value becomes available, it will calculate difference between current value and previous value (0) which is not correct. So, inserting value UNKNOWN makes much more sense here.
⑤ min : max 记录数据的最小值和最大值
DS数值的有效范围,超出就是UN喽。也可以不限制用U
3.RRA(Round Robin Archive)
这个我也不知道,该如何给一个中文的翻译。它的作用就是定义更新的数据是如何记录的。比如我们每5分钟产生一条刷新的数据,那么一个小时就是12条。每天就是288条。这么庞大的数据量,一定不可能都存下来。肯定有一个合并(consolidate)数据的方式,那么这个就是RRA的作用了。
语法:RRA : CF : xff : step : rows
① RRA用以声明RRAs的关键字
② CF(consolidation function)合并方式,有几个可选项:AVERAGE, MINIMUM, MAXIMUM, LAST 。
上面说过了,经过一个刷新周期,会获得一个主数据点(PDP),将若干个PDPs使用合并方式(CF)合并后会产生一个合并数据点CDP(consolidated data point)。
③ xff:xfiles factor 和unkown数据有关,很多资料都取0.5
④ step:有step条PDP合并形成一条CDP
⑤ row:记录的合并数据点CDP条数。
对应官方的描述为:
RRA is the keyword to declare RRAs. The consolidation function (CF) can be AVERAGE, MINIMUM, MAXIMUM, and LAST. The concept of the consolidated data point (CDP) comes into the picture here. A CDP is CFed (averaged, maximum/minimum value or last value) from step number of PDPs. This RRA will hold rows CDPs.
我们在例子中对RRA是这样定义的:
RRA:AVERAGE:0.5:12:24
RRA:AVERAGE:0.5:288:31
对于第一个RRA,12条的PDP(每经过一个刷新周期产生一个PDP)经过CFed(AVERAGE),也就是取平均值,产生一个CDP,24个CDPs存档。我们一起来计算一下时间,如果一个周期是300秒,那么12个PDP的产生时间就是1一个小时,也就是一个小时产生一个CDP。24个CDPs时间就是一天。说明这条通过这条RRA,我们可以取得一天的数据值。
一天后,又经过一个小时。就会产生第25条,那么如何记录这个第25条数据呢?根据我们这个RRA的定义,它将会替代第一条CDP的位置。我们通过下面这个图来说明:












文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面