コード例 #1
0
ファイル: rwaggbag.c プロジェクト: bbayles/netsa-pkg
/*
 *  appSetup(argc, argv);
 *
 *    Perform all the setup for this application include setting up
 *    required modules, parsing options, etc.  This function should be
 *    passed the same arguments that were passed into main().
 *
 *    Returns to the caller if all setup succeeds.  If anything fails,
 *    this function will cause the application to exit with a FAILURE
 *    exit status.
 */
static void
appSetup(
    int                 argc,
    char              **argv)
{
    SILK_FEATURES_DEFINE_STRUCT(features);
    unsigned int optctx_flags;
    int rv;

    /* verify same number of options and help strings */
    assert((sizeof(appHelp)/sizeof(char *)) ==
           (sizeof(appOptions)/sizeof(struct option)));

    /* register the application */
    skAppRegister(argv[0]);
    skAppVerifyFeatures(&features, NULL);
    skOptionsSetUsageCallback(&appUsageLong);

    /* initialize globals */
    memset(&ab_options, 0, sizeof(sk_aggbag_options_t));
    ab_options.existing_silk_files = 1;
    ab_options.argc = argc;
    ab_options.argv = argv;

    optctx_flags = (SK_OPTIONS_CTX_INPUT_SILK_FLOW | SK_OPTIONS_CTX_ALLOW_STDIN
                    | SK_OPTIONS_CTX_XARGS | SK_OPTIONS_CTX_COPY_INPUT
                    | SK_OPTIONS_CTX_PRINT_FILENAMES);

    /* register the options */
    if (skOptionsCtxCreate(&optctx, optctx_flags)
        || skOptionsCtxOptionsRegister(optctx)
        || skOptionsRegister(appOptions, &appOptionsHandler, NULL)
        || skAggBagOptionsRegister(&ab_options)
        || skIPv6PolicyOptionsRegister(&ipv6_policy)
        || sksiteOptionsRegister(SK_SITE_FLAG_CONFIG_FILE))
    {
        skAppPrintErr("Unable to register options");
        exit(EXIT_FAILURE);
    }

    /* register the teardown handler */
    if (atexit(appTeardown) < 0) {
        skAppPrintErr("Unable to register appTeardown() with atexit()");
        exit(EXIT_FAILURE);
    }

    /* parse options */
    rv = skOptionsCtxOptionsParse(optctx, argc, argv);
    if (rv < 0) {
        skAppUsage();           /* never returns */
    }

    /* try to load site config file; if it fails, we will not be able
     * to resolve flowtype and sensor from input file names, but we
     * should not consider it a complete failure */
    sksiteConfigure(0);

    /* make sure the user specified at least one key field and one
     * counter field */
    if (keys_arg == NULL || keys_arg[0] == '\0') {
        skAppPrintErr("The --%s switch is required",
                      appOptions[OPT_KEYS].name);
        skAppUsage();         /* never returns */
    }
    if (counters_arg == NULL || counters_arg[0] == '\0') {
        skAppPrintErr("The --%s switch is required",
                      appOptions[OPT_COUNTERS].name);
        skAppUsage();         /* never returns */
    }

    /* set up the key_name_map and counter_name_map */
    if (createStringmaps()) {
        exit(EXIT_FAILURE);
    }

    /* create the aggregate bag */
    if (skAggBagCreate(&ab)) {
        exit(EXIT_FAILURE);
    }
    skAggBagOptionsBind(ab, &ab_options);

    /* parse the --keys and --counters switches */
    if (parseFields(key_name_map, keys_arg, OPT_KEYS)) {
        exit(EXIT_FAILURE);
    }
    if (parseFields(counter_name_map, counters_arg, OPT_COUNTERS)) {
        exit(EXIT_FAILURE);
    }

    /* create output stream to stdout if no --output-path was given */
    if (NULL == output) {
        if ((rv = skStreamCreate(&output, SK_IO_WRITE, SK_CONTENT_SILK))
            || (rv = skStreamBind(output, "-")))
        {
            skStreamPrintLastErr(output, rv, &skAppPrintErr);
            skStreamDestroy(&output);
            exit(EXIT_FAILURE);
        }
    }

    /* make certain stdout is not being used for multiple outputs */
    if (skStreamIsStdout(output) && skOptionsCtxCopyStreamIsStdout(optctx)) {
        skAppPrintErr("May not use stdout for multiple output streams");
        exit(EXIT_FAILURE);
    }

    /* open the output stream but do not write anything yet */
    rv = skStreamOpen(output);
    if (rv) {
        skStreamPrintLastErr(output, rv, &skAppPrintErr);
        skStreamDestroy(&output);
        exit(EXIT_FAILURE);
    }

    /* open the --copy-input stream */
    if (skOptionsCtxOpenStreams(optctx, &skAppPrintErr)) {
        exit(EXIT_FAILURE);
    }

    return;                       /* OK */
}
コード例 #2
0
ファイル: rwcountsetup.c プロジェクト: brettmeyers/silk
/*
 *  appSetup(argc, argv);
 *
 *    Perform all the setup for this application include setting up
 *    required modules, parsing options, etc.  This function should be
 *    passed the same arguments that were passed into main().
 *
 *    Returns to the caller if all setup succeeds.  If anything fails,
 *    this function will cause the application to exit with a FAILURE
 *    exit status.
 */
