RRDtool是由Tobias Oetiker 编写并由全球各地的许多人贡献的工具。本篇文档的作者是Alex van den Bogaerdt ,主要是帮助你理解RRDtool是什么,它能够帮助你作些什么。 RRDtool的文档对于有些人来说过于技术化。本教程帮助你理解RRDtool的基本概念。它为你自学RRDtool的文档做好准备。本文档还重点介绍了网络统计方面的知识。
说明
关于本文档的重要说明
Important
Please don't skip ahead in this document! The first part of this document explains the basics and may be boring. But if you don't understand the basics, the examples will not be as meaningful to you.

RRDtool是什么东西?
RRDtool是什么东西?
RRDtool refers to Round Robin Database tool. Round robin is a technique that works with a fixed amount of data, and a pointer to the current element. Think of a circle with some dots plotted on the edge -- these dots are the places where data can be stored. Draw an arrow from the center of the circle to one of the dots -- this is the pointer. When the current data is read or written, the pointer moves to the next element. As we are on a circle there is neither a beginning nor an end, you can go on and on and on. After a while, all the available places will be used and the process automatically reuses old locations. This way, the dataset will not grow in size and therefore requires no maintenance. RRDtool works with with Round Robin Databases (RRDs). It stores and retrieves data from them.

RRDtool是指Round Robin Database 工具(环状数据库)。Round robin是一种处理定量数据、以及当前元素指针的技术。想象一个周边标有点的圆环--这些点就是时间存储的位置。从圆心画一条到圆周的某个点的箭头--这就是指针。就像我们在一个圆环上一样,没有起点和终点,你可以一直往下走下去。过来一段时间,所有可用的位置都会被用过,该循环过程会自动重用原来的位置。这样,数据集不会增大,并且不需要维护。RRDtool处理RRD数据库。它用向RRD数据库存储数据、从RRD数据库中提取数据。
RRD中可以存放什么样的数据
RRD中可以存放什么样的数据
You name it, it will probably fit as long as it is some sort of time-series data. This means you have to be able to measure some value at several points in time and provide this information to RRDtool. If you can do this, RRDtool will be able to store it. The values must be numerical but don't have to be integers, as is the case with MRTG (the next section will give more details on this more specialized application).

可以适合时间序列的数据。就是说你必须能够在时间的几个点上度量某些值,并提供这些信息给RRDtool。如果你能够做到这一点,RRDtool就能够存储它们。这些数值必须是数字,但是不一定要是整数,在与MRTG合用时。(下一节会给出更详细的介绍)。
Many examples below talk about SNMP which is an acronym for Simple Network Management Protocol. Simple refers to the protocol -- it does not mean it is simple to manage or monitor a network. After working your way through this document, you should know enough to be able to understand what people are talking about. For now, just realize that SNMP can be used to query devices for the values of counters they keep. It is the value from those counters that we want to store in the RRD.

下面的许多例子是关于SNMP的,SNMP是简单网络过来协议的缩写。简单是指协议简单--并不表示管理或监视网络简单。读完本篇文档后,你应当能够对人们谈论的SNMP的东西有更多的理解。现在,只要知道SNMP可以用来查询设备中保持的计数器的值就可以了。我们要存放到RRD数据库中的正是这些计数器中的数值。
RRDtool可以用来干什么?
RRDtool可以用来干什么?
RRDtool originated from MRTG (Multi Router Traffic Grapher). MRTG started as a tiny little script for graphing the use of a university's connection to the Internet. MRTG was later (ab-)used as a tool for graphing other data sources including temperature, speed, voltage, number of printouts and the like.

RRDtool源自MRTG(多路由器流量绘图器)。MRTG是有一个大学连接到互联网链路的使用率的小脚本开始的。MRTG后来被当作绘制其他数据源的工具使用,包括温度、速度、电压、输出量等等。
Most likely you will start to use RRDtool to store and process data collected via SNMP. The data will most likely be bytes (or bits) transfered from and to a network or a computer. But it can also be used to display tidal waves, solar radiation, power consumption, number of visitors at an exhibition, noise levels near an airport, temperature on your favorite holiday location, temperature in the fridge and whatever you imagination can come up with.

很可能你会从使用RRDtool来存储和处理通过SNMP收集到的数据开始。这些数据很可能是某个网络或计算机接收或发送的字节数(比特数)。它也可以用来显示潮水的波浪、阳光射线、电力消耗、展会的参观人员、机场附近的噪音等级、你喜欢的度假区的温度、电冰箱的温度、以及任何你可以想象的东西。
You only need a sensor to measure the data and be able to feed the numbers into RRDtool. RRDtool then lets you create a database, store data in it, retrieve that data and create graphs in PNG format for display on a web browser. Those PNG p_w_picpaths are dependent on the data you collected and could be, for instance, an overview of the average network usage, or the peaks that occurred.

你最需要一个度量数据,以及能够提供这些数据给RRDtool的感应器就可以了。RRDtool会让你创建数据库、存储数据、提取数据、创建用于在Web浏览器中显示的PNG格式的图像。这些PNG图像以来于你收集的数据,它可以是网络平均使用率、峰值。
读过本文后仍有问题怎么办?
读过本文后仍有问题怎么办?
First of all: read it again! You may have missed something. If you are unable to compile the sources and you have a fairly common OS, it will probably not be the fault of RRDtool. There may be pre-compiled versions around on the Internet. If they come from trusted sources, get one of those.

