fixed version checks to only complain if xml version is > than current RRD version
[rrdtool.git] / src / rrd_fetch.c
1 /*****************************************************************************
2  * RRDtool 1.1.x  Copyright Tobias Oetiker, 1997 - 2002
3  *****************************************************************************
4  * rrd_fetch.c  read date from an rrd to use for further processing
5  *****************************************************************************
6  * $Id$
7  * $Log$
8  * Revision 1.6  2003/01/16 23:27:54  oetiker
9  * fix border condition in rra selection of rrd_fetch
10  * -- Stanislav Sinyagin <ssinyagin@yahoo.com>
11  *
12  * Revision 1.5  2002/06/23 22:29:40  alex
13  * Added "step=1800" and such to "DEF"
14  * Cleaned some of the signed vs. unsigned problems
15  *
16  * Revision 1.4  2002/02/01 20:34:49  oetiker
17  * fixed version number and date/time
18  *
19  * Revision 1.3  2001/12/24 06:51:49  alex
20  * A patch of size 44Kbytes... in short:
21  *
22  * Found and repaired the off-by-one error in rrd_fetch_fn().
23  * As a result I had to remove the hacks in rrd_fetch_fn(),
24  * rrd_tool.c, vdef_calc(), data_calc(), data_proc() and
25  * reduce_data().  There may be other places which I didn't
26  * find so be careful.
27  *
28  * Enhanced debugging in rrd_fetch_fn(), it shows the RRA selection
29  * process.
30  *
31  * Added the ability to print VDEF timestamps.  At the moment it
32  * is a hack, I needed it now to fix the off-by-one error.
33  * If the format string is "%c" (and nothing else!), the time
34  * will be printed by both ctime() and as a long int.
35  *
36  * Moved some code around (slightly altering it) from rrd_graph()
37  *   initializing     now in rrd_graph_init()
38  *   options parsing  now in rrd_graph_options()
39  *   script parsing   now in rrd_graph_script()
40  *
41  * Revision 1.2  2001/12/17 12:48:43  oetiker
42  * fix overflow error ...
43  *
44  * Revision 1.1.1.1  2001/02/25 22:25:05  oetiker
45  * checkin
46  *
47  *****************************************************************************/
48
49 #include "rrd_tool.h"
50 /*#define DEBUG*/
51
52 int
53 rrd_fetch(int argc, 
54           char **argv,
55           time_t         *start,
56           time_t         *end,       /* which time frame do you want ?
57                                       * will be changed to represent reality */
58           unsigned long  *step,      /* which stepsize do you want? 
59                                       * will be changed to represent reality */
60           unsigned long  *ds_cnt,    /* number of data sources in file */
61           char           ***ds_namv,   /* names of data sources */
62           rrd_value_t    **data)     /* two dimensional array containing the data */
63 {
64
65
66     long     step_tmp =1;
67     time_t   start_tmp=0, end_tmp=0;
68     enum     cf_en cf_idx;
69
70     struct time_value start_tv, end_tv;
71     char     *parsetime_error = NULL;
72
73     /* init start and end time */
74     parsetime("end-24h", &start_tv);
75     parsetime("now", &end_tv);
76
77     while (1){
78         static struct option long_options[] =
79         {
80             {"resolution",      required_argument, 0, 'r'},
81             {"start",      required_argument, 0, 's'},
82             {"end",      required_argument, 0, 'e'},
83             {0,0,0,0}
84         };
85         int option_index = 0;
86         int opt;
87         opt = getopt_long(argc, argv, "r:s:e:", 
88                           long_options, &option_index);
89
90         if (opt == EOF)
91             break;
92
93         switch(opt) {
94         case 's':
95             if ((parsetime_error = parsetime(optarg, &start_tv))) {
96                 rrd_set_error( "start time: %s", parsetime_error );
97                 return -1;
98             }
99             break;
100         case 'e':
101             if ((parsetime_error = parsetime(optarg, &end_tv))) {
102                 rrd_set_error( "end time: %s", parsetime_error );
103                 return -1;
104             }
105             break;
106         case 'r':
107             step_tmp = atol(optarg);
108             break;
109         case '?':
110             rrd_set_error("unknown option '-%c'",optopt);
111             return(-1);
112         }
113     }
114
115     
116     if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
117         return -1;
118     }  
119
120     
121     if (start_tmp < 3600*24*365*10){
122         rrd_set_error("the first entry to fetch should be after 1980");
123         return(-1);
124     }
125     
126     if (end_tmp < start_tmp) {
127         rrd_set_error("start (%ld) should be less than end (%ld)", start_tmp, end_tmp);
128         return(-1);
129     }
130     
131     *start = start_tmp;
132     *end = end_tmp;
133
134     if (step_tmp < 1) {
135         rrd_set_error("step must be >= 1 second");
136         return -1;
137     }
138     *step = step_tmp;
139     
140     if (optind + 1 >= argc){
141         rrd_set_error("not enough arguments");
142         return -1;
143     }
144     
145     if ((int)(cf_idx=cf_conv(argv[optind+1])) == -1 ){
146         return -1;
147     }
148
149     if (rrd_fetch_fn(argv[optind],cf_idx,start,end,step,ds_cnt,ds_namv,data) == -1)
150         return(-1);
151     return (0);
152 }
153
154 int
155 rrd_fetch_fn(
156     char           *filename,  /* name of the rrd */
157     enum cf_en     cf_idx,         /* which consolidation function ?*/
158     time_t         *start,
159     time_t         *end,       /* which time frame do you want ?
160                                 * will be changed to represent reality */
161     unsigned long  *step,      /* which stepsize do you want? 
162                                 * will be changed to represent reality */
163     unsigned long  *ds_cnt,    /* number of data sources in file */
164     char           ***ds_namv,   /* names of data_sources */
165     rrd_value_t    **data)     /* two dimensional array containing the data */
166 {
167     long           i,ii;
168     FILE           *in_file;
169     time_t         cal_start,cal_end, rra_start_time,rra_end_time;
170     long  best_full_rra=0, best_part_rra=0, chosen_rra=0, rra_pointer=0;
171     long  best_step_diff=0, tmp_step_diff=0, tmp_match=0, best_match=0;
172     long  full_match, rra_base;
173     long           start_offset, end_offset;
174     int            first_full = 1;
175     int            first_part = 1;
176     rrd_t     rrd;
177     rrd_value_t    *data_ptr;
178     unsigned long  rows = (*end - *start) / *step;
179
180 #ifdef DEBUG
181 fprintf(stderr,"Entered rrd_fetch_fn() searching for the best match\n");
182 fprintf(stderr,"Looking for: start %10lu end %10lu step %5lu rows  %lu\n",
183                                                 *start,*end,*step,rows);
184 #endif
185
186     if(rrd_open(filename,&in_file,&rrd, RRD_READONLY)==-1)
187         return(-1);
188     
189     /* when was the realy last update of this file ? */
190
191     if (((*ds_namv) = (char **) malloc(rrd.stat_head->ds_cnt * sizeof(char*)))==NULL){
192         rrd_set_error("malloc fetch ds_namv array");
193         rrd_free(&rrd);
194         fclose(in_file);
195         return(-1);
196     }
197     
198     for(i=0;(unsigned long)i<rrd.stat_head->ds_cnt;i++){
199         if ((((*ds_namv)[i]) = malloc(sizeof(char) * DS_NAM_SIZE))==NULL){
200             rrd_set_error("malloc fetch ds_namv entry");
201             rrd_free(&rrd);
202             free(*ds_namv);
203             fclose(in_file);
204             return(-1);
205         }
206         strncpy((*ds_namv)[i],rrd.ds_def[i].ds_nam,DS_NAM_SIZE-1);
207         (*ds_namv)[i][DS_NAM_SIZE-1]='\0';
208
209     }
210     
211     /* find the rra which best matches the requirements */
212     for(i=0;(unsigned)i<rrd.stat_head->rra_cnt;i++){
213         if(cf_conv(rrd.rra_def[i].cf_nam) == cf_idx){
214             
215             cal_end = (rrd.live_head->last_up - (rrd.live_head->last_up 
216                           % (rrd.rra_def[i].pdp_cnt 
217                              * rrd.stat_head->pdp_step)));
218             cal_start = (cal_end 
219                          - (rrd.rra_def[i].pdp_cnt 
220                             * rrd.rra_def[i].row_cnt
221                             * rrd.stat_head->pdp_step));
222
223             full_match = *end -*start;
224 #ifdef DEBUG
225 fprintf(stderr,"Considering: start %10lu end %10lu step %5lu ",
226                                                         cal_start,cal_end,
227                         rrd.stat_head->pdp_step * rrd.rra_def[i].pdp_cnt);
228 #endif
229             /* we need step difference in either full or partial case */
230             tmp_step_diff = labs(*step - (rrd.stat_head->pdp_step
231                                            * rrd.rra_def[i].pdp_cnt));
232             /* best full match */
233             if(cal_end >= *end 
234                && cal_start <= *start){
235                 if (first_full || (tmp_step_diff < best_step_diff)){
236                     first_full=0;
237                     best_step_diff = tmp_step_diff;
238                     best_full_rra=i;
239 #ifdef DEBUG
240 fprintf(stderr,"best full match so far\n");
241 #endif
242                 } else {
243 #ifdef DEBUG
244 fprintf(stderr,"full match, not best\n");
245 #endif
246                 }
247                 
248             } else {
249                 /* best partial match */
250                 tmp_match = full_match;
251                 if (cal_start>*start)
252                     tmp_match -= (cal_start-*start);
253                 if (cal_end<*end)
254                     tmp_match -= (*end-cal_end);                
255                 if (first_part ||
256                     (best_match < tmp_match) ||
257                     (best_match == tmp_match && 
258                      tmp_step_diff < best_step_diff)){ 
259 #ifdef DEBUG
260 fprintf(stderr,"best partial so far\n");
261 #endif
262                     first_part=0;
263                     best_match = tmp_match;
264                     best_step_diff = tmp_step_diff;
265                     best_part_rra =i;
266                 } else {
267 #ifdef DEBUG
268 fprintf(stderr,"partial match, not best\n");
269 #endif
270                 }
271             }
272         }
273     }
274
275     /* lets see how the matching went. */
276     if (first_full==0)
277         chosen_rra = best_full_rra;
278     else if (first_part==0)
279         chosen_rra = best_part_rra;
280     else {
281         rrd_set_error("the RRD does not contain an RRA matching the chosen CF");
282         rrd_free(&rrd);
283         fclose(in_file);
284         return(-1);
285     }
286         
287     /* set the wish parameters to their real values */
288     *step = rrd.stat_head->pdp_step * rrd.rra_def[chosen_rra].pdp_cnt;
289     *start -= (*start % *step);
290     if (*end % *step) *end += (*step - *end % *step);
291     rows = (*end - *start) / *step;
292
293 #ifdef DEBUG
294     fprintf(stderr,"We found:    start %10lu end %10lu step %5lu rows  %lu\n",
295                                                 *start,*end,*step,rows);
296 #endif
297
298 /* Start and end are now multiples of the step size.  The amount of
299 ** steps we want is (end-start)/step and *not* an extra one.
300 ** Reasoning:  if step is s and we want to graph from t to t+s,
301 ** we need exactly ((t+s)-t)/s rows.  The row to collect from the
302 ** database is the one with time stamp (t+s) which means t to t+s.
303 */
304     *ds_cnt =   rrd.stat_head->ds_cnt; 
305     if (((*data) = malloc(*ds_cnt * rows * sizeof(rrd_value_t)))==NULL){
306         rrd_set_error("malloc fetch data area");
307         for (i=0;(unsigned long)i<*ds_cnt;i++)
308               free((*ds_namv)[i]);
309         free(*ds_namv);
310         rrd_free(&rrd);
311         fclose(in_file);
312         return(-1);
313     }
314     
315     data_ptr=(*data);
316     
317     /* find base address of rra */
318     rra_base=ftell(in_file);
319     for(i=0;i<chosen_rra;i++)
320         rra_base += ( *ds_cnt
321                       * rrd.rra_def[i].row_cnt
322                       * sizeof(rrd_value_t));
323
324     /* find start and end offset */
325     rra_end_time = (rrd.live_head->last_up 
326                     - (rrd.live_head->last_up % *step));
327     rra_start_time = (rra_end_time
328                  - ( *step * (rrd.rra_def[chosen_rra].row_cnt-1)));
329     /* here's an error by one if we don't be careful */
330     start_offset =(long)(*start + *step - rra_start_time) / (long)*step;
331     end_offset = (long)(rra_end_time - *end ) / (long)*step; 
332 #ifdef DEBUG
333     fprintf(stderr,"rra_start %lu, rra_end %lu, start_off %li, end_off %li\n",
334             rra_start_time,rra_end_time,start_offset,end_offset);
335 #endif
336
337     /* fill the gap at the start if needs be */
338
339     if (start_offset <= 0)
340         rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1;
341     else 
342         rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1+start_offset;
343     
344     if(fseek(in_file,(rra_base 
345                    + (rra_pointer
346                       * *ds_cnt
347                       * sizeof(rrd_value_t))),SEEK_SET) != 0){
348         rrd_set_error("seek error in RRA");
349         for (i=0;(unsigned)i<*ds_cnt;i++)
350               free((*ds_namv)[i]);
351         free(*ds_namv);
352         rrd_free(&rrd);
353         free(*data);
354         *data = NULL;
355         fclose(in_file);
356         return(-1);
357
358     }
359 #ifdef DEBUG
360     fprintf(stderr,"First Seek: rra_base %lu rra_pointer %lu\n",
361             rra_base, rra_pointer);
362 #endif
363     /* step trough the array */
364
365     for (i=start_offset;
366          i< (signed)rrd.rra_def[chosen_rra].row_cnt - end_offset;
367          i++){
368         /* no valid data yet */
369         if (i<0) {
370 #ifdef DEBUG
371             fprintf(stderr,"pre fetch %li -- ",i);
372 #endif
373             for(ii=0;(unsigned)ii<*ds_cnt;ii++){
374                 *(data_ptr++) = DNAN;
375 #ifdef DEBUG
376                 fprintf(stderr,"%10.2f ",*(data_ptr-1));
377 #endif
378             }
379         } 
380         /* past the valid data area */
381         else if (i >= (signed)rrd.rra_def[chosen_rra].row_cnt) {
382 #ifdef DEBUG
383             fprintf(stderr,"post fetch %li -- ",i);
384 #endif
385             for(ii=0;(unsigned)ii<*ds_cnt;ii++){
386                 *(data_ptr++) = DNAN;
387 #ifdef DEBUG
388                 fprintf(stderr,"%10.2f ",*(data_ptr-1));
389 #endif
390             }
391         } else {
392             /* OK we are inside the valid area but the pointer has to 
393              * be wrapped*/
394             if (rra_pointer >= (signed)rrd.rra_def[chosen_rra].row_cnt) {
395                 rra_pointer -= rrd.rra_def[chosen_rra].row_cnt;
396                 if(fseek(in_file,(rra_base+rra_pointer
397                                * *ds_cnt
398                                * sizeof(rrd_value_t)),SEEK_SET) != 0){
399                     rrd_set_error("wrap seek in RRA did fail");
400                     for (ii=0;(unsigned)ii<*ds_cnt;ii++)
401                         free((*ds_namv)[ii]);
402                     free(*ds_namv);
403                     rrd_free(&rrd);
404                     free(*data);
405                     *data = NULL;
406                     fclose(in_file);
407                     return(-1);
408                 }
409 #ifdef DEBUG
410                 fprintf(stderr,"wrap seek ...\n");
411 #endif      
412             }
413             
414             if(fread(data_ptr,
415                      sizeof(rrd_value_t),
416                      *ds_cnt,in_file) != rrd.stat_head->ds_cnt){
417                 rrd_set_error("fetching cdp from rra");
418                 for (ii=0;(unsigned)ii<*ds_cnt;ii++)
419                     free((*ds_namv)[ii]);
420                 free(*ds_namv);
421                 rrd_free(&rrd);
422                 free(*data);
423                 *data = NULL;
424                 fclose(in_file);
425                 return(-1);
426             }
427 #ifdef DEBUG
428             fprintf(stderr,"post fetch %li -- ",i);
429             for(ii=0;ii<*ds_cnt;ii++)
430                 fprintf(stderr,"%10.2f ",*(data_ptr+ii));
431 #endif
432             data_ptr += *ds_cnt;
433             rra_pointer ++;
434         }
435 #ifdef DEBUG
436             fprintf(stderr,"\n");
437 #endif      
438         
439     }
440     rrd_free(&rrd);
441     fclose(in_file);
442     return(0);
443 }