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