首先:再读一遍!你可能漏了某些地方。如果你无法编译源代码,而且你的操作系统相当普遍,很可能不是RRDtool的错误。互联网上有已经编译好的版本。如果来自可信赖的网站,就下载一个。
If on the other hand the program works but does not give you the expected results, it will be a problem with configuring it. Review your configuration and compare it with the examples that follow.

另一种情况是程序能够工作,但是没有得到预期的结果,可能是配置方面的问题。检查你的配置,与后面例子的配置比较一下。
There is a mailing list and an archive of it. Read the list for a few weeks and search the archive. It is considered rude to just ask a question without searching the archives: your problem may already have been solved for somebody else! This is true for most, if not all, mailing lists and not only for this particular one. Look in the documentation that came with RRDtool for the location and usage of the list.

RRDtool有一个邮件列表及其归档。读一下几周的列表并搜索归档。没有搜索过归档就提问是很粗鲁的做法:你遇到的问题可能已经由其他人解决了!尽管不是全部,大多数问题都会这样,邮件列表不是经济为了某个参与者服务的。看看RRDtool的文档了解邮件列表的位置和使用方法。
I suggest you take a moment to subscribe to the mailing list right now by sending an email to rrd-user-request@list.ee.ethz.ch with a subject of subscribe . If you ever want to leave this list, just write an email to the same address but now with a subject of unsubscribe .

我建议你马上就花点时间订阅邮件列表,只要发送一封主题为 subscribe 的邮件到 rrd-user-request@list.ee.ethz.ch 就可以了。如果要退定该邮件列表,只需要发送主题为 unsubscribe 到同样的地址即可。
你的第一个RRD数据库
你的第一个RRD数据库
In my opinion the best way to learn something is to actually do it. Why not start right now? We will create a database, put some values in it and extract this data again. Your output should be the same as the output that is included in this document.

在我看来,学习某个东西的最好办法就是实践。为什么现在不开始呢?我们会创建一个数据库,放一些数值到它里面,然后提取这些数据。你的输出应当与本文档中的输出是一样的。
We will start with some easy stuff and compare a car with a router, or compare kilometers (miles if you wish) with bits and bytes. It's all the same: some number over some time.

我们会从一些简单的入手,然后把汽车与路由器比较,或者将公里和比特、字节数比较。他们都是一样的:都是某些时段的某些数值。
Assume we have a device that transfers bytes to and from the Internet. This device keeps a counter that starts at zero when it is turned on, increasing with every byte that is transfered. This counter will probably have a maximum value. If this value is reached and an extra byte is counted, the counter starts over at zero. This is the same as many counters in the world such as the mileage counter in a car.

假设我们有一个向互联网发送数据和接收数据的设备。该设备保留一个计数器,该计数器在开启设备时设置为0,并在每传送一个字节就加1。该计数器可能会有一个最大值。如果该值达到最大时,在加一个字节的计数,该计数器就会再次从0开始。这与世界上的许多计数器都是一样的,比如车辆上的里程计数器。
Most discussions about networking talk about bits per second so lets get used to that right away. Assume a byte is eight bits and start to think in bits not bytes. The counter, however, still counts bytes! In the SNMP world most of the counters are 32 bits. That means they are counting from 0 to 4'294'967'295. We will use these values in the examples. The device, when asked, returns the current value of the counter. We know the time that has passes since we last asked so we now know how many bytes have been transfered *on average* per second. This is not very hard to calculate. First in words, then in calculations:

关于网络的讨论通常用每秒比特数来衡量,因此我们要习惯这种用法。把一个字节看成是8个比特,并且开始用比特而不是字节来思考问题。不过,计数器仍然用字节数为单位来计量!在SNMP世界里,大部分的计数器都是32比特的。这就意味着他们计数范围是0-4294967。我们在例子里会用到这些数值。该设备在被查询时,会返回计数器的当前值。我们知道从上次查询设备开始到现在的时间,因此我们现在就知道每秒 平均 传输了多少字节数。这不难计算。首先用文字来描述,然后计算:
Take the current counter, subtract the previous value from it.
Do the same with the current time and the previous time (in seconds).
Divide the outcome of (1) by the outcome of (2), the result is the amount of bytes per second. Multiply by eight to get the number of bits per second (bps).
用当前查询到的计数器值,减去上一次查询的计数器值
把当前查询时间和上次查询时间作上述同样操作(秒)
将(1)的结果除以(2)的结果,得到的结果就是每秒的字节数。乘以8就得到每秒的比特数(bps)
bps = (counter_now - counter_before) / (time_now - time_before) * 8

For some people it may help to translate this to an automobile example. Do not try this example, and if you do, don't blame me for the results!

对一些人来说,这会有助于他们把此例换成一个汽车的例子。不要用这个例子来试,如果做了,试验的结果可不要埋怨我!
People who are not used to think in kilometers per hour can translate most into miles per hour by dividing km by 1.6 (close enough). I will use the following abbreviations:

