Code

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