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_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); } }
int rrd_restore( int argc, char **argv) { struct optparse_long longopts[] = { {"range-check", 'r', OPTPARSE_NONE}, {"force-overwrite", 'f', OPTPARSE_NONE}, {0}, }; struct optparse options; int opt; rrd_t *rrd; optparse_init(&options, argc, argv); while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 'r': opt_range_check = 1; break; case 'f': opt_force_overwrite = 1; break; case '?': rrd_set_error("%s", options.errmsg); return -1; } } /* while (opt != -1) */ if (options.argc - options.optind != 2) { rrd_set_error("usage rrdtool %s [--range-check|-r] " "[--force-overwrite|-f] file.xml file.rrd", options.argv[0]); return -1; } rrd = parse_file(options.argv[options.optind]); if (rrd == NULL) return (-1); if (write_file(options.argv[options.optind + 1], rrd) != 0) { local_rrd_free(rrd); return (-1); } local_rrd_free(rrd); return (0); } /* int rrd_restore */
int optparse_long(struct optparse *options, const struct optparse_long *longopts, int *longindex) { char *option = options->argv[options->optind]; if (option == 0) { return -1; } else if (is_shortopt(option)) { return long_fallback(options, longopts, longindex); } else if (!is_longopt(option)) { if (options->permute) { int index = options->optind; options->optind++; int r = optparse_long(options, longopts, longindex); permute(options, index); options->optind--; return r; } else { return -1; } } /* Parse as long option. */ options->errmsg[0] = '\0'; options->optopt = 0; options->optarg = 0; option += 2; /* skip "--" */ options->optind++; for (int i = 0; !longopts_end(longopts, i); i++) { const char *name = longopts[i].longname; if (longopts_match(name, option)) { if (longindex) *longindex = i; options->optopt = longopts[i].shortname; char *arg = longopts_arg(option); if (longopts[i].argtype == OPTPARSE_NONE && arg != 0) { return opterror(options, MSG_TOOMANY, name); } if (arg != 0) { options->optarg = arg; } else if (longopts[i].argtype == OPTPARSE_REQUIRED) { options->optarg = options->argv[options->optind++]; if (options->optarg == 0) return opterror(options, MSG_MISSING, name); } return options->optopt; } } return opterror(options, MSG_INVALID, option); }
void try_optparse_long(char **argv) { print_argv(argv); struct optparse options; optparse_init(&options, argv); struct optparse_long longopts[] = { {"amend", 'a', OPTPARSE_NONE}, {"brief", 'b', OPTPARSE_NONE}, {"color", 'c', OPTPARSE_REQUIRED}, {"delay", 'd', OPTPARSE_OPTIONAL}, {0} }; int opt, longindex; while ((opt = optparse_long(&options, longopts, &longindex)) != -1) { if (opt == '?') printf("%s: %s\n", argv[0], options.errmsg); printf("%c (%d, %d) = '%s'\n", opt, options.optind, longindex, options.optarg); } printf("optind = %d\n", options.optind); char *arg; while ((arg = optparse_arg(&options))) printf("argument: %s\n", arg); }
int rrd_xport( int argc, char **argv, int UNUSED(*xsize), 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 *col_cnt, /* number of data columns in the result */ char ***legend_v, /* legend entries */ rrd_value_t **data) { /* two dimensional array containing the data */ image_desc_t im; time_t start_tmp = 0, end_tmp = 0; rrd_time_value_t start_tv, end_tv; char *parsetime_error = NULL; struct optparse options; optparse_init(&options, argc, argv); struct optparse_long longopts[] = { {"start", 's', OPTPARSE_REQUIRED}, {"end", 'e', OPTPARSE_REQUIRED}, {"maxrows",'m', OPTPARSE_REQUIRED}, {"step", 261, OPTPARSE_REQUIRED}, {"enumds", 262, OPTPARSE_NONE}, {"json", 263, OPTPARSE_NONE}, {"showtime", 't', OPTPARSE_NONE}, {"daemon", 'd', OPTPARSE_REQUIRED}, {0} }; rrd_graph_init(&im); rrd_parsetime("end-24h", &start_tv); rrd_parsetime("now", &end_tv); int enumds=0; int json=0; int showtime=0; int opt; while ((opt = optparse_long(&options,longopts,NULL)) != -1){ switch (opt) { case 261: im.step = atoi(options.optarg); break; case 262: enumds=1; break; case 263: json=1; break; case 't': showtime=1; break; 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 'm': im.xsize = atol(options.optarg); if (im.xsize < 10) { rrd_set_error("maxrows below 10 rows"); return -1; } break; case 'd': { if (im.daemon_addr != NULL) { rrd_set_error ("You cannot specify --daemon " "more than once."); return (-1); } im.daemon_addr = strdup(options.optarg); if (im.daemon_addr == NULL) { rrd_set_error("strdup error"); 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 (%ld)", start_tmp); return -1; } if (end_tmp < start_tmp) { rrd_set_error("start (%ld) should be less than end (%ld)", start_tmp, end_tmp); return -1; } im.start = start_tmp; im.end = end_tmp; im.step = max((long) im.step, (im.end - im.start) / im.xsize); rrd_graph_script(options.argc, options.argv, &im, options.optind); if (rrd_test_error()) { im_free(&im); return -1; } if (im.gdes_c == 0) { rrd_set_error("can't make an xport without contents"); im_free(&im); return (-1); } { /* try to connect to rrdcached */ int status = rrdc_connect(im.daemon_addr); if (status != 0) return status; } if (rrd_xport_fn(&im, start, end, step, col_cnt, legend_v, data,0) == -1) { im_free(&im); return -1; } /* and create the export */ if (!xsize) { int flags=0; if (json) { flags|=1; } if (showtime) { flags|=2; } if (enumds) { flags|=4; } stringbuffer_t buffer={0,0,NULL,stdout}; rrd_xport_format_xmljson(flags,&buffer,&im, *start, *end, *step, *col_cnt, *legend_v, *data); } im_free(&im); return 0; }
int rrd_dump( int argc, char **argv) { int opt; struct optparse_long longopts[] = { {"daemon", 'd', OPTPARSE_REQUIRED}, {"header", 'h', OPTPARSE_REQUIRED}, {"no-header", 'n', OPTPARSE_NONE}, {0}, }; struct optparse options; int rc; /** * 0 = no header * 1 = dtd header * 2 = xsd header */ int opt_header = 1; char *opt_daemon = NULL; /* init rrd clean */ 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 'n': opt_header = 0; break; case 'h': if (strcmp(options.optarg, "dtd") == 0) { opt_header = 1; } else if (strcmp(options.optarg, "xsd") == 0) { opt_header = 2; } else if (strcmp(options.optarg, "none") == 0) { opt_header = 0; } break; default: rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}]\n" "[--no-header|-n]\n" "[--daemon|-d address]\n" "file.rrd [file.xml]", options.argv[0]); return (-1); break; } } /* while (opt != -1) */ if ((options.argc - options.optind) < 1 || (options.argc - options.optind) > 2) { rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}]\n" "[--no-header|-n]\n" "[--daemon|-d address]\n" "file.rrd [file.xml]", options.argv[0]); return (-1); } rc = rrdc_flush_if_daemon(opt_daemon, options.argv[options.optind]); if (opt_daemon) free(opt_daemon); if (rc) return (rc); if ((options.argc - options.optind) == 2) { rc = rrd_dump_opt_r(options.argv[options.optind], options.argv[options.optind + 1], opt_header); } else { rc = rrd_dump_opt_r(options.argv[options.optind], NULL, opt_header); } return rc; }
int rrd_tune( int argc, char **argv) { rrd_t rrd; int matches; int optcnt = 0; long ds; char ds_nam[DS_NAM_SIZE]; char ds_new[DS_NAM_SIZE]; long heartbeat; double min = 0; double max = 0; char dst[DST_SIZE]; int rc = -1; int opt_newstep = -1; rrd_file_t *rrd_file = NULL; char *opt_daemon = NULL; char double_str[ 41 ] = {0}; const char *in_filename = NULL; struct optparse_long longopts[] = { {"heartbeat", 'h', OPTPARSE_REQUIRED}, {"minimum", 'i', OPTPARSE_REQUIRED}, {"maximum", 'a', OPTPARSE_REQUIRED}, {"data-source-type", 'd', OPTPARSE_REQUIRED}, {"data-source-rename", 'r', OPTPARSE_REQUIRED}, /* added parameter tuning options for aberrant behavior detection */ {"deltapos", 'p', OPTPARSE_REQUIRED}, {"deltaneg", 'n', OPTPARSE_REQUIRED}, {"window-length", 'w', OPTPARSE_REQUIRED}, {"failure-threshold", 'f', OPTPARSE_REQUIRED}, {"alpha", 'x', OPTPARSE_REQUIRED}, {"beta", 'y', OPTPARSE_REQUIRED}, {"gamma", 'z', OPTPARSE_REQUIRED}, {"gamma-deviation", 'v', OPTPARSE_REQUIRED}, {"smoothing-window", 's', OPTPARSE_REQUIRED}, {"smoothing-window-deviation", 'S', OPTPARSE_REQUIRED}, {"aberrant-reset", 'b', OPTPARSE_REQUIRED}, // integration of rrd_modify functionality. {"step", 't', OPTPARSE_REQUIRED}, /* unfortunately, '-d' is already taken */ {"daemon", 'D', OPTPARSE_REQUIRED}, {0} }; struct optparse options; int opt; /* before we open the input RRD, we should flush it from any caching daemon, because we might totally rewrite it later on */ /* for this, we FIRST have to find the daemon, this means we must parse options twice... */ 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; default: break; } } // connect to daemon (will take care of environment variable automatically) if (rrdc_connect(opt_daemon) != 0) { rrd_set_error("Cannot connect to daemon"); return 1; } if (opt_daemon) { free(opt_daemon); opt_daemon = NULL; } if (options.optind < 0 || options.optind >= options.argc) { // missing file name... rrd_set_error("missing file name"); goto done; } /* NOTE: optparse_long reorders argv and places all NON option arguments to the back, starting with optind. This means the file name has travelled to options.argv[options.optind] */ in_filename = options.argv[options.optind]; if (rrdc_is_any_connected()) { // is it a good idea to just ignore the error ???? rrdc_flush(in_filename); rrd_clear_error(); } rrd_init(&rrd); rrd_file = rrd_open(in_filename, &rrd, RRD_READWRITE | RRD_READAHEAD | RRD_READVALUES); if (rrd_file == NULL) { goto done; } optparse_init(&options, argc, argv); /* re-initialize optparse */ while ((opt = optparse_long(&options, longopts, NULL)) != -1) { unsigned int strtod_ret_val; optcnt++; switch (opt) { case 'h': if ((matches = sscanf(options.optarg, DS_NAM_FMT ":%ld", ds_nam, &heartbeat)) != 2) { rrd_set_error("invalid arguments for heartbeat"); goto done; } if ((ds = ds_match(&rrd, ds_nam)) == -1) { goto done; } rrd.ds_def[ds].par[DS_mrhb_cnt].u_cnt = heartbeat; break; case 'i': matches = sscanf(options.optarg, DS_NAM_FMT ":%40[U0-9.e+-]", ds_nam, double_str); if ( matches == 2 ) { if (strcmp(double_str,"U") == 0) { min = DNAN; } else { strtod_ret_val = rrd_strtodbl( double_str, NULL, &min, NULL ); if ((strtod_ret_val != 2)) { rrd_set_error("invalid arguments for minimum ds value"); goto done; } } } else { rrd_set_error("invalid arguments for minimum ds value"); goto done; } if ((ds = ds_match(&rrd, ds_nam)) == -1) { goto done; } rrd.ds_def[ds].par[DS_min_val].u_val = min; break; case 'a': matches = sscanf(options.optarg, DS_NAM_FMT ":%40[U0-9.e+-]", ds_nam, double_str); if ( matches == 2 ) { if (strcmp(double_str,"U") == 0) { max = DNAN; } else { strtod_ret_val = rrd_strtodbl( double_str, NULL, &max, NULL ); if ((strtod_ret_val != 2)) { rrd_set_error("invalid arguments for maximum ds value"); goto done; } } } else { rrd_set_error("invalid arguments for maximum ds value"); goto done; } if ((ds = ds_match(&rrd, ds_nam)) == -1) { goto done; } rrd.ds_def[ds].par[DS_max_val].u_val = max; break; case 'd': if ((matches = sscanf(options.optarg, DS_NAM_FMT ":" DST_FMT, ds_nam, dst)) != 2) { rrd_set_error("invalid arguments for data source type"); goto done; } if ((ds = ds_match(&rrd, ds_nam)) == -1) { goto done; } if ((int) dst_conv(dst) == -1) { goto done; } /* only reset when something is changed */ if (strncmp(rrd.ds_def[ds].dst, dst, DST_SIZE - 1) != 0) { strncpy(rrd.ds_def[ds].dst, dst, DST_SIZE - 1); rrd.ds_def[ds].dst[DST_SIZE - 1] = '\0'; rrd.pdp_prep[ds].last_ds[0] = 'U'; rrd.pdp_prep[ds].last_ds[1] = 'N'; rrd.pdp_prep[ds].last_ds[2] = 'K'; rrd.pdp_prep[ds].last_ds[3] = 'N'; rrd.pdp_prep[ds].last_ds[4] = '\0'; } break; case 'r': if ((matches = sscanf(options.optarg, DS_NAM_FMT ":" DS_NAM_FMT, ds_nam, ds_new)) != 2) { rrd_set_error("invalid arguments for data source type"); goto done; } if ((ds = ds_match(&rrd, ds_nam)) == -1) { goto done; } strncpy(rrd.ds_def[ds].ds_nam, ds_new, DS_NAM_SIZE - 1); rrd.ds_def[ds].ds_nam[DS_NAM_SIZE - 1] = '\0'; break; case 'p': if (set_deltaarg(&rrd, RRA_delta_pos, options.optarg)) { goto done; } break; case 'n': if (set_deltaarg(&rrd, RRA_delta_neg, options.optarg)) { goto done; } break; case 'f': if (set_windowarg(&rrd, RRA_failure_threshold, options.optarg)) { goto done; } break; case 'w': if (set_windowarg(&rrd, RRA_window_len, options.optarg)) { goto done; } break; case 'x': if (set_hwarg(&rrd, CF_HWPREDICT, RRA_hw_alpha, options.optarg)) { if (set_hwarg(&rrd, CF_MHWPREDICT, RRA_hw_alpha, options.optarg)) { goto done; } rrd_clear_error(); } break; case 'y': if (set_hwarg(&rrd, CF_HWPREDICT, RRA_hw_beta, options.optarg)) { if (set_hwarg(&rrd, CF_MHWPREDICT, RRA_hw_beta, options.optarg)) { goto done; } rrd_clear_error(); } break; case 'z': if (set_hwarg(&rrd, CF_SEASONAL, RRA_seasonal_gamma, options.optarg)) { goto done; } break; case 'v': if (set_hwarg(&rrd, CF_DEVSEASONAL, RRA_seasonal_gamma, options.optarg)) { goto done; } break; case 'b': if (sscanf(options.optarg, DS_NAM_FMT, ds_nam) != 1) { rrd_set_error("invalid argument for aberrant-reset"); goto done; } if ((ds = ds_match(&rrd, ds_nam)) == -1) { /* ds_match handles it own errors */ goto done; } reset_aberrant_coefficients(&rrd, rrd_file, (unsigned long) ds); if (rrd_test_error()) { goto done; } break; case 's': if (atoi(rrd.stat_head->version) < atoi(RRD_VERSION4)) { strcpy(rrd.stat_head->version, RRD_VERSION4); /* smoothing_window causes Version 4 */ } if (set_hwsmootharg (&rrd, CF_SEASONAL, RRA_seasonal_smoothing_window, options.optarg)) { goto done; } break; case 'S': if (atoi(rrd.stat_head->version) < atoi(RRD_VERSION4)) { strcpy(rrd.stat_head->version, RRD_VERSION4); /* smoothing_window causes Version 4 */ } if (set_hwsmootharg (&rrd, CF_DEVSEASONAL, RRA_seasonal_smoothing_window, options.optarg)) { goto done; } break; case 't': opt_newstep = atoi(options.optarg); break; case 'D': // ignore, handled in previous argv parsing round break; case '?': rrd_set_error("%s", options.errmsg); goto done; } } if (optcnt > 0) { rrd_seek(rrd_file, 0, SEEK_SET); rrd_write(rrd_file, rrd.stat_head, sizeof(stat_head_t) * 1); rrd_write(rrd_file, rrd.ds_def, sizeof(ds_def_t) * rrd.stat_head->ds_cnt); /* need to write rra_defs for RRA parameter changes */ rrd_write(rrd_file, rrd.rra_def, sizeof(rra_def_t) * rrd.stat_head->rra_cnt); } if (options.optind >= options.argc) { int i; for (i = 0; i < (int) rrd.stat_head->ds_cnt; i++) if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) { printf("DS[%s] typ: %s\thbt: %ld\tmin: %1.4f\tmax: %1.4f\n", rrd.ds_def[i].ds_nam, rrd.ds_def[i].dst, rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt, rrd.ds_def[i].par[DS_min_val].u_val, rrd.ds_def[i].par[DS_max_val].u_val); } else { char *buffer = NULL; rpn_compact2str((rpn_cdefds_t *) & (rrd.ds_def[i].par[DS_cdef]), rrd.ds_def, &buffer); printf("DS[%s] typ: %s\tcdef: %s\n", rrd.ds_def[i].ds_nam, rrd.ds_def[i].dst, buffer); if (buffer) free(buffer); } } optind = handle_modify(&rrd, in_filename, options.argc, options.argv, options.optind + 1, opt_newstep); if (options.optind < 0) { goto done; } rc = 0; done: if (in_filename && rrdc_is_any_connected()) { // save any errors.... char *e = strdup(rrd_get_error()); // is it a good idea to just ignore the error ???? rrdc_forget(in_filename); rrd_clear_error(); if (e && *e) { rrd_set_error(e); } if (e) free(e); } if (rrd_file) { rrd_close(rrd_file); } rrd_free(&rrd); return rc; }
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 main(int argc,char* argv[]) { int res; //Initialize MPI stuff res = MPI_Init(&argc, &argv); if (res == MPI_SUCCESS) atexit(asynch_onexit); else { print_err("Failed to initialize MPI"); exit(EXIT_FAILURE); } //MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); MPI_Comm_size(MPI_COMM_WORLD, &np); //Command line options bool debug = false; bool help = false; bool version = false; //Parse command line struct optparse options; optparse_init(&options, argv); struct optparse_long longopts[] = { { "debug", 'd', OPTPARSE_NONE }, { "help", 'h', OPTPARSE_NONE }, { "version", 'v', OPTPARSE_NONE }, { 0 } }; int option; while ((option = optparse_long(&options, longopts, NULL)) != -1) { switch (option) { case 'd': debug = true; break; case 'h': help = true; break; case 'v': version = true; break; case '?': print_err("%s: %s\n", argv[0], options.errmsg); exit(EXIT_FAILURE); } } if (version) print_out("This is %s\n", PACKAGE_STRING); if (help) { print_out("Usage: asynch <global file>\n", PACKAGE_STRING); print_out( " -d [--debug] : Wait for the user input at the begining of the program (useful" \ " for attaching a debugger)\n" \ " -v [--version] : Print the current version of ASYNCH\n"); exit(EXIT_SUCCESS); } if (version || help) exit(EXIT_SUCCESS); //Parse remaining arguments char *global_filename = optparse_arg(&options); if (global_filename == NULL && my_rank == 0) { print_err("Command line parameter required: A universal variable file (.gbl).\n"); exit(EXIT_FAILURE); } if (debug) { //Disable stdout buffering setvbuf(stdout, NULL, _IONBF, 0); //When the program first starts to execute, at the very beginning of our program, we //ask the user to type some sort of input to simply stall the application until start your //"Attach to Process" and you can attach to all the different threads in your program. if (my_rank == 0) { printf("You may now attach the debugger then press enter.\n"); //fflush(stdout); getchar(); } MPI_Barrier(MPI_COMM_WORLD); // All threads will wait here until you give thread 0 an input } //Declare variables double start, stop; double total_time; asynchsolver* asynch; print_out("\nBeginning initialization...\n*****************************\n"); MPI_Barrier(MPI_COMM_WORLD); start = MPI_Wtime(); //Init asynch object and the river network asynch = Asynch_Init(MPI_COMM_WORLD,&argc,&argv); print_out("Reading global file...\n"); Asynch_Parse_GBL(asynch,global_filename); print_out("Loading network...\n"); Asynch_Load_Network(asynch); print_out("Partitioning network...\n"); Asynch_Partition_Network(asynch); print_out("Loading parameters...\n"); Asynch_Load_Network_Parameters(asynch,0); print_out("Reading dam and reservoir data...\n"); Asynch_Load_Dams(asynch); print_out("Setting up numerical error data...\n"); Asynch_Load_Numerical_Error_Data(asynch); print_out("Initializing model...\n"); Asynch_Initialize_Model(asynch); print_out("Loading initial conditions...\n"); Asynch_Load_Initial_Conditions(asynch); print_out("Loading forcings...\n"); Asynch_Load_Forcings(asynch); print_out("Loading output data information...\n"); Asynch_Load_Save_Lists(asynch); print_out("Finalizing network...\n"); Asynch_Finalize_Network(asynch); print_out("Calculating initial step sizes...\n"); Asynch_Calculate_Step_Sizes(asynch); if(my_rank == 0) { printf("\nModel type is %u.\nGlobal parameters are:\n",asynch->GlobalVars->type); Print_Vector(asynch->GlobalVars->global_params); printf("\n"); } //Setup output for link id, if needed int id_setup = Asynch_Check_Output(asynch,"LinkID"); if(id_setup != -1) { Set_Output_User_LinkID(asynch); Asynch_Set_Output(asynch,"LinkID",ASYNCH_INT,&Output_Linkid,NULL,0); } //Prepare output files Asynch_Prepare_Temp_Files(asynch); Asynch_Write_Current_Step(asynch); //!!!! Wow, this sucks. Is there a way to get rid of it? !!!! Asynch_Prepare_Peakflow_Output(asynch); Asynch_Prepare_Output(asynch); //Make sure everyone is good before getting down to it... printf("Process %i (%i total) is good to go with %i links.\n",my_rank,np,asynch->my_N); ASYNCH_SLEEP(1); MPI_Barrier(MPI_COMM_WORLD); if(my_rank == 0) { stop = MPI_Wtime(); total_time = stop - start; printf("Finished initialization. Total time for initialization: %f\n\n\nComputing solution at each link...\n************************************\n", stop - start); } fflush(stdout); MPI_Barrier(MPI_COMM_WORLD); //Perform the calculations start = MPI_Wtime(); Asynch_Advance(asynch,1); MPI_Barrier(MPI_COMM_WORLD); stop = MPI_Wtime(); //Out information total_time += stop - start; print_out("\nComputations complete. Total time for calculations: %f\n", stop - start); if(asynch->sys[asynch->my_sys[0]]->c == NULL) { printf("[%i]: The solution at ID %i at time %.12f is\n",my_rank,asynch->sys[asynch->my_sys[0]]->ID,asynch->sys[asynch->my_sys[0]]->last_t); Print_Vector(asynch->sys[asynch->my_sys[0]]->list->tail->y_approx); } //Take a snapshot Asynch_Take_System_Snapshot(asynch,NULL); //Create output files Asynch_Create_Output(asynch,NULL); Asynch_Create_Peakflows_Output(asynch); //Clean up Asynch_Delete_Temporary_Files(asynch); Asynch_Free(asynch); return 0; }
int main( int argc, char *argv[]) { char *buffer; long i; long filter = 0; struct optparse_long longopts[] = { {"filter", 'f', OPTPARSE_NONE}, {0}, }; struct optparse options; int opt; #ifdef MUST_DISABLE_SIGFPE signal(SIGFPE, SIG_IGN); #endif #ifdef MUST_DISABLE_FPMASK fpsetmask(0); #endif /* what do we get for cmdline arguments? for (i=0;i<argc;i++) printf("%d-'%s'\n",i,argv[i]); */ optparse_init(&options, argc, argv); while ((opt = optparse_long(&options,longopts,NULL)) != -1) { switch (opt) { case 'f': filter = 1; break; case '?': printf("%s\n", options.errmsg); return -1; } } if (!filter) { rrdcgiDebug(0, 0); rrdcgiArg = rrdcgiInit(); } /* make sure we have one extra argument, if there are others, we do not care Apache gives several */ /* if ( (options.optind != options.argc-2 && strstr( getenv("SERVER_SOFTWARE"),"Apache/2") != NULL) && options.optind != options.argc-1) { */ if (options.optind >= options.argc) { fprintf(stderr, "ERROR: expected a filename\n"); exit(1); } else { readfile(options.argv[options.optind], &buffer, 1); } if (rrd_test_error()) { fprintf(stderr, "ERROR: %s\n", rrd_get_error()); exit(1); } /* initialize variable heap */ initvar(); #ifdef DEBUG_PARSER /* some fake header for testing */ printf("Content-Type: text/html\nContent-Length: 10000000\n\n\n"); #endif /* expand rrd directives in buffer recursivley */ for (i = 0; buffer[i]; i++) { if (buffer[i] != '<') continue; if (!filter) { parse(&buffer, i, "<RRD::CV", cgiget); parse(&buffer, i, "<RRD::CV::PATH", cgigetqp); parse(&buffer, i, "<RRD::CV::QUOTE", cgigetq); parse(&buffer, i, "<RRD::GETENV", rrdgetenv); } parse(&buffer, i, "<RRD::GETVAR", rrdgetvar); parse(&buffer, i, "<RRD::GOODFOR", rrdgoodfor); parse(&buffer, i, "<RRD::GRAPH", drawgraph); parse(&buffer, i, "<RRD::INCLUDE", includefile); parse(&buffer, i, "<RRD::PRINT", drawprint); parse(&buffer, i, "<RRD::SETCONSTVAR", rrdsetvarconst); parse(&buffer, i, "<RRD::SETENV", rrdsetenv); parse(&buffer, i, "<RRD::SETVAR", rrdsetvar); parse(&buffer, i, "<RRD::TIME::LAST", printtimelast); parse(&buffer, i, "<RRD::TIME::NOW", printtimenow); parse(&buffer, i, "<RRD::TIME::STRFTIME", printstrftime); parse(&buffer, i, "<RRD::INTERNAL", rrdgetinternal); } if (!filter) { printf("Content-Type: text/html\n" "Content-Length: %zd\n", strlen(buffer)); if (labs(goodfor) > 0) { time_t now; now = time(NULL); printf("Last-Modified: %s\n", http_time(&now)); now += labs(goodfor); printf("Expires: %s\n", http_time(&now)); if (goodfor < 0) { printf("Refresh: %ld\n", labs(goodfor)); } } printf("\n"); } /* output result */ printf("%s", buffer); /* cleanup */ calfree(); if (buffer) { free(buffer); } donevar(); exit(0); }
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); }