Code

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