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 *****************************************************************************
8 * Revision 1.4 2002/02/01 20:34:49 oetiker
9 * fixed version number and date/time
11 * Revision 1.3 2001/12/24 06:51:49 alex
12 * A patch of size 44Kbytes... in short:
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
20 * Enhanced debugging in rrd_fetch_fn(), it shows the RRA selection
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.
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()
33 * Revision 1.2 2001/12/17 12:48:43 oetiker
34 * fix overflow error ...
36 * Revision 1.1.1.1 2001/02/25 22:25:05 oetiker
39 *****************************************************************************/
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 */
59 time_t start_tmp=0, end_tmp=0;
62 struct time_value start_tv, end_tv;
63 char *parsetime_error = NULL;
65 /* init start and end time */
66 parsetime("end-24h", &start_tv);
67 parsetime("now", &end_tv);
70 static struct option long_options[] =
72 {"resolution", required_argument, 0, 'r'},
73 {"start", required_argument, 0, 's'},
74 {"end", required_argument, 0, 'e'},
79 opt = getopt_long(argc, argv, "r:s:e:",
80 long_options, &option_index);
87 if ((parsetime_error = parsetime(optarg, &start_tv))) {
88 rrd_set_error( "start time: %s", parsetime_error );
93 if ((parsetime_error = parsetime(optarg, &end_tv))) {
94 rrd_set_error( "end time: %s", parsetime_error );
99 step_tmp = atol(optarg);
102 rrd_set_error("unknown option '-%c'",optopt);
108 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
113 if (start_tmp < 3600*24*365*10){
114 rrd_set_error("the first entry to fetch should be after 1980");
118 if (end_tmp < start_tmp) {
119 rrd_set_error("start (%ld) should be less than end (%ld)", start_tmp, end_tmp);
127 rrd_set_error("step must be >= 1 second");
132 if (optind + 1 >= argc){
133 rrd_set_error("not enough arguments");
137 if ((cf_idx=cf_conv(argv[optind+1])) == -1 ){
141 if (rrd_fetch_fn(argv[optind],cf_idx,start,end,step,ds_cnt,ds_namv,data) == -1)
148 char *filename, /* name of the rrd */
149 enum cf_en cf_idx, /* which consolidation function ?*/
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 */
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;
169 rrd_value_t *data_ptr;
170 unsigned long rows = (*end - *start) / *step;
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);
178 if(rrd_open(filename,&in_file,&rrd, RRD_READONLY)==-1)
181 /* when was the realy last update of this file ? */
183 if (((*ds_namv) = (char **) malloc(rrd.stat_head->ds_cnt * sizeof(char*)))==NULL){
184 rrd_set_error("malloc fetch ds_namv array");
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");
198 strncpy((*ds_namv)[i],rrd.ds_def[i].ds_nam,DS_NAM_SIZE-1);
199 (*ds_namv)[i][DS_NAM_SIZE-1]='\0';
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){
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)));
211 - (rrd.rra_def[i].pdp_cnt
212 * rrd.rra_def[i].row_cnt
213 * rrd.stat_head->pdp_step));
215 full_match = *end -*start;
217 fprintf(stderr,"Considering: start %10lu end %10lu step %5lu ",
219 rrd.stat_head->pdp_step * rrd.rra_def[i].pdp_cnt);
221 /* best full match */
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)){
228 best_step_diff = tmp_step_diff;
231 fprintf(stderr,"best full match so far\n");
235 fprintf(stderr,"full match, not best\n");
240 /* best partial match */
241 tmp_match = full_match;
242 if (cal_start>*start)
243 tmp_match -= (cal_start-*start);
245 tmp_match -= (*end-cal_end);
246 if (first_part || best_match < tmp_match){
248 fprintf(stderr,"best partial so far\n");
251 best_match = tmp_match;
255 fprintf(stderr,"partial match, not best\n");
262 /* lets see how the matching went. */
264 chosen_rra = best_full_rra;
265 else if (first_part==0)
266 chosen_rra = best_part_rra;
268 rrd_set_error("the RRD does not contain an RRA matching the chosen CF");
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;
281 fprintf(stderr,"We found: start %10lu end %10lu step %5lu rows %lu\n",
282 *start,*end,*step,rows);
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.
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++)
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));
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;
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);
324 /* fill the gap at the start if needs be */
326 if (start_offset <= 0)
327 rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1;
329 rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1+start_offset;
331 if(fseek(in_file,(rra_base
334 * sizeof(rrd_value_t))),SEEK_SET) != 0){
335 rrd_set_error("seek error in RRA");
336 for (i=0;i<*ds_cnt;i++)
347 fprintf(stderr,"First Seek: rra_base %lu rra_pointer %lu\n",
348 rra_base, rra_pointer);
350 /* step trough the array */
353 i<(long)(rrd.rra_def[chosen_rra].row_cnt-end_offset);
355 /* no valid data yet */
358 fprintf(stderr,"pre fetch %li -- ",i);
360 for(ii=0;ii<*ds_cnt;ii++){
361 *(data_ptr++) = DNAN;
363 fprintf(stderr,"%10.2f ",*(data_ptr-1));
367 /* past the valid data area */
368 else if (i>=rrd.rra_def[chosen_rra].row_cnt) {
370 fprintf(stderr,"post fetch %li -- ",i);
372 for(ii=0;ii<*ds_cnt;ii++){
373 *(data_ptr++) = DNAN;
375 fprintf(stderr,"%10.2f ",*(data_ptr-1));
379 /* OK we are inside the valid area but the pointer has to
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
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]);
397 fprintf(stderr,"wrap seek ...\n");
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]);
415 fprintf(stderr,"post fetch %li -- ",i);
416 for(ii=0;ii<*ds_cnt;ii++)
417 fprintf(stderr,"%10.2f ",*(data_ptr+ii));
423 fprintf(stderr,"\n");