Thursday, October 10, 2013

Simple moving average in RRD

It's quite easy to add simple moving average (SMA) to RRD with rrd PREV function.
PREV allows you to get data from previous time stamp. The magic is that you can run that function on sequences of variables to dig deeper in the past.

Here is SMA from 10 variables, current value and 9 from the past (php template for PNP).

$def[1] .= "DEF:idle=$rrdfile:$DS[3]:AVERAGE " ;

$def[1] .= "CDEF:idle1=PREV(idle), " ;
$def[1] .= "CDEF:idle2=PREV(idle1), " ;
$def[1] .= "CDEF:idle3=PREV(idle2), " ;
$def[1] .= "CDEF:idle4=PREV(idle3), " ;
$def[1] .= "CDEF:idle5=PREV(idle4), " ;
$def[1] .= "CDEF:idle6=PREV(idle5), " ;
$def[1] .= "CDEF:idle7=PREV(idle6), " ;
$def[1] .= "CDEF:idle8=PREV(idle7), " ;
$def[1] .= "CDEF:idle9=PREV(idle8), " ;
$def[1] .= "CDEF:idle_sma=idle,idle1,idle2,idle3,idle4,idle5,idle6,idle7,idle8,idle9,+,+,+,+,+,+,+,+,+,10,/ " ;


2 comments:

  1. Why not use the built-in TREND operator?

    If your data is recorded per minute, and you want the moving average of the past ten data points, that's the last 600 seconds:

    CDEF:10minaverage=idle,600,TREND

    ReplyDelete
    Replies
    1. You are right, TREND and PREDICT, they look great.
      Perhaps I didn't read the documentation carefully enough or these functions didn't exist 5 years ago.

      Delete