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.1 2001/02/25 22:25:05 oetiker
11 *****************************************************************************/
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 */
31 time_t start_tmp=0, end_tmp=0;
34 struct time_value start_tv, end_tv;
35 char *parsetime_error = NULL;
37 /* init start and end time */
38 parsetime("end-24h", &start_tv);
39 parsetime("now", &end_tv);
42 static struct option long_options[] =
44 {"resolution", required_argument, 0, 'r'},
45 {"start", required_argument, 0, 's'},
46 {"end", required_argument, 0, 'e'},
51 opt = getopt_long(argc, argv, "r:s:e:",
52 long_options, &option_index);
59 if ((parsetime_error = parsetime(optarg, &start_tv))) {
60 rrd_set_error( "start time: %s", parsetime_error );
65 if ((parsetime_error = parsetime(optarg, &end_tv))) {
66 rrd_set_error( "end time: %s", parsetime_error );
71 step_tmp = atol(optarg);
74 rrd_set_error("unknown option '-%c'",optopt);
80 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
85 if (start_tmp < 3600*24*365*10){
86 rrd_set_error("the first entry to fetch should be after 1980");
90 if (end_tmp < start_tmp) {
91 rrd_set_error("start (%ld) should be less than end (%ld)", start_tmp, end_tmp);
99 rrd_set_error("step must be >= 1 second");
104 if (optind + 1 >= argc){
105 rrd_set_error("not enough arguments");
109 if ((cf_idx=cf_conv(argv[optind+1])) == -1 ){
113 if (rrd_fetch_fn(argv[optind],cf_idx,start,end,step,ds_cnt,ds_namv,data) == -1)
120 char *filename, /* name of the rrd */
121 enum cf_en cf_idx, /* which consolidation function ?*/
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 */
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;
141 rrd_value_t *data_ptr;
144 if(rrd_open(filename,&in_file,&rrd, RRD_READONLY)==-1)
147 /* when was the realy last update of this file ? */
149 if (((*ds_namv) = (char **) malloc(rrd.stat_head->ds_cnt * sizeof(char*)))==NULL){
150 rrd_set_error("malloc fetch ds_namv array");
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");
164 strncpy((*ds_namv)[i],rrd.ds_def[i].ds_nam,DS_NAM_SIZE-1);
165 (*ds_namv)[i][DS_NAM_SIZE-1]='\0';
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){
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)));
178 - (rrd.rra_def[i].pdp_cnt
179 * rrd.rra_def[i].row_cnt
180 * rrd.stat_head->pdp_step));
182 full_match = *end -*start;
183 /* best full match */
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)){
190 best_step_diff = tmp_step_diff;
195 /* best partial match */
196 tmp_match = full_match;
197 if (cal_start>*start)
198 tmp_match -= (cal_start-*start);
200 tmp_match -= (*end-cal_end);
201 if (first_part || best_match < tmp_match){
203 best_match = tmp_match;
210 /* lets see how the matching went. */
213 chosen_rra = best_full_rra;
214 else if (first_part==0)
215 chosen_rra = best_part_rra;
217 rrd_set_error("the RRD does not contain an RRA matching the chosen CF");
223 /* set the wish parameters to their real values */
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;
231 fprintf(stderr,"start %lu end %lu step %lu rows %lu\n",
232 *start,*end,*step,rows);
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++)
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));
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;
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);
267 /* fill the gap at the start if needs be */
269 if (start_offset <= 0)
270 rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1;
272 rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1+start_offset;
274 if(fseek(in_file,(rra_base
277 * sizeof(rrd_value_t))),SEEK_SET) != 0){
278 rrd_set_error("seek error in RRA");
279 for (i=0;i<*ds_cnt;i++)
290 fprintf(stderr,"First Seek: rra_base %lu rra_pointer %lu\n",
291 rra_base, rra_pointer);
293 /* step trough the array */
296 i<(long)(rrd.rra_def[chosen_rra].row_cnt-end_offset);
298 /* no valid data yet */
301 fprintf(stderr,"pre fetch %li -- ",i);
303 for(ii=0;ii<*ds_cnt;ii++){
304 *(data_ptr++) = DNAN;
306 fprintf(stderr,"%10.2f ",*(data_ptr-1));
310 /* past the valid data area */
311 else if (i>=rrd.rra_def[chosen_rra].row_cnt) {
313 fprintf(stderr,"post fetch %li -- ",i);
315 for(ii=0;ii<*ds_cnt;ii++){
316 *(data_ptr++) = DNAN;
318 fprintf(stderr,"%10.2f ",*(data_ptr-1));
322 /* OK we are inside the valid area but the pointer has to
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
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]);
340 fprintf(stderr,"wrap seek ...\n");
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]);
358 fprintf(stderr,"post fetch %li -- ",i);
359 for(ii=0;ii<*ds_cnt;ii++)
360 fprintf(stderr,"%10.2f ",*(data_ptr+ii));
366 fprintf(stderr,"\n");