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