void
appSetup(
    int                 argc,
    char              **argv)
{
    SILK_FEATURES_DEFINE_STRUCT(features);
    int optctx_flags;
    sktime_t t;
    unsigned int end_precision;
    unsigned int is_epoch;
    int64_t bin_count;
    int rv;

    /* make sure count of option's declarations and help-strings match */
    assert((sizeof(appOptions)/sizeof(struct option)) ==
           (sizeof(appHelp)/sizeof(char *)));

    /* register the application */
    skAppRegister(argv[0]);
    skAppVerifyFeatures(&features, NULL);
    skOptionsSetUsageCallback(&appUsageLong);

    /* initialize globals */
    memset(&flags, 0, sizeof(flags));
    flags.delimiter = '|';
    flags.load_scheme = DEFAULT_LOAD_SCHEME;

    memset(&output, 0, sizeof(output));
    output.of_fp = stdout;

    memset(&bins, 0, sizeof(bins));
    bins.start_time = RWCO_UNINIT_START;
    bins.end_time = RWCO_UNINIT_END;
    bins.size = DEFAULT_BINSIZE;

    optctx_flags = (SK_OPTIONS_CTX_INPUT_SILK_FLOW | SK_OPTIONS_CTX_ALLOW_STDIN
                    | SK_OPTIONS_CTX_XARGS | SK_OPTIONS_CTX_PRINT_FILENAMES
                    | SK_OPTIONS_CTX_COPY_INPUT);

    /* register the options */
    if (skOptionsCtxCreate(&optctx, optctx_flags)
        || skOptionsCtxOptionsRegister(optctx)
        || skOptionsRegister(appOptions, &appOptionsHandler, NULL)
        || skOptionsRegister(deprecatedOptions, &appOptionsHandler, NULL)
        || skOptionsRegister(deprecatedOptionsShort, &appOptionsHandler, NULL)
        || sksiteOptionsRegister(SK_SITE_FLAG_CONFIG_FILE))
    {
        skAppPrintErr("Unable to register options");
        exit(EXIT_FAILURE);
    }

    /* register the teardown handler */
    if (atexit(appTeardown) < 0) {
        skAppPrintErr("Unable to register appTeardown() with atexit()");
        appTeardown();
        exit(EXIT_FAILURE);
    }

    /* parse options; print usage if error */
    rv = skOptionsCtxOptionsParse(optctx, argc, argv);
    if (rv < 0) {
        skAppUsage();
    }

    /* try to load site config file; if it fails, we will not be able
     * to resolve flowtype and sensor from input file names */
    sksiteConfigure(0);

    /* parse the times */
    if (start_time) {
        rv = skStringParseDatetime(&bins.start_time, start_time, NULL);
        if (rv) {
            skAppPrintErr("Invalid %s '%s': %s",
                          appOptions[OPT_START_TIME].name, start_time,
                          skStringParseStrerror(rv));
            exit(EXIT_FAILURE);
        }
    }

    if (end_time) {
        rv = skStringParseDatetime(&t, end_time, &end_precision);
        if (rv) {
            skAppPrintErr("Invalid %s '%s': %s",
                          appOptions[OPT_END_TIME].name, end_time,
                          skStringParseStrerror(rv));
            exit(EXIT_FAILURE);
        }
        /* get the precision; treat epoch time as seconds resolution
         * unless its precision is already seconds or milliseconds */
        is_epoch = SK_PARSED_DATETIME_EPOCH & end_precision;
        end_precision &= SK_PARSED_DATETIME_MASK_PRECISION;
        if (is_epoch && end_precision < SK_PARSED_DATETIME_SECOND) {
            end_precision = SK_PARSED_DATETIME_SECOND;
        }

        if (start_time) {
            /* move end-time to its ceiling */
            skDatetimeCeiling(&t, &t, end_precision);
            ++t;

            /* verify times */
            if (t <= bins.start_time) {
                char buf_s[SKTIMESTAMP_STRLEN];
                char buf_e[SKTIMESTAMP_STRLEN];
                skAppPrintErr("The %s is less than %s: %s < %s",
                              appOptions[OPT_END_TIME].name,
                              appOptions[OPT_START_TIME].name,
                              sktimestamp_r(buf_e, t, SKTIMESTAMP_NOMSEC),
                              sktimestamp_r(buf_s, bins.start_time,
                                            SKTIMESTAMP_NOMSEC));
                exit(EXIT_FAILURE);
            }

            /* make certain end-time fails on a boundary by computing
             * the number of bins required to hold the end-time.  we
             * subtract 1 then add 1 to get a valid count whether or
             * not the division would have a remainder. */
            bin_count = 1 + ((t - bins.start_time - 1) / bins.size);
            bins.end_time = bins.start_time + bins.size * bin_count;
        } else {
            /* when only end_time is given, create bins up to its
             * ceiling value */
            bins.end_time = t;
            skDatetimeCeiling(&t, &t, end_precision);
            ++t;
            /* determine the number of bins between the end-time
             * specified by the user and the ceiling of that end-time,
             * then increase the end time by that number of bins */
            bin_count = 1 + ((t - bins.end_time - 1) / bins.size);
            bins.end_time += bin_count * bins.size;
        }
    }

    /* make certain stdout is not being used for multiple outputs */
    if (skOptionsCtxCopyStreamIsStdout(optctx)) {
        if ((NULL == output.of_name)
            || (0 == strcmp(output.of_name, "-"))
            || (0 == strcmp(output.of_name, "stdout")))
        {
            skAppPrintErr("May not use stdout for multiple output streams");
            exit(EXIT_FAILURE);
        }
    }

    /* open the --output-path: the 'of_name' member is non-NULL when
     * the switch is given */
    if (output.of_name) {
        rv = skFileptrOpen(&output, SK_IO_WRITE);
        if (rv) {
            skAppPrintErr("Cannot open '%s': %s",
                          output.of_name, skFileptrStrerror(rv));
            exit(EXIT_FAILURE);
        }
    }

    /* looks good, open the --copy-input destination */
    if (skOptionsCtxOpenStreams(optctx, &skAppPrintErr)) {
        exit(EXIT_FAILURE);
    }

    return;                     /* OK */
}