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