不习惯用公里/小时的人来说可以将公里除以1.6来得到英里/小时(足够精确)。我会用到下面的所写:
M: meter
KM: kilometer (= 1'000 meters).
H: hour
S: second
KM/H: kilometers per hour
M/S: meters per second
M : 米
KM: 公里(1000米)
H: 小时
S: 秒
KM/H:公里/小时
M/S: 米/秒
You are driving a car. At 12:05 you read the counter in the dashboard and it tells you that the car has moved 12'345 KM until that moment. At 12:10 you look again, it reads 12'357 KM. This means you have traveled 12 KM in five minutes. A scientist would translate that into meters per second and this makes a nice comparison toward the problem of (bytes per five minutes) versus (bits per second).

你现在正在开车。12:05时你看了一下仪表盘上的里程计数器,它显示这俩车已经行驶了12345公里。12:10分时,你有看了一下里程计数器,它显示12357公里。这表示你在5分钟内行驶了12公里。科技人员会把它转换成米/秒,这样可以更好的进行比较(每5分钟的字节数)和(每秒比特数)。
We traveled 12 kilometers which is 12'000 meters. We did that in five minutes or 300 seconds. Our speed is 12'000M / 300S or 40 M/S.

我们行驶了12公里,也就是12000米。我们在5分钟内,或者说是300秒内完成。我们的速度是1200米/300秒,或者说是40米/秒。
We could also calculate the speed in KM/H: 12 times 5 minutes is an hour, so we have to multiply 12 KM by 12 to get 144 KM/H. For our native English speaking friends: that's 90 MPH so don't try this example at home or where I live :)

我们可以用公里/小时来计算速度:12乘以5分钟就是一个小时,因此我们必须把12公里乘以12得到144公里/小时。对于和我一样说英语的朋友来说,就是90英里/小时,因此不要在家里或我生活的地方尝试这个速度 :)
Remember: these numbers are averages only. There is no way to figure out from the numbers, if you drove at a constant speed. There is an example later on in this tutorial that explains this.

记住:这些数值都仅仅是平均值。如果无法从数字中得到你是否以固定的速度在行驶。本教程后面有一个例子说明这个问题。
I hope you understand that there is no difference in calculating M/S or bps; only the way we collect the data is different. Even the K from kilo is the same as in networking terms k also means 1'000.

我希望你理解在计算米/秒或者比特/秒。唯一的差别在于收集数据的方式。即使是K这个单位也是一样的,因为在网络术语中,K同样表示1000。
We will now create a database where we can keep all these interesting numbers. The method used to start the program may differ slightly from OS to OS, but I assume you can figure it out if it works different on your's. Make sure you do not overwrite any file on your system when executing the following command and type the whole line as one long line (I had to split it for readability) and skip all of the '' characters.

我们现在要创建一个数据库,在此数据库中我们能够保存所有这些有趣的数字。启动这个程序的方法可能在各个操作系统上各不相同,但是我假设你可以搞清楚它是否与你的操作系统不同,键入下面的行作为一个长长的行(为了可读性,我得把他分成几行)并且用 \ 来风格。
rrdtool create test.rrd \
--start 920804400 \
DS:speed:COUNTER:600:U:U \
RRA:AVERAGE:0.5:1:24 \
RRA:AVERAGE:0.5:6:10

(So enter: rrdtool create test.rrd --start 920804400 DS ...)

因此实际键入的内容为:rrdtool create test.rrd --start 920804400 DS ...
创建了什么东西?
创建了什么东西?
We created the round robin database called test (test.rrd) which starts at noon the day I started writing this document, 7th of March, 1999 (this date translates to 920'804'400 seconds as explained below). Our database holds one data source (DS) named speed that represents a counter. This counter is read every five minutes (default). In the same database two round robin archives (RRAs) are kept, one averages the data every time it is read (e.g., there's nothing to average) and keeps 24 samples (24 times 5 minutes is 2 hours). The other averages 6 values (half hour) and contains 10 such averages (e.g., 5 hours).

我们创建的rrd数据库名为test (test.rrd), 它的起始时间是我写这篇文档的当天下午,也就是1999年3月7日(该日期转换成920804400秒)。我们的数据库存放一个名为 'speed' 的数据源(DS),它表示一个计数器。该计数器每5分钟(缺省)读取一次。在同一个数据库中,保存有2个环状归档(RRA),一个是每次读取时的平均数据(例如:没有东西进行平均)并保留24个样本(24乘以5分钟是2小时)。另一个RRA有6个平均值(半小时)并包含10个这样的平均值(例如5个小时)。
RRDtool works with special time stamps coming from the UNIX world. This time stamp is the number of seconds that passed since January 1st 1970 UTC. The time stamp value is translated into local time and it will therefore look different for different time zones.

RRDtool使用来源于UNIX世界的特殊时间戳。该时间戳是自1979年1月1日UTC时间开始到当前逝去的秒数。该时间戳的值被转换成本地时间,它在不同的时区会不一样。
Chances are that you are not in the same part of the world as I am. This means your time zone is different. In all examples where I talk about time, the hours may be wrong for you. This has little effect on the results of the examples, just correct the hours while reading. As an example: where I will see 12:05 the UK folks will see 11:05 .

可能你不是和我在地球的同一个地方。这就是说时区不同。在所有的例子中,我所说的时间当中,小时可能对你来说是错误的。这对这些例子中的结果有一点影响,在阅读时,只需要修正时间中的小时即可。例如:我看到 12:05 的话,在英国的家伙看到的时间就是 11:05 。
We now have to fill our database with some numbers. We'll pretend to have read the following numbers:

现在我们得向数据库中填入一些数字。我们希望读到以下数据:
12:05 12345 KM
12:10 12357 KM
12:15 12363 KM
12:20 12363 KM
12:25 12363 KM
12:30 12373 KM
12:35 12383 KM
12:40 12393 KM
12:45 12399 KM
12:50 12405 KM
12:55 12411 KM
13:00 12415 KM
13:05 12420 KM
13:10 12422 KM
13:15 12423 KM

We fill the database as follows:

我们用下面的命令把上述数据填入数据库:
rrdtool update test.rrd 920804700:12345 920805000:12357 920805300:12363
rrdtool update test.rrd 920805600:12363 920805900:12363 920806200:12373
rrdtool update test.rrd 920806500:12383 920806800:12393 920807100:12399
rrdtool update test.rrd 920807400:12405 920807700:12411 920808000:12415
rrdtool update test.rrd 920808300:12420 920808600:12422 920808900:12423

This reads: update our test database with the following numbers

这就是说:用下面的数据来更新我们的数据库
time 920804700, value 12345
time 920805000, value 12357

etcetera.

等等....
As you can see, it is possible to feed more than one value into the database in one command. I had to stop at three for readability but the real maximum per line is OS dependent.

正如你说看到的那样,可以在一个命令里向数据库中填入多个值。为了可读性,我得只用三个数据,实际一行里运行的最大数据与操作系统相关。
We can now retrieve the data from our database using rrdtool fetch :

我们可以使用 rrdtool fetch 命令从数据库中提取数据。
rrdtool fetch test.rrd AVERAGE --start 920804400 --end 920809200

It should return the following output:

该命令会返回如下输出结果:
speed

920804700: nan
920805000: 4.0000000000e-02
920805300: 2.0000000000e-02
920805600: 0.0000000000e+00
920805900: 0.0000000000e+00
920806200: 3.3333333333e-02
920806500: 3.3333333333e-02
920806800: 3.3333333333e-02
920807100: 2.0000000000e-02
920807400: 2.0000000000e-02
920807700: 2.0000000000e-02
920808000: 1.3333333333e-02
920808300: 1.6666666667e-02
920808600: 6.6666666667e-03
920808900: 3.3333333333e-03
920809200: nan

If it doesn't, something may be wrong. Perhaps your OS will print NaN in a different form. NaN stands for Not A Number. If your OS writes U or UNKN or something similar that's okay. If something else is wrong, it will probably be due to an error you made (assuming that my tutorial is correct of course :-). In that case: delete the database and try again. Sometimes things change. This example used to provide numbers like 0.04 in stead of 4.00000e-02. Those are really the same numbers, just written down differently. Don't be alarmed if a future version of rrdtool displays a slightly different form of output. The examples in this document are correct for version 1.2.0 of RRDtool.

如果不是像上面的输出结果,可能哪里有错误。也许你的操作系统会打印出不同的格式 NaN 。 NaN 表示 非数字 。 如果你的操作系统输出 U 或 UNKN 或者其他类似东西都是正常的。如果其他地方错误,可能是因为你的过程中的那些步骤出错了(当然假设我的教程是完全正确的 :-))。这样的话,删除数据库文件然后再重新尝试。 有时事情就会变化。本例哟难道的数据像 0.04 而不是 4.0000e-02 。这些实际上是一样的数字,只是写法不同而已。如果rrdtool今后的版本显示略有不同是输出也不要大惊小怪。本文档中的例子对于RRDtool 1.2.0版本都是正确的。
The meaning of the above output will become clear below.

