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