/* convenience function; if there is a daemon specified, or if we can * detect one from the environment, then flush the file. Otherwise, no-op */ int rrdc_flush_if_daemon (const char *opt_daemon, const char *filename) /* {{{ */ { int status = 0; rrdc_connect(opt_daemon); if (rrdc_is_connected(opt_daemon)) { rrd_clear_error(); status = rrdc_flush (filename); if (status != 0 && !rrd_test_error()) { if (status > 0) { rrd_set_error("rrdc_flush (%s) failed: %s", filename, rrd_strerror(status)); } else if (status < 0) { rrd_set_error("rrdc_flush (%s) failed with status %i.", filename, status); } } } /* if (rrdc_is_connected(..)) */ return status; } /* }}} int rrdc_flush_if_daemon */
rrd_info_t *rrd_info( int argc, char **argv) { struct optparse_long longopts[] = { {"daemon", 'd', OPTPARSE_REQUIRED}, {"noflush", 'F', OPTPARSE_NONE}, {0}, }; struct optparse options; int opt; rrd_info_t *info; char *opt_daemon = NULL; int status; int flushfirst = 1; optparse_init(&options, argc, argv); while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 'd': if (opt_daemon != NULL) free (opt_daemon); opt_daemon = strdup(options.optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); return NULL; } break; case 'F': flushfirst = 0; break; case '?': rrd_set_error("%s", options.errmsg); return NULL; } } /* while (opt != -1) */ if (options.argc - options.optind != 1) { rrd_set_error ("Usage: rrdtool %s [--daemon |-d <addr> [--noflush|-F]] <file>", options.argv[0]); return NULL; } if (flushfirst) { status = rrdc_flush_if_daemon(opt_daemon, options.argv[options.optind]); if (status) return (NULL); } rrdc_connect (opt_daemon); if (rrdc_is_connected (opt_daemon)) info = rrdc_info(options.argv[options.optind]); else info = rrd_info_r(options.argv[options.optind]); if (opt_daemon) free(opt_daemon); return (info); } /* rrd_info_t *rrd_info */
time_t rrd_last( int argc, char **argv) { char *opt_daemon = NULL; time_t lastupdate; optind = 0; opterr = 0; /* initialize getopt */ while (42) { int opt; int option_index = 0; static struct option long_options[] = { {"daemon", required_argument, 0, 'd'}, {0, 0, 0, 0} }; opt = getopt_long(argc, argv, "d:", long_options, &option_index); if (opt == EOF) break; switch (opt) { 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; default: rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>", argv[0]); return (-1); break; } } /* while (42) */ if ((argc - optind) != 1) { rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>", argv[0]); return (-1); } rrdc_connect (opt_daemon); if (rrdc_is_connected (opt_daemon)) lastupdate = rrdc_last (argv[optind]); else lastupdate = rrd_last_r(argv[optind]); if (opt_daemon) free(opt_daemon); return (lastupdate); }
time_t rrd_first( int argc, char **argv) { struct optparse_long longopts[] = { {"rraindex", 129, OPTPARSE_REQUIRED}, {"daemon", 'd', OPTPARSE_REQUIRED}, {0}, }; struct optparse options; int opt; int target_rraindex = 0; char *endptr; char *opt_daemon = NULL; optparse_init(&options, argc, argv); while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 129: target_rraindex = strtol(options.optarg, &endptr, 0); if (target_rraindex < 0) { rrd_set_error("invalid rraindex number"); return (-1); } break; case 'd': if (opt_daemon != NULL) free (opt_daemon); opt_daemon = strdup(options.optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); return -1; } break; case '?': rrd_set_error("%s", options.errmsg); return -1; } } if (options.optind >= options.argc) { rrd_set_error("usage rrdtool %s [--rraindex number] [--daemon|-d <addr>] file.rrd", options.argv[0]); return -1; } rrdc_connect (opt_daemon); if (rrdc_is_connected (opt_daemon)) { return rrdc_first(options.argv[options.optind], target_rraindex); } else { return rrd_first_r(options.argv[options.optind], target_rraindex); } }
static bool rrdcached_connect(char *addr) { #ifdef HAVE_RRD_CLIENT_H rrd_clear_error(); if (! rrdc_is_connected(addr)) { if (rrdc_connect(addr)) { sdb_log(SDB_LOG_ERR, "Failed to connectd to RRDCacheD at %s: %s", addr, rrd_get_error()); return 0; } } #else sdb_log(SDB_LOG_ERR, "Callback called with RRDCacheD address " "but your build of SysDB does not support that"); return 0; #endif return 1; } /* rrdcached_connect */
int rrd_fetch( int argc, char **argv, time_t *start, time_t *end, /* which time frame do you want ? * will be changed to represent reality */ unsigned long *step, /* which stepsize do you want? * will be changed to represent reality */ unsigned long *ds_cnt, /* number of data sources in file */ char ***ds_namv, /* names of data sources */ rrd_value_t **data) { /* two dimensional array containing the data */ unsigned long step_tmp = 1; time_t start_tmp = 0, end_tmp = 0; const char *cf; char *opt_daemon = NULL; int align_start = 0; int status; rrd_time_value_t start_tv, end_tv; const char *parsetime_error = NULL; struct option long_options[] = { {"resolution", required_argument, 0, 'r'}, {"start", required_argument, 0, 's'}, {"end", required_argument, 0, 'e'}, {"align-start", optional_argument, 0, 'a'}, {"daemon", required_argument, 0, 'd'}, {0, 0, 0, 0} }; optind = 0; opterr = 0; /* initialize getopt */ /* init start and end time */ rrd_parsetime("end-24h", &start_tv); rrd_parsetime("now", &end_tv); while (1) { int option_index = 0; int opt; opt = getopt_long(argc, argv, "ar:s:e:d:", long_options, &option_index); if (opt == EOF) break; switch (opt) { case 's': if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) { rrd_set_error("start time: %s", parsetime_error); return -1; } break; case 'e': if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) { rrd_set_error("end time: %s", parsetime_error); return -1; } break; case 'a': align_start = 1; break; case 'r': if ((parsetime_error = rrd_scaled_duration(optarg, 1, &step_tmp))) { rrd_set_error("resolution: %s", parsetime_error); return -1; } 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); } } if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) { return -1; } if (start_tmp < 3600 * 24 * 365 * 10) { rrd_set_error("the first entry to fetch should be after 1980"); return (-1); } if (align_start) { time_t delta = (start_tmp % step_tmp); start_tmp -= delta; end_tmp -= delta; } if (end_tmp < start_tmp) { rrd_set_error("start (%ld) should be less than end (%ld)", start_tmp, end_tmp); return (-1); } *start = start_tmp; *end = end_tmp; *step = step_tmp; if (optind + 1 >= argc) { rrd_set_error("Usage: rrdtool %s <file> <CF> [options]", argv[0]); return -1; } cf = argv[optind + 1]; rrdc_connect (opt_daemon); if (rrdc_is_connected (opt_daemon)) status = rrdc_fetch (argv[optind], cf, start, end, step, ds_cnt, ds_namv, data); else status = rrd_fetch_r(argv[optind], cf, start, end, step, ds_cnt, ds_namv, data); if (status != 0) return (-1); return (0); }
int rrd_flushcached (int argc, char **argv) { struct optparse_long longopts[] = { {"daemon", 'd', OPTPARSE_REQUIRED}, {0}, }; struct optparse options; int opt; char *opt_daemon = NULL; int status; int i; optparse_init(&options, argc, argv); while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 'd': if (opt_daemon != NULL) free (opt_daemon); opt_daemon = strdup(options.optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); return -1; } break; case '?': rrd_set_error("%s", options.errmsg); return -1; } } /* while (opt!=-1) */ if ((options.argc - options.optind) < 1) { rrd_set_error("Usage: rrdtool %s [--daemon|-d <addr>] <file> [<file> ...]", options.argv[0]); return -1; } /* try to connect to rrdcached */ status = rrdc_connect(opt_daemon); if (status != 0) goto out; if (! rrdc_is_connected(opt_daemon)) { rrd_set_error ("Daemon address \"%s\" unknown. Please use the \"--daemon\" " "option to set an address on the command line or set the " "\"%s\" environment variable.", opt_daemon, ENV_RRDCACHED_ADDRESS); status = -1; goto out; } status = 0; for (i = options.optind; i < options.argc; i++) { status = rrdc_flush(options.argv[i]); if (status) { char *error; int remaining; error = strdup(rrd_get_error()); remaining = options.argc - options.optind - 1; rrd_set_error("Flushing of file \"%s\" failed: %s. Skipping " "remaining %i file%s.", options.argv[i], ((! error) || (*error == '\0')) ? "unknown error" : error, remaining, (remaining == 1) ? "" : "s"); free(error); break; } } out: if (opt_daemon) free(opt_daemon); return status; } /* int rrd_flush */
int rrd_fetch( int argc, char **argv, time_t *start, time_t *end, /* which time frame do you want ? * will be changed to represent reality */ unsigned long *step, /* which stepsize do you want? * will be changed to represent reality */ unsigned long *ds_cnt, /* number of data sources in file */ char ***ds_namv, /* names of data sources */ rrd_value_t **data) { /* two dimensional array containing the data */ unsigned long step_tmp = 1; time_t start_tmp = 0, end_tmp = 0; const char *cf; char *opt_daemon = NULL; int align_start = 0; int status; rrd_time_value_t start_tv, end_tv; const char *parsetime_error = NULL; struct optparse_long longopts[] = { {"resolution", 'r', OPTPARSE_REQUIRED}, {"start", 's', OPTPARSE_REQUIRED}, {"end", 'e', OPTPARSE_REQUIRED}, {"align-start", 'a', OPTPARSE_NONE}, {"daemon", 'd', OPTPARSE_REQUIRED}, {0}, }; struct optparse options; int opt; /* init start and end time */ rrd_parsetime("end-24h", &start_tv); rrd_parsetime("now", &end_tv); optparse_init(&options, argc, argv); while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 's': if ((parsetime_error = rrd_parsetime(options.optarg, &start_tv))) { rrd_set_error("start time: %s", parsetime_error); return -1; } break; case 'e': if ((parsetime_error = rrd_parsetime(options.optarg, &end_tv))) { rrd_set_error("end time: %s", parsetime_error); return -1; } break; case 'a': align_start = 1; break; case 'r': if ((parsetime_error = rrd_scaled_duration(options.optarg, 1, &step_tmp))) { rrd_set_error("resolution: %s", parsetime_error); return -1; } break; case 'd': if (opt_daemon != NULL) free (opt_daemon); opt_daemon = strdup(options.optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); return -1; } break; case '?': rrd_set_error("%s", options.errmsg); return -1; } } if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) { return -1; } if (start_tmp < 3600 * 24 * 365 * 10) { rrd_set_error("the first entry to fetch should be after 1980"); return (-1); } if (align_start) { time_t delta = (start_tmp % step_tmp); start_tmp -= delta; end_tmp -= delta; } if (end_tmp < start_tmp) { rrd_set_error("start (%ld) should be less than end (%ld)", start_tmp, end_tmp); return (-1); } *start = start_tmp; *end = end_tmp; *step = step_tmp; if (options.optind + 1 >= options.argc) { rrd_set_error("Usage: rrdtool %s <file> <CF> [options]", options.argv[0]); return -1; } cf = options.argv[options.optind + 1]; rrdc_connect (opt_daemon); if (rrdc_is_connected (opt_daemon)) status = rrdc_fetch (options.argv[options.optind], cf, start, end, step, ds_cnt, ds_namv, data); else status = rrd_fetch_r(options.argv[options.optind], cf, start, end, step, ds_cnt, ds_namv, data); if (status != 0) return (-1); return (0); }
int rrd_create( int argc, char **argv) { struct option long_options[] = { {"start", required_argument, 0, 'b'}, {"step", required_argument, 0, 's'}, {"daemon", required_argument, 0, 'd'}, {"no-overwrite", no_argument, 0, 'O'}, {0, 0, 0, 0} }; int option_index = 0; int opt; time_t last_up = time(NULL) - 10; unsigned long pdp_step = 300; rrd_time_value_t last_up_tv; char *parsetime_error = NULL; long long_tmp; int rc; char * opt_daemon = NULL; int opt_no_overwrite = 0; optind = 0; opterr = 0; /* initialize getopt */ while (1) { opt = getopt_long(argc, argv, "Ob:s:d:", long_options, &option_index); if (opt == EOF) break; switch (opt) { 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 'b': if ((parsetime_error = rrd_parsetime(optarg, &last_up_tv))) { rrd_set_error("start time: %s", parsetime_error); return (-1); } if (last_up_tv.type == RELATIVE_TO_END_TIME || last_up_tv.type == RELATIVE_TO_START_TIME) { rrd_set_error("specifying time relative to the 'start' " "or 'end' makes no sense here"); return (-1); } last_up = mktime(&last_up_tv.tm) +last_up_tv.offset; if (last_up < 3600 * 24 * 365 * 10) { rrd_set_error ("the first entry to the RRD should be after 1980"); return (-1); } break; case 's': long_tmp = atol(optarg); if (long_tmp < 1) { rrd_set_error("step size should be no less than one second"); return (-1); } pdp_step = long_tmp; break; case 'O': opt_no_overwrite = 1; break; case '?': if (optopt != 0) rrd_set_error("unknown option '%c'", optopt); else rrd_set_error("unknown option '%s'", argv[optind - 1]); return (-1); } } if (optind == argc) { rrd_set_error("need name of an rrd file to create"); return -1; } rrdc_connect (opt_daemon); if (rrdc_is_connected (opt_daemon)) { rc = rrdc_create (argv[optind], pdp_step, last_up, opt_no_overwrite, argc - optind - 1, (const char **) (argv + optind + 1)); } else { rc = rrd_create_r2(argv[optind], pdp_step, last_up, opt_no_overwrite, argc - optind - 1, (const char **) (argv + optind + 1)); } return rc; }
rrd_info_t *rrd_info( int argc, char **argv) { rrd_info_t *info; char *opt_daemon = NULL; int status; int flushfirst = 1; optind = 0; opterr = 0; /* initialize getopt */ while (42) { int opt; int option_index = 0; static struct option long_options[] = { {"daemon", required_argument, 0, 'd'}, {"noflush", no_argument, 0, 'F'}, {0, 0, 0, 0} }; opt = getopt_long(argc, argv, "d:F", long_options, &option_index); if (opt == EOF) break; switch (opt) { case 'd': if (opt_daemon != NULL) free (opt_daemon); opt_daemon = strdup (optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); return (NULL); } break; case 'F': flushfirst = 0; break; default: rrd_set_error ("Usage: rrdtool %s [--daemon <addr> [--noflush]] <file>", argv[0]); return (NULL); break; } } /* while (42) */ if ((argc - optind) != 1) { rrd_set_error ("Usage: rrdtool %s [--daemon <addr> [--noflush]] <file>", argv[0]); return (NULL); } if( flushfirst ) { status = rrdc_flush_if_daemon(opt_daemon, argv[optind]); if (status) return (NULL); } rrdc_connect (opt_daemon); if (rrdc_is_connected (opt_daemon)) info = rrdc_info (argv[optind]); else info = rrd_info_r(argv[optind]); if (opt_daemon) free(opt_daemon); return (info); } /* rrd_info_t *rrd_info */