X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Frrd_fetch.c;h=563e76b3c05973509630084b7b812dddf0f7034c;hb=8fdbf4660969f9bde7f0f434f2d1002028d36814;hp=618f3d3036a4561eda0c833ac3cbee89c02b5f3e;hpb=7f30ee2ae089520606cbb7e38fab9981618771f5;p=rrdtool.git diff --git a/src/rrd_fetch.c b/src/rrd_fetch.c index 618f3d3..563e76b 100644 --- a/src/rrd_fetch.c +++ b/src/rrd_fetch.c @@ -1,5 +1,5 @@ /***************************************************************************** - * RRDtool 1.2.23 Copyright by Tobi Oetiker, 1997-2007 + * RRDtool 1.3.2 Copyright by Tobi Oetiker, 1997-2008 ***************************************************************************** * rrd_fetch.c read date from an rrd to use for further processing ***************************************************************************** @@ -53,6 +53,7 @@ *****************************************************************************/ #include "rrd_tool.h" +#include "rrd_client.h" #include "rrd_is_thread_safe.h" /*#define DEBUG*/ @@ -69,46 +70,47 @@ int rrd_fetch( char ***ds_namv, /* names of data sources */ rrd_value_t **data) { /* two dimensional array containing the data */ - - long step_tmp = 1; time_t start_tmp = 0, end_tmp = 0; const char *cf; + char *opt_daemon = NULL; + int status; - struct rrd_time_value start_tv, end_tv; + rrd_time_value_t start_tv, end_tv; char *parsetime_error = NULL; + struct option long_options[] = { + {"resolution", required_argument, 0, 'r'}, + {"start", required_argument, 0, 's'}, + {"end", required_argument, 0, 'e'}, + {"daemon", required_argument, 0, 'd'}, + {0, 0, 0, 0} + }; optind = 0; opterr = 0; /* initialize getopt */ /* init start and end time */ - parsetime("end-24h", &start_tv); - parsetime("now", &end_tv); + rrd_parsetime("end-24h", &start_tv); + rrd_parsetime("now", &end_tv); while (1) { - static struct option long_options[] = { - {"resolution", required_argument, 0, 'r'}, - {"start", required_argument, 0, 's'}, - {"end", required_argument, 0, 'e'}, - {0, 0, 0, 0} - }; int option_index = 0; int opt; - opt = getopt_long(argc, argv, "r:s:e:", long_options, &option_index); + opt = getopt_long(argc, argv, "r:s:e:d:", long_options, &option_index); if (opt == EOF) break; switch (opt) { case 's': - if ((parsetime_error = parsetime(optarg, &start_tv))) { + if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) { rrd_set_error("start time: %s", parsetime_error); return -1; } break; case 'e': - if ((parsetime_error = parsetime(optarg, &end_tv))) { + if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) { rrd_set_error("end time: %s", parsetime_error); return -1; } @@ -116,6 +118,18 @@ int rrd_fetch( case 'r': step_tmp = atol(optarg); break; + + case 'd': + if (opt_daemon != NULL) + free (opt_daemon); + opt_daemon = strdup (optarg); + if (opt_daemon == NULL) + { + rrd_set_error ("strdup failed."); + return (-1); + } + break; + case '?': rrd_set_error("unknown option '-%c'", optopt); return (-1); @@ -123,7 +137,7 @@ int rrd_fetch( } - if (proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) { + if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) { return -1; } @@ -153,10 +167,47 @@ int rrd_fetch( return -1; } + if (opt_daemon == NULL) + { + char *temp; + + temp = getenv (ENV_RRDCACHED_ADDRESS); + if (temp != NULL) + { + opt_daemon = strdup (temp); + if (opt_daemon == NULL) + { + rrd_set_error("strdup failed."); + return (-1); + } + } + } + + if (opt_daemon != NULL) + { + status = rrdc_connect (opt_daemon); + if (status != 0) + { + rrd_set_error ("rrdc_connect failed with status %i.", status); + return (-1); + } + + status = rrdc_flush (argv[optind]); + if (status != 0) + { + rrd_set_error ("rrdc_flush (%s) failed with status %i.", + argv[optind], status); + return (-1); + } + + rrdc_disconnect (); + } /* if (opt_daemon) */ + cf = argv[optind + 1]; - if (rrd_fetch_r(argv[optind], cf, start, end, step, ds_cnt, ds_namv, data) - != 0) + status = rrd_fetch_r(argv[optind], cf, start, end, step, + ds_cnt, ds_namv, data); + if (status != 0) return (-1); return (0); } @@ -181,7 +232,7 @@ int rrd_fetch_r( return (rrd_fetch_fn (filename, cf_idx, start, end, step, ds_cnt, ds_namv, data)); -} +} /* int rrd_fetch_r */ int rrd_fetch_fn( const char *filename, /* name of the rrd */ @@ -262,7 +313,7 @@ int rrd_fetch_fn( tmp_step_diff = labs(*step - (rrd.stat_head->pdp_step * rrd.rra_def[i].pdp_cnt)); /* best full match */ - if (cal_end >= *end && cal_start <= *start) { + if (cal_start <= *start) { if (first_full || (tmp_step_diff < best_full_step_diff)) { first_full = 0; best_full_step_diff = tmp_step_diff; @@ -279,8 +330,6 @@ int rrd_fetch_fn( tmp_match = full_match; if (cal_start > *start) tmp_match -= (cal_start - *start); - if (cal_end < *end) - tmp_match -= (*end - cal_end); if (first_part || (best_match < tmp_match) || (best_match == tmp_match && @@ -422,18 +471,6 @@ int rrd_fetch_fn( rrd_set_error("fetching cdp from rra"); goto err_free_data; } -#ifdef HAVE_POSIX_FADVISE - /* don't pollute the buffer cache with data read from the file. We do this while reading to - keep damage minimal */ - if (0 != - posix_fadvise(rrd_file->fd, rrd_file->header_len, 0, - POSIX_FADV_DONTNEED)) { - rrd_set_error("setting POSIX_FADV_DONTNEED on '%s': %s", - filename, rrd_strerror(errno)); - goto err_close;/*XXX: should use err_free_all_ds_namv */ - } -#endif - #ifdef DEBUG fprintf(stderr, "post fetch %li -- ", i); for (ii = 0; ii < *ds_cnt; ii++) @@ -447,29 +484,20 @@ int rrd_fetch_fn( #endif } -#ifdef HAVE_POSIX_FADVISE - /* and just to be sure we drop everything except the header at the end */ - if (0 != - posix_fadvise(rrd_file->fd, rrd_file->header_len, 0, - POSIX_FADV_DONTNEED)) { - rrd_set_error("setting POSIX_FADV_DONTNEED on '%s': %s", filename, - rrd_strerror(errno)); - goto err_free; /*XXX: should use err_free_all_ds_namv */ - } -#endif + rrd_close(rrd_file); return (0); -err_free_data: + err_free_data: free(*data); *data = NULL; -err_free_all_ds_namv: - for (i = 0; (unsigned long)i < rrd.stat_head->ds_cnt; ++i) + err_free_all_ds_namv: + for (i = 0; (unsigned long) i < rrd.stat_head->ds_cnt; ++i) free((*ds_namv)[i]); -err_free_ds_namv: + err_free_ds_namv: free(*ds_namv); -err_close: + err_close: rrd_close(rrd_file); -err_free: + err_free: rrd_free(&rrd); return (-1); }