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 *****************************************************************************
8 * Revision 1.3 2001/12/24 06:51:49 alex
9 * A patch of size 44Kbytes... in short:
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
17 * Enhanced debugging in rrd_fetch_fn(), it shows the RRA selection
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.
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()
30 * Revision 1.2 2001/12/17 12:48:43 oetiker
31 * fix overflow error ...
33 * Revision 1.1.1.1 2001/02/25 22:25:05 oetiker
36 *****************************************************************************/
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 */
56 time_t start_tmp=0, end_tmp=0;
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);
67 static struct option long_options[] =
69 {"resolution", required_argument, 0, 'r'},
70 {"start", required_argument, 0, 's'},
71 {"end", required_argument, 0, 'e'},
76 opt = getopt_long(argc, argv, "r:s:e:",
77 long_options, &option_index);
84 if ((parsetime_error = parsetime(optarg, &start_tv))) {
85 rrd_set_error( "start time: %s", parsetime_error );
90 if ((parsetime_error = parsetime(optarg, &end_tv))) {
91 rrd_set_error( "end time: %s", parsetime_error );
96 step_tmp = atol(optarg);
99 rrd_set_error("unknown option '-%c'",optopt);
105 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
110 if (start_tmp < 3600*24*365*10){
111 rrd_set_error("the first entry to fetch should be after 1980");
115 if (end_tmp < start_tmp) {
116 rrd_set_error("start (%ld) should be less than end (%ld)", start_tmp, end_tmp);
124 rrd_set_error("step must be >= 1 second");
129 if (optind + 1 >= argc){
130 rrd_set_error("not enough arguments");
134 if ((cf_idx=cf_conv(argv[optind+1])) == -1 ){
138 if (rrd_fetch_fn(argv[optind],cf_idx,start,end,step,ds_cnt,ds_namv,data) == -1)
145 char *filename, /* name of the rrd */
146 enum cf_en cf_idx, /* which consolidation function ?*/
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 */
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;
166 rrd_value_t *data_ptr;
167 unsigned long rows = (*end - *start) / *step;
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);
175 if(rrd_open(filename,&in_file,&rrd, RRD_READONLY)==-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");
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");
195 strncpy((*ds_namv)[i],rrd.ds_def[i].ds_nam,DS_NAM_SIZE-1);
196 (*ds_namv)[i][DS_NAM_SIZE-1]='\0';
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)));
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;
214 fprintf(stderr,"Considering: start %10lu end %10lu step %5lu ",
216 rrd.stat_head->pdp_step * rrd.rra_def[i].pdp_cnt);
218 /* best full match */
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)){
225 best_step_diff = tmp_step_diff;
228 fprintf(stderr,"best full match so far\n");
232 fprintf(stderr,"full match, not best\n");
237 /* best partial match */
238 tmp_match = full_match;
239 if (cal_start>*start)
240 tmp_match -= (cal_start-*start);
242 tmp_match -= (*end-cal_end);
243 if (first_part || best_match < tmp_match){
245 fprintf(stderr,"best partial so far\n");
248 best_match = tmp_match;
252 fprintf(stderr,"partial match, not best\n");
259 /* lets see how the matching went. */
261 chosen_rra = best_full_rra;
262 else if (first_part==0)
263 chosen_rra = best_part_rra;
265 rrd_set_error("the RRD does not contain an RRA matching the chosen CF");
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;
278 fprintf(stderr,"We found: start %10lu end %10lu step %5lu rows %lu\n",
279 *start,*end,*step,rows);
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.
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++)
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;
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);
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;
326 rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1+start_offset;
328 if(fseek(in_file,(rra_base
331 * sizeof(rrd_value_t))),SEEK_SET) != 0){
332 rrd_set_error("seek error in RRA");
333 for (i=0;i<*ds_cnt;i++)
344 fprintf(stderr,"First Seek: rra_base %lu rra_pointer %lu\n",
345 rra_base, rra_pointer);
347 /* step trough the array */
350 i<(long)(rrd.rra_def[chosen_rra].row_cnt-end_offset);
352 /* no valid data yet */
355 fprintf(stderr,"pre fetch %li -- ",i);
357 for(ii=0;ii<*ds_cnt;ii++){
358 *(data_ptr++) = DNAN;
360 fprintf(stderr,"%10.2f ",*(data_ptr-1));
364 /* past the valid data area */
365 else if (i>=rrd.rra_def[chosen_rra].row_cnt) {
367 fprintf(stderr,"post fetch %li -- ",i);
369 for(ii=0;ii<*ds_cnt;ii++){
370 *(data_ptr++) = DNAN;
372 fprintf(stderr,"%10.2f ",*(data_ptr-1));
376 /* OK we are inside the valid area but the pointer has to
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
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]);
394 fprintf(stderr,"wrap seek ...\n");
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]);
412 fprintf(stderr,"post fetch %li -- ",i);
413 for(ii=0;ii<*ds_cnt;ii++)
414 fprintf(stderr,"%10.2f ",*(data_ptr+ii));
420 fprintf(stderr,"\n");