3bb97085416fa9aabea4df0907b07451714e5f8d
[rrdtool.git] / src / rrd_fetch.c
1 /*****************************************************************************
2  * RRDtool 1.0.33  Copyright Tobias Oetiker, 1997 - 2000
3  *****************************************************************************
4  * rrd_fetch.c  read date from an rrd to use for further processing
5  *****************************************************************************
6  * $Id$
7  * $Log$
8  * Revision 1.2  2001/12/17 12:48:43  oetiker
9  * fix overflow error ...
10  *
11  * Revision 1.1.1.1  2001/02/25 22:25:05  oetiker
12  * checkin
13  *
14  *****************************************************************************/
15
16 #include "rrd_tool.h"
17 /*#define DEBUG*/
18
19 int
20 rrd_fetch(int argc, 
21           char **argv,
22           time_t         *start,
23           time_t         *end,       /* which time frame do you want ?
24                                       * will be changed to represent reality */
25           unsigned long  *step,      /* which stepsize do you want? 
26                                       * will be changed to represent reality */
27           unsigned long  *ds_cnt,    /* number of data sources in file */
28           char           ***ds_namv,   /* names of data sources */
29           rrd_value_t    **data)     /* two dimensional array containing the data */
30 {
31
32
33     long     step_tmp =1;
34     time_t   start_tmp=0, end_tmp=0;
35     enum     cf_en cf_idx;
36
37     struct time_value start_tv, end_tv;
38     char     *parsetime_error = NULL;
39
40     /* init start and end time */
41     parsetime("end-24h", &start_tv);
42     parsetime("now", &end_tv);
43
44     while (1){
45         static struct option long_options[] =
46         {
47             {"resolution",      required_argument, 0, 'r'},
48             {"start",      required_argument, 0, 's'},
49             {"end",      required_argument, 0, 'e'},
50             {0,0,0,0}
51         };
52         int option_index = 0;
53         int opt;
54         opt = getopt_long(argc, argv, "r:s:e:", 
55                           long_options, &option_index);
56
57         if (opt == EOF)
58             break;
59
60         switch(opt) {
61         case 's':
62             if ((parsetime_error = parsetime(optarg, &start_tv))) {
63                 rrd_set_error( "start time: %s", parsetime_error );
64                 return -1;
65             }
66             break;
67         case 'e':
68             if ((parsetime_error = parsetime(optarg, &end_tv))) {
69                 rrd_set_error( "end time: %s", parsetime_error );
70                 return -1;
71             }
72             break;
73         case 'r':
74             step_tmp = atol(optarg);
75             break;
76         case '?':
77             rrd_set_error("unknown option '-%c'",optopt);
78             return(-1);
79         }
80     }
81
82     
83     if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
84         return -1;
85     }  
86
87     
88     if (start_tmp < 3600*24*365*10){
89         rrd_set_error("the first entry to fetch should be after 1980");
90         return(-1);
91     }
92     
93     if (end_tmp < start_tmp) {
94         rrd_set_error("start (%ld) should be less than end (%ld)", start_tmp, end_tmp);
95         return(-1);
96     }
97     
98     *start = start_tmp;
99     *end = end_tmp;
100
101     if (step_tmp < 1) {
102         rrd_set_error("step must be >= 1 second");
103         return -1;
104     }
105     *step = step_tmp;
106     
107     if (optind + 1 >= argc){
108         rrd_set_error("not enough arguments");
109         return -1;
110     }
111     
112     if ((cf_idx=cf_conv(argv[optind+1])) == -1 ){
113         return -1;
114     }
115
116     if (rrd_fetch_fn(argv[optind],cf_idx,start,end,step,ds_cnt,ds_namv,data) == -1)
117         return(-1);
118     return (0);
119 }
120
121 int
122 rrd_fetch_fn(
123     char           *filename,  /* name of the rrd */
124     enum cf_en     cf_idx,         /* which consolidation function ?*/
125     time_t         *start,
126     time_t         *end,       /* which time frame do you want ?
127                                 * will be changed to represent reality */
128     unsigned long  *step,      /* which stepsize do you want? 
129                                 * will be changed to represent reality */
130     unsigned long  *ds_cnt,    /* number of data sources in file */
131     char           ***ds_namv,   /* names of data_sources */
132     rrd_value_t    **data)     /* two dimensional array containing the data */
133 {
134     long           i,ii;
135     FILE           *in_file;
136     time_t         cal_start,cal_end, rra_start_time,rra_end_time;
137     long  best_full_rra=0, best_part_rra=0, chosen_rra=0, rra_pointer=0;
138     long  best_step_diff=0, tmp_step_diff=0, tmp_match=0, best_match=0;
139     long  full_match, rra_base;
140     long           start_offset, end_offset;
141     int            first_full = 1;
142     int            first_part = 1;
143     rrd_t     rrd;
144     rrd_value_t    *data_ptr;
145     unsigned long  rows;
146
147     if(rrd_open(filename,&in_file,&rrd, RRD_READONLY)==-1)
148         return(-1);
149     
150     /* when was the realy last update of this file ? */
151
152     if (((*ds_namv) = (char **) malloc(rrd.stat_head->ds_cnt * sizeof(char*)))==NULL){
153         rrd_set_error("malloc fetch ds_namv array");
154         rrd_free(&rrd);
155         fclose(in_file);
156         return(-1);
157     }
158     
159     for(i=0;i<rrd.stat_head->ds_cnt;i++){
160         if ((((*ds_namv)[i]) = malloc(sizeof(char) * DS_NAM_SIZE))==NULL){
161             rrd_set_error("malloc fetch ds_namv entry");
162             rrd_free(&rrd);
163             free(*ds_namv);
164             fclose(in_file);
165             return(-1);
166         }
167         strncpy((*ds_namv)[i],rrd.ds_def[i].ds_nam,DS_NAM_SIZE-1);
168         (*ds_namv)[i][DS_NAM_SIZE-1]='\0';
169
170     }
171     
172     /* find the rra which best matches the requirements */
173     for(i=0;i<rrd.stat_head->rra_cnt;i++){
174         if(cf_conv(rrd.rra_def[i].cf_nam) == cf_idx){
175             
176                
177             cal_end = (rrd.live_head->last_up - (rrd.live_head->last_up 
178                           % (rrd.rra_def[i].pdp_cnt 
179                              * rrd.stat_head->pdp_step)));
180             cal_start = (cal_end 
181                          - (rrd.rra_def[i].pdp_cnt 
182                             * rrd.rra_def[i].row_cnt
183                             * rrd.stat_head->pdp_step));
184
185             full_match = *end -*start;
186             /* best full match */
187             if(cal_end >= *end 
188                && cal_start <= *start){
189                 tmp_step_diff = labs(*step - (rrd.stat_head->pdp_step
190                                          * rrd.rra_def[i].pdp_cnt));
191                 if (first_full || (tmp_step_diff < best_step_diff)){
192                     first_full=0;
193                     best_step_diff = tmp_step_diff;
194                     best_full_rra=i;
195                 } 
196                 
197             } else {
198                 /* best partial match */
199                 tmp_match = full_match;
200                 if (cal_start>*start)
201                     tmp_match -= (cal_start-*start);
202                 if (cal_end<*end)
203                     tmp_match -= (*end-cal_end);                
204                 if (first_part || best_match < tmp_match){
205                     first_part=0;
206                     best_match = tmp_match;
207                     best_part_rra =i;
208                 } 
209             }
210         }
211     }
212
213     /* lets see how the matching went. */
214     
215     if (first_full==0)
216         chosen_rra = best_full_rra;
217     else if (first_part==0)
218         chosen_rra = best_part_rra;
219     else {
220         rrd_set_error("the RRD does not contain an RRA matching the chosen CF");
221         rrd_free(&rrd);
222         fclose(in_file);
223         return(-1);
224     }
225         
226     /* set the wish parameters to their real values */
227     
228     *step = rrd.stat_head->pdp_step * rrd.rra_def[chosen_rra].pdp_cnt;
229     *start -= (*start % *step);
230     if (*end % *step) *end += (*step - *end % *step);
231     rows = (*end - *start) / *step +1;
232
233 #ifdef DEBUG
234     fprintf(stderr,"start %lu end %lu step %lu rows  %lu\n",
235             *start,*end,*step,rows);
236 #endif
237
238     *ds_cnt =   rrd.stat_head->ds_cnt; 
239     if (((*data) = malloc(*ds_cnt * rows * sizeof(rrd_value_t)))==NULL){
240         rrd_set_error("malloc fetch data area");
241         for (i=0;i<*ds_cnt;i++)
242               free((*ds_namv)[i]);
243         free(*ds_namv);
244         rrd_free(&rrd);
245         fclose(in_file);
246         return(-1);
247     }
248     
249     data_ptr=(*data);
250     
251     /* find base address of rra */
252     rra_base=ftell(in_file);
253     for(i=0;i<chosen_rra;i++)
254         rra_base += ( *ds_cnt
255                       * rrd.rra_def[i].row_cnt
256                       * sizeof(rrd_value_t));
257
258     /* find start and end offset */
259     rra_end_time = (rrd.live_head->last_up 
260                     - (rrd.live_head->last_up % *step));
261     rra_start_time = (rra_end_time
262                  - ( *step * (rrd.rra_def[chosen_rra].row_cnt-1)));
263     start_offset =(long)(*start - rra_start_time) / (long)*step;
264     end_offset = (long)(rra_end_time - *end ) / (long)*step; 
265 #ifdef DEBUG
266     fprintf(stderr,"rra_start %lu, rra_end %lu, start_off %li, end_off %li\n",
267             rra_start_time,rra_end_time,start_offset,end_offset);
268 #endif
269
270     /* fill the gap at the start if needs be */
271
272     if (start_offset <= 0)
273         rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1;
274     else 
275         rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1+start_offset;
276     
277     if(fseek(in_file,(rra_base 
278                    + (rra_pointer
279                       * *ds_cnt
280                       * sizeof(rrd_value_t))),SEEK_SET) != 0){
281         rrd_set_error("seek error in RRA");
282         for (i=0;i<*ds_cnt;i++)
283               free((*ds_namv)[i]);
284         free(*ds_namv);
285         rrd_free(&rrd);
286         free(*data);
287         *data = NULL;
288         fclose(in_file);
289         return(-1);
290
291     }
292 #ifdef DEBUG
293     fprintf(stderr,"First Seek: rra_base %lu rra_pointer %lu\n",
294             rra_base, rra_pointer);
295 #endif
296     /* step trough the array */
297
298     for (i=start_offset;
299          i<(long)(rrd.rra_def[chosen_rra].row_cnt-end_offset);
300          i++){
301         /* no valid data yet */
302         if (i<0) {
303 #ifdef DEBUG
304             fprintf(stderr,"pre fetch %li -- ",i);
305 #endif
306             for(ii=0;ii<*ds_cnt;ii++){
307                 *(data_ptr++) = DNAN;
308 #ifdef DEBUG
309                 fprintf(stderr,"%10.2f ",*(data_ptr-1));
310 #endif
311             }
312         } 
313         /* past the valid data area */
314         else if (i>=rrd.rra_def[chosen_rra].row_cnt) {
315 #ifdef DEBUG
316             fprintf(stderr,"post fetch %li -- ",i);
317 #endif
318             for(ii=0;ii<*ds_cnt;ii++){
319                 *(data_ptr++) = DNAN;
320 #ifdef DEBUG
321                 fprintf(stderr,"%10.2f ",*(data_ptr-1));
322 #endif
323             }
324         } else {
325             /* OK we are inside the valid area but the pointer has to 
326              * be wrapped*/
327             if (rra_pointer >= rrd.rra_def[chosen_rra].row_cnt) {
328                 rra_pointer -= rrd.rra_def[chosen_rra].row_cnt;
329                 if(fseek(in_file,(rra_base+rra_pointer
330                                * *ds_cnt
331                                * sizeof(rrd_value_t)),SEEK_SET) != 0){
332                     rrd_set_error("wrap seek in RRA did fail");
333                     for (ii=0;ii<*ds_cnt;ii++)
334                         free((*ds_namv)[ii]);
335                     free(*ds_namv);
336                     rrd_free(&rrd);
337                     free(*data);
338                     *data = NULL;
339                     fclose(in_file);
340                     return(-1);
341                 }
342 #ifdef DEBUG
343                 fprintf(stderr,"wrap seek ...\n");
344 #endif      
345             }
346             
347             if(fread(data_ptr,
348                      sizeof(rrd_value_t),
349                      *ds_cnt,in_file) != rrd.stat_head->ds_cnt){
350                 rrd_set_error("fetching cdp from rra");
351                 for (ii=0;ii<*ds_cnt;ii++)
352                     free((*ds_namv)[ii]);
353                 free(*ds_namv);
354                 rrd_free(&rrd);
355                 free(*data);
356                 *data = NULL;
357                 fclose(in_file);
358                 return(-1);
359             }
360 #ifdef DEBUG
361             fprintf(stderr,"post fetch %li -- ",i);
362             for(ii=0;ii<*ds_cnt;ii++)
363                 fprintf(stderr,"%10.2f ",*(data_ptr+ii));
364 #endif
365             data_ptr += *ds_cnt;
366             rra_pointer ++;
367         }
368 #ifdef DEBUG
369             fprintf(stderr,"\n");
370 #endif      
371         
372     }
373     rrd_free(&rrd);
374     fclose(in_file);
375     return(0);
376 }