上面的输出结果会在后面加以解释。
建几个图示
建几个图示的时候到了
Try the following command:

试试下面的命令:

rrdtool graph speed.png \
--start 920804400 --end 920808000 \
DEF:myspeed=test.rrd:speed:AVERAGE \
LINE2:myspeed#FF0000

This will create speed.png which starts at 12:00 and ends at 13:00. There is a definition of a variable called myspeed, using the data from RRA speed out of database test.rrd . The line drawn is 2 pixels high and represents the variable myspeed. The color is red (specified by its rgb-representation, see below).

该命令会创建名为speed.png的图像文件,该图像从12:00开始,到13:00。有一个名为myspeed的变量定义,它使用来自 test.rrd数据库的 speed RRA中的数据。绘制的线条是2像素高,表示myspeed变量。颜色是红色的(用rgb-表示法指定,见下文).
You'll notice that the start of the graph is not at 12:00 but at 12:05. This is because we have insufficient data to tell the average before that time. This will only happen when you miss some samples, this will not happen a lot, hopefully.

你会注意到图像的起始不是12:00而是12:05。这是因为在此时间之前的数据不够计算出平均值。这只会在缺少某些样本的情况下发生,不会经常发生。
If this has worked: congratulations! If not, check what went wrong.

如果能够绘制出正确的图像:恭喜你!如果没有成功,查查哪里出错了。
The colors are built up from red, green and blue. For each of the components, you specify how much to use in hexadecimal where 00 means not included and FF means fully included. The color white is a mixture of red, green and blue: FFFFFF The color black is all colors off: 000000

颜色是由红、绿、蓝构成的。对每种颜色成分,你可以用16进制来表示使用多少,其中00表示不包含,FF表示完全包含。白色是由红、绿、蓝组成的:FFFFFF。黑色是全部不包含:000000。
red #FF0000
green #00FF00
blue #0000FF
magenta #FF00FF (mixed red with blue)
gray #555555 (one third of all components)

Additionally you can add an alpha channel (transparency). The default will be FF which means non-transparent.

此外,你可以加入一个alpha通道(透明度)。缺省是FF,表示不透明。
The PNG you just created can be displayed using your favorite p_w_picpath viewer. Web browsers will display the PNG via the URL file:///the/path/to/speed.png .

刚才创建的PNG文件可以用你喜欢的图像查看器来显示。通过URL file:///the/path/to/speed.png 可以在Web浏览器中显示该PNG图像。
用几种数学方法来绘图
用几个数学方法来绘图
When looking at the p_w_picpath, you notice that the horizontal axis is labeled 12:10, 12:20, 12:30, 12:40 and 12:50. Sometimes a label doesn't fit (12:00 and 13:00 would be candidates) so they are skipped.

查看图像时,你会注意到横轴下标为 12:10、12:20、12:30、12:40、12:59。有时某些下标不适合(可能是12:00和13:00)会被忽略掉。
The vertical axis displays the range we entered. We provided kilometers and when divided by 300 seconds, we get very small numbers. To be exact, the first value was 12 (12'357-12'345) and divided by 300 this makes 0.04, which is displayed by RRDtool as 40 m meaning 40/1'000. The m (milli) has nothing to do with meters, kilometers or millimeters! RRDtool doesn't know about the physical units of our data, it just works with dimensionless numbers.

纵轴显示我们输入的范围。下面提供的公里数,以及除以300妙的结果,我们得到非常小的数值。为了更加精确,第一个值是12(12567-12456),除以300后得到0.04,RRDtool显示时为 40m 表示 40/1000 。 其中的 m 与米、公里、或者毫米都没有任何关系!RRDtool不知道我们的数据单位,它只处理没有单位的数据。
If we had measured our distances in meters, this would have been (12'357'000-12'345'000)/300 = 12'000/300 = 40.

如果我们用米来衡量我们的距离,就会是这样:(12'357'000-12'345'000)/300 = 12'000/300 = 40.
As most people have a better feel for numbers in this range, we'll correct that. We could recreate our database and store the correct data, but there is a better way: we do some calculations while creating the png file!

因为许多人都对这样的数值范围感觉更好,我们就来修正一下。我们将重新创建数据库,并存储正确的数据。但是有更好的办法:在创建png文件时进行一些计算!
rrdtool graph speed2.png \
--start 920804400 --end 920808000 \
--vertical-label m/s \
DEF:myspeed=test.rrd:speed:AVERAGE \
CDEF:realspeed=myspeed,1000,\* \
LINE2:realspeed#FF0000

Note: Make sure not to forget the backslash in front of the multiplication operator * above. The backslash is needed to escape the * as some operating systems might interpret and expand * instead of passing it to the rrdtool command.

注意:不要忘记操作符 * 后面的 。这个反斜杆用来将*从操作系统可能解释的符号转义,而不是直接传递给rrdtool命令。
After viewing this PNG, you notice the m (milli) has disappeared. This it what the correct result would be. Also, a label has been added to the p_w_picpath. Apart from the things mentioned above, the PNG should look the same.

在查看PNG文件后,你会注意到 m 不见了。正确的结果就是这样。同样,在图像中加入了一个标注。出了上面提到的几点外,PNG看起来应当是一样的。
The calculations are specified in the CDEF part above and are in Reverse Polish Notation ( RPN ). What we requested RRDtool to do is: take the data source myspeed and the number 1000; multiply those . Don't bother with RPN yet, it will be explained later on in more detail. Also, you may want to read my tutorial on CDEFs and Steve Rader's tutorial on RPN. But first finish this tutorial.

计算是在CDEF部分中指定的,使用逆波兰表达式( RPN )表示的。我们要求RRDtool所作的事情是:`取数据源myspeed, 以及数值1000;把他们相乘` 。在此不要被RPN表达式困扰了,后面会详细介绍。同时,你可能想读一读关于CDEF的教程。以及Steve Rader的RPN表达式的教程。不过哦首先看我这篇教程吧。
Hang on! If we can multiply values with 1'000, it should also be possible to display kilometers per hour from the same data!

如果我们用1000乘以这些数值,显示把同样的数据显示成公里/小时也是可以的。
To change a value that is measured in meters per second:

要修改米/秒为单位的值:
计算米/小时: value * 3'600
计算公里/小时: value / 1'000
合起来: value * (3'600/1'000) 或者 value * 3.6

In our example database we made a mistake and we need to compensate for this by multiplying with 1'000. Applying that correction:

在我们的例子数据库中,我们犯了一个错误,我们需要乘以1000来补偿。使用下面的修正办法:
value * 3.6 * 1'000 == value * 3'600

Now let's create this PNG, and add some more magic ...

下子我们来创建这个PNG文件,并加入更多的魔幻功能...
rrdtool graph speed3.png \
--start 920804400 --end 920808000 \
--vertical-label km/h \
DEF:myspeed=test.rrd:speed:AVERAGE \
"CDEF:kmh=myspeed,3600,*" \
CDEF:fast=kmh,100,GT,kmh,0,IF \
CDEF:good=kmh,100,GT,0,kmh,IF \
HRULE:100#0000FF:"Maximum allowed" \
AREA:good#00FF00:"Good speed" \
AREA:fast#FF0000:"Too fast"

Note: here we use another means to escape the * operator by enclosing the whole string in double quotes.

注意:在此我们使用了转义操作符的另一中方法:在双引号中将整个字符串括起来。
This graph looks much better. Speed is shown in KM/H and there is even an extra line with the maximum allowed speed (on the road I travel on). I also changed the colors used to display speed and changed it from a line into an area.

这个图像看起来更好。速度用KM/H表示,有一个附加的线条表示最大允许的速度(在我行驶的道路上的最大限速)。我还修改了速度的显示颜色,把它从线条改为区块。
The calculations are more complex now. For speed measurements within the speed limit they are:

现在计算更加复杂一些。对于在限速内的速度衡量方法是:
检查公里/小时是否大于100 ( kmh,100 ) GT
如果是,返回0,否则返回公里/小时。 ((( kmh,100 ) GT ), 0, kmh) IF

For values above the speed limit:

对于上述的限速值:
检查公里/小时是否大于100 ( kmh,100 ) GT
如果是,返回公里/小时,否则返回0。 ((( kmh,100) GT ), kmh, 0) IF

图形的魔力
图形的魔力
I like to believe there are virtually no limits to how RRDtool graph can manipulate data. I will not explain how it works, but look at the following PNG:

我愿意相信RRDtool的绘图功能能够处理的数据没有任何虚拟的限制。我会解释他们是如何工作的,不过看看下面的PNG图像:
rrdtool graph speed4.png \
--start 920804400 --end 920808000 \
--vertical-label km/h \
DEF:myspeed=test.rrd:speed:AVERAGE \
"CDEF:kmh=myspeed,3600,*" \
CDEF:fast=kmh,100,GT,100,0,IF \
CDEF:over=kmh,100,GT,kmh,100,-,0,IF \
CDEF:good=kmh,100,GT,0,kmh,IF \
HRULE:100#0000FF:"Maximum allowed" \
AREA:good#00FF00:"Good speed" \
AREA:fast#550000:"Too fast" \
STACK:over#FF0000:"Over speed"

Let's create a quick and dirty HTML page to view the three PNGs:

我们来创建一个快速、糟糕的HTML页面来查看这三个PNG图像。

Speed in meters per second


Speed in kilometers per hour


Traveled too fast?


Name the file speed.html or similar, and look at it in your web browser.

把这个文件命名为 speed.html 或者类似的文件名,在浏览器中看看。
Now, all you have to do is measure the values regularly and update the database. When you want to view the data, recreate the PNGs and make sure to refresh them in your browser. (Note: just clicking reload may not be enough, especially when proxies are involved. Try shift-reload or ctrl-F5).

现在,你所需要做的只有按照常规的方式度量数值,然后更新数据库。当你想查看数据时,重新创建PNG,确定在浏览器中刷新它们。(注意,点击刷新是不够的,特别是在有proxy代理服务器的情况下,试一试 shift-reload 或者ctrl-F5).
数据库更新实例
RRD数据库的更新实例
We've already used the update command: it took one or more parameters in the form of : . You'll be glad to know that you can specify the current time by filling in a N as the time. or you could use the time function in Perl (the shortest example in this tutorial):

我们已经使用了 update 命令:它使用1个或多个参数,其格式为: : . 如果你知道可以用一个 N 表示当前的时间,你一定会高兴的。或者你可以用Perl中的 time 函数来指定时间:
perl -e 'print time, "\n" '

How to run a program on regular intervals is OS specific. But here is an example in pseudo code:

如何按照固定间隔运行某个程序各操作系统不同。不过可以用伪码来表示:
- Get the value and put it in variable "$speed"
- rrdtool update speed.rrd N:$speed

(do not try this with our test database, we'll use it in further examples)

不要用我们的测试数据库来实验,我们会在后面的例子中使用到定时运行。
This is all. Run the above script every five minutes. When you need to know what the graphs look like, run the examples above. You could put them in a script as well. After running that script, view the page index.html we created above.

就这些。每5分钟运行一次上面的脚本。在你想知道图像看起来是啥样时,运行上面的例子。你可以把他们放入一个脚本中。运行此脚本后,查看我们刚才创建的页面index.html.
SNMP相关知识
SNMP相关知识
I can imagine very few people that will be able to get real data from their car every five minutes. All other people will have to settle for some other kind of counter. You could measure the number of pages printed by a printer, for example, the cups of coffee made by the coffee machine, a device that counts the electricity used, whatever. Any incrementing counter can be monitored and graphed using the stuff you learned so far. Later on we will also be able to monitor other types of values like temperature.

我可以想像到只有少数的人能够每5分钟从他们的汽车中读取一次真实的数据。其他人不得不清算其他类型的计数器。你可以测量打印机打印的页数,咖啡机做的咖啡杯数,计算使用的电流的设备,都可以。递增的计数器可以被监视,并且用你已经学会的方法被绘制成图像。稍后我们就能够监视像温度计这样的值了。
Most (?) people interested in RRDtool will use the counter that keeps track of octets (bytes) transfered by a network device. So let's do just that next. We will start with a description of how to collect data.

大多数对RRDtool感兴趣的人会使用一个跟踪网络设备传输数据量的计数器。这样我们下一步就来作这个。我们会从解释如何收集数据开始。
Some people will make a remark that there are tools which can do this data collection for you. They are right! However, I feel it is important that you understand they are not necessary. When you have to determine why things went wrong you need to know how they work.

有些人会说有些工具可以帮助你收集数据。他们是对的!不过,我感觉理解这些工具不是必须的非常重要。在你能够确定为什么事情发生了错误,你要知道他们是如何工作的。
One tool used in the example has been talked about very briefly in the beginning of this document, it is called SNMP. It is a way of talking to networked equipment. The tool I use below is called snmpget and this is how it works:

本例中使用的工具在本文档前面非常简短地提到过,它就是所谓的SNMP。它是与联网设备交谈的方式。下面用到的工具名为 snmpget ,以下是关于它是如何工作的说明:
snmpget device password OID



snmpget -v[version] -c[password] device OID

For device you substitute the name, or the IP address, of your device. For password you use the community read string as it is called in the SNMP world. For some devices the default of public might work, however this can be disabled, altered or protected for privacy and security reasons. Read the documentation that comes with your device or program.

对于device, 你要用设备的名称或者IP地址来替换。对于password, 你需要使用SNMP领域中称为 comunity read string 来替换。对于某些设备来说,缺省的 public 可以工作,但是该设置可能会被关闭,调整或者由于安全和机密的原因而被保护起来。请阅读你的设备或程序的文档。
Then there is this parameter, called OID, which means object identifier .

接下来有一个称为OID的参数,它用来表示 对象标识符 。
When you start to learn about SNMP it looks very confusing. It isn't all that difficult when you look at the Management Information Base ( MIB ). It is an upside-down tree that describes data, with a single node as the root and from there a number of branches. These branches end up in another node, they branch out, etc. All the branches have a name and they form the path that we follow all the way down. The branches that we follow are named: iso, org, dod, internet, mgmt and mib-2. These names can also be written down as numbers and are 1 3 6 1 2 1.

刚开始学习SNMP时,它看起来有些令人困惑。在你看到MIB管理信息基时,就不会那么困难了。MIB是用来描述数据的倒状树,它有只有一个根结点,并且由根结点开始有多个分支。这些分支都以另一个结点终结,他们继续向下分支,如此继续。所以的分支都有一个名称,它们构成了一个我们能够沿着往下的路径。我们所沿着的这些分支都被命名了:iso, org, dod, internet, mgmt 和mib-2.这些名称也可以用数字方式记录,就像 1 3 6 1 2 1。
iso.org.dod.internet.mgmt.mib-2 (1.3.6.1.2.1)

There is a lot of confusion about the leading dot that some programs use. There is no leading dot in an OID. However, some programs can use the above part of OIDs as a default. To indicate the difference between abbreviated OIDs and full OIDs they need a leading dot when you specify the complete OID. Often those programs will leave out the default portion when returning the data to you. To make things worse, they have several default prefixes ...

有些程序会使用前导点 . , 令人感到许多困惑。在一个OID中并 没有 前导点。为了显示OID缩记法和OID完整记法的区别,(通常约定)在使用OID的完整记法时使用前导点。通常这些程序在返回数据给你时,会省略却缺省的部分。这些程序有的有几个缺省前缀,这会让事情显得更加糟糕。
Ok, lets continue to the start of our OID: we had 1.3.6.1.2.1 From there, we are especially interested in the branch interfaces which has number 2 (e.g., 1.3.6.1.2.1.2 or 1.3.6.1.2.1.interfaces).

好了,我们继续OID的初步学习:我们有一个1.3.6.1.2.1的OID,我们对(其下面的) interface 分支特别感兴趣,其编号为2(例如:1.3.6.1.2.1.2 或 1.3.6.1.2.1.interface)。
First, we have to get some SNMP program. First look if there is a pre-compiled package available for your OS. This is the preferred way. If not, you will have to get the sources yourself and compile those. The Internet is full of sources, programs etc. Find information using a search engine or whatever you prefer.

首先,我们得要有几个SNMP的程序。先看看你的OS平台上是否有已编译好的软件包。这是最便捷的方式。如果没有,你就得下载源代码、并编译。互联网上到处都是源代码、程序。你可以用你喜欢的任何方式、搜索引擎来找到相关的信息。
Assume you got the program. First try to collect some data that is available on most systems. Remember: there is a short name for the part of the tree that interests us most in the world we live in!

假设你已经有了这些程序。先试着收集大多数系统都有的数据。记住:那些引起我们兴趣的树,它上面的部分都有一个简略名。
I will give an example which can be used on Fedora Core 3. If it doesn't work for you, work your way through the manual of snmp and adapt the example to make it work.

在此我会给出一个在Fedora Core 3操作系统上可用的例子。如果对你的操作系统不可用,请查看snmp的手册,并作相应的调整让它能够运行。
snmpget -v2c -c public myrouter system.sysDescr.0

The device should answer with a description of itself, perhaps an empty one. Until you got a valid answer from a device, perhaps using a different password , or a different device, there is no point in continuing.

该设备应当回应其自身的描述,该描述可能是一个空的。只有在你从某个设备获得响应后,你才能够继续。其中可能需要使用不同的口令、或者不同的设备。
snmpget -v2c -c public myrouter interfaces.ifNumber.0

Hopefully you get a number as a result, the number of interfaces. If so, you can carry on and try a different program called snmpwalk .

最好你得到的结果是一个数字。如果这样的话,你就可以继续往下,并试试另一个叫做 snmpwalk 的程序。
snmpwalk -v2c -c public myrouter interfaces.ifTable.ifEntry.ifDescr

If it returns with a list of interfaces, you're almost there. Here's an example:

如果该命令返回一系列的接口,这就对了。以下是该命令运行的返回结果的例子:
[user@host /home/alex]$ snmpwalk -v2c -c public cisco 2.2.1.2
interfaces.ifTable.ifEntry.ifDescr.1 = "BRI0: B-Channel 1"
interfaces.ifTable.ifEntry.ifDescr.2 = "BRI0: B-Channel 2"
interfaces.ifTable.ifEntry.ifDescr.3 = "BRI0" Hex: 42 52 49 30
interfaces.ifTable.ifEntry.ifDescr.4 = "Ethernet0"
interfaces.ifTable.ifEntry.ifDescr.5 = "Loopback0"

On this cisco equipment, I would like to monitor the Ethernet0 interface and from the above output I see that it is number four. I try:

对于cisco设备,我想监视 ‘Ethernet0` 接口,从上面的输出结果能够看到该接口的编号是 4。 我试着运行如下命令:
[user@host /home/alex]$ snmpget -v2c -c public cisco 2.2.1.10.4 2.2.1.16.4

interfaces.ifTable.ifEntry.ifInOctets.4 = 2290729126
interfaces.ifTable.ifEntry.ifOutOctets.4 = 1256486519

So now I have two OIDs to monitor and they are (in full, this time):

这样,我就有两个OID需要监视,他们是(这次使用完整记法):
1.3.6.1.2.1.2.2.1.10

1.3.6.1.2.1.2.2.1.16

both with an interface number of 4.

这两个OID都有接口编号4。
Don't get fooled, this wasn't my first try. It took some time for me too to understand what all these numbers mean. It does help a lot when they get translated into descriptive text... At least, when people are talking about MIBs and OIDs you know what it's all about. Do not forget the interface number (0 if it is not interface dependent) and try snmpwalk if you don't get an answer from snmpget.

别被糊弄了,我可不是试一次就搞定的。我花了一些数据来搞清这些数字都是什么意思。把这些编号转换成描述文字大有帮助... 至少在大家谈论MIB和OID时,你知道他们是什么东西。别忘了接口编号(如果它不是附属接口,就是0),如果用snmpget没有得到响应,试试snmpwalk。
If you understand the above section and get numbers from your device, continue on with this tutorial. If not, then go back and re-read this part.

应用RRDtool的实例
实例应用RRDtool的实例
Let the fun begin. First, create a new database. It contains data from two counters, called input and output. The data is put into archives that average it. They take 1, 6, 24 or 288 samples at a time. They also go into archives that keep the maximum numbers. This will be explained later on. The time in-between samples is 300 seconds, a good starting point, which is the same as five minutes.

开始有趣的东东吧。首先,创建一个新的数据库。它包含输入和输出2个计数器的数据。该数据被放入能够进行平均的归档中。他们一次使用1、6、24、或 288个样本。他们同时被存入保存最大数字的归档中。稍后会解释。样本间隔时间为300秒,也就是5分钟,这是一个好的开始。:

1 sample "averaged" stays 1 period of 5 minutes
6 samples averaged become one average on 30 minutes
24 samples averaged become one average on 2 hours
288 samples averaged become one average on 1 day

1个样本 “平均” 保留5分钟的周期
6个样本 每30分钟进行一次平均
24个样本 每2小时进行一次平均
288个样本 每1天进行一次平均

Lets try to be compatible with MRTG which stores about the following amount of data:

我们试着与MRTG互相兼容,MRTG存储以下数据::

600 5-minute samples: 2 days and 2 hours
600 30-minute samples: 12.5 days
600 2-hour samples: 50 days
732 1-day samples: 732 days

600 5分钟样本数: 2天和2小时
600 30分钟样本数: 12.5天
600 2小时样本数: 50天
732 1天样本数: 732天

These ranges are appended, so the total amount of data stored in the database is approximately 797 days. RRDtool stores the data differently, it doesn't start the weekly archive where the daily archive stopped. For both archives the most recent data will be near now and therefore we will need to keep more data than MRTG does!

这些范围被补充进去,因此在数据库中保存的数据总数大约797天。RRDtool存储不同的数据,它不会在 每日 归档停止的地方开始 每周 归档。对于这两个归档,最新的数据会是在 now 附近,因此我们需要比MRTG保存更多的数据!
We will need:

我们需要::

600 samples of 5 minutes (2 days and 2 hours)
700 samples of 30 minutes (2 days and 2 hours, plus 12.5 days)
775 samples of 2 hours (above + 50 days)
797 samples of 1 day (above + 732 days, rounded up to 797)

600个5分钟的样本 (2天和2小时)
700个30分钟的样本 (2天和2小时,加12.5天)
775个2小时的样本 (上述+50天)
797个1天的样本 (上述+732天,环型回绕最大797)

rrdtool create myrouter.rrd \
DS:input:COUNTER:600:U:U \
DS:output:COUNTER:600:U:U \
RRA:AVERAGE:0.5:1:600 \
RRA:AVERAGE:0.5:6:700 \
RRA:AVERAGE:0.5:24:775 \
RRA:AVERAGE:0.5:288:797 \
RRA:MAX:0.5:1:600 \
RRA:MAX:0.5:6:700 \
RRA:MAX:0.5:24:775 \
RRA:MAX:0.5:288:797

Next thing to do is to collect data and store it. Here is an example. It is written partially in pseudo code, you will have to find out what to do exactly on your OS to make it work.

接下来要做的就是收集数据并把它保存起来。以下是一个例子。它是用伪码写的,你得根据OS调整后让它能够运行。:

while not the end of the universe
do
get result of
snmpget router community 2.2.1.10.4
into variable $in
get result of
snmpget router community 2.2.1.16.4
into variable $out

rrdtool update myrouter.rrd N:$in:$out

wait for 5 minutes
done

Then, after collecting data for a day, try to create an p_w_picpath using:

在收集了1天的数据后,试着用下面的命令创建图像::

rrdtool graph myrouter-day.png --start -86400 \
DEF:inoctets=myrouter.rrd:input:AVERAGE \
DEF:outoctets=myrouter.rrd:output:AVERAGE \
AREA:inoctets#00FF00:"In traffic" \
LINE1:outoctets#0000FF:"Out traffic"

This should produce a picture with one day worth of traffic. One day is 24 hours of 60 minutes of 60 seconds: 24*60*60=86'400, we start at now minus 86'400 seconds. We define (with DEFs) inoctets and outoctets as the average values from the database myrouter.rrd and draw an area for the in traffic and a line for the out traffic.

这会产生一个具有1天流量值的图像。1天有24(小时)x60(分钟)x60(秒)。我们从当前时间-86400秒开始。我们用DEF把输入和输出字节数定义成myrouter.rrd数据库中的的平均值,并且绘制输入流量区和输出流量线。