RRDtool翻譯文稿

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 [email protected] 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 的郵件到 [email protected] 就可以了。如果要退定該郵件列表,只需要發送主題爲 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數據庫中的的平均值,並且繪製輸入流量區和輸出流量線。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章