Code

control: Updated standards-version to 3.9.5 -- no changes.
[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 result in <em>unknown</em> returned ... which will also be
87 treated as false by the <strong>IF</strong> call.</p>
88 <p><strong>UN, ISINF</strong></p>
89 <p>Pop one element from the stack, compare this to <em>unknown</em> respectively
90 to <em>positive or negative infinity</em>. Returns 1 for true or 0 for false.</p>
91 <p><strong>IF</strong></p>
92 <p>Pops three elements from the stack.  If the element popped last is 0
93 (false), the value popped first is pushed back onto the stack,
94 otherwise the value popped second is pushed back. This does, indeed,
95 mean that any value other than 0 is considered to be true.</p>
96 <p>Example: <code>A,B,C,IF</code> should be read as <code>if (A) then (B) else (C)</code></p>
97 <p></p>
98 </dd>
99 <dt><strong><a name="comparing_values" class="item">Comparing values</a></strong></dt>
101 <dd>
102 <p><strong>MIN, MAX</strong></p>
103 <p>Pops two elements from the stack and returns the smaller or larger,
104 respectively.  Note that <em>infinite</em> is larger than anything else.
105 If one of the input numbers is <em>unknown</em> then the result of the operation will be
106 <em>unknown</em> too.</p>
107 <p><strong>LIMIT</strong></p>
108 <p>Pops two elements from the stack and uses them to define a range.
109 Then it pops another element and if it falls inside the range, it
110 is pushed back. If not, an <em>unknown</em> is pushed.</p>
111 <p>The range defined includes the two boundaries (so: a number equal
112 to one of the boundaries will be pushed back). If any of the three
113 numbers involved is either <em>unknown</em> or <em>infinite</em> this function
114 will always return an <em>unknown</em></p>
115 <p>Example: <code>CDEF:a=alpha,0,100,LIMIT</code> will return <em>unknown</em> if
116 alpha is lower than 0 or if it is higher than 100.</p>
117 <p></p>
118 </dd>
119 <dt><strong><a name="arithmetics" class="item">Arithmetics</a></strong></dt>
121 <dd>
122 <p><strong>+, -, *, /, %</strong></p>
123 <p>Add, subtract, multiply, divide, modulo</p>
124 <p><strong>ADDNAN</strong></p>
125 <p>NAN-safe addition. If one parameter is NAN/UNKNOWN it'll be treated as
126 zero. If both parameters are NAN/UNKNOWN, NAN/UNKNOWN will be returned.</p>
127 <p><strong>SIN, COS, LOG, EXP, SQRT</strong></p>
128 <p>Sine and cosine (input in radians), log and exp (natural logarithm),
129 square root.</p>
130 <p><strong>ATAN</strong></p>
131 <p>Arctangent (output in radians).</p>
132 <p><strong>ATAN2</strong></p>
133 <p>Arctangent of y,x components (output in radians).
134 This pops one element from the stack, the x (cosine) component, and then
135 a second, which is the y (sine) component.
136 It then pushes the arctangent of their ratio, resolving the ambiguity between
137 quadrants.</p>
138 <p>Example: <code>CDEF:angle=Y,X,ATAN2,RAD2DEG</code> will convert <code>X,Y</code>
139 components into an angle in degrees.</p>
140 <p><strong>FLOOR, CEIL</strong></p>
141 <p>Round down or up to the nearest integer.</p>
142 <p><strong>DEG2RAD, RAD2DEG</strong></p>
143 <p>Convert angle in degrees to radians, or radians to degrees.</p>
144 <p><strong>ABS</strong></p>
145 <p>Take the absolute value.</p>
146 </dd>
147 <dt><strong><a name="set_operations" class="item">Set Operations</a></strong></dt>
149 <dd>
150 <p><strong>SORT, REV</strong></p>
151 <p>Pop one element from the stack.  This is the <em>count</em> of items to be sorted
152 (or reversed).  The top <em>count</em> of the remaining elements are then sorted
153 (or reversed) in place on the stack.</p>
154 <p>Example: <code>CDEF:x=v1,v2,v3,v4,v5,v6,6,SORT,POP,5,REV,POP,+,+,+,4,/</code> will
155 compute the average of the values v1 to v6 after removing the smallest and
156 largest.</p>
157 <p><strong>AVG</strong></p>
158 <p>Pop one element (<em>count</em>) from the stack. Now pop <em>count</em> elements and build the
159 average, ignoring all UNKNOWN values in the process.</p>
160 <p>Example: <code>CDEF:x=a,b,c,d,4,AVG</code></p>
161 <p><strong>TREND, TRENDNAN</strong></p>
162 <p>Create a &quot;sliding window&quot; average of another data series.</p>
163 <p>Usage:
164 CDEF:smoothed=x,1800,TREND</p>
165 <p>This will create a half-hour (1800 second) sliding window average of x.  The
166 average is essentially computed as shown here:</p>
167 <pre>
168                  +---!---!---!---!---!---!---!---!---&gt;
169                                                      now
170                        delay     t0
171                  &lt;---------------&gt;
172                          delay       t1
173                      &lt;---------------&gt;
174                               delay      t2
175                          &lt;---------------&gt;</pre>
176 <pre>
177      Value at sample (t0) will be the average between (t0-delay) and (t0)
178      Value at sample (t1) will be the average between (t1-delay) and (t1)
179      Value at sample (t2) will be the average between (t2-delay) and (t2)</pre>
180 <p>TRENDNAN is - in contrast to TREND - NAN-safe. If you use TREND and one 
181 source value is NAN the complete sliding window is affected. The TRENDNAN 
182 operation ignores all NAN-values in a sliding window and computes the 
183 average of the remaining values.</p>
184 <p><strong>PREDICT, PREDICTSIGMA</strong></p>
185 <p>Create a &quot;sliding window&quot; average/sigma of another data series, that also
186 shifts the data series by given amounts of of time as well</p>
187 <p>Usage - explicit stating shifts:
188 CDEF:predict=&lt;shift n&gt;,...,&lt;shift 1&gt;,n,&lt;window&gt;,x,PREDICT
189 CDEF:sigma=&lt;shift n&gt;,...,&lt;shift 1&gt;,n,&lt;window&gt;,x,PREDICTSIGMA</p>
190 <p>Usage - shifts defined as a base shift and a number of time this is applied
191 CDEF:predict=&lt;shift multiplier&gt;,-n,&lt;window&gt;,x,PREDICT
192 CDEF:sigma=&lt;shift multiplier&gt;,-n,&lt;window&gt;,x,PREDICTSIGMA</p>
193 <p>Example:
194 CDEF:predict=172800,86400,2,1800,x,PREDICT</p>
195 <p>This will create a half-hour (1800 second) sliding window average/sigma of x, that
196 average is essentially computed as shown here:</p>
197 <pre>
198  +---!---!---!---!---!---!---!---!---!---!---!---!---!---!---!---!---!---&gt;
199                                                                      now
200                                                   shift 1        t0
201                                          &lt;-----------------------&gt;
202                                window
203                          &lt;---------------&gt;
204                                        shift 2
205                  &lt;-----------------------------------------------&gt;
206        window
207  &lt;---------------&gt;
208                                                       shift 1        t1
209                                              &lt;-----------------------&gt;
210                                    window
211                              &lt;---------------&gt;
212                                             shift 2
213                      &lt;-----------------------------------------------&gt;
214            window
215      &lt;---------------&gt;</pre>
216 <pre>
217  Value at sample (t0) will be the average between (t0-shift1-window) and (t0-shift1)
218                                       and between (t0-shift2-window) and (t0-shift2)
219  Value at sample (t1) will be the average between (t1-shift1-window) and (t1-shift1)
220                                       and between (t1-shift2-window) and (t1-shift2)</pre>
221 <p>The function is by design NAN-safe. 
222 This also allows for extrapolation into the future (say a few days)
223 - you may need to define the data series whit the optional start= parameter, so that 
224 the source data series has enough data to provide prediction also at the beginning of a graph...</p>
225 <p>Here an example, that will create a 10 day graph that also shows the 
226 prediction 3 days into the future with its uncertainty value (as defined by avg+-4*sigma)
227 This also shows if the prediction is exceeded at a certain point.</p>
228 <p>rrdtool graph image.png --imgformat=PNG \
229  --start=-7days --end=+3days --width=1000 --height=200 --alt-autoscale-max \
230  DEF:value=value.rrd:value:AVERAGE:start=-14days \
231  LINE1:value#ff0000:value \
232  CDEF:predict=86400,-7,1800,value,PREDICT \
233  CDEF:sigma=86400,-7,1800,value,PREDICTSIGMA \
234  CDEF:upper=predict,sigma,3,*,+ \
235  CDEF:lower=predict,sigma,3,*,- \
236  LINE1:predict#00ff00:prediction \
237  LINE1:upper#0000ff:upper\ certainty\ limit \
238  LINE1:lower#0000ff:lower\ certainty\ limit \
239  CDEF:exceeds=value,UN,0,value,lower,upper,LIMIT,UN,IF \
240  TICK:exceeds#aa000080:1</p>
241 <p>Note: Experience has shown that a factor between 3 and 5 to scale sigma is a good 
242 discriminator to detect abnormal behavior. This obviously depends also on the type 
243 of data and how &quot;noisy&quot; the data series is.</p>
244 <p>This prediction can only be used for short term extrapolations - say a few days into the future-</p>
245 </dd>
246 <dt><strong><a name="special_values" class="item">Special values</a></strong></dt>
248 <dd>
249 <p><strong>UNKN</strong></p>
250 <p>Pushes an unknown value on the stack</p>
251 <p><strong>INF, NEGINF</strong></p>
252 <p>Pushes a positive or negative infinite value on the stack. When
253 such a value is graphed, it appears at the top or bottom of the
254 graph, no matter what the actual value on the y-axis is.</p>
255 <p><strong>PREV</strong></p>
256 <p>Pushes an <em>unknown</em> value if this is the first value of a data
257 set or otherwise the result of this <strong>CDEF</strong> at the previous time
258 step. This allows you to do calculations across the data.  This
259 function cannot be used in <strong>VDEF</strong> instructions.</p>
260 <p><strong>PREV(vname)</strong></p>
261 <p>Pushes an <em>unknown</em> value if this is the first value of a data
262 set or otherwise the result of the vname variable at the previous time
263 step. This allows you to do calculations across the data. This
264 function cannot be used in <strong>VDEF</strong> instructions.</p>
265 <p><strong>COUNT</strong></p>
266 <p>Pushes the number 1 if this is the first value of the data set, the
267 number 2 if it is the second, and so on. This special value allows
268 you to make calculations based on the position of the value within
269 the data set. This function cannot be used in <strong>VDEF</strong> instructions.</p>
270 </dd>
271 <dt><strong><a name="time" class="item">Time</a></strong></dt>
273 <dd>
274 <p>Time inside RRDtool is measured in seconds since the epoch. The
275 epoch is defined to be <code>Thu&nbsp;Jan&nbsp;&nbsp;1&nbsp;00:00:00&nbsp;UTC&nbsp;1970</code>.</p>
276 <p><strong>NOW</strong></p>
277 <p>Pushes the current time on the stack.</p>
278 <p><strong>TIME</strong></p>
279 <p>Pushes the time the currently processed value was taken at onto the stack.</p>
280 <p><strong>LTIME</strong></p>
281 <p>Takes the time as defined by <strong>TIME</strong>, applies the time zone offset
282 valid at that time including daylight saving time if your OS supports
283 it, and pushes the result on the stack.  There is an elaborate example
284 in the examples section below on how to use this.</p>
285 </dd>
286 <dt><strong><a name="processing_the_stack_directly" class="item">Processing the stack directly</a></strong></dt>
288 <dd>
289 <p><strong>DUP, POP, EXC</strong></p>
290 <p>Duplicate the top element, remove the top element, exchange the two
291 top elements.</p>
292 <p></p>
293 </dd>
294 </dl>
295 <p>
296 </p>
297 <hr />
298 <h1><a name="variables">VARIABLES</a></h1>
299 <p>These operators work only on <strong>VDEF</strong> statements. Note that currently ONLY these work for <strong>VDEF</strong>.</p>
300 <dl>
301 <dt><strong><a name="maximum_minimum_average" class="item">MAXIMUM, MINIMUM, AVERAGE</a></strong></dt>
303 <dd>
304 <p>Return the corresponding value, MAXIMUM and MINIMUM also return
305 the first occurrence of that value in the time component.</p>
306 <p>Example: <code>VDEF:avg=mydata,AVERAGE</code></p>
307 </dd>
308 <dt><strong><a name="stdev" class="item">STDEV</a></strong></dt>
310 <dd>
311 <p>Returns the standard deviation of the values.</p>
312 <p>Example: <code>VDEF:stdev=mydata,STDEV</code></p>
313 </dd>
314 <dt><strong><a name="last_first" class="item">LAST, FIRST</a></strong></dt>
316 <dd>
317 <p>Return the last/first non-nan or infinite value for the selected data
318 stream, including its timestamp.</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 transferred
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>