Code

0da9f789808b768d3cee9dbe259ed1a961c633d0
[pkg-rrdtool.git] / doc / rrdgraph_rpn.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>rrdgraph_rpn</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="#synopsis">SYNOPSIS</a></li>
18         <li><a href="#description">DESCRIPTION</a></li>
19         <li><a href="#operators">OPERATORS</a></li>
20         <li><a href="#variables">VARIABLES</a></li>
21         <li><a href="#see_also">SEE ALSO</a></li>
22         <li><a href="#author">AUTHOR</a></li>
23 </ul>
24 -->
25 <!-- INDEX END -->
27 <p>
28 </p>
29 <h1><a name="name">NAME</a></h1>
30 <p>rrdgraph_rpn - About RPN Math in rrdtool graph</p>
31 <p>
32 </p>
33 <hr />
34 <h1><a name="synopsis">SYNOPSIS</a></h1>
35 <p><em>RPN expression</em>:=<em>vname</em>|<em>operator</em>|<em>value</em>[,<em>RPN expression</em>]</p>
36 <p>
37 </p>
38 <hr />
39 <h1><a name="description">DESCRIPTION</a></h1>
40 <p>If you have ever used a traditional HP calculator you already know
41 <strong>RPN</strong> (Reverse Polish Notation).
42 The idea behind <strong>RPN</strong> is that you have a stack and push
43 your data onto this stack. Whenever you execute an operation, it
44 takes as many elements from the stack as needed. Pushing is done
45 implicitly, so whenever you specify a number or a variable, it gets
46 pushed onto the stack automatically.</p>
47 <p>At the end of the calculation there should be one and only one value left on
48 the stack.  This is the outcome of the function and this is what is put into
49 the <em>vname</em>.  For <strong>CDEF</strong> instructions, the stack is processed for each
50 data point on the graph. <strong>VDEF</strong> instructions work on an entire data set in
51 one run. Note, that currently <strong>VDEF</strong> instructions only support a limited
52 list of functions.</p>
53 <p>Example: <code>VDEF:maximum=mydata,MAXIMUM</code></p>
54 <p>This will set variable ``maximum'' which you now can use in the rest
55 of your RRD script.</p>
56 <p>Example: <code>CDEF:mydatabits=mydata,8,*</code></p>
57 <p>This means:  push variable <em>mydata</em>, push the number 8, execute
58 the operator <em>*</em>. The operator needs two elements and uses those
59 to return one value.  This value is then stored in <em>mydatabits</em>.
60 As you may have guessed, this instruction means nothing more than
61 <em>mydatabits = mydata * 8</em>.  The real power of <strong>RPN</strong> lies in the
62 fact that it is always clear in which order to process the input.
63 For expressions like <code>a = b + 3 * 5</code> you need to multiply 3 with
64 5 first before you add <em>b</em> to get <em>a</em>. However, with parentheses
65 you could change this order: <code>a = (b + 3) * 5</code>. In <strong>RPN</strong>, you
66 would do <code>a = b, 3, +, 5, *</code> without the need for parentheses.</p>
67 <p>
68 </p>
69 <hr />
70 <h1><a name="operators">OPERATORS</a></h1>
71 <dl>
72 <dt><strong><a name="item_boolean_operators">Boolean operators</a></strong><br />
73 </dt>
74 <dd>
75 <strong>LT, LE, GT, GE, EQ, NE</strong>
76 </dd>
77 <dd>
78 <p>Pop two elements from the stack, compare them for the selected condition
79 and return 1 for true or 0 for false. Comparing an <em>unknown</em> or an
80 <em>infinite</em> value will always result in 0 (false).</p>
81 </dd>
82 <dd>
83 <p><strong>UN, ISINF</strong></p>
84 </dd>
85 <dd>
86 <p>Pop one element from the stack, compare this to <em>unknown</em> respectively
87 to <em>positive or negative infinity</em>. Returns 1 for true or 0 for false.</p>
88 </dd>
89 <dd>
90 <p><strong>IF</strong></p>
91 </dd>
92 <dd>
93 <p>Pops three elements from the stack.  If the element popped last is 0
94 (false), the value popped first is pushed back onto the stack,
95 otherwise the value popped second is pushed back. This does, indeed,
96 mean that any value other than 0 is considered to be true.</p>
97 </dd>
98 <dd>
99 <p>Example: <code>A,B,C,IF</code> should be read as <code>if (A) then (B) else (C)</code></p>
100 </dd>
101 <dd>
102 <p></p>
103 </dd>
104 <p></p>
105 <dt><strong><a name="item_comparing_values">Comparing values</a></strong><br />
106 </dt>
107 <dd>
108 <strong>MIN, MAX</strong>
109 </dd>
110 <dd>
111 <p>Pops two elements from the stack and returns the smaller or larger,
112 respectively.  Note that <em>infinite</em> is larger than anything else.
113 If one of the input numbers is <em>unknown</em> then the result of the operation will be
114 <em>unknown</em> too.</p>
115 </dd>
116 <dd>
117 <p><strong>LIMIT</strong></p>
118 </dd>
119 <dd>
120 <p>Pops two elements from the stack and uses them to define a range.
121 Then it pops another element and if it falls inside the range, it
122 is pushed back. If not, an <em>unknown</em> is pushed.</p>
123 </dd>
124 <dd>
125 <p>The range defined includes the two boundaries (so: a number equal
126 to one of the boundaries will be pushed back). If any of the three
127 numbers involved is either <em>unknown</em> or <em>infinite</em> this function
128 will always return an <em>unknown</em></p>
129 </dd>
130 <dd>
131 <p>Example: <code>CDEF:a=alpha,0,100,LIMIT</code> will return <em>unknown</em> if
132 alpha is lower than 0 or if it is higher than 100.</p>
133 </dd>
134 <dd>
135 <p></p>
136 </dd>
137 <p></p>
138 <dt><strong><a name="item_arithmetics">Arithmetics</a></strong><br />
139 </dt>
140 <dd>
141 <strong>+, -, *, /, %</strong>
142 </dd>
143 <dd>
144 <p>Add, subtract, multiply, divide, modulo</p>
145 </dd>
146 <dd>
147 <p><strong>ADDNAN</strong></p>
148 </dd>
149 <dd>
150 <p>NAN-safe addition. If one parameter is NAN/UNKNOWN it'll be treated as
151 zero. If both parameters are NAN/UNKNOWN, NAN/UNKNOWN will be returned.</p>
152 </dd>
153 <dd>
154 <p><strong>SIN, COS, LOG, EXP, SQRT</strong></p>
155 </dd>
156 <dd>
157 <p>Sine and cosine (input in radians), log and exp (natural logarithm),
158 square root.</p>
159 </dd>
160 <dd>
161 <p><strong>ATAN</strong></p>
162 </dd>
163 <dd>
164 <p>Arctangent (output in radians).</p>
165 </dd>
166 <dd>
167 <p><strong>ATAN2</strong></p>
168 </dd>
169 <dd>
170 <p>Arctangent of y,x components (output in radians).
171 This pops one element from the stack, the x (cosine) component, and then
172 a second, which is the y (sine) component.
173 It then pushes the arctangent of their ratio, resolving the ambiguity between
174 quadrants.</p>
175 </dd>
176 <dd>
177 <p>Example: <code>CDEF:angle=Y,X,ATAN2,RAD2DEG</code> will convert <code>X,Y</code>
178 components into an angle in degrees.</p>
179 </dd>
180 <dd>
181 <p><strong>FLOOR, CEIL</strong></p>
182 </dd>
183 <dd>
184 <p>Round down or up to the nearest integer.</p>
185 </dd>
186 <dd>
187 <p><strong>DEG2RAD, RAD2DEG</strong></p>
188 </dd>
189 <dd>
190 <p>Convert angle in degrees to radians, or radians to degrees.</p>
191 </dd>
192 <dd>
193 <p><strong>ABS</strong></p>
194 </dd>
195 <dd>
196 <p>Take the absolute value.</p>
197 </dd>
198 <p></p>
199 <dt><strong><a name="item_set_operations">Set Operations</a></strong><br />
200 </dt>
201 <dd>
202 <strong>SORT, REV</strong>
203 </dd>
204 <dd>
205 <p>Pop one element from the stack.  This is the <em>count</em> of items to be sorted
206 (or reversed).  The top <em>count</em> of the remaining elements are then sorted
207 (or reversed) in place on the stack.</p>
208 </dd>
209 <dd>
210 <p>Example: <code>CDEF:x=v1,v2,v3,v4,v5,v6,6,SORT,POP,5,REV,POP,+,+,+,4,/</code> will
211 compute the average of the values v1 to v6 after removing the smallest and
212 largest.</p>
213 </dd>
214 <dd>
215 <p><strong>AVG</strong></p>
216 </dd>
217 <dd>
218 <p>Pop one element (<em>count</em>) from the stack. Now pop <em>count</em> elements and build the
219 average, ignoring all UNKNOWN values in the process.</p>
220 </dd>
221 <dd>
222 <p>Example: <code>CDEF:x=a,b,c,d,4,AVG</code></p>
223 </dd>
224 <dd>
225 <p><strong>TREND, TRENDNAN</strong></p>
226 </dd>
227 <dd>
228 <p>Create a ``sliding window'' average of another data series.</p>
229 </dd>
230 <dd>
231 <p>Usage:
232 CDEF:smoothed=x,1800,TREND</p>
233 </dd>
234 <dd>
235 <p>This will create a half-hour (1800 second) sliding window average of x.  The
236 average is essentially computed as shown here:</p>
237 </dd>
238 <dd>
239 <pre>
240                  +---!---!---!---!---!---!---!---!---&gt;
241                                                      now
242                        delay     t0
243                  &lt;---------------&gt;
244                          delay       t1
245                      &lt;---------------&gt;
246                               delay      t2
247                          &lt;---------------&gt;</pre>
248 </dd>
249 <dd>
250 <pre>
251      Value at sample (t0) will be the average between (t0-delay) and (t0)
252      Value at sample (t1) will be the average between (t1-delay) and (t1)
253      Value at sample (t2) will be the average between (t2-delay) and (t2)</pre>
254 </dd>
255 <dd>
256 <p>TRENDNAN is - in contrast to TREND - NAN-safe. If you use TREND and one 
257 source value is NAN the complete sliding window is affected. The TRENDNAN 
258 operation ignores all NAN-values in a sliding window and computes the 
259 average of the remaining values.</p>
260 </dd>
261 <dd>
262 <p><strong>PREDICT, PREDICTSIGMA</strong></p>
263 </dd>
264 <dd>
265 <p>Create a ``sliding window'' average/sigma of another data series, that also
266 shifts the data series by given amounts of of time as well</p>
267 </dd>
268 <dd>
269 <p>Usage - explicit stating shifts:
270 CDEF:predict=&lt;shift n&gt;,...,&lt;shift 1&gt;,n,&lt;window&gt;,x,PREDICT
271 CDEF:sigma=&lt;shift n&gt;,...,&lt;shift 1&gt;,n,&lt;window&gt;,x,PREDICTSIGMA</p>
272 </dd>
273 <dd>
274 <p>Usage - shifts defined as a base shift and a number of time this is applied
275 CDEF:predict=&lt;shift multiplier&gt;,-n,&lt;window&gt;,x,PREDICT
276 CDEF:sigma=&lt;shift multiplier&gt;,-n,&lt;window&gt;,x,PREDICTSIGMA</p>
277 </dd>
278 <dd>
279 <p>Example:
280 CDEF:predict=172800,86400,2,1800,x,PREDICT</p>
281 </dd>
282 <dd>
283 <p>This will create a half-hour (1800 second) sliding window average/sigma of x, that
284 average is essentially computed as shown here:</p>
285 </dd>
286 <dd>
287 <pre>
288  +---!---!---!---!---!---!---!---!---!---!---!---!---!---!---!---!---!---&gt;
289                                                                      now
290                                                   shift 1        t0
291                                          &lt;-----------------------&gt;
292                                window
293                          &lt;---------------&gt;
294                                        shift 2
295                  &lt;-----------------------------------------------&gt;
296        window
297  &lt;---------------&gt;
298                                                       shift 1        t1
299                                              &lt;-----------------------&gt;
300                                    window
301                              &lt;---------------&gt;
302                                             shift 2
303                      &lt;-----------------------------------------------&gt;
304            window
305      &lt;---------------&gt;</pre>
306 </dd>
307 <dd>
308 <pre>
309  Value at sample (t0) will be the average between (t0-shift1-window) and (t0-shift1)
310                                       and between (t0-shift2-window) and (t0-shift2)
311  Value at sample (t1) will be the average between (t1-shift1-window) and (t1-shift1)
312                                       and between (t1-shift2-window) and (t1-shift2)</pre>
313 </dd>
314 <dd>
315 <p>The function is by design NAN-safe. 
316 This also allows for extrapolation into the future (say a few days)
317 - you may need to define the data series whit the optional start= parameter, so that 
318 the source data series has enough data to provide prediction also at the beginning of a graph...</p>
319 </dd>
320 <dd>
321 <p>Here an example, that will create a 10 day graph that also shows the 
322 prediction 3 days into the future with its uncertainty value (as defined by avg+-4*sigma)
323 This also shows if the prediction is exceeded at a certain point.</p>
324 </dd>
325 <dd>
326 <p>rrdtool graph image.png --imgformat=PNG \
327  --start=-7days --end=+3days --width=1000 --height=200 --alt-autoscale-max \
328  DEF:value=value.rrd:value:AVERAGE:start=-14days \
329  LINE1:value#ff0000:value \
330  CDEF:predict=86400,-7,1800,value,PREDICT \
331  CDEF:sigma=86400,-7,1800,value,PREDICTSIGMA \
332  CDEF:upper=predict,sigma,3,*,+ \
333  CDEF:lower=predict,sigma,3,*,- \
334  LINE1:predict#00ff00:prediction \
335  LINE1:upper#0000ff:upper\ certainty\ limit \
336  LINE1:lower#0000ff:lower\ certainty\ limit \
337  CDEF:exceeds=value,UN,0,value,lower,upper,LIMIT,UN,IF \
338  TICK:exceeds#aa000080:1</p>
339 </dd>
340 <dd>
341 <p>Note: Experience has shown that a factor between 3 and 5 to scale sigma is a good 
342 discriminator to detect abnormal behaviour. This obviously depends also on the type 
343 of data and how ``noisy'' the data series is.</p>
344 </dd>
345 <dd>
346 <p>This prediction can only be used for short term extrapolations - say a few days into the future-</p>
347 </dd>
348 <p></p>
349 <dt><strong><a name="item_special_values">Special values</a></strong><br />
350 </dt>
351 <dd>
352 <strong>UNKN</strong>
353 </dd>
354 <dd>
355 <p>Pushes an unknown value on the stack</p>
356 </dd>
357 <dd>
358 <p><strong>INF, NEGINF</strong></p>
359 </dd>
360 <dd>
361 <p>Pushes a positive or negative infinite value on the stack. When
362 such a value is graphed, it appears at the top or bottom of the
363 graph, no matter what the actual value on the y-axis is.</p>
364 </dd>
365 <dd>
366 <p><strong>PREV</strong></p>
367 </dd>
368 <dd>
369 <p>Pushes an <em>unknown</em> value if this is the first value of a data
370 set or otherwise the result of this <strong>CDEF</strong> at the previous time
371 step. This allows you to do calculations across the data.  This
372 function cannot be used in <strong>VDEF</strong> instructions.</p>
373 </dd>
374 <dd>
375 <p><strong>PREV(vname)</strong></p>
376 </dd>
377 <dd>
378 <p>Pushes an <em>unknown</em> value if this is the first value of a data
379 set or otherwise the result of the vname variable at the previous time
380 step. This allows you to do calculations across the data. This
381 function cannot be used in <strong>VDEF</strong> instructions.</p>
382 </dd>
383 <dd>
384 <p><strong>COUNT</strong></p>
385 </dd>
386 <dd>
387 <p>Pushes the number 1 if this is the first value of the data set, the
388 number 2 if it is the second, and so on. This special value allows
389 you to make calculations based on the position of the value within
390 the data set. This function cannot be used in <strong>VDEF</strong> instructions.</p>
391 </dd>
392 <p></p>
393 <dt><strong><a name="item_time">Time</a></strong><br />
394 </dt>
395 <dd>
396 Time inside RRDtool is measured in seconds since the epoch. The
397 epoch is defined to be <code>Thu&nbsp;Jan&nbsp;&nbsp;1&nbsp;00:00:00&nbsp;UTC&nbsp;1970</code>.
398 </dd>
399 <dd>
400 <p><strong>NOW</strong></p>
401 </dd>
402 <dd>
403 <p>Pushes the current time on the stack.</p>
404 </dd>
405 <dd>
406 <p><strong>TIME</strong></p>
407 </dd>
408 <dd>
409 <p>Pushes the time the currently processed value was taken at onto the stack.</p>
410 </dd>
411 <dd>
412 <p><strong>LTIME</strong></p>
413 </dd>
414 <dd>
415 <p>Takes the time as defined by <strong>TIME</strong>, applies the time zone offset
416 valid at that time including daylight saving time if your OS supports
417 it, and pushes the result on the stack.  There is an elaborate example
418 in the examples section below on how to use this.</p>
419 </dd>
420 <p></p>
421 <dt><strong><a name="item_processing_the_stack_directly">Processing the stack directly</a></strong><br />
422 </dt>
423 <dd>
424 <strong>DUP, POP, EXC</strong>
425 </dd>
426 <dd>
427 <p>Duplicate the top element, remove the top element, exchange the two
428 top elements.</p>
429 </dd>
430 <dd>
431 <p></p>
432 </dd>
433 <p></p></dl>
434 <p>
435 </p>
436 <hr />
437 <h1><a name="variables">VARIABLES</a></h1>
438 <p>These operators work only on <strong>VDEF</strong> statements. Note that currently ONLY these work for <strong>VDEF</strong>.</p>
439 <dl>
440 <dt><strong><a name="item_maximum_2c_minimum_2c_average">MAXIMUM, MINIMUM, AVERAGE</a></strong><br />
441 </dt>
442 <dd>
443 Return the corresponding value, MAXIMUM and MINIMUM also return
444 the first occurrence of that value in the time component.
445 </dd>
446 <dd>
447 <p>Example: <code>VDEF:avg=mydata,AVERAGE</code></p>
448 </dd>
449 <p></p>
450 <dt><strong><a name="item_stdev">STDEV</a></strong><br />
451 </dt>
452 <dd>
453 Returns the standard deviation of the values.
454 </dd>
455 <dd>
456 <p>Example: <code>VDEF:stdev=mydata,STDEV</code></p>
457 </dd>
458 <p></p>
459 <dt><strong><a name="item_last_2c_first">LAST, FIRST</a></strong><br />
460 </dt>
461 <dd>
462 Return the last/first value including its time.  The time for
463 FIRST is actually the start of the corresponding interval, whereas
464 LAST returns the end of the corresponding interval.
465 </dd>
466 <dd>
467 <p>Example: <code>VDEF:first=mydata,FIRST</code></p>
468 </dd>
469 <p></p>
470 <dt><strong><a name="item_total">TOTAL</a></strong><br />
471 </dt>
472 <dd>
473 Returns the rate from each defined time slot multiplied with the
474 step size.  This can, for instance, return total bytes transfered
475 when you have logged bytes per second. The time component returns
476 the number of seconds.
477 </dd>
478 <dd>
479 <p>Example: <code>VDEF:total=mydata,TOTAL</code></p>
480 </dd>
481 <p></p>
482 <dt><strong><a name="item_percent_2c_percentnan">PERCENT, PERCENTNAN</a></strong><br />
483 </dt>
484 <dd>
485 This should follow a <strong>DEF</strong> or <strong>CDEF</strong> <em>vname</em>. The <em>vname</em> is popped,
486 another number is popped which is a certain percentage (0..100). The
487 data set is then sorted and the value returned is chosen such that
488 <em>percentage</em> percent of the values is lower or equal than the result.
489 For PERCENTNAN <em>Unknown</em> values are ignored, but for PERCENT
490 <em>Unknown</em> values are considered lower than any finite number for this
491 purpose so if this operator returns an <em>unknown</em> you have quite a lot
492 of them in your data.  <strong>Inf</strong>inite numbers are lesser, or more, than the
493 finite numbers and are always more than the <em>Unknown</em> numbers.
494 (NaN &lt; -INF &lt; finite values &lt; INF)
495 </dd>
496 <dd>
497 <p>Example: <code>VDEF:perc95=mydata,95,PERCENT</code>
498          <code>VDEF:percnan95=mydata,95,PERCENTNAN</code></p>
499 </dd>
500 <p></p>
501 <dt><strong><a name="item_lslslope_2c_lslint_2c_lslcorrel">LSLSLOPE, LSLINT, LSLCORREL</a></strong><br />
502 </dt>
503 <dd>
504 Return the parameters for a <strong>L</strong>east <strong>S</strong>quares <strong>L</strong>ine <em>(y = mx +b)</em> 
505 which approximate the provided dataset.  LSLSLOPE is the slope <em>(m)</em> of
506 the line related to the COUNT position of the data.  LSLINT is the 
507 y-intercept <em>(b)</em>, which happens also to be the first data point on the 
508 graph. LSLCORREL is the Correlation Coefficient (also know as Pearson's 
509 Product Moment Correlation Coefficient).  It will range from 0 to +/-1 
510 and represents the quality of fit for the approximation.
511 </dd>
512 <dd>
513 <p>Example: <code>VDEF:slope=mydata,LSLSLOPE</code></p>
514 </dd>
515 <p></p></dl>
516 <p>
517 </p>
518 <hr />
519 <h1><a name="see_also">SEE ALSO</a></h1>
520 <p><a href="././rrdgraph.html">the rrdgraph manpage</a> gives an overview of how <strong>rrdtool graph</strong> works.
521 <a href="././rrdgraph_data.html">the rrdgraph_data manpage</a> describes <strong>DEF</strong>,<strong>CDEF</strong> and <strong>VDEF</strong> in detail.
522 <a href="././rrdgraph_rpn.html">the rrdgraph_rpn manpage</a> describes the <strong>RPN</strong> language used in the <strong>?DEF</strong> statements.
523 <a href="././rrdgraph_graph.html">the rrdgraph_graph manpage</a> page describes all of the graph and print functions.</p>
524 <p>Make sure to read <a href="././rrdgraph_examples.html">the rrdgraph_examples manpage</a> for tips&amp;tricks.</p>
525 <p>
526 </p>
527 <hr />
528 <h1><a name="author">AUTHOR</a></h1>
529 <p>Program by Tobias Oetiker &lt;<a href="mailto:tobi@oetiker.ch">tobi@oetiker.ch</a>&gt;</p>
530 <p>This manual page by Alex van den Bogaerdt &lt;<a href="mailto:alex@vandenbogaerdt.nl">alex@vandenbogaerdt.nl</a>&gt;
531 with corrections and/or additions by several people</p>
533 </body>
535 </html>