Code

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