# [rrd-users] Calculation that is a bit over my head.

Simon Hobson linux at thehobsons.co.uk
Mon Nov 25 15:48:34 CET 2013

Jan Johansson wrote:
>Is that something I can build into the query? To do the arithmetics and be able to feed an arbitrary start/end into the query?
>
>Such as
>
>rrdtool graph /usr/share/nginx/www/test.png -s "20130101" -e "now" DEF:ds0=/opt/Krokopool/RRDDatabases/Styrning_Varmare.rrd:Styrning_Varmare:AVERAGE VDEF:ds1=ds0,AVERAGE PRINT:ds1:%20.2lf%s
>0x0
>              123.86m

Yes, lookup the RPN functions. Eg :

DEF:onpercent=/opt/Krokopool/RRDDatabases/Styrning_Varmare.rrd:Styrning_Varmare:AVERAGE
CDEF:ontime=onpercent,86400,*,3600,/
CDEF:totpower=ontime,3,*

This would give you "ontime" which is the total on time for the day in hours (this can be simplified to "onpercent,24,*" !), and "totpower" which would be the power consumed (I've assumed 3kW here) in kWH. It can also be better to do "totpower=onpercent,24,*,3,*" which is slightly more processing, but makes totpower independent of ontime - so less chance of making a mistake by changing one and fogetting to alter the other.
There is also (IIRC) the total function, so "VDEF:ontime,3,*,TOTAL" to get a single value that you can later [g]print.

Simply adjust to suit what you want out. In particular, I script all my graphs, so instead of using a hardcoded time period, I'll have the script insert a time period appropriate to the graph being drawn - I may use the same graph definition over several different time periods.

Also, I'd suggest not using terms like "now" and "now - 1 day". Better to specify start and end times which are integer multiples of the consolidated step period you intend to use. Eg, if you are working with a 300s copnsolidation, then start and end should be an integer multiple of 300; for 1/2 hour consolidations it would be 1800 and so on. And make sure that end-start = pixels*step time so that the data isn't further processed to fit your graph.
RRD will try to do the right thing, but you can reduce the scope for mistakes by being explicit.

More information about the rrd-users mailing list