Domanda

I'm playing with RRDTool, but it shows wrong values. I have little python script:

import sys
import rrdtool
import time

i = 0

rrdtool.create(
    'tempo.rrd',
    '--step', '10',
    'DS:temp:GAUGE:20:-40:100',
    'RRA:LAST:0.5:1:1500'
)

while 1:
    ret = rrdtool.update('tempo.rrd','N:' + `i`);
    print "i %i" % i

    rrdtool.graph(
    'test.png',
    '--imgformat', 'PNG',
    '--width', '540',
    '--height', '200',
    '--start', "-%i" % 60,
    '--end', "-1",
    '--vertical-label', 'Temperatura',
    '--title', 'Temperatura lauke',
    '--lower-limit', '-1',
    'DEF:actualtemp=tempo.rrd:temp:LAST',
    'LINE1:actualtemp#ff0000:Actual',
    'GPRINT:actualtemp:LAST:Actual %0.1lf C'
    )   

    i += 1

    time.sleep(10)

After inserting [0, 1, 2], I get graph with wrong values - http://i.imgur.com/rfWWDMm.png (sorry, I can't post images). As you see, after inserting 0, graph shows 0, after inserting 1, graph shows 0.8 and after inserting 2, graph shows 1.8. Sometimes after inserting 1, graph shows 0.6 and so on. Am I doing something wrong?

È stato utile?

Soluzione

This is how RRDtool works. RRDtool works with rates, exclusively. You can input gauge data (discrete values in time) but RRDtool will always treat them internally as rates.

When you created your RRD file (tempo.rrd), internally RRDtool created buckets with a starting timestamp at creation time and each subsequent bucket +10s from that timestamp. For example

bucket 1    - 1379713706
bucket 2    - 1379713716
bucket 3    - 1379713726
...
bucket 100  - 1379714706
bucket 101  - 1379714716
bucket 102  - 1379714726

If you were to insert your integer values at exactly the timestamps matching the buckets, you'd be ok but you're not. Your script is inserting values using the current timestamp which is almost certainly not going to be equal to a bucket value. Hypothetically, lets say current timestamp is 1379714708 and you want to insert a value of 2. When you insert your value, RRDtool needs to choose which bucket to put it in. In this case 1379714706 is the nearest so it will choose that one (there's a bit more logic here but that's the gist). You might think it would insert '2' into the bucket, but to RRDtool, that would be a lie. It might be 2 now, but it probably wasn't 2 a few seconds ago. Bearing in mind that it sees all these values as rates, it tries to figure out how much it should subtract from that value to make it right by looking at the rate of change of previous values That's why you see values such as 1.8 and 2.8 and not the integer values you expect. Things get more complicate if you insert multiple values between buckets or skip buckets.

There's an excellent tutorial at http://oss.oetiker.ch/rrdtool/tut/rrdtutorial.en.html that goes into more detail.

Altri suggerimenti

I don't know to what extent this will match your needs, but this is how I trick my graphs to display a discrete number of users connected to my system: I use the CEIL arithmetic operator. It is not meant to be accurate, only to be more satisfying to the eye than 0.324 online user.

Based on what the tool I use to manipulate the rrds spits out as a command line, I expect your code to look like

rrdtool.graph(
'test.png',
'--imgformat', 'PNG',
'--width', '540',
'--height', '200',
'--start', "-%i" % 60,
'--end', "-1",
'--vertical-label', 'Temperatura',
'--title', 'Temperatura lauke',
'--lower-limit', '-1',
'DEF:actualtemp=tempo.rrd:temp:LAST',
'CDEF:ACTUALTEMP=actualtemp,CEIL',
'LINE1:ACTUALTEMP#ff0000:Actual',
'GPRINT:ACTUALTEMP:LAST:Actual %0.1lf C'
)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top