Code

Imported upstream version 1.2.26
[pkg-rrdtool.git] / doc / cdeftutorial.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>cdeftutorial</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">
12 <p><a name="__index__"></a></p>
13 <!-- INDEX BEGIN -->
14 <!--
16 <ul>
18         <li><a href="#name">NAME</a></li>
19         <li><a href="#description">DESCRIPTION</a></li>
20         <ul>
22                 <li><a href="#why_this_tutorial">Why this tutorial?</a></li>
23                 <li><a href="#more_reading">More reading</a></li>
24         </ul>
26         <li><a href="#what_are_cdefs">What are CDEFs?</a></li>
27         <li><a href="#syntax">Syntax</a></li>
28         <li><a href="#rpnexpressions">RPN-expressions</a></li>
29         <li><a href="#converting_your_wishes_to_rpn">Converting your wishes to RPN</a></li>
30         <li><a href="#some_special_numbers">Some special numbers</a></li>
31         <ul>
33                 <li><a href="#the_unknown_value">The unknown value</a></li>
34                 <li><a href="#working_with_unknown_data_in_your_database">Working with unknown data in your database</a></li>
35                 <li><a href="#infinity">Infinity</a></li>
36                 <li><a href="#working_with_unknown_data_and_infinity">Working with unknown data and infinity</a></li>
37         </ul>
39         <li><a href="#some_examples">Some examples</a></li>
40         <ul>
42                 <li><a href="#example__using_a_recently_created_rrd">Example: using a recently created RRD</a></li>
43                 <li><a href="#example__better_handling_of_unknown_data__by_using_time">Example: better handling of unknown data, by using time</a></li>
44                 <li><a href="#example__pretending_weird_data_isn_t_there">Example: Pretending weird data isn't there</a></li>
45                 <li><a href="#example__working_on_a_certain_time_span">Example: working on a certain time span</a></li>
46                 <li><a href="#example__you_suspect_to_have_problems_and_want_to_see_unknown_data_">Example: You suspect to have problems and want to see unknown data.</a></li>
47                 <li><a href="#same_example_useful_with_stacked_data_">Same example useful with STACKed data:</a></li>
48         </ul>
50         <li><a href="#the_examples_from_the_rrd_graph_manual_page">The examples from the RRD graph manual page</a></li>
51         <ul>
53                 <li><a href="#degrees_celsius_vs__degrees_fahrenheit">Degrees Celsius vs. Degrees Fahrenheit</a></li>
54                 <li><a href="#infinity_demo">Infinity demo</a></li>
55                 <li><a href="#filtering_data">Filtering data</a></li>
56         </ul>
58         <li><a href="#out_of_ideas_for_now">Out of ideas for now</a></li>
59         <li><a href="#see_also">SEE ALSO</a></li>
60         <li><a href="#author">AUTHOR</a></li>
61 </ul>
62 -->
63 <!-- INDEX END -->
65 <p>
66 </p>
67 <h1><a name="name">NAME</a></h1>
68 <p>cdeftutorial - Alex van den Bogaerdt's CDEF tutorial</p>
69 <p>
70 </p>
71 <hr />
72 <h1><a name="description">DESCRIPTION</a></h1>
73 <p>Intention of this document: to provide some examples of the commonly
74 used parts of RRDtool's CDEF language.</p>
75 <p>If you think some important feature is not explained properly, and if
76 adding it to this document would benefit most users, please do ask me
77 to add it.  I will then try to provide an answer in the next release
78 of this tutorial.  No feedback equals no changes! Additions to
79 this document are also welcome.  -- Alex van den Bogaerdt
80 &lt;<a href="mailto:alex@ergens.op.het.net">alex@ergens.op.het.net</a>&gt;</p>
81 <p>
82 </p>
83 <h2><a name="why_this_tutorial">Why this tutorial?</a></h2>
84 <p>One of the powerful parts of RRDtool is its ability to do all sorts
85 of calculations on the data retrieved from its databases. However,
86 RRDtool's many options and syntax make it difficult for the average
87 user to understand. The manuals are good at explaining what these
88 options do; however they do not (and should not) explain in detail
89 why they are useful. As with my RRDtool tutorial: if you want a
90 simple document in simple language you should read this tutorial.
91 If you are happy with the official documentation, you may find this
92 document too simple or even boring. If you do choose to read this
93 tutorial, I also expect you to have read and fully understand my
94 other tutorial.</p>
95 <p>
96 </p>
97 <h2><a name="more_reading">More reading</a></h2>
98 <p>If you have difficulties with the way I try to explain it please read
99 Steve Rader's <a href="././rpntutorial.html">the rpntutorial manpage</a>. It may help you understand how this all works.</p>
100 <p>
101 </p>
102 <hr />
103 <h1><a name="what_are_cdefs">What are CDEFs?</a></h1>
104 <p>When retrieving data from an RRD, you are using a ``DEF'' to work with
105 that data. Think of it as a variable that changes over time (where
106 time is the x-axis). The value of this variable is what is found in
107 the database at that particular time and you can't do any
108 modifications on it. This is what CDEFs are for: they takes values
109 from DEFs and perform calculations on them.</p>
110 <p>
111 </p>
112 <hr />
113 <h1><a name="syntax">Syntax</a></h1>
114 <pre>
115    DEF:var_name_1=some.rrd:ds_name:CF
116    CDEF:var_name_2=RPN_expression</pre>
117 <p>You first define ``var_name_1'' to be data collected from data source
118 ``ds_name'' found in RRD ``some.rrd'' with consolidation function ``CF''.</p>
119 <p>Assume the ifInOctets SNMP counter is saved in mrtg.rrd as the DS ``in''.
120 Then the following DEF defines a variable for the average of that
121 data source:</p>
122 <pre>
123    DEF:inbytes=mrtg.rrd:in:AVERAGE</pre>
124 <p>Say you want to display bits per second (instead of bytes per second
125 as stored in the database.)  You have to define a calculation
126 (hence ``CDEF'') on variable ``inbytes'' and use that variable (inbits)
127 instead of the original:</p>
128 <pre>
129    CDEF:inbits=inbytes,8,*</pre>
130 <p>This tells RRDtool to multiply inbytes by eight to get inbits. I'll
131 explain later how this works. In the graphing or printing functions,
132 you can now use inbits where you would use inbytes otherwise.</p>
133 <p>Note that the variable name used in the CDEF (inbits) must not be the
134 same as the variable named in the DEF (inbytes)!</p>
135 <p>
136 </p>
137 <hr />
138 <h1><a name="rpnexpressions">RPN-expressions</a></h1>
139 <p>RPN is short-hand for Reverse Polish Notation. It works as follows.
140 You put the variables or numbers on a stack. You also put operations
141 (things-to-do) on the stack and this stack is then processed. The result
142 will be placed on the stack. At the end, there should be exactly one
143 number left: the outcome of the series of operations. If there is not
144 exactly one number left, RRDtool will complain loudly.</p>
145 <p>Above multiplication by eight will look like:</p>
146 <ol>
147 <li>
148 <p>Start with an empty stack</p>
149 </li>
150 <li>
151 <p>Put the content of variable inbytes on the stack</p>
152 </li>
153 <li>
154 <p>Put the number eight on the stack</p>
155 </li>
156 <li>
157 <p>Put the operation multiply on the stack</p>
158 </li>
159 <li>
160 <p>Process the stack</p>
161 </li>
162 <li>
163 <p>Retrieve the value from the stack and put it in variable inbits</p>
164 </li>
165 </ol>
166 <p>We will now do an example with real numbers. Suppose the variable
167 inbytes would have value 10, the stack would be:</p>
168 <ol>
169 <li>
170 <p>||</p>
171 </li>
172 <li>
173 <p>|10|</p>
174 </li>
175 <li>
176 <p>|10|8|</p>
177 </li>
178 <li>
179 <p>|10|8|*|</p>
180 </li>
181 <li>
182 <p>|80|</p>
183 </li>
184 <li>
185 <p>||</p>
186 </li>
187 </ol>
188 <p>Processing the stack (step 5) will retrieve one value from the stack
189 (from the right at step 4). This is the operation multiply and this
190 takes two values off the stack as input. The result is put back on the
191 stack (the value 80 in this case). For multiplication the order doesn't
192 matter, but for other operations like subtraction and division it does.
193 Generally speaking you have the following order:</p>
194 <pre>
195    y = A - B  --&gt;  y=minus(A,B)  --&gt;  CDEF:y=A,B,-</pre>
196 <p>This is not very intuitive (at least most people don't think so). For
197 the function <code>f(A,B)</code> you reverse the position of ``f'', but you do not
198 reverse the order of the variables.</p>
199 <p>
200 </p>
201 <hr />
202 <h1><a name="converting_your_wishes_to_rpn">Converting your wishes to RPN</a></h1>
203 <p>First, get a clear picture of what you want to do. Break down the problem
204 in smaller portions until they cannot be split anymore. Then it is rather
205 simple to convert your ideas into RPN.</p>
206 <p>Suppose you have several RRDs and would like to add up some counters in
207 them. These could be, for instance, the counters for every WAN link you
208 are monitoring.</p>
209 <p>You have:</p>
210 <pre>
211    router1.rrd with link1in link2in
212    router2.rrd with link1in link2in
213    router3.rrd with link1in link2in</pre>
214 <p>Suppose you would like to add up all these counters, except for link2in
215 inside router2.rrd. You need to do:</p>
216 <p>(in this example, ``router1.rrd:link1in'' means the DS link1in inside the
217 RRD router1.rrd)</p>
218 <pre>
219    router1.rrd:link1in
220    router1.rrd:link2in
221    router2.rrd:link1in
222    router3.rrd:link1in
223    router3.rrd:link2in
224    --------------------   +
225    (outcome of the sum)</pre>
226 <p>As a mathematical function, this could be written:</p>
227 <p><code>add(router1.rrd:link1in , router1.rrd:link2in , router2.rrd:link1in , router3.rrd:link1in , router3.rrd:link2.in)</code></p>
228 <p>With RRDtool and RPN, first, define the inputs:</p>
229 <pre>
230    DEF:a=router1.rrd:link1in:AVERAGE
231    DEF:b=router1.rrd:link2in:AVERAGE
232    DEF:c=router2.rrd:link1in:AVERAGE
233    DEF:d=router3.rrd:link1in:AVERAGE
234    DEF:e=router3.rrd:link2in:AVERAGE</pre>
235 <p>Now, the mathematical function becomes: <code>add(a,b,c,d,e)</code></p>
236 <p>In RPN, there's no operator that sums more than two values so you need
237 to do several additions. You add a and b, add c to the result, add d
238 to the result and add e to the result.</p>
239 <pre>
240    push a:         a     stack contains the value of a
241    push b and add: b,+   stack contains the result of a+b
242    push c and add: c,+   stack contains the result of a+b+c
243    push d and add: d,+   stack contains the result of a+b+c+d
244    push e and add: e,+   stack contains the result of a+b+c+d+e</pre>
245 <p>What was calculated here would be written down as:</p>
246 <pre>
247    ( ( ( (a+b) + c) + d) + e) &gt;</pre>
248 <p>This is in RPN:  <code>CDEF:result=a,b,+,c,+,d,+,e,+</code></p>
249 <p>This is correct but it can be made more clear to humans. It does
250 not matter if you add a to b and then add c to the result or first
251 add b to c and then add a to the result. This makes it possible to
252 rewrite the RPN into <code>CDEF:result=a,b,c,d,e,+,+,+,+</code> which is
253 evaluated differently:</p>
254 <pre>
255    push value of variable a on the stack: a
256    push value of variable b on the stack: a b
257    push value of variable c on the stack: a b c
258    push value of variable d on the stack: a b c d
259    push value of variable e on the stack: a b c d e
260    push operator + on the stack:          a b c d e +
261    and process it:                        a b c P   (where P == d+e)
262    push operator + on the stack:          a b c P +
263    and process it:                        a b Q     (where Q == c+P)
264    push operator + on the stack:          a b Q +
265    and process it:                        a R       (where R == b+Q)
266    push operator + on the stack:          a R +
267    and process it:                        S         (where S == a+R)</pre>
268 <p>As you can see the RPN expression <code>a,b,c,d,e,+,+,+,+,+</code> will evaluate in
269 <code>((((d+e)+c)+b)+a)</code> and it has the same outcome as <code>a,b,+,c,+,d,+,e,+</code>.
270 This is called the commutative law of addition,
271 but you may forget this right away, as long as you remember what it
272 means.</p>
273 <p>Now look at an expression that contains a multiplication:</p>
274 <p>First in normal math: <code>let result = a+b*c</code>. In this case you can't
275 choose the order yourself, you have to start with the multiplication
276 and then add a to it. You may alter the position of b and c, you must
277 not alter the position of a and b.</p>
278 <p>You have to take this in consideration when converting this expression
279 into RPN. Read it as: ``Add the outcome of b*c to a'' and then it is
280 easy to write the RPN expression: <code>result=a,b,c,*,+</code>
281 Another expression that would return the same: <code>result=b,c,*,a,+</code></p>
282 <p>In normal math, you may encounter something like ``a*(b+c)'' and this
283 can also be converted into RPN. The parenthesis just tell you to first
284 add b and c, and then multiply a with the result. Again, now it is
285 easy to write it in RPN: <code>result=a,b,c,+,*</code>. Note that this is very
286 similar to one of the expressions in the previous paragraph, only the
287 multiplication and the addition changed places.</p>
288 <p>When you have problems with RPN or when RRDtool is complaining, it's
289 usually a good thing to write down the stack on a piece of paper
290 and see what happens. Have the manual ready and pretend to be RRDtool.
291 Just do all the math by hand to see what happens, I'm sure this will
292 solve most, if not all, problems you encounter.</p>
293 <p>
294 </p>
295 <hr />
296 <h1><a name="some_special_numbers">Some special numbers</a></h1>
297 <p>
298 </p>
299 <h2><a name="the_unknown_value">The unknown value</a></h2>
300 <p>Sometimes collecting your data will fail. This can be very common,
301 especially when querying over busy links. RRDtool can be configured
302 to allow for one (or even more) unknown <code>value(s)</code> and calculate the missing
303 update. You can, for instance, query your device every minute. This is
304 creating one so called PDP or primary data point per minute. If you
305 defined your RRD to contain an RRA that stores 5-minute values, you need
306 five of those PDPs to create one CDP (consolidated data point).
307 These PDPs can become unknown in two cases:</p>
308 <ol>
309 <li>
310 <p>The updates are too far apart. This is tuned using the ``heartbeat'' setting.</p>
311 </li>
312 <li>
313 <p>The update was set to unknown on purpose by inserting no value (using the
314 template option) or by using ``U'' as the value to insert.</p>
315 </li>
316 </ol>
317 <p>When a CDP is calculated, another mechanism determines if this CDP is valid
318 or not. If there are too many PDPs unknown, the CDP is unknown as well.
319 This is determined by the xff factor. Please note that one unknown counter
320 update can result in two unknown PDPs! If you only allow for one unknown
321 PDP per CDP, this makes the CDP go unknown!</p>
322 <p>Suppose the counter increments with one per second and you retrieve it
323 every minute:</p>
324 <pre>
325    counter value    resulting rate
326    10'000
327    10'060            1; (10'060-10'000)/60 == 1
328    10'120            1; (10'120-10'060)/60 == 1
329    unknown           unknown; you don't know the last value
330    10'240            unknown; you don't know the previous value
331    10'300            1; (10'300-10'240)/60 == 1</pre>
332 <p>If the CDP was to be calculated from the last five updates, it would get
333 two unknown PDPs and three known PDPs. If xff would have been set to 0.5
334 which by the way is a commonly used factor, the CDP would have a known
335 value of 1. If xff would have been set to 0.2 then the resulting CDP
336 would be unknown.</p>
337 <p>You have to decide the proper values for heartbeat, number of PDPs per
338 CDP and the xff factor. As you can see from the previous text they define
339 the behavior of your RRA.</p>
340 <p>
341 </p>
342 <h2><a name="working_with_unknown_data_in_your_database">Working with unknown data in your database</a></h2>
343 <p>As you have read in the previous chapter, entries in an RRA can be
344 set to the unknown value. If you do calculations with this type of
345 value, the result has to be unknown too. This means that an expression
346 such as <code>result=a,b,+</code> will be unknown if either a or b is unknown.
347 It would be wrong to just ignore the unknown value and return the value
348 of the other parameter. By doing so, you would assume ``unknown'' means ``zero''
349 and this is not true.</p>
350 <p>There has been a case where somebody was collecting data for over a year.
351 A new piece of equipment was installed, a new RRD was created and the
352 scripts were changed to add a counter from the old database and a counter
353 from the new database. The result was disappointing, a large part of
354 the statistics seemed to have vanished mysteriously ...
355 They of course didn't, values from the old database (known values) were
356 added to values from the new database (unknown values) and the result was
357 unknown.</p>
358 <p>In this case, it is fairly reasonable to use a CDEF that alters unknown
359 data into zero. The counters of the device were unknown (after all, it
360 wasn't installed yet!) but you know that the data rate through the device
361 had to be zero (because of the same reason: it was not installed).</p>
362 <p>There are some examples below that make this change.</p>
363 <p>
364 </p>
365 <h2><a name="infinity">Infinity</a></h2>
366 <p>Infinite data is another form of a special number. It cannot be
367 graphed because by definition you would never reach the infinite
368 value. You can think of positive and negative infinity depending on
369 the position relative to zero.</p>
370 <p>RRDtool is capable of representing (-not- graphing!) infinity by stopping
371 at its current maximum (for positive infinity) or minimum (for negative
372 infinity) without knowing this maximum (minimum).</p>
373 <p>Infinity in RRDtool is mostly used to draw an AREA without knowing its
374 vertical dimensions. You can think of it as drawing an AREA with an
375 infinite height and displaying only the part that is visible in the
376 current graph. This is probably a good way to approximate infinity
377 and it sure allows for some neat tricks. See below for examples.</p>
378 <p>
379 </p>
380 <h2><a name="working_with_unknown_data_and_infinity">Working with unknown data and infinity</a></h2>
381 <p>Sometimes you would like to discard unknown data and pretend it is zero
382 (or any other value for that matter) and sometimes you would like to
383 pretend that known data is unknown (to discard known-to-be-wrong data).
384 This is why CDEFs have support for unknown data. There are also examples
385 available that show unknown data by using infinity.</p>
386 <p>
387 </p>
388 <hr />
389 <h1><a name="some_examples">Some examples</a></h1>
390 <p>
391 </p>
392 <h2><a name="example__using_a_recently_created_rrd">Example: using a recently created RRD</a></h2>
393 <p>You are keeping statistics on your router for over a year now. Recently
394 you installed an extra router and you would like to show the combined
395 throughput for these two devices.</p>
396 <p>If you just add up the counters from router.rrd and router2.rrd, you
397 will add known data (from router.rrd) to unknown data (from router2.rrd) for
398 the bigger part of your stats. You could solve this in a few ways:</p>
399 <ul>
400 <li>
401 <p>While creating the new database, fill it with zeros from the start to now.
402 You have to make the database start at or before the least recent time in
403 the other database.</p>
404 </li>
405 <li>
406 <p>Alternatively, you could use CDEF and alter unknown data to zero.</p>
407 </li>
408 </ul>
409 <p>Both methods have their pros and cons. The first method is troublesome and
410 if you want to do that you have to figure it out yourself. It is not
411 possible to create a database filled with zeros, you have to put them in
412 manually. Implementing the second method is described next:</p>
413 <p>What we want is: ``if the value is unknown, replace it with zero''. This
414 could be written in pseudo-code as:  if (value is unknown) then (zero)
415 else (value). When reading the <a href="././rrdgraph.html">the rrdgraph manpage</a> manual you notice the ``UN''
416 function that returns zero or one. You also notice the ``IF'' function
417 that takes zero or one as input.</p>
418 <p>First look at the ``IF'' function. It takes three values from the stack,
419 the first value is the decision point, the second value is returned to
420 the stack if the evaluation is ``true'' and if not, the third value is
421 returned to the stack. We want the ``UN'' function to decide what happens
422 so we combine those two functions in one CDEF.</p>
423 <p>Lets write down the two possible paths for the ``IF'' function:</p>
424 <pre>
425    if true  return a
426    if false return b</pre>
427 <p>In RPN:  <code>result=x,a,b,IF</code> where ``x'' is either true or false.</p>
428 <p>Now we have to fill in ``x'', this should be the ``(value is unknown)'' part
429 and this is in RPN:  <code>result=value,UN</code></p>
430 <p>We now combine them: <code>result=value,UN,a,b,IF</code> and when we fill in the
431 appropriate things for ``a'' and ``b'' we're finished:</p>
432 <p><code>CDEF:result=value,UN,0,value,IF</code></p>
433 <p>You may want to read Steve Rader's RPN guide if you have difficulties
434 with the way I explained this last example.</p>
435 <p>If you want to check this RPN expression, just mimic RRDtool behavior:</p>
436 <pre>
437    For any known value, the expression evaluates as follows:
438    CDEF:result=value,UN,0,value,IF  (value,UN) is not true so it becomes 0
439    CDEF:result=0,0,value,IF         &quot;IF&quot; will return the 3rd value
440    CDEF:result=value                The known value is returned</pre>
441 <pre>
442    For the unknown value, this happens:
443    CDEF:result=value,UN,0,value,IF  (value,UN) is true so it becomes 1
444    CDEF:result=1,0,value,IF         &quot;IF&quot; sees 1 and returns the 2nd value
445    CDEF:result=0                    Zero is returned</pre>
446 <p>Of course, if you would like to see another value instead of zero, you
447 can use that other value.</p>
448 <p>Eventually, when all unknown data is removed from the RRD, you may want
449 to remove this rule so that unknown data is properly displayed.</p>
450 <p>
451 </p>
452 <h2><a name="example__better_handling_of_unknown_data__by_using_time">Example: better handling of unknown data, by using time</a></h2>
453 <p>The above example has one drawback. If you do log unknown data in
454 your database after installing your new equipment, it will also be
455 translated into zero and therefore you won't see that there was a
456 problem. This is not good and what you really want to do is:</p>
457 <ul>
458 <li>
459 <p>If there is unknown data, look at the time that this sample was taken.</p>
460 </li>
461 <li>
462 <p>If the unknown value is before time xxx, make it zero.</p>
463 </li>
464 <li>
465 <p>If it is after time xxx, leave it as unknown data.</p>
466 </li>
467 </ul>
468 <p>This is doable: you can compare the time that the sample was taken
469 to some known time. Assuming you started to monitor your device on
470 Friday September 17, 1999, 00:35:57 MET DST. Translate this time in seconds
471 since 1970-01-01 and it becomes 937'521'357. If you process unknown values
472 that were received after this time, you want to leave them unknown and
473 if they were ``received'' before this time, you want to translate them
474 into zero (so you can effectively ignore them while adding them to your
475 other routers counters).</p>
476 <p>Translating Friday September 17, 1999, 00:35:57 MET DST into 937'521'357 can
477 be done by, for instance, using gnu date:</p>
478 <pre>
479    date -d &quot;19990917 00:35:57&quot; +%s</pre>
480 <p>You could also dump the database and see where the data starts to be
481 known. There are several other ways of doing this, just pick one.</p>
482 <p>Now we have to create the magic that allows us to process unknown
483 values different depending on the time that the sample was taken.
484 This is a three step process:</p>
485 <ol>
486 <li>
487 <p>If the timestamp of the value is after 937'521'357, leave it as is.</p>
488 </li>
489 <li>
490 <p>If the value is a known value, leave it as is.</p>
491 </li>
492 <li>
493 <p>Change the unknown value into zero.</p>
494 </li>
495 </ol>
496 <p>Lets look at part one:</p>
497 <pre>
498     if (true) return the original value</pre>
499 <p>We rewrite this:</p>
500 <pre>
501     if (true) return &quot;a&quot;
502     if (false) return &quot;b&quot;</pre>
503 <p>We need to calculate true or false from step 1. There is a function
504 available that returns the timestamp for the current sample. It is
505 called, how surprisingly, ``TIME''. This time has to be compared to
506 a constant number, we need ``GT''. The output of ``GT'' is true or false
507 and this is good input to ``IF''. We want ``if (time &gt; 937521357) then
508 (return a) else (return b)''.</p>
509 <p>This process was already described thoroughly in the previous chapter
510 so lets do it quick:</p>
511 <pre>
512    if (x) then a else b
513       where x represents &quot;time&gt;937521357&quot;
514       where a represents the original value
515       where b represents the outcome of the previous example</pre>
516 <pre>
517    time&gt;937521357       --&gt; TIME,937521357,GT</pre>
518 <pre>
519    if (x) then a else b --&gt; x,a,b,IF
520    substitute x         --&gt; TIME,937521357,GT,a,b,IF
521    substitute a         --&gt; TIME,937521357,GT,value,b,IF
522    substitute b         --&gt; TIME,937521357,GT,value,value,UN,0,value,IF,IF</pre>
523 <p>We end up with:
524 <code>CDEF:result=TIME,937521357,GT,value,value,UN,0,value,IF,IF</code></p>
525 <p>This looks very complex, however, as you can see, it was not too hard to
526 come up with.</p>
527 <p>
528 </p>
529 <h2><a name="example__pretending_weird_data_isn_t_there">Example: Pretending weird data isn't there</a></h2>
530 <p>Suppose you have a problem that shows up as huge spikes in your graph.
531 You know this happens and why, so you decide to work around the problem.
532 Perhaps you're using your network to do a backup at night and by doing
533 so you get almost 10mb/s while the rest of your network activity does
534 not produce numbers higher than 100kb/s.</p>
535 <p>There are two options:</p>
536 <ol>
537 <li>
538 <p>If the number exceeds 100kb/s it is wrong and you want it masked out
539 by changing it into unknown.</p>
540 </li>
541 <li>
542 <p>You don't want the graph to show more than 100kb/s.</p>
543 </li>
544 </ol>
545 <p>Pseudo code: if (number &gt; 100) then unknown else number
546 or
547 Pseudo code: if (number &gt; 100) then 100 else number.</p>
548 <p>The second ``problem'' may also be solved by using the rigid option of
549 RRDtool graph, however this has not the same result. In this example
550 you can end up with a graph that does autoscaling. Also, if you use
551 the numbers to display maxima they will be set to 100kb/s.</p>
552 <p>We use ``IF'' and ``GT'' again. ``if (x) then (y) else (z)'' is written
553 down as ``CDEF:result=x,y,z,IF''; now fill in x, y and z.
554 For x you fill in ``number greater than 100kb/s'' becoming
555 ``number,100000,GT'' (kilo is 1'000 and b/s is what we measure!).
556 The ``z'' part is ``number'' in both cases and the ``y'' part is either
557 ``UNKN'' for unknown or ``100000'' for 100kb/s.</p>
558 <p>The two CDEF expressions would be:</p>
559 <pre>
560     CDEF:result=number,100000,GT,UNKN,number,IF
561     CDEF:result=number,100000,GT,100000,number,IF</pre>
562 <p>
563 </p>
564 <h2><a name="example__working_on_a_certain_time_span">Example: working on a certain time span</a></h2>
565 <p>If you want a graph that spans a few weeks, but would only want to
566 see some routers' data for one week, you need to ``hide'' the rest of
567 the time frame. Don't ask me when this would be useful, it's just
568 here for the example :)</p>
569 <p>We need to compare the time stamp to a begin date and an end date.
570 Comparing isn't difficult:</p>
571 <pre>
572         TIME,begintime,GE
573         TIME,endtime,LE</pre>
574 <p>These two parts of the CDEF produce either 0 for false or 1 for true.
575 We can now check if they are both 0 (or 1) using a few IF statements
576 but, as Wataru Satoh pointed out, we can use the ``*'' or ``+'' functions
577 as logical AND and logical OR.</p>
578 <p>For ``*'', the result will be zero (false) if either one of the two
579 operators is zero.  For ``+'', the result will only be false (0) when
580 two false (0) operators will be added.  Warning: *any* number not
581 equal to 0 will be considered ``true''. This means that, for instance,
582 ``-1,1,+'' (which should be ``true or true'') will become FALSE ...
583 In other words, use ``+'' only if you know for sure that you have positive
584 numbers (or zero) only.</p>
585 <p>Let's compile the complete CDEF:</p>
586 <pre>
587         DEF:ds0=router1.rrd:AVERAGE
588         CDEF:ds0modified=TIME,begintime,GE,TIME,endtime,LE,*,UNKN,ds0,IF</pre>
589 <p>This will return the value of ds0 if both comparisons return true. You
590 could also do it the other way around:</p>
591 <pre>
592         DEF:ds0=router1.rrd:AVERAGE
593         CDEF:ds0modified=TIME,begintime,LT,TIME,endtime,GT,+,UNKN,ds0,IF</pre>
594 <p>This will return an UNKNOWN if either comparison returns true.</p>
595 <p>
596 </p>
597 <h2><a name="example__you_suspect_to_have_problems_and_want_to_see_unknown_data_">Example: You suspect to have problems and want to see unknown data.</a></h2>
598 <p>Suppose you add up the number of active users on several terminal servers.
599 If one of them doesn't give an answer (or an incorrect one) you get ``NaN''
600 in the database (``Not a Number'') and NaN is evaluated as Unknown.</p>
601 <p>In this case, you would like to be alerted to it and the sum of the
602 remaining values is of no value to you.</p>
603 <p>It would be something like:</p>
604 <pre>
605     DEF:users1=location1.rrd:onlineTS1:LAST
606     DEF:users2=location1.rrd:onlineTS2:LAST
607     DEF:users3=location2.rrd:onlineTS1:LAST
608     DEF:users4=location2.rrd:onlineTS2:LAST
609     CDEF:allusers=users1,users2,users3,users4,+,+,+</pre>
610 <p>If you now plot allusers, unknown data in one of users1..users4 will
611 show up as a gap in your graph. You want to modify this to show a
612 bright red line, not a gap.</p>
613 <p>Define an extra CDEF that is unknown if all is okay and is infinite if
614 there is an unknown value:</p>
615 <pre>
616     CDEF:wrongdata=allusers,UN,INF,UNKN,IF</pre>
617 <p>``allusers,UN'' will evaluate to either true or false, it is the (x) part
618 of the ``IF'' function and it checks if allusers is unknown.
619 The (y) part of the ``IF'' function is set to ``INF'' (which means infinity)
620 and the (z) part of the function returns ``UNKN''.</p>
621 <p>The logic is: if (allusers == unknown) then return INF else return UNKN.</p>
622 <p>You can now use AREA to display this ``wrongdata'' in bright red. If it
623 is unknown (because allusers is known) then the red AREA won't show up.
624 If the value is INF (because allusers is unknown) then the red AREA will
625 be filled in on the graph at that particular time.</p>
626 <pre>
627    AREA:allusers#0000FF:combined user count
628    AREA:wrongdata#FF0000:unknown data</pre>
629 <p>
630 </p>
631 <h2><a name="same_example_useful_with_stacked_data_">Same example useful with STACKed data:</a></h2>
632 <p>If you use stack in the previous example (as I would do) then you don't
633 add up the values. Therefore, there is no relationship between the
634 four values and you don't get a single value to test.
635 Suppose users3 would be unknown at one point in time: users1 is plotted,
636 users2 is stacked on top of users1, users3 is unknown and therefore
637 nothing happens, users4 is stacked on top of users2.
638 Add the extra CDEFs anyway and use them to overlay the ``normal'' graph:</p>
639 <pre>
640    DEF:users1=location1.rrd:onlineTS1:LAST
641    DEF:users2=location1.rrd:onlineTS2:LAST
642    DEF:users3=location2.rrd:onlineTS1:LAST
643    DEF:users4=location2.rrd:onlineTS2:LAST
644    CDEF:allusers=users1,users2,users3,users4,+,+,+
645    CDEF:wrongdata=allusers,UN,INF,UNKN,IF
646    AREA:users1#0000FF:users at ts1
647    STACK:users2#00FF00:users at ts2
648    STACK:users3#00FFFF:users at ts3
649    STACK:users4#FFFF00:users at ts4
650    AREA:wrongdata#FF0000:unknown data</pre>
651 <p>If there is unknown data in one of users1..users4, the ``wrongdata'' AREA
652 will be drawn and because it starts at the X-axis and has infinite height
653 it will effectively overwrite the STACKed parts.</p>
654 <p>You could combine the two CDEF lines into one (we don't use ``allusers'')
655 if you like.  But there are good reasons for writing two CDEFS:</p>
656 <ul>
657 <li>
658 <p>It improves the readability of the script.</p>
659 </li>
660 <li>
661 <p>It can be used inside GPRINT to display the total number of users.</p>
662 </li>
663 </ul>
664 <p>If you choose to combine them, you can substitute the ``allusers'' in the
665 second CDEF with the part after the equal sign from the first line:</p>
666 <pre>
667    CDEF:wrongdata=users1,users2,users3,users4,+,+,+,UN,INF,UNKN,IF</pre>
668 <p>If you do so, you won't be able to use these next GPRINTs:</p>
669 <pre>
670    COMMENT:&quot;Total number of users seen&quot;
671    GPRINT:allusers:MAX:&quot;Maximum: %6.0lf&quot;
672    GPRINT:allusers:MIN:&quot;Minimum: %6.0lf&quot;
673    GPRINT:allusers:AVERAGE:&quot;Average: %6.0lf&quot;
674    GPRINT:allusers:LAST:&quot;Current: %6.0lf\n&quot;</pre>
675 <p>
676 </p>
677 <hr />
678 <h1><a name="the_examples_from_the_rrd_graph_manual_page">The examples from the RRD graph manual page</a></h1>
679 <p>
680 </p>
681 <h2><a name="degrees_celsius_vs__degrees_fahrenheit">Degrees Celsius vs. Degrees Fahrenheit</a></h2>
682 <p>To convert Celsius into Fahrenheit use the formula
683 F=9/5*C+32</p>
684 <pre>
685    rrdtool graph demo.png --title=&quot;Demo Graph&quot; \
686       DEF:cel=demo.rrd:exhaust:AVERAGE \
687       CDEF:far=9,5,/,cel,*,32,+ \
688       LINE2:cel#00a000:&quot;D. Celsius&quot; \
689       LINE2:far#ff0000:&quot;D. Fahrenheit\c&quot;</pre>
690 <p>This example gets the DS called ``exhaust'' from database ``demo.rrd''
691 and puts the values in variable ``cel''. The CDEF used is evaluated
692 as follows:</p>
693 <pre>
694    CDEF:far=9,5,/,cel,*,32,+
695    1. push 9, push 5
696    2. push function &quot;divide&quot; and process it
697       the stack now contains 9/5
698    3. push variable &quot;cel&quot;
699    4. push function &quot;multiply&quot; and process it
700       the stack now contains 9/5*cel
701    5. push 32
702    6. push function &quot;plus&quot; and process it
703       the stack contains now the temperature in Fahrenheit
704    
705 =head2 Changing unknown into zero</pre>
706 <pre>
707    rrdtool graph demo.png --title=&quot;Demo Graph&quot; \
708       DEF:idat1=interface1.rrd:ds0:AVERAGE \
709       DEF:idat2=interface2.rrd:ds0:AVERAGE \
710       DEF:odat1=interface1.rrd:ds1:AVERAGE \
711       DEF:odat2=interface2.rrd:ds1:AVERAGE \
712       CDEF:agginput=idat1,UN,0,idat1,IF,idat2,UN,0,idat2,IF,+,8,* \
713       CDEF:aggoutput=odat1,UN,0,odat1,IF,odat2,UN,0,odat2,IF,+,8,* \
714       AREA:agginput#00cc00:Input Aggregate \
715       LINE1:aggoutput#0000FF:Output Aggregate</pre>
716 <p>These two CDEFs are built from several functions. It helps to split
717 them when viewing what they do. Starting with the first CDEF we would
718 get:</p>
719 <pre>
720  idat1,UN --&gt; a
721  0        --&gt; b
722  idat1    --&gt; c
723  if (a) then (b) else (c)</pre>
724 <p>The result is therefore ``0'' if it is true that ``idat1'' equals ``UN''.
725 If not, the original value of ``idat1'' is put back on the stack.
726 Lets call this answer ``d''. The process is repeated for the next
727 five items on the stack, it is done the same and will return answer
728 ``h''. The resulting stack is therefore ``d,h''.
729 The expression has been simplified to ``d,h,+,8,*'' and it will now be
730 easy to see that we add ``d'' and ``h'', and multiply the result with eight.</p>
731 <p>The end result is that we have added ``idat1'' and ``idat2'' and in the
732 process we effectively ignored unknown values. The result is multiplied
733 by eight, most likely to convert bytes/s to bits/s.</p>
734 <p>
735 </p>
736 <h2><a name="infinity_demo">Infinity demo</a></h2>
737 <pre>
738    rrdtool graph example.png --title=&quot;INF demo&quot; \
739       DEF:val1=some.rrd:ds0:AVERAGE \
740       DEF:val2=some.rrd:ds1:AVERAGE \
741       DEF:val3=some.rrd:ds2:AVERAGE \
742       DEF:val4=other.rrd:ds0:AVERAGE \
743       CDEF:background=val4,POP,TIME,7200,%,3600,LE,INF,UNKN,IF \
744       CDEF:wipeout=val1,val2,val3,val4,+,+,+,UN,INF,UNKN,IF \
745       AREA:background#F0F0F0 \
746       AREA:val1#0000FF:Value1 \
747       STACK:val2#00C000:Value2 \
748       STACK:val3#FFFF00:Value3 \
749       STACK:val4#FFC000:Value4 \
750       AREA:whipeout#FF0000:Unknown</pre>
751 <p>This demo demonstrates two ways to use infinity. It is a bit tricky
752 to see what happens in the ``background'' CDEF.</p>
753 <pre>
754    &quot;val4,POP,TIME,7200,%,3600,LE,INF,UNKN,IF&quot;</pre>
755 <p>This RPN takes the value of ``val4'' as input and then immediately
756 removes it from the stack using ``POP''. The stack is now empty but
757 as a side effect we now know the time that this sample was taken.
758 This time is put on the stack by the ``TIME'' function.</p>
759 <p>``TIME,7200,%'' takes the modulo of time and 7'200 (which is two hours).
760 The resulting value on the stack will be a number in the range from
761 0 to 7199.</p>
762 <p>For people who don't know the modulo function: it is the remainder
763 after an integer division. If you divide 16 by 3, the answer would
764 be 5 and the remainder would be 1. So, ``16,3,%'' returns 1.</p>
765 <p>We have the result of ``TIME,7200,%'' on the stack, lets call this
766 ``a''. The start of the RPN has become ``a,3600,LE'' and this checks
767 if ``a'' is less or equal than ``3600''. It is true half of the time.
768 We now have to process the rest of the RPN and this is only a simple
769 ``IF'' function that returns either ``INF'' or ``UNKN'' depending on the
770 time. This is returned to variable ``background''.</p>
771 <p>The second CDEF has been discussed earlier in this document so we
772 won't do that here.</p>
773 <p>Now you can draw the different layers. Start with the background
774 that is either unknown (nothing to see) or infinite (the whole
775 positive part of the graph gets filled).</p>
776 <p>Next you draw the data on top of this background, it will overlay
777 the background. Suppose one of val1..val4 would be unknown, in that
778 case you end up with only three bars stacked on top of each other.
779 You don't want to see this because the data is only valid when all
780 four variables are valid. This is why you use the second CDEF, it
781 will overlay the data with an AREA so the data cannot be seen anymore.</p>
782 <p>If your data can also have negative values you also need to overwrite
783 the other half of your graph. This can be done in a relatively simple
784 way: what you need is the ``wipeout'' variable and place a negative
785 sign before it:  ``CDEF:wipeout2=wipeout,-1,*''</p>
786 <p>
787 </p>
788 <h2><a name="filtering_data">Filtering data</a></h2>
789 <p>You may do some complex data filtering:</p>
790 <pre>
791   MEDIAN FILTER: filters shot noise</pre>
792 <pre>
793     DEF:var=database.rrd:traffic:AVERAGE
794     CDEF:prev1=PREV(var)
795     CDEF:prev2=PREV(prev1)
796     CDEF:prev3=PREV(prev2)
797     CDEF:median=prev1,prev2,prev3,+,+,3,/
798     LINE3:median#000077:filtered
799     LINE1:prev2#007700:'raw data'</pre>
800 <pre>
801   DERIVATE:</pre>
802 <pre>
803     DEF:var=database.rrd:traffic:AVERAGE
804     CDEF:prev1=PREV(var)
805     CDEF:time=TIME
806     CDEF:prevtime=PREV(time)
807     CDEF:derivate=var,prev1,-,time,prevtime,-,/
808     LINE3:derivate#000077:derivate
809     LINE1:var#007700:'raw data'</pre>
810 <p>
811 </p>
812 <hr />
813 <h1><a name="out_of_ideas_for_now">Out of ideas for now</a></h1>
814 <p>This document was created from questions asked by either myself or by
815 other people on the RRDtool mailing list. Please let me know if you
816 find errors in it or if you have trouble understanding it. If you
817 think there should be an addition, mail me:
818 &lt;<a href="mailto:alex@ergens.op.het.net">alex@ergens.op.het.net</a>&gt;</p>
819 <p>Remember: <strong>No feedback equals no changes!</strong></p>
820 <p>
821 </p>
822 <hr />
823 <h1><a name="see_also">SEE ALSO</a></h1>
824 <p>The RRDtool manpages</p>
825 <p>
826 </p>
827 <hr />
828 <h1><a name="author">AUTHOR</a></h1>
829 <p>Alex van den Bogaerdt
830 &lt;<a href="mailto:alex@ergens.op.het.net">alex@ergens.op.het.net</a>&gt;</p>
832 </body>
834 </html>