1 /*****************************************************************************
2 * RRDtool 1.2.23 Copyright by Tobi Oetiker, 1997-2007
3 *****************************************************************************
4 * rrd_fetch.c read date from an rrd to use for further processing
5 *****************************************************************************
8 * Revision 1.8 2004/05/18 18:53:03 oetiker
9 * big spell checking patch -- slif@bellsouth.net
11 * Revision 1.7 2003/11/11 19:46:21 oetiker
12 * replaced time_value with rrd_time_value as MacOS X introduced a struct of that name in their standard headers
14 * Revision 1.6 2003/01/16 23:27:54 oetiker
15 * fix border condition in rra selection of rrd_fetch
16 * -- Stanislav Sinyagin <ssinyagin@yahoo.com>
18 * Revision 1.5 2002/06/23 22:29:40 alex
19 * Added "step=1800" and such to "DEF"
20 * Cleaned some of the signed vs. unsigned problems
22 * Revision 1.4 2002/02/01 20:34:49 oetiker
23 * fixed version number and date/time
25 * Revision 1.3 2001/12/24 06:51:49 alex
26 * A patch of size 44Kbytes... in short:
28 * Found and repaired the off-by-one error in rrd_fetch_fn().
29 * As a result I had to remove the hacks in rrd_fetch_fn(),
30 * rrd_tool.c, vdef_calc(), data_calc(), data_proc() and
31 * reduce_data(). There may be other places which I didn't
34 * Enhanced debugging in rrd_fetch_fn(), it shows the RRA selection
37 * Added the ability to print VDEF timestamps. At the moment it
38 * is a hack, I needed it now to fix the off-by-one error.
39 * If the format string is "%c" (and nothing else!), the time
40 * will be printed by both ctime() and as a long int.
42 * Moved some code around (slightly altering it) from rrd_graph()
43 * initializing now in rrd_graph_init()
44 * options parsing now in rrd_graph_options()
45 * script parsing now in rrd_graph_script()
47 * Revision 1.2 2001/12/17 12:48:43 oetiker
48 * fix overflow error ...
50 * Revision 1.1.1.1 2001/02/25 22:25:05 oetiker
53 *****************************************************************************/
57 #include "rrd_is_thread_safe.h"
64 time_t *end, /* which time frame do you want ?
65 * will be changed to represent reality */
66 unsigned long *step, /* which stepsize do you want?
67 * will be changed to represent reality */
68 unsigned long *ds_cnt, /* number of data sources in file */
69 char ***ds_namv, /* names of data sources */
70 rrd_value_t **data) /* two dimensional array containing the data */
75 time_t start_tmp=0, end_tmp=0;
78 struct rrd_time_value start_tv, end_tv;
79 char *parsetime_error = NULL;
80 optind = 0; opterr = 0; /* initialize getopt */
82 /* init start and end time */
83 parsetime("end-24h", &start_tv);
84 parsetime("now", &end_tv);
87 static struct option long_options[] =
89 {"resolution", required_argument, 0, 'r'},
90 {"start", required_argument, 0, 's'},
91 {"end", required_argument, 0, 'e'},
96 opt = getopt_long(argc, argv, "r:s:e:",
97 long_options, &option_index);
104 if ((parsetime_error = parsetime(optarg, &start_tv))) {
105 rrd_set_error( "start time: %s", parsetime_error );
110 if ((parsetime_error = parsetime(optarg, &end_tv))) {
111 rrd_set_error( "end time: %s", parsetime_error );
116 step_tmp = atol(optarg);
119 rrd_set_error("unknown option '-%c'",optopt);
125 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
130 if (start_tmp < 3600*24*365*10){
131 rrd_set_error("the first entry to fetch should be after 1980");
135 if (end_tmp < start_tmp) {
136 rrd_set_error("start (%ld) should be less than end (%ld)", start_tmp, end_tmp);
144 rrd_set_error("step must be >= 1 second");
149 if (optind + 1 >= argc){
150 rrd_set_error("not enough arguments");
156 if (rrd_fetch_r(argv[optind],cf,start,end,step,ds_cnt,ds_namv,data) == -1)
163 const char *filename, /* name of the rrd */
164 const char *cf, /* which consolidation function ?*/
166 time_t *end, /* which time frame do you want ?
167 * will be changed to represent reality */
168 unsigned long *step, /* which stepsize do you want?
169 * will be changed to represent reality */
170 unsigned long *ds_cnt, /* number of data sources in file */
171 char ***ds_namv, /* names of data_sources */
172 rrd_value_t **data) /* two dimensional array containing the data */
176 if ((int)(cf_idx=cf_conv(cf)) == -1 ){
180 return (rrd_fetch_fn(filename,cf_idx,start,end,step,ds_cnt,ds_namv,data));
185 const char *filename, /* name of the rrd */
186 enum cf_en cf_idx, /* which consolidation function ?*/
188 time_t *end, /* which time frame do you want ?
189 * will be changed to represent reality */
190 unsigned long *step, /* which stepsize do you want?
191 * will be changed to represent reality */
192 unsigned long *ds_cnt, /* number of data sources in file */
193 char ***ds_namv, /* names of data_sources */
194 rrd_value_t **data) /* two dimensional array containing the data */
197 time_t cal_start,cal_end, rra_start_time,rra_end_time;
198 long best_full_rra=0, best_part_rra=0, chosen_rra=0, rra_pointer=0;
199 long best_full_step_diff=0, best_part_step_diff=0, tmp_step_diff=0, tmp_match=0, best_match=0;
200 long full_match, rra_base;
201 long start_offset, end_offset;
205 rrd_file_t *rrd_file;
206 rrd_value_t *data_ptr;
208 #ifdef HAVE_POSIX_FADVISE
213 fprintf(stderr,"Entered rrd_fetch_fn() searching for the best match\n");
214 fprintf(stderr,"Looking for: start %10lu end %10lu step %5lu\n",
218 rrd_file = rrd_open(filename,&rrd, RRD_READONLY);
219 if (rrd_file == NULL)
222 #ifdef HAVE_POSIX_FADVISE
223 rrd_head_size = rrd_file->header_len;
225 /* when was the really last update of this file ? */
227 if (((*ds_namv) = (char **) malloc(rrd.stat_head->ds_cnt * sizeof(char*)))==NULL){
228 rrd_set_error("malloc fetch ds_namv array");
234 for(i=0;(unsigned long)i<rrd.stat_head->ds_cnt;i++){
235 if ((((*ds_namv)[i]) = malloc(sizeof(char) * DS_NAM_SIZE))==NULL){
236 rrd_set_error("malloc fetch ds_namv entry");
242 strncpy((*ds_namv)[i],rrd.ds_def[i].ds_nam,DS_NAM_SIZE-1);
243 (*ds_namv)[i][DS_NAM_SIZE-1]='\0';
247 /* find the rra which best matches the requirements */
248 for(i=0;(unsigned)i<rrd.stat_head->rra_cnt;i++){
249 if(cf_conv(rrd.rra_def[i].cf_nam) == cf_idx){
251 cal_end = (rrd.live_head->last_up - (rrd.live_head->last_up
252 % (rrd.rra_def[i].pdp_cnt
253 * rrd.stat_head->pdp_step)));
255 - (rrd.rra_def[i].pdp_cnt
256 * rrd.rra_def[i].row_cnt
257 * rrd.stat_head->pdp_step));
259 full_match = *end -*start;
261 fprintf(stderr,"Considering: start %10lu end %10lu step %5lu ",
263 rrd.stat_head->pdp_step * rrd.rra_def[i].pdp_cnt);
265 /* we need step difference in either full or partial case */
266 tmp_step_diff = labs(*step - (rrd.stat_head->pdp_step
267 * rrd.rra_def[i].pdp_cnt));
268 /* best full match */
270 && cal_start <= *start){
271 if (first_full || (tmp_step_diff < best_full_step_diff)){
273 best_full_step_diff = tmp_step_diff;
276 fprintf(stderr,"best full match so far\n");
280 fprintf(stderr,"full match, not best\n");
285 /* best partial match */
286 tmp_match = full_match;
287 if (cal_start>*start)
288 tmp_match -= (cal_start-*start);
290 tmp_match -= (*end-cal_end);
292 (best_match < tmp_match) ||
293 (best_match == tmp_match &&
294 tmp_step_diff < best_part_step_diff)){
296 fprintf(stderr,"best partial so far\n");
299 best_match = tmp_match;
300 best_part_step_diff = tmp_step_diff;
304 fprintf(stderr,"partial match, not best\n");
311 /* lets see how the matching went. */
313 chosen_rra = best_full_rra;
314 else if (first_part==0)
315 chosen_rra = best_part_rra;
317 rrd_set_error("the RRD does not contain an RRA matching the chosen CF");
323 /* set the wish parameters to their real values */
324 *step = rrd.stat_head->pdp_step * rrd.rra_def[chosen_rra].pdp_cnt;
325 *start -= (*start % *step);
326 *end += (*step - *end % *step);
327 rows = (*end - *start) / *step + 1;
330 fprintf(stderr,"We found: start %10lu end %10lu step %5lu rows %lu\n",
331 *start,*end,*step,rows);
334 /* Start and end are now multiples of the step size. The amount of
335 ** steps we want is (end-start)/step and *not* an extra one.
336 ** Reasoning: if step is s and we want to graph from t to t+s,
337 ** we need exactly ((t+s)-t)/s rows. The row to collect from the
338 ** database is the one with time stamp (t+s) which means t to t+s.
340 *ds_cnt = rrd.stat_head->ds_cnt;
341 if (((*data) = malloc(*ds_cnt * rows * sizeof(rrd_value_t)))==NULL){
342 rrd_set_error("malloc fetch data area");
343 for (i=0;(unsigned long)i<*ds_cnt;i++)
353 /* find base address of rra */
354 rra_base = rrd_file->header_len;
355 for(i=0;i<chosen_rra;i++)
356 rra_base += ( *ds_cnt
357 * rrd.rra_def[i].row_cnt
358 * sizeof(rrd_value_t));
360 /* find start and end offset */
361 rra_end_time = (rrd.live_head->last_up
362 - (rrd.live_head->last_up % *step));
363 rra_start_time = (rra_end_time
364 - ( *step * (rrd.rra_def[chosen_rra].row_cnt-1)));
365 /* here's an error by one if we don't be careful */
366 start_offset =(long)(*start + *step - rra_start_time) / (long)*step;
367 end_offset = (long)(rra_end_time - *end ) / (long)*step;
369 fprintf(stderr,"rra_start %lu, rra_end %lu, start_off %li, end_off %li\n",
370 rra_start_time,rra_end_time,start_offset,end_offset);
373 /* fill the gap at the start if needs be */
375 if (start_offset <= 0)
376 rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1;
378 rra_pointer = rrd.rra_ptr[chosen_rra].cur_row+1+start_offset;
380 if(rrd_seek(rrd_file,(rra_base
383 * sizeof(rrd_value_t))),SEEK_SET) != 0){
384 rrd_set_error("seek error in RRA");
385 for (i=0;(unsigned)i<*ds_cnt;i++)
396 fprintf(stderr,"First Seek: rra_base %lu rra_pointer %lu\n",
397 rra_base, rra_pointer);
399 /* step trough the array */
402 i< (signed)rrd.rra_def[chosen_rra].row_cnt - end_offset;
404 /* no valid data yet */
407 fprintf(stderr,"pre fetch %li -- ",i);
409 for(ii=0;(unsigned)ii<*ds_cnt;ii++){
410 *(data_ptr++) = DNAN;
412 fprintf(stderr,"%10.2f ",*(data_ptr-1));
416 /* past the valid data area */
417 else if (i >= (signed)rrd.rra_def[chosen_rra].row_cnt) {
419 fprintf(stderr,"post fetch %li -- ",i);
421 for(ii=0;(unsigned)ii<*ds_cnt;ii++){
422 *(data_ptr++) = DNAN;
424 fprintf(stderr,"%10.2f ",*(data_ptr-1));
428 /* OK we are inside the valid area but the pointer has to
430 if (rra_pointer >= (signed)rrd.rra_def[chosen_rra].row_cnt) {
431 rra_pointer -= rrd.rra_def[chosen_rra].row_cnt;
432 if(rrd_seek(rrd_file,(rra_base+rra_pointer
434 * sizeof(rrd_value_t)),SEEK_SET) != 0){
435 rrd_set_error("wrap seek in RRA did fail");
436 for (ii=0;(unsigned)ii<*ds_cnt;ii++)
437 free((*ds_namv)[ii]);
446 fprintf(stderr,"wrap seek ...\n");
450 if(rrd_read(rrd_file,data_ptr,
451 sizeof(rrd_value_t)* (*ds_cnt))
452 != (ssize_t)(sizeof(rrd_value_t)*(*ds_cnt)*rrd.stat_head->ds_cnt)){
453 rrd_set_error("fetching cdp from rra");
454 for (ii=0;(unsigned)ii<*ds_cnt;ii++)
455 free((*ds_namv)[ii]);
463 #ifdef HAVE_POSIX_FADVISE
464 /* don't pollute the buffer cache with data read from the file. We do this while reading to
465 keep damage minimal */
466 if (0 != posix_fadvise(rrd_file->fd, rrd_head_size, 0, POSIX_FADV_DONTNEED)) {
467 rrd_set_error("setting POSIX_FADV_DONTNEED on '%s': %s",filename, rrd_strerror(errno));
474 fprintf(stderr,"post fetch %li -- ",i);
475 for(ii=0;ii<*ds_cnt;ii++)
476 fprintf(stderr,"%10.2f ",*(data_ptr+ii));
482 fprintf(stderr,"\n");
487 #ifdef HAVE_POSIX_FADVISE
488 /* and just to be sure we drop everything except the header at the end */
489 if (0 != posix_fadvise(rrd_file->fd, rrd_head_size, 0, POSIX_FADV_DONTNEED)) {
490 rrd_set_error("setting POSIX_FADV_DONTNEED on '%s': %s",filename, rrd_strerror(errno));