Code

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