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