Code

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