Tuesday, January 5, 2010

rrdtool - every data is a rate!

This article is about understanding the rate calculating in the rrdtool.

I'm doing a SA project recently. We get the RTTs(round-trip time) by the ping command and write the RTT to a rrd file, print out the graph, ...

More information about the rrdtool can be found on their website.
http://oss.oetiker.ch/rrdtool/

I first created a rrd file:
rrdtool create F.rrd \
--step 10 \
DS:rtt1:GAUGE:20:0:U \
RRA:MAX:0.5:1:35712

Then, I updated some values to the F.rrd file. A interesting fact is that when I update the value at exact the time step (say 10, 20, 30, etc.), I'll have a accurate value.

# rrdtool update F 1262733290:3
# rrdtool update F 1262733300:4
# rrdtool update F 1262733310:5
# rrdtool update F 1262733320:6

1262733300: 4.0000000000e+00
1262733310: 5.0000000000e+00
1262733320: 6.0000000000e+00

OK, there is a thing called "heartbeat" in the rddtool, it helps the updates that occur at a not-accurate timestamps. For example, if we update a value at time 1262733331, the rrdtool will put the value to 1262733330. It's a nice feature, since there is always some delays in the networking world.

Let's take a look

# rrdtool update F 1262733331:7
# rrdtool update F 1262733340:8

1262733330: 7.0000000000e+00
1262733340: 7.9000000000e+00

As you can see, the value 7 is updated at 1262733331 but is shifted to 126273330 in the rrd file. But what happened with the next update at 1262733340? We update 8 but the rrd file recorded 7.9. Strange, right?

Here is how 7.9 comes out: since rrdtool treats every data as a rate, if there are more than one samples before a time step, rrdtool tries to calculate the average rate of all the samples before the time step (after the previous time step, of course).

previous time step: 1262733330
when we update 7 at 1262733331; 8 at 1262733340.
the calculation is
(7 * (1262733331 - 1262733330) +
8 * (1262733340 - 1262733331) ) / 10 = 7.9

Here is a figure to help understanding:


OK, Let's do the math with another example:

# rrdtool update F 1262733341:54
# rrdtool update F 1262733342:3
# rrdtool update F 1262733347:31
# rrdtool update F 1262733350:3

To find out the average value at 1262733350:
previous time step: 1262733340
(54 * (1262733341 - 1262733340) +
3 * (1262733342 - 1262733341) +
31 * (1262733347 - 1262733342) +
3 * (1262733350 - 1262733347)) / 10 = 22.1

Read the rrd file to verify our answer:
1262733350: 2.2100000000e+01

It's a perfect match. :-)

I hope this article can help someone who has the same question as I did.

No comments: