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