Code

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