Attached a patch for the rrd-tool LIBDBI integration with the following improvements:
[rrdtool.git] / doc / rrdtutorial.pod
1 =head1 NAME
2
3 rrdtutorial - Alex van den Bogaerdt's RRDtool tutorial
4
5 =head1 DESCRIPTION
6
7 RRDtool is written by Tobias Oetiker E<lt>tobi@oetiker.chE<gt> with
8 contributions from many people all around the world. This document is
9 written by Alex van den Bogaerdt E<lt>alex@ergens.op.het.netE<gt> to help you
10 understand what RRDtool is and what it can do for you.
11
12 The documentation provided with RRDtool can be too technical for some
13 people. This tutorial is here to help you understand the basics of
14 RRDtool. It should prepare you to read the documentation yourself.
15 It also explains the general things about statistics with a focus on
16 networking.
17
18 =head1 TUTORIAL
19
20 =head2 Important
21
22 Please don't skip ahead in this document!  The first part of this
23 document explains the basics and may be boring.  But if you don't
24 understand the basics, the examples will not be as meaningful to you.
25
26 =head2 What is RRDtool?
27
28 RRDtool refers to Round Robin Database tool.
29 Round robin is a technique that works with a fixed amount of data, and a
30 pointer to the current element. Think of a circle with some dots plotted
31 on the edge -- these dots are the places where data can be stored. Draw an
32 arrow from the center of the circle to one of the dots -- this is the pointer.
33 When the current data is read or written, the pointer moves to the next
34 element. As we are on a circle there is neither a beginning nor an end, you can
35 go on and on and on. After a while, all the available places will be used and
36 the process automatically reuses old locations. This way, the dataset
37 will not grow in size and therefore requires no maintenance.
38 RRDtool works with with Round Robin Databases (RRDs). It stores and retrieves
39 data from them.
40
41 =head2 What data can be put into an RRD?
42
43 You name it, it will probably fit as long as it is some sort of time-series
44 data. This means you have to be able to measure some value at several points in time and
45 provide this information to RRDtool. If you can do this, RRDtool will be
46 able to store it. The values must be numerical but don't have to be
47 integers, as is the case with MRTG (the next section will give more details
48 on this more specialized application).
49
50 Many examples below talk about SNMP which is an acronym for Simple Network
51 Management Protocol. "Simple" refers to the protocol -- it does not
52 mean it is simple to manage or monitor a network. After working your
53 way through this document, you should know enough to be able to
54 understand what people are talking about. For now, just realize that
55 SNMP can be used to query devices for the values of counters they keep. It
56 is the value from those counters that we want to store in the RRD.
57
58 =head2 What can I do with this tool?
59
60 RRDtool originated from MRTG (Multi Router Traffic Grapher). MRTG
61 started as a tiny little script for graphing the use of a university's
62 connection to the Internet. MRTG was later (ab-)used as a tool for
63 graphing other data sources including temperature, speed, voltage,
64 number of printouts and the like.
65
66 Most likely you will start to use RRDtool to store and process data
67 collected via SNMP. The data will most likely be bytes (or bits)
68 transfered from and to a network or a computer.  But it can also be
69 used to display tidal waves, solar radiation, power consumption,
70 number of visitors at an exhibition, noise levels near an airport,
71 temperature on your favorite holiday location, temperature in the
72 fridge and whatever you imagination can come up with.
73
74 You only need a sensor to measure the data and be able to feed the
75 numbers into RRDtool. RRDtool then lets you create a database, store
76 data in it, retrieve that data and create graphs in PNG format for
77 display on a web browser. Those PNG images are dependent on the data
78 you collected and could be, for instance, an overview of the average
79 network usage, or the peaks that occurred.
80
81 =head2 What if I still have problems after reading this document?
82
83 First of all: read it again! You may have missed something.
84 If you are unable to compile the sources and you have a fairly common
85 OS, it will probably not be the fault of RRDtool. There may be pre-compiled
86 versions around on the Internet. If they come from trusted sources, get
87 one of those.
88
89 If on the other hand the program works but does not give you the
90 expected results, it will be a problem with configuring it. Review
91 your configuration and compare it with the examples that follow.
92
93 There is a mailing list and an archive of it. Read the list for a few
94 weeks and search the archive. It is considered rude to just ask
95 a question without searching the archives: your problem may already have been
96 solved for somebody else!  This is true for most, if not all, mailing lists
97 and not only for this particular one. Look in the documentation that
98 came with RRDtool for the location and usage of the list.
99
100 I suggest you take a moment to subscribe to the mailing list right now
101 by sending an email to E<lt>rrd-users-request@lists.oetiker.chE<gt> with a
102 subject of "subscribe". If you ever want to leave this list, just write
103 an email to the same address but now with a subject of "unsubscribe".
104
105 =head2 How will you help me?
106
107 By giving you some detailed descriptions with detailed examples.
108 I assume that following the instructions in the order presented
109 will give you enough knowledge of RRDtool to experiment for yourself.
110 If it doesn't work the first time, don't give up. Reread the stuff that
111 you did understand, you may have missed something.
112
113 By following the examples you get some hands-on experience and, even
114 more important, some background information of how it works.
115
116 You will need to know something about hexadecimal numbers. If you don't
117 then start with reading L<bin_dec_hex> before you continue here.
118
119 =head2 Your first Round Robin Database
120
121 In my opinion the best way to learn something is to actually do it.
122 Why not start right now?  We will create a database, put some values
123 in it and extract this data again.  Your output should be the same
124 as the output that is included in this document.
125
126 We will start with some easy stuff and compare a car with a router,
127 or compare kilometers (miles if you wish) with bits and bytes. It's
128 all the same: some number over some time.
129
130 Assume we have a device that transfers bytes to and from the Internet.
131 This device keeps a counter that starts at zero when it is turned on,
132 increasing with every byte that is transfered. This counter will probably have
133 a maximum value. If this value is reached and an extra byte is counted,
134 the counter starts over at zero. This is the same as many counters
135 in the world such as the mileage counter in a car.
136
137 Most discussions about networking talk about bits per second so lets
138 get used to that right away. Assume a byte is eight bits and start to
139 think in bits not bytes. The counter, however, still counts bytes!
140 In the SNMP world most of the counters are 32 bits. That means they are
141 counting from 0 to 4'294'967'295. We will use these values in the examples.
142 The device, when asked, returns the current value of the counter. We
143 know the time that has passes since we last asked so we now know how
144 many bytes have been transfered ***on average*** per second. This is
145 not very hard to calculate. First in words, then in calculations:
146
147 =over 3
148
149 =item 1.
150
151 Take the current counter, subtract the previous value from it.
152
153 =item 2.
154
155 Do the same with the current time and the previous time (in seconds).
156
157 =item 3.
158
159 Divide the outcome of (1) by the outcome of (2), the result is
160 the amount of bytes per second. Multiply by eight to get the
161 number of bits per second (bps).
162
163 =back
164
165   bps = (counter_now - counter_before) / (time_now - time_before) * 8
166
167 For some people it may help to translate this to an automobile example.
168 Do not try this example, and if you do, don't blame me for the results!
169
170 People who are not used to think in kilometers per hour can translate
171 most into miles per hour by dividing km by 1.6 (close enough).
172 I will use the following abbreviations:
173
174  M:    meter
175  KM:   kilometer (= 1'000 meters).
176  H:    hour
177  S:    second
178  KM/H: kilometers per hour
179  M/S:  meters per second
180
181 You are driving a car. At 12:05 you read the counter in the dashboard
182 and it tells you that the car has moved 12'345 KM until that moment.
183 At 12:10 you look again, it reads 12'357 KM. This means you have
184 traveled 12 KM in five minutes. A scientist would translate that
185 into meters per second and this makes a nice comparison toward the
186 problem of (bytes per five minutes) versus (bits per second).
187
188 We traveled 12 kilometers which is 12'000 meters. We did that in five
189 minutes or 300 seconds. Our speed is 12'000M / 300S or 40 M/S.
190
191 We could also calculate the speed in KM/H: 12 times 5 minutes
192 is an hour, so we have to multiply 12 KM by 12 to get 144 KM/H.
193 For our native English speaking friends: that's 90 MPH so don't
194 try this example at home or where I live :)
195
196 Remember: these numbers are averages only.  There is no way to figure out
197 from the numbers, if you drove at a constant speed.  There is an example
198 later on in this tutorial that explains this.
199
200 I hope you understand that there is no difference in calculating M/S or
201 bps; only the way we collect the data is different. Even the K from kilo
202 is the same as in networking terms k also means 1'000.
203
204 We will now create a database where we can keep all these interesting
205 numbers. The method used to start the program may differ slightly from
206 OS to OS, but I assume you can figure it out if it works different on
207 your's. Make sure you do not overwrite any file on your system when
208 executing the following command and type the whole line as one long
209 line (I had to split it for readability)
210 and skip all of the '\' characters.
211
212    rrdtool create test.rrd             \
213             --start 920804400          \
214             DS:speed:COUNTER:600:U:U   \
215             RRA:AVERAGE:0.5:1:24       \
216             RRA:AVERAGE:0.5:6:10
217
218 (So enter: C<rrdtool create test.rrd --start 920804400 DS ...>)
219
220 =head2 What has been created?
221
222 We created the round robin database called test (test.rrd) which starts at
223 noon the day I started writing this document, 7th of March, 1999 (this date
224 translates to 920'804'400 seconds as explained below). Our database holds
225 one data source (DS) named "speed" that represents a counter. This counter
226 is read every five minutes (this is the default therefore you don't have to
227 put C<--step=300>).  In the same database two round robin archives (RRAs)
228 are kept, one averages the data every time it is read (e.g., there's nothing
229 to average) and keeps 24 samples (24 times 5 minutes is 2 hours). The other
230 averages 6 values (half hour) and contains 10 such averages (e.g. 5 hours).
231
232 RRDtool works with special time stamps coming from the UNIX world.
233 This time stamp is the number of seconds that passed since January
234 1st 1970 UTC.  The time stamp value is translated into local time and
235 it will therefore look different for different time zones.
236
237 Chances are that you are not in the same part of the world as I am.
238 This means your time zone is different. In all examples where I talk
239 about time, the hours may be wrong for you. This has little effect on
240 the results of the examples, just correct the hours while reading.
241 As an example: where I will see "12:05" the UK folks will see "11:05".
242
243 We now have to fill our database with some numbers. We'll pretend to
244 have read the following numbers:
245
246  12:05  12345 KM
247  12:10  12357 KM
248  12:15  12363 KM
249  12:20  12363 KM
250  12:25  12363 KM
251  12:30  12373 KM
252  12:35  12383 KM
253  12:40  12393 KM
254  12:45  12399 KM
255  12:50  12405 KM
256  12:55  12411 KM
257  13:00  12415 KM
258  13:05  12420 KM
259  13:10  12422 KM
260  13:15  12423 KM
261
262 We fill the database as follows:
263
264  rrdtool update test.rrd 920804700:12345 920805000:12357 920805300:12363
265  rrdtool update test.rrd 920805600:12363 920805900:12363 920806200:12373
266  rrdtool update test.rrd 920806500:12383 920806800:12393 920807100:12399
267  rrdtool update test.rrd 920807400:12405 920807700:12411 920808000:12415
268  rrdtool update test.rrd 920808300:12420 920808600:12422 920808900:12423
269
270 This reads: update our test database with the following numbers
271
272  time 920804700, value 12345
273  time 920805000, value 12357
274
275 etcetera.
276
277 As you can see, it is possible to feed more than one value into the
278 database in one command. I had to stop at three for readability but
279 the real maximum per line is OS dependent.
280
281 We can now retrieve the data from our database using "rrdtool fetch":
282
283  rrdtool fetch test.rrd AVERAGE --start 920804400 --end 920809200
284
285 It should return the following output:
286
287                           speed
288
289  920804700: nan
290  920805000: 4.0000000000e-02
291  920805300: 2.0000000000e-02
292  920805600: 0.0000000000e+00
293  920805900: 0.0000000000e+00
294  920806200: 3.3333333333e-02
295  920806500: 3.3333333333e-02
296  920806800: 3.3333333333e-02
297  920807100: 2.0000000000e-02
298  920807400: 2.0000000000e-02
299  920807700: 2.0000000000e-02
300  920808000: 1.3333333333e-02
301  920808300: 1.6666666667e-02
302  920808600: 6.6666666667e-03
303  920808900: 3.3333333333e-03
304  920809200: nan
305
306 If it doesn't, something may be wrong.  Perhaps your OS will print
307 "NaN" in a different form. "NaN" stands for "Not A Number".  If your OS
308 writes "U" or "UNKN" or something similar that's okay.  If something
309 else is wrong, it will probably be due to an error you made (assuming
310 that my tutorial is correct of course :-). In that case: delete the
311 database and try again.  Sometimes things change.  This example used
312 to provide numbers like "0.04" in stead of "4.00000e-02".  Those are
313 really the same numbers, just written down differently.  Don't be
314 alarmed if a future version of rrdtool displays a slightly different
315 form of output. The examples in this document are correct for version
316 1.2.0 of RRDtool.
317
318 The meaning of the above output will become clear below.
319
320 =head2 Time to create some graphics
321
322 Try the following command:
323
324  rrdtool graph speed.png                                 \
325          --start 920804400 --end 920808000               \
326          DEF:myspeed=test.rrd:speed:AVERAGE              \
327          LINE2:myspeed#FF0000
328
329 This will create speed.png which starts at 12:00 and ends at 13:00.
330 There is a definition of a variable called myspeed, using the data from RRA
331 "speed" out of database "test.rrd". The line drawn is 2 pixels high
332 and represents the variable myspeed. The color is red (specified by
333 its rgb-representation, see below).
334
335 You'll notice that the start of the graph is not at 12:00 but at 12:05.
336 This is because we have insufficient data to tell the average before
337 that time. This will only happen when you miss some samples, this will
338 not happen a lot, hopefully.
339
340 If this has worked: congratulations! If not, check what went wrong.
341
342
343 The colors are built up from red, green and blue. For each of the
344 components, you specify how much to use in hexadecimal where 00 means
345 not included and FF means fully included.
346 The "color" white is a mixture of red, green and blue: FFFFFF
347 The "color" black is all colors off: 000000
348
349    red     #FF0000
350    green   #00FF00
351    blue    #0000FF
352    magenta #FF00FF     (mixed red with blue)
353    gray    #555555     (one third of all components)
354
355 Additionally you can add an alpha channel (transparency).  The default
356 will be "FF" which means non-transparent.
357
358 The PNG you just created can be displayed using your favorite image
359 viewer.  Web browsers will display the PNG via the URL
360 "file:///the/path/to/speed.png"
361
362 =head2 Graphics with some math
363
364 When looking at the image, you notice that the horizontal axis is labeled
365 12:10, 12:20, 12:30, 12:40 and 12:50. Sometimes a label doesn't fit (12:00
366 and 13:00 would be candidates) so they are skipped.
367
368 The vertical axis displays the range we entered. We provided
369 kilometers and when divided by 300 seconds, we get very small
370 numbers. To be exact, the first value was 12 (12'357-12'345) and divided
371 by 300 this makes 0.04, which is displayed by RRDtool as "40 m"
372 meaning "40/1'000". The "m" (milli) has nothing to do with meters,
373 kilometers or millimeters! RRDtool doesn't know about the physical
374 units of our data, it just works with dimensionless numbers.
375
376 If we had measured our distances in meters, this would have been
377 (12'357'000-12'345'000)/300 = 12'000/300 = 40.
378
379 As most people have a better feel for numbers in this range, we'll
380 correct that. We could recreate our database and store the correct
381 data, but there is a better way: we do some calculations while creating
382 the png file!
383
384    rrdtool graph speed2.png                           \
385       --start 920804400 --end 920808000               \
386       --vertical-label m/s                            \
387       DEF:myspeed=test.rrd:speed:AVERAGE              \
388       CDEF:realspeed=myspeed,1000,\*                  \
389       LINE2:realspeed#FF0000
390
391 Note: Make sure not to forget the backslash \ in front of the
392 multiplication operator * above. The backslash is needed to "escape"
393 the * as some operating systems might interpret and expand * instead
394 of passing it to the rrdtool command.
395
396 After viewing this PNG, you notice the "m" (milli) has
397 disappeared. This it what the correct result would be. Also, a label
398 has been added to the image.  Apart from the things mentioned above,
399 the PNG should look the same.
400
401 The calculations are specified in the CDEF part above and are in
402 Reverse Polish Notation ("RPN"). What we requested RRDtool to do is:
403 "take the data source myspeed and the number 1000; multiply
404 those". Don't bother with RPN yet, it will be explained later on in
405 more detail. Also, you may want to read my tutorial on CDEFs and Steve
406 Rader's tutorial on RPN. But first finish this tutorial.
407
408 Hang on! If we can multiply values with 1'000, it should also be possible
409 to display kilometers per hour from the same data!
410
411 To change a value that is measured in meters per second:
412
413  Calculate meters per hour:     value * 3'600
414  Calculate kilometers per hour: value / 1'000
415  Together this makes:           value * (3'600/1'000) or value * 3.6
416
417 In our example database we made a mistake and we need to compensate for
418 this by multiplying with 1'000. Applying that correction:
419
420  value * 3.6  * 1'000 == value * 3'600
421
422 Now let's create this PNG, and add some more magic ...
423
424  rrdtool graph speed3.png                             \
425       --start 920804400 --end 920808000               \
426       --vertical-label km/h                           \
427       DEF:myspeed=test.rrd:speed:AVERAGE              \
428       "CDEF:kmh=myspeed,3600,*"                       \
429       CDEF:fast=kmh,100,GT,kmh,0,IF                   \
430       CDEF:good=kmh,100,GT,0,kmh,IF                   \
431       HRULE:100#0000FF:"Maximum allowed"              \
432       AREA:good#00FF00:"Good speed"                   \
433       AREA:fast#FF0000:"Too fast"
434
435 Note: here we use another means to escape the * operator by enclosing
436 the whole string in double quotes.
437
438 This graph looks much better. Speed is shown in KM/H and there is even
439 an extra line with the maximum allowed speed (on the road I travel
440 on). I also changed the colors used to display speed and changed it
441 from a line into an area.
442
443 The calculations are more complex now. For speed measurements within
444 the speed limit they are:
445
446    Check if kmh is greater than 100    ( kmh,100 ) GT
447    If so, return 0, else kmh           ((( kmh,100 ) GT ), 0, kmh) IF
448
449 For values above the speed limit:
450
451    Check if kmh is greater than 100    ( kmh,100 ) GT
452    If so, return kmh, else return 0    ((( kmh,100) GT ), kmh, 0) IF
453
454 =head2 Graphics Magic
455
456 I like to believe there are virtually no limits to how RRDtool graph
457 can manipulate data. I will not explain how it works, but look at the
458 following PNG:
459
460    rrdtool graph speed4.png                           \
461       --start 920804400 --end 920808000               \
462       --vertical-label km/h                           \
463       DEF:myspeed=test.rrd:speed:AVERAGE              \
464       "CDEF:kmh=myspeed,3600,*"                       \
465       CDEF:fast=kmh,100,GT,100,0,IF                   \
466       CDEF:over=kmh,100,GT,kmh,100,-,0,IF             \
467       CDEF:good=kmh,100,GT,0,kmh,IF                   \
468       HRULE:100#0000FF:"Maximum allowed"              \
469       AREA:good#00FF00:"Good speed"                   \
470       AREA:fast#550000:"Too fast"                     \
471       STACK:over#FF0000:"Over speed"
472
473 Let's create a quick and dirty HTML page to view the three PNGs:
474
475    <HTML><HEAD><TITLE>Speed</TITLE></HEAD><BODY>
476    <IMG src="speed2.png" alt="Speed in meters per second">
477    <BR>
478    <IMG src="speed3.png" alt="Speed in kilometers per hour">
479    <BR>
480    <IMG src="speed4.png" alt="Traveled too fast?">
481    </BODY></HTML>
482
483 Name the file "speed.html" or similar, and look at it in your web browser.
484
485 Now, all you have to do is measure the values regularly and update the
486 database.  When you want to view the data, recreate the PNGs and make
487 sure to refresh them in your browser. (Note: just clicking reload may
488 not be enough, especially when proxies are involved.  Try shift-reload
489 or ctrl-F5).
490
491 =head2 Updates in Reality
492
493 We've already used the C<update> command: it took one or more
494 parameters in the form of "<time>:<value>". You'll be glad to know
495 that you can specify the current time by filling in a "N" as the time.
496 Or you could use the "time" function in Perl (the shortest example in
497 this tutorial):
498
499    perl -e 'print time, "\n" '
500
501 How to run a program on regular intervals is OS specific. But here is
502 an example in pseudo code:
503
504    - Get the value and put it in variable "$speed"
505    - rrdtool update speed.rrd N:$speed
506
507 (do not try this with our test database, we'll use it in further examples)
508
509 This is all. Run the above script every five minutes. When you need to know
510 what the graphs look like, run the examples above. You could put them
511 in a script as well. After running that script, view the page
512 index.html we created above.
513
514 =head2 Some words on SNMP
515
516 I can imagine very few people that will be able to get real data from
517 their car every five minutes. All other people will have to settle for
518 some other kind of counter. You could measure the number of pages
519 printed by a printer, for example, the cups of coffee made by the
520 coffee machine, a device that counts the electricity used,
521 whatever. Any incrementing counter can be monitored and graphed using
522 the stuff you learned so far. Later on we will also be able to monitor
523 other types of values like temperature.
524
525 Most (?) people interested in RRDtool will use the counter that keeps track
526 of octets (bytes) transfered by a network device. So let's do just
527 that next. We will start with a description of how to collect data.
528
529 Some people will make a remark that there are tools which can do this data
530 collection for you. They are right! However, I feel it is important that
531 you understand they are not necessary. When you have to determine why
532 things went wrong you need to know how they work.
533
534 One tool used in the example has been talked about very briefly in the
535 beginning of this document, it is called SNMP. It is a way of talking
536 to networked equipment. The tool I use below is called "snmpget" and
537 this is how it works:
538
539    snmpget device password OID
540
541 or
542
543    snmpget -v[version] -c[password] device OID
544
545 For device you substitute the name, or the IP address, of your device.
546 For password you use the "community read string" as it is called in the
547 SNMP world.  For some devices the default of "public" might work, however
548 this can be disabled, altered or protected for privacy and security
549 reasons.  Read the documentation that comes with your device or program.
550
551 Then there is this parameter, called OID, which means "object identifier".
552
553 When you start to learn about SNMP it looks very confusing. It isn't
554 all that difficult when you look at the Management Information Base
555 ("MIB").  It is an upside-down tree that describes data, with a single node
556 as the root and from there a number of branches.  These branches end
557 up in another node, they branch out, etc.  All the branches have a name
558 and they form the path that we follow all the way down.  The branches
559 that we follow are named: iso, org, dod, internet, mgmt and mib-2.
560 These names can also be written down as numbers and are 1 3 6 1 2 1.
561
562    iso.org.dod.internet.mgmt.mib-2 (1.3.6.1.2.1)
563
564 There is a lot of confusion about the leading dot that some programs
565 use.  There is *no* leading dot in an OID.  However, some programs
566 can use the above part of OIDs as a default.  To indicate the difference
567 between abbreviated OIDs and full OIDs they need a leading dot when
568 you specify the complete OID.  Often those programs will leave out
569 the default portion when returning the data to you.  To make things
570 worse, they have several default prefixes ...
571
572 Ok, lets continue to the start of our OID: we had 1.3.6.1.2.1
573 From there, we are especially interested in the branch "interfaces"
574 which has number 2 (e.g., 1.3.6.1.2.1.2 or 1.3.6.1.2.1.interfaces).
575
576 First, we have to get some SNMP program. First look if there is a
577 pre-compiled package available for your OS. This is the preferred way.
578 If not, you will have to get the sources yourself and compile those.
579 The Internet is full of sources, programs etc. Find information using
580 a search engine or whatever you prefer.
581
582 Assume you got the program. First try to collect some data that is
583 available on most systems. Remember: there is a short name for the
584 part of the tree that interests us most in the world we live in!
585
586 I will give an example which can be used on Fedora Core 3.  If it
587 doesn't work for you, work your way through the manual of snmp and
588 adapt the example to make it work.
589
590    snmpget -v2c -c public myrouter system.sysDescr.0
591
592 The device should answer with a description of itself, perhaps an
593 empty one. Until you got a valid answer from a device, perhaps using a
594 different "password", or a different device, there is no point in
595 continuing.
596
597    snmpget -v2c -c public myrouter interfaces.ifNumber.0
598
599 Hopefully you get a number as a result, the number of interfaces.
600 If so, you can carry on and try a different program called "snmpwalk".
601
602    snmpwalk -v2c -c public myrouter interfaces.ifTable.ifEntry.ifDescr
603
604 If it returns with a list of interfaces, you're almost there.
605 Here's an example:
606    [user@host /home/alex]$ snmpwalk -v2c -c public cisco 2.2.1.2
607
608    interfaces.ifTable.ifEntry.ifDescr.1 = "BRI0: B-Channel 1"
609    interfaces.ifTable.ifEntry.ifDescr.2 = "BRI0: B-Channel 2"
610    interfaces.ifTable.ifEntry.ifDescr.3 = "BRI0" Hex: 42 52 49 30
611    interfaces.ifTable.ifEntry.ifDescr.4 = "Ethernet0"
612    interfaces.ifTable.ifEntry.ifDescr.5 = "Loopback0"
613
614 On this cisco equipment, I would like to monitor the "Ethernet0"
615 interface and from the above output I see that it is number four. I try:
616
617    [user@host /home/alex]$ snmpget -v2c -c public cisco 2.2.1.10.4 2.2.1.16.4
618
619    interfaces.ifTable.ifEntry.ifInOctets.4 = 2290729126
620    interfaces.ifTable.ifEntry.ifOutOctets.4 = 1256486519
621
622 So now I have two OIDs to monitor and they are (in full, this time):
623
624    1.3.6.1.2.1.2.2.1.10
625
626 and
627
628    1.3.6.1.2.1.2.2.1.16
629
630 both with an interface number of 4.
631
632 Don't get fooled, this wasn't my first try. It took some time for me too
633 to understand what all these numbers mean. It does help a lot when they
634 get translated into descriptive text... At least, when people are talking
635 about MIBs and OIDs you know what it's all about.
636 Do not forget the interface number (0 if it is not interface dependent)
637 and try snmpwalk if you don't get an answer from snmpget.
638
639 If you understand the above section and get numbers from your device, continue
640 on with this tutorial. If not, then go back and re-read this part.
641
642 =head2 A Real World Example
643
644 Let the fun begin. First, create a new database. It contains data from
645 two counters, called input and output. The data is put into archives
646 that average it. They take 1, 6, 24 or 288 samples at a time.
647 They also go into archives that keep the maximum numbers. This will be
648 explained later on. The time in-between samples is 300 seconds, a good
649 starting point, which is the same as five minutes.
650
651  1 sample "averaged" stays 1 period of 5 minutes
652  6 samples averaged become one average on 30 minutes
653  24 samples averaged become one average on 2 hours
654  288 samples averaged become one average on 1 day
655
656 Lets try to be compatible with MRTG which stores about the following
657 amount of data:
658
659  600 5-minute samples:    2   days and 2 hours
660  600 30-minute samples:  12.5 days
661  600 2-hour samples:     50   days
662  732 1-day samples:     732   days
663
664 These ranges are appended, so the total amount of data stored in the
665 database is approximately 797 days. RRDtool stores the data
666 differently, it doesn't start the "weekly" archive where the "daily"
667 archive stopped. For both archives the most recent data will be near
668 "now" and therefore we will need to keep more data than MRTG does!
669
670 We will need:
671
672  600 samples of 5 minutes  (2 days and 2 hours)
673  700 samples of 30 minutes (2 days and 2 hours, plus 12.5 days)
674  775 samples of 2 hours    (above + 50 days)
675  797 samples of 1 day      (above + 732 days, rounded up to 797)
676
677    rrdtool create myrouter.rrd         \
678             DS:input:COUNTER:600:U:U   \
679             DS:output:COUNTER:600:U:U  \
680             RRA:AVERAGE:0.5:1:600      \
681             RRA:AVERAGE:0.5:6:700      \
682             RRA:AVERAGE:0.5:24:775     \
683             RRA:AVERAGE:0.5:288:797    \
684             RRA:MAX:0.5:1:600          \
685             RRA:MAX:0.5:6:700          \
686             RRA:MAX:0.5:24:775         \
687             RRA:MAX:0.5:288:797
688
689 Next thing to do is to collect data and store it. Here is an example.
690 It is written partially in pseudo code,  you will have to find out what
691 to do exactly on your OS to make it work.
692
693    while not the end of the universe
694    do
695       get result of
696          snmpget router community 2.2.1.10.4
697       into variable $in
698       get result of
699          snmpget router community 2.2.1.16.4
700       into variable $out
701
702       rrdtool update myrouter.rrd N:$in:$out
703
704       wait for 5 minutes
705    done
706
707 Then, after collecting data for a day, try to create an image using:
708
709    rrdtool graph myrouter-day.png --start -86400 \
710             DEF:inoctets=myrouter.rrd:input:AVERAGE \
711             DEF:outoctets=myrouter.rrd:output:AVERAGE \
712             AREA:inoctets#00FF00:"In traffic" \
713             LINE1:outoctets#0000FF:"Out traffic"
714
715 This should produce a picture with one day worth of traffic.
716 One day is 24 hours of 60 minutes of 60 seconds: 24*60*60=86'400, we
717 start at now minus 86'400 seconds. We define (with DEFs) inoctets and
718 outoctets as the average values from the database myrouter.rrd and draw
719 an area for the "in" traffic and a line for the "out" traffic.
720
721 View the image and keep logging data for a few more days.
722 If you like, you could try the examples from the test database and
723 see if you can get various options and calculations to work.
724
725 Suggestion: Display in bytes per second and in bits per second. Make
726 the Ethernet graphics go red if they are over four megabits per
727 second.
728
729 =head2 Consolidation Functions
730
731 A few paragraphs back I mentioned the possibility of keeping
732 the maximum values instead of the average values. Let's go
733 into this a bit more.
734
735 Recall all the stuff about the speed of the car. Suppose we drove at 144
736 KM/H during 5 minutes and then were stopped by the police for 25 minutes.
737 At the end of the lecture we would take our laptop and create and view the
738 image taken from the database. If we look at the second RRA we did
739 create, we would have the average from 6 samples. The samples measured
740 would be 144+0+0+0+0+0=144, divided by 30 minutes, corrected for the
741 error by 1000, translated into KM/H, with a result of 24 KM/H.
742 I would still get a ticket but not for speeding anymore :)
743
744 Obviously, in this case we shouldn't look at the averages. In some
745 cases they are handy. If you want to know how many KM you had traveled,
746 the averaged picture would be the right one to look at. On the other hand, for
747 the speed that we traveled at, the maximum numbers seen is much more
748 interesting. Later we will see more types.
749
750 It is the same for data. If you want to know the amount, look at the
751 averages. If you want to know the rate, look at the maximum.
752 Over time, they will grow apart more and more. In the last database
753 we have created, there are two archives that keep data per day. The
754 archive that keeps averages will show low numbers, the archive that
755 shows maxima will have higher numbers.
756
757 For my car this would translate in averages per day of 96/24=4 KM/H
758 (as I travel about 94 kilometers on a day) during working days, and
759 maxima of 120 KM/H (my top speed that I reach every day).
760
761 Big difference. Do not look at the second graph to estimate the
762 distances that I travel and do not look at the first graph to
763 estimate my speed. This will work if the samples are close together,
764 as they are in five minutes, but not if you average.
765
766 On some days, I go for a long ride. If I go across Europe and travel
767 for 12 hours, the first graph will rise to about 60 KM/H. The second
768 one will show 180 KM/H. This means that I traveled a distance of 60
769 KM/H times 24 H = 1440 KM. I did this with a higher speed and a
770 maximum around 180 KM/H. However, it probably doesn't mean that I
771 traveled for 8 hours at a constant speed of 180 KM/H!
772
773 This is a real example: go with the flow through Germany (fast!) and stop
774 a few times for gas and coffee. Drive slowly through Austria and the
775 Netherlands. Be careful in the mountains and villages. If you would
776 look at the graphs created from the five-minute averages you would
777 get a totally different picture. You would see the same values on the
778 average and maximum graphs (provided I measured every 300 seconds).
779 You would be able to see when I stopped, when I was in top gear, when
780 I drove over fast highways etc. The granularity of the data is much
781 higher, so you can see more. However, this takes 12 samples per hour,
782 or 288 values per day, so it would be a lot of data over a longer
783 period of time. Therefore we average it, eventually to one value per
784 day. From this one value, we cannot see much detail, of course.
785
786 Make sure you understand the last few paragraphs. There is no value
787 in only a line and a few axis, you need to know what they mean and
788 interpret the data in ana appropriate way. This is true for all data.
789
790 The biggest mistake you can make is to use the collected data for
791 something that it is not suitable for. You would be better off if
792 you didn't have the graph at all.
793
794
795 =head2 Let's review what you now should know
796
797 You know how to create a database and can put data in it. You can get
798 the numbers out again by creating an image, do math on the data from
799 the database and view the resulte instead of the raw data.  You know
800 about the difference between averages and maxima, and when to use
801 which (or at least you should have an idea).
802
803 RRDtool can do more than what we have learned up to now. Before you
804 continue with the rest of this doc, I recommend that you reread from
805 the start and try some modifications on the examples. Make sure you
806 fully understand everything. It will be worth the effort and helps
807 you not only with the rest of this tutorial, but also in your day to day
808 monitoring long after you read this introduction.
809
810 =head2 Data Source Types
811
812 All right, you feel like continuing. Welcome back and get ready
813 for an increased speed in the examples and explanations.
814
815 You know that in order to view a counter over time, you have to
816 take two numbers and divide the difference of them between the
817 time lapsed.  This makes sense for the examples I gave you but there
818 are other possibilities.  For instance, I'm able to retrieve the
819 temperature from my router in three places namely the inlet, the
820 so called hot-spot and the exhaust.  These values are not counters.
821 If I take the difference of the two samples and divide that by
822 300 seconds I would be asking for the temperature change per second.
823 Hopefully this is zero! If not, the computer room is probably on fire :)
824
825 So, what can we do?  We can tell RRDtool to store the values we measure
826 directly as they are (this is not entirely true but close enough). The
827 graphs we make will look much better, they will show a rather constant
828 value. I know when the router is busy (it
829 works -> it uses more electricity -> it generates more heat -> the
830 temperature rises). I know when the doors are left open (the room is
831 air conditioned) -> the warm air from the rest of the building flows into the
832 computer room -> the inlet temperature rises). Etc. The data type we
833 use when creating the database before was counter, we now have a
834 different data type and thus a different name for it. It is called
835 GAUGE. There are more such data types:
836
837  - COUNTER   we already know this one
838  - GAUGE     we just learned this one
839  - DERIVE
840  - ABSOLUTE
841
842 The two additional types are DERIVE and ABSOLUTE. Absolute can be used like
843 counter with one difference: RRDtool assumes the counter is reset when
844 it's read. That is: its delta is known without calculation by RRDtool
845 whereas RRDtool needs to calculate it for the counter type.
846 Example: our first example (12'345, 12'357, 12'363, 12'363) would read:
847 unknown, 12, 6, 0. The rest of the calculations stay the same.
848 The other one, derive, is like counter. Unlike counter, it can also
849 decrease so it can have a negative delta. Again, the rest of the
850 calculations stay the same.
851
852 Let's try them all:
853
854    rrdtool create all.rrd --start 978300900 \
855             DS:a:COUNTER:600:U:U \
856             DS:b:GAUGE:600:U:U \
857             DS:c:DERIVE:600:U:U \
858             DS:d:ABSOLUTE:600:U:U \
859             RRA:AVERAGE:0.5:1:10
860    rrdtool update all.rrd \
861             978301200:300:1:600:300    \
862             978301500:600:3:1200:600   \
863             978301800:900:5:1800:900   \
864             978302100:1200:3:2400:1200 \
865             978302400:1500:1:2400:1500 \
866             978302700:1800:2:1800:1800 \
867             978303000:2100:4:0:2100    \
868             978303300:2400:6:600:2400  \
869             978303600:2700:4:600:2700  \
870             978303900:3000:2:1200:3000
871    rrdtool graph all1.png -s 978300600 -e 978304200 -h 400 \
872             DEF:linea=all.rrd:a:AVERAGE LINE3:linea#FF0000:"Line A" \
873             DEF:lineb=all.rrd:b:AVERAGE LINE3:lineb#00FF00:"Line B" \
874             DEF:linec=all.rrd:c:AVERAGE LINE3:linec#0000FF:"Line C" \
875             DEF:lined=all.rrd:d:AVERAGE LINE3:lined#000000:"Line D"
876
877 =head2 RRDtool under the Microscope
878
879 =over 2
880
881 =item *
882
883 Line A is a COUNTER type, so it should continuously increment and RRDtool
884 must calculate the differences. Also, RRDtool needs to divide the
885 difference by the amount of time lapsed. This should end up as a
886 straight line at 1 (the deltas are 300, the time is 300).
887
888 =item *
889
890 Line B is of type GAUGE. These are "real" values so they should match
891 what we put in: a sort of a wave.
892
893 =item *
894
895 Line C is of type DERIVE. It should be a counter that can decrease. It does
896 so between 2'400 and 0, with 1'800 in-between.
897
898 =item *
899
900 Line D is of type ABSOLUTE. This is like counter but it works on
901 values without calculating the difference. The numbers are the same
902 and as you can see (hopefully) this has a different result.
903
904 =back
905
906 This translates in the following values, starting at 23:10 and ending
907 at 00:10 the next day (where "u" means unknown/unplotted):
908
909  - Line A:  u  u  1  1  1  1  1  1  1  1  1  u
910  - Line B:  u  1  3  5  3  1  2  4  6  4  2  u
911  - Line C:  u  u  2  2  2  0 -2 -6  2  0  2  u
912  - Line D:  u  1  2  3  4  5  6  7  8  9 10  u
913
914 If your PNG shows all this, you know you have entered the data correctly,
915 the RRDtool executable is working properly, your viewer doesn't fool you,
916 and you successfully entered the year 2000 :)
917
918 You could try the same example four times, each time with only one of
919 the lines.
920
921 Let's go over the data again:
922
923 =over 2
924
925 =item *
926
927 Line A: 300,600,900 and so on. The counter delta is a constant 300 and
928 so is the time delta. A number divided by itself is always 1 (except
929 when dividing by zero which is undefined/illegal).
930
931 Why is it that the first point is unknown? We do know what we put into
932 the database, right? True, But we didn't have a value to calculate the delta
933 from, so we don't know where we started. It would be wrong to assume we
934 started at zero so we don't!
935
936 =item *
937
938 Line B: There is nothing to calculate. The numbers are as they are.
939
940 =item *
941
942 Line C: Again, the start-out value is unknown. The same story is holds
943 as for line A. In this case the deltas are not constant, therefore the line
944 is not either. If we would put the same numbers in the database as we did for
945 line A, we would have gotten the same line. Unlike type counter,
946 this type can decrease and I hope to show you later on why
947 this makes a difference.
948
949 =item *
950
951 Line D: Here the device calculates the deltas. Therefore we DO know the
952 first delta and it is plotted. We had the same input as with line A, but
953 the meaning of this input is different and thus the line is different.
954 In this case the deltas increase each time with 300. The time delta
955 stays at a constant 300 and therefore the division of the two gives
956 increasing values.
957
958 =back
959
960 =head2 Counter Wraps
961
962 There are a few more basics to show. Some important options are still to
963 be covered and we haven't look at counter wraps yet. First the counter wrap:
964 In our car we notice that the counter shows 999'987. We travel 20 KM and
965 the counter should go to 1'000'007. Unfortunately, there are only six digits
966 on our counter so it really shows 000'007. If we would plot that on a type
967 DERIVE, it would mean that the counter was set back 999'980 KM. It wasn't,
968 and there has to be some protection for this. This protection is only
969 available for type COUNTER which should be used for this kind of counter
970 anyways. How does it work? Type counter should never decrease and
971 therefore RRDtool must assume it wrapped if it does decrease!
972 If the delta is negative, this can be compensated for by adding the
973 maximum value of the counter + 1. For our car this would be:
974
975  Delta = 7 - 999'987 = -999'980    (instead of 1'000'007-999'987=20)
976
977  Real delta = -999'980 + 999'999 + 1 = 20
978
979 At the time of writing this document, RRDtool knows of counters that
980 are either 32 bits or 64 bits of size. These counters can handle the
981 following different values:
982
983  - 32 bits: 0 ..           4'294'967'295
984  - 64 bits: 0 .. 18'446'744'073'709'551'615
985
986 If these numbers look strange to you, you can view them in
987 their hexadecimal form:
988
989  - 32 bits: 0 ..         FFFFFFFF
990  - 64 bits: 0 .. FFFFFFFFFFFFFFFF
991
992 RRDtool handles both counters the same. If an overflow occurs and
993 the delta would be negative, RRDtool first adds the maximum of a small
994 counter + 1 to the delta. If the delta is still negative, it had to be
995 the large counter that wrapped. Add the maximum possible value of the
996 large counter + 1 and subtract the erroneously added small value.
997
998 There is a risk in this: suppose the large counter wrapped while adding
999 a huge delta, it could happen, theoretically, that adding the smaller value
1000 would make the delta positive. In this unlikely case the results would
1001 not be correct. The increase should be nearly as high as the maximum
1002 counter value for that to happen, so chances are you would have several
1003 other problems as well and this particular problem would not even be
1004 worth thinking about. Even though, I did include an example, so you
1005 can judge for yourself.
1006
1007 The next section gives you some numerical examples for counter-wraps.
1008 Try to do the calculations yourself or just believe me if your calculator
1009 can't handle the numbers :)
1010
1011 Correction numbers:
1012
1013  - 32 bits: (4'294'967'295 + 1) =                                4'294'967'296
1014  - 64 bits: (18'446'744'073'709'551'615 + 1)
1015                                     - correction1 = 18'446'744'069'414'584'320
1016
1017  Before:        4'294'967'200
1018  Increase:                100
1019  Should become: 4'294'967'300
1020  But really is:             4
1021  Delta:        -4'294'967'196
1022  Correction1:  -4'294'967'196 + 4'294'967'296 = 100
1023
1024  Before:        18'446'744'073'709'551'000
1025  Increase:                             800
1026  Should become: 18'446'744'073'709'551'800
1027  But really is:                        184
1028  Delta:        -18'446'744'073'709'550'816
1029  Correction1:  -18'446'744'073'709'550'816
1030                                 + 4'294'967'296 = -18'446'744'069'414'583'520
1031  Correction2:  -18'446'744'069'414'583'520
1032                    + 18'446'744'069'414'584'320 = 800
1033
1034  Before:        18'446'744'073'709'551'615 ( maximum value )
1035  Increase:      18'446'744'069'414'584'320 ( absurd increase, minimum for
1036  Should become: 36'893'488'143'124'135'935             this example to work )
1037  But really is: 18'446'744'069'414'584'319
1038  Delta:                     -4'294'967'296
1039  Correction1:  -4'294'967'296 + 4'294'967'296 = 0
1040  (not negative -> no correction2)
1041
1042  Before:        18'446'744'073'709'551'615 ( maximum value )
1043  Increase:      18'446'744'069'414'584'319 ( one less increase )
1044  Should become: 36'893'488'143'124'135'934
1045  But really is: 18'446'744'069'414'584'318
1046  Delta:                     -4'294'967'297
1047  Correction1:  -4'294'967'297 + 4'294'967'296 = -1
1048  Correction2:  -1 + 18'446'744'069'414'584'320 = 18'446'744'069'414'584'319
1049
1050 As you can see from the last two examples, you need strange numbers
1051 for RRDtool to fail (provided it's bug free of course), so this should
1052 not happen. However, SNMP or whatever method you choose to collect the
1053 data, might also report wrong numbers occasionally.  We can't prevent all
1054 errors, but there are some things we can do. The RRDtool "create" command
1055 takes two special parameters for this. They define
1056 the minimum and maximum allowed values. Until now, we used "U", meaning
1057 "unknown". If you provide values for one or both of them and if RRDtool
1058 receives data points that are outside these limits, it will ignore those
1059 values. For a thermometer in degrees Celsius, the absolute minimum is
1060 just under -273. For my router, I can assume this minimum is much higher
1061 so I would set it to 10, where as the maximum temperature I would
1062 set to 80. Any higher and the device would be out of order.
1063
1064 For the speed of my car, I would never expect negative numbers and
1065 also I would not expect a speed  higher than 230. Anything else,
1066 and there must have been an error. Remember: the opposite is not true,
1067 if the numbers pass this check, it doesn't mean that they are
1068 correct. Always judge the graph with a healthy dose of suspicion if it
1069 seems weird to you.
1070
1071 =head2 Data Resampling
1072
1073 One important feature of RRDtool has not been explained yet: it is
1074 virtually impossible to collect data and feed it into RRDtool on exact
1075 intervals. RRDtool therefore interpolates the data, so they are stored
1076 on exact intervals. If you do not know what this means or how it
1077 works, then here's the help you seek:
1078
1079 Suppose a counter increases by exactly one for every second. You want
1080 to measure it in 300 seconds intervals. You should retrieve values
1081 that are exactly 300 apart. However, due to various circumstances you
1082 are a few seconds late and the interval is 303. The delta will also be
1083 303 in that case. Obviously, RRDtool should not put 303 in the database
1084 and make you believe that the counter increased by 303 in 300 seconds.
1085 This is where RRDtool interpolates: it alters the 303 value as if it
1086 would have been stored earlier and it will be 300 in 300 seconds.
1087 Next time you are at exactly the right time. This means that the current
1088 interval is 297 seconds and also the counter increased by 297. Again,
1089 RRDtool interpolates and stores 300 as it should be.
1090
1091       in the RRD                 in reality
1092
1093  time+000:   0 delta="U"   time+000:    0 delta="U"
1094  time+300: 300 delta=300   time+300:  300 delta=300
1095  time+600: 600 delta=300   time+603:  603 delta=303
1096  time+900: 900 delta=300   time+900:  900 delta=297
1097
1098 Let's create two identical databases. I've chosen the time range 920'805'000
1099 to 920'805'900 as this goes very well with the example numbers.
1100
1101    rrdtool create seconds1.rrd   \
1102       --start 920804700          \
1103       DS:seconds:COUNTER:600:U:U \
1104       RRA:AVERAGE:0.5:1:24
1105
1106 Make a copy
1107
1108    for Unix: cp seconds1.rrd seconds2.rrd
1109    for Dos:  copy seconds1.rrd seconds2.rrd
1110    for vms:  how would I know :)
1111
1112 Put in some data
1113
1114    rrdtool update seconds1.rrd \
1115       920805000:000 920805300:300 920805600:600 920805900:900
1116    rrdtool update seconds2.rrd \
1117       920805000:000 920805300:300 920805603:603 920805900:900
1118
1119 Create output
1120
1121    rrdtool graph seconds1.png                       \
1122       --start 920804700 --end 920806200             \
1123       --height 200                                  \
1124       --upper-limit 1.05 --lower-limit 0.95 --rigid \
1125       DEF:seconds=seconds1.rrd:seconds:AVERAGE      \
1126       CDEF:unknown=seconds,UN                       \
1127       LINE2:seconds#0000FF                          \
1128       AREA:unknown#FF0000
1129    rrdtool graph seconds2.png                       \
1130       --start 920804700 --end 920806200             \
1131       --height 200                                  \
1132       --upper-limit 1.05 --lower-limit 0.95 --rigid \
1133       DEF:seconds=seconds2.rrd:seconds:AVERAGE      \
1134       CDEF:unknown=seconds,UN                       \
1135       LINE2:seconds#0000FF                          \
1136       AREA:unknown#FF0000
1137
1138 View both images together (add them to your index.html file)
1139 and compare. Both graphs should show the same, despite the
1140 input being different.
1141
1142 =head1 WRAPUP
1143
1144 It's time now to wrap up this tutorial. We covered all the basics for
1145 you to be able to work with RRDtool and to read the additional
1146 documentation available. There is plenty more to discover about
1147 RRDtool and you will find more and more uses for this package. You can
1148 easly create graphs using just the examples provided and using only
1149 RRDtool. You can also use one of the front ends to RRDtool that are
1150 available.
1151
1152 =head1 MAILINGLIST
1153
1154 Remember to subscribe to the RRDtool mailing list. Even if you are not
1155 answering to mails that come by, it helps both you and the rest of the
1156 users. A lot of the stuff that I know about MRTG (and therefore about
1157 RRDtool) I've learned while just reading the list without posting to
1158 it. I did not need to ask the basic questions as they are answered in
1159 the FAQ (read it!) and in various mails by other users. With
1160 thousands of users all over the world, there will always be people who
1161 ask questions that you can answer because you read this and other
1162 documentation and they didn't.
1163
1164 =head1 SEE ALSO
1165
1166 The RRDtool manpages
1167
1168 =head1 AUTHOR
1169
1170 I hope you enjoyed the examples and their descriptions. If you do, help
1171 other people by pointing them to this document when they are asking
1172 basic questions. They will not only get their answers, but at the same
1173 time learn a whole lot more.
1174
1175 Alex van den Bogaerdt
1176 E<lt>alex@ergens.op.het.netE<gt>
1177