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