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