[rrd-users] Re: Multiple, similar calls to rrd_fetch() returns different data sets

Martin Fejrskov Pedersen mfpe01 at kom.aau.dk
Sat May 29 17:28:13 MEST 2004


Onsdag den 19. maj 2004 14:49 skrev Martin Fejrskov Pedersen:
> Hi!
>  I'm using the rrd_fetch C API to fetch data from an RRD archive. My
> problem is, that similar, subsequent calls to rrd_fetch return different
> results using the following code snippet:
>
>   time_t        start,end;
>   rrd_value_t   *datatmp;
>   unsigned long step, ds_cnt,i;
>   char          **ds_namv;
>   char          parameter[7][255];
>   char          *parampointer[7];
>   strcpy(parampointer[0]=parameter[0],"fetch");
>   strcpy(parampointer[1]=parameter[1],"--start");
>   strcpy(parampointer[2]=parameter[2],"1084961940");
>   strcpy(parampointer[3]=parameter[3],"--end");
>   strcpy(parampointer[4]=parameter[4],"1084962480");
>   sprintf(parampointer[5]=parameter[5],"%s",rrdfilename);
>   strcpy(parampointer[6]=parameter[6],"AVERAGE");
>   if(rrd_fetch(7, (char
> **)parampointer,&start,&end,&step,&ds_cnt,&ds_namv,&datatmp) != -1)
>
> The first time rrd_fetch is called, the data between the described
> timestamps are returned correctly. The second time rrd_fethc is called,
> the data from an entire day is returned. Furthermore, the timestamps are
> completely ignored, because it is the data between "now" and 24 hours
> back in time.
>
> Is this a bug or is it intentional? If the latter, how do I make it
> behave properly? Btw. this bug is reproducible using the code from
> rrdtool.c....
>
> Thanks in advance!
>
>
> Martin Fejrskov
> Student at Aalborg University, Denmark
>
> --
> Unsubscribe mailto:rrd-users-request at list.ee.ethz.ch?subject=unsubscribe
> Help        mailto:rrd-users-request at list.ee.ethz.ch?subject=help
> Archive     http://www.ee.ethz.ch/~slist/rrd-users
> WebAdmin    http://www.ee.ethz.ch/~slist/lsg2.cgi

I found the reason and the solution. In the following I will try to explain my 
findings so that a newcomer should be able to see though this problem:

The reason that it does not work is that during the parsing of the options to 
rrd_fetch(), the GNU getopt() functions is used. Getopt() is called several 
times within rrd_fetch() to return one option at a time, and getopt keeps a 
static "pointer" to the next option to be returned. When all the options are 
returned to rrd_fetch, this pointer doesn't reset itself. So the next time, 
rrd_fetch is called, this pointer still points to the last option, which 
creates the illusion to rrd_fetch, that the arguments are illegal, and thus 
the default values for "start" and "end" time are used.

The solution to this problem is of course to reset the pointer before each use 
of rrd_fetch(). This is done by adding the following lines to your code in 
appropriate places:
extern int optind;
optind=0;

Why rrd_fetch() doesn't do this, I don't understand, but since this problem 
has been known for a long time, there's probably a reason for it.... :-)

Martin Fejrskov
Aalborg University, Denmark

--
Unsubscribe mailto:rrd-users-request at list.ee.ethz.ch?subject=unsubscribe
Help        mailto:rrd-users-request at list.ee.ethz.ch?subject=help
Archive     http://www.ee.ethz.ch/~slist/rrd-users
WebAdmin    http://www.ee.ethz.ch/~slist/lsg2.cgi



More information about the rrd-users mailing list