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