Code

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