示例#1
0
/**
 * Callback function to use with INIH for parsing histogram configs
 * @arg user Opaque value. Actually a statsite_config pointer
 * @arg name The config name
 * @value = The config value
 * @return 1 on success
 */
static int histogram_callback(void* user, const char* section, const char* name, const char* value) {
    // Make sure we don't change sections with an unfinished config
    if (in_progress && strcasecmp(histogram_section, section)) {
        syslog(LOG_WARNING, "Unfinished configuration for section: %s", histogram_section);
        return 0;
    }

    // Ensure we have something in progress
    if (!in_progress) {
        in_progress = calloc(1, sizeof(histogram_config));
        histogram_section = strdup(section);
    }

    // Cast the user handle
    statsite_config *config = (statsite_config*)user;

    // Switch on the config
    #define NAME_MATCH(param) (strcasecmp(param, name) == 0)

    int res = 1;
    if (NAME_MATCH("prefix")) {
        in_progress->parts |= 1;
        in_progress->prefix = strdup(value);

    } else if (NAME_MATCH("min")) {
        in_progress->parts |= 1 << 1;
        res = value_to_double(value, &in_progress->min_val);

    } else if (NAME_MATCH("max")) {
        in_progress->parts |= 1 << 2;
        res = value_to_double(value, &in_progress->max_val);

    } else if (NAME_MATCH("width")) {
        in_progress->parts |= 1 << 3;
        res = value_to_double(value, &in_progress->bin_width);

    } else {
        syslog(LOG_NOTICE, "Unrecognized histogram config parameter: %s", value);
    }

    // Check if this config is done, and push into the list of configs
    if (in_progress->parts == 15) {
        in_progress->next = config->hist_configs;
        config->hist_configs = in_progress;
        in_progress = NULL;
        free(histogram_section);
        histogram_section = NULL;
    }
    return res;
}
示例#2
0
/**
 * Callback function to use with INI-H.
 * @arg user Opaque user value. We use the statsite_config pointer
 * @arg section The INI seciton
 * @arg name The config name
 * @arg value The config value
 * @return 1 on success.
 */
static int config_callback(void* user, const char* section, const char* name, const char* value) {
    // Ignore any non-statsite sections
    if (strcasecmp("statsite", section) != 0) {
        return 0;
    }

    // Cast the user handle
    statsite_config *config = (statsite_config*)user;

    // Switch on the config
    #define NAME_MATCH(param) (strcasecmp(param, name) == 0)

    // Handle the int cases
    if (NAME_MATCH("port")) {
        return value_to_int(value, &config->tcp_port);
    } else if (NAME_MATCH("tcp_port")) {
        return value_to_int(value, &config->tcp_port);
    } else if (NAME_MATCH("udp_port")) {
        return value_to_int(value, &config->udp_port);
    } else if (NAME_MATCH("flush_interval")) {
         return value_to_int(value, &config->flush_interval);
    } else if (NAME_MATCH("daemonize")) {
        return value_to_bool(value, &config->daemonize);

    // Handle the double cases
    } else if (NAME_MATCH("timer_eps")) {
        return value_to_double(value, &config->timer_eps);

    // Copy the string values
    } else if (NAME_MATCH("log_level")) {
        config->log_level = strdup(value);
    } else if (NAME_MATCH("stream_cmd")) {
        config->stream_cmd = strdup(value);
    } else if (NAME_MATCH("pid_file")) {
        config->pid_file = strdup(value);

    // Unknown parameter?
    } else {
        // Log it, but ignore
        syslog(LOG_NOTICE, "Unrecognized config parameter: %s", value);
    }

    // Success
    return 1;
}
示例#3
0
/**
 * Callback function to use with INI-H.
 * @arg user Opaque user value. We use the statsite_config pointer
 * @arg section The INI seciton
 * @arg name The config name
 * @arg value The config value
 * @return 1 on success.
 */
static int config_callback(void* user, const char* section, const char* name, const char* value) {
    // Specially handle histogram sections
    if (strncasecmp("histogram", section, 9) == 0) {
        return histogram_callback(user, section, name, value);
    }

    // Ignore any non-statsite sections
    if (strcasecmp("statsite", section) != 0) {
        return 0;
    }

    // Cast the user handle
    statsite_config *config = (statsite_config*)user;

    // Switch on the config
    #define NAME_MATCH(param) (strcasecmp(param, name) == 0)

    // Handle the int cases
    if (NAME_MATCH("port")) {
        return value_to_int(value, &config->tcp_port);
    } else if (NAME_MATCH("tcp_port")) {
        return value_to_int(value, &config->tcp_port);
    } else if (NAME_MATCH("udp_port")) {
        return value_to_int(value, &config->udp_port);
    } else if (NAME_MATCH("flush_interval")) {
         return value_to_int(value, &config->flush_interval);
    } else if (NAME_MATCH("parse_stdin")) {
        return value_to_bool(value, &config->parse_stdin);
    } else if (NAME_MATCH("daemonize")) {
        return value_to_bool(value, &config->daemonize);
    } else if (NAME_MATCH("binary_stream")) {
        return value_to_bool(value, &config->binary_stream);
    } else if (NAME_MATCH("use_type_prefix")) {
        return value_to_bool(value, &config->use_type_prefix);
    } else if (NAME_MATCH("extended_counters")) {
        return value_to_bool(value, &config->extended_counters);

    // Handle the double cases
    } else if (NAME_MATCH("timer_eps")) {
        return value_to_double(value, &config->timer_eps);
    } else if (NAME_MATCH("set_eps")) {
        return value_to_double(value, &config->set_eps);

    // Copy the string values
    } else if (NAME_MATCH("log_level")) {
        config->log_level = strdup(value);
    } else if (NAME_MATCH("stream_cmd")) {
        config->stream_cmd = strdup(value);
    } else if (NAME_MATCH("pid_file")) {
        config->pid_file = strdup(value);
    } else if (NAME_MATCH("input_counter")) {
        config->input_counter = strdup(value);
    } else if (NAME_MATCH("bind_address")) {
        config->bind_address = strdup(value);
    } else if (NAME_MATCH("global_prefix")) {
        config->global_prefix = strdup(value);
    } else if (NAME_MATCH("counts_prefix")) {
        config->prefixes[COUNTER] = strdup(value);
    } else if (NAME_MATCH("gauges_prefix")) {
        config->prefixes[GAUGE] = strdup(value);
    } else if (NAME_MATCH("timers_prefix")) {
        config->prefixes[TIMER] = strdup(value);
    } else if (NAME_MATCH("sets_prefix")) {
        config->prefixes[SET] = strdup(value);
    } else if (NAME_MATCH("kv_prefix")) {
        config->prefixes[KEY_VAL] = strdup(value);

    // Unknown parameter?
    } else {
        // Log it, but ignore
        syslog(LOG_NOTICE, "Unrecognized config parameter: %s", name);
    }

    // Success
    return 1;
}
示例#4
0
/**
 * Callback function to use with INIH for parsing sink configurations.
 * The sink type is currently encoded into the section name to
 * simplify instantiating the correct configuration type.
 */
static int sink_callback(void* user, const char* section, const char* name, const char* value) {

    statsite_config *all_config = (statsite_config*)user;

    /* The sink section does not match - lets commit since we're on to a new one now */
    if (sink_in_progress && strcasecmp(sink_section, section)) {
        sink_commit(all_config);
    }

    /* Nothing in progress? Create it! */
    if (!sink_in_progress) {
        if (!sink_section)
            sink_section = strdup(section);

        char* section_to_tokenize = strdup(section);
        char* tok = NULL;
        char* header = strtok_r(section_to_tokenize, "_", &tok);
        char* type = strtok_r(NULL, "_", &tok);
        char* name = strtok_r(NULL, "_", &tok);

        if (header == NULL || type == NULL || name == NULL) {
            free(section_to_tokenize);
            syslog(LOG_WARNING, "Sink section %s is not of the form \"sink_[type]_[name]\"", section);
            return 0;
        }
        /* Match various sink types to find their type */
        if (strcasecmp(type, "stream") == 0) {
            sink_config_stream* config = calloc(1, sizeof(sink_config_stream));
            sink_in_progress = (sink_config*)config;
            config->super.type = SINK_TYPE_STREAM;
            config->super.name = strdup(name);
            config->stream_cmd = DEFAULT_SINK.stream_cmd;
        } else if (strcasecmp(type, "http") == 0) {
            sink_config_http* config = malloc(sizeof(sink_config_http));

            memcpy(config, &DEFAULT_HTTP_SINK, sizeof(sink_config_http));
            sink_in_progress = (sink_config*)config;
            config->super.name = strdup(name);
        } else {
            free(section_to_tokenize);
            /* Unknown sink type - abort! */
            syslog(LOG_WARNING, "Unknown sink type: %s for sink: %s", type, name);
            return 0;
        }
        free(section_to_tokenize);
    }

    switch(sink_in_progress->type) {
    case SINK_TYPE_STREAM:
    {
        sink_config_stream* config = (sink_config_stream*)sink_in_progress;
        if (NAME_MATCH("binary")) {
            return value_to_bool(value, &config->binary_stream);
        } else if (NAME_MATCH("command")) {
            config->stream_cmd = strdup(value);
        } else {
            syslog(LOG_NOTICE, "Unrecognized stream sink parameter: %s", name);
            return 0;
        }
        break;
    }
    case SINK_TYPE_HTTP:
    {
        sink_config_http* config = (sink_config_http*)sink_in_progress;
        if (NAME_MATCH("url")) {
            config->post_url = strdup(value);
        } else if (NAME_MATCH("metrics_name")) {
            config->metrics_name = strdup(value);
        } else if (NAME_MATCH("timestamp_name")) {
            config->timestamp_name = strdup(value);
        } else if (NAME_MATCH("timestamp_format")) {
            config->timestamp_format = strdup(value);
        } else if (NAME_MATCH("ciphers")) {
            config->ciphers = strdup(value);
        } else if (NAME_MATCH("oauth_key")) {
            config->oauth_key = strdup(value);
        } else if (NAME_MATCH("oauth_secret")) {
            config->oauth_secret = strdup(value);
        } else if (NAME_MATCH("oauth_token_url")) {
            config->oauth_token_url = strdup(value);
        } else if (NAME_MATCH("max_buffer_size")) {
            value_to_int(value, &config->max_buffer_size);
        } else if (NAME_MATCH("send_backoff_ms")) {
            value_to_int(value, &config->send_backoff_ms);
        } else {
            /* Attempt to locate keys
             * of the form param_PNAME */
            char* param_tok = strdup(name);
            char* tok = NULL;
            char* header = strtok_r(param_tok, "_", &tok);
            char* param = strtok_r(NULL, "_", &tok);
            if (header == NULL || param == NULL || strcasecmp("param", header) != 0) {
                syslog(LOG_NOTICE, "Unrecognized http sink parameters: %s: %s", name, header);
                free(param_tok);
                return 0;
            }
            kv_config* last_kv = config->params;
            config->params = calloc(1, sizeof(kv_config));
            config->params->k = strdup(param);
            config->params->v = strdup(value);
            config->params->next = last_kv;
            free(param_tok);
        }
        break;
    }
    default:
        syslog(LOG_WARNING, "Grevious state problem");
        return 0;
    }

    return 1;
}
示例#5
0
文件: config.c 项目: geetarista/hlld
/**
 * Callback function to use with INI-H.
 * @arg user Opaque user value. We use the hlld_config pointer
 * @arg section The INI seciton
 * @arg name The config name
 * @arg value The config value
 * @return 1 on success.
 */
static int config_callback(void* user, const char* section, const char* name, const char* value) {
    // Ignore any non-hlld sections
    if (strcasecmp("hlld", section) != 0) {
        return 0;
    }

    // Cast the user handle
    hlld_config *config = (hlld_config*)user;

    // Switch on the config
#define NAME_MATCH(param) (strcasecmp(param, name) == 0)

    // Handle the int cases
    if (NAME_MATCH("port")) {
        return value_to_int(value, &config->tcp_port);
    } else if (NAME_MATCH("tcp_port")) {
        return value_to_int(value, &config->tcp_port);
    } else if (NAME_MATCH("udp_port")) {
        return value_to_int(value, &config->udp_port);
    } else if (NAME_MATCH("flush_interval")) {
        return value_to_int(value, &config->flush_interval);
    } else if (NAME_MATCH("cold_interval")) {
        return value_to_int(value, &config->cold_interval);
    } else if (NAME_MATCH("in_memory")) {
        return value_to_int(value, &config->in_memory);
    } else if (NAME_MATCH("use_mmap")) {
        return value_to_int(value, &config->use_mmap);
    } else if (NAME_MATCH("workers")) {
        return value_to_int(value, &config->worker_threads);
    } else if (NAME_MATCH("default_precision")) {
        int res = value_to_int(value, &config->default_precision);
        // Compute expected error given precision
        config->default_eps = hll_error_for_precision(config->default_precision);
        return res;

        // Handle the double cases
    } else if (NAME_MATCH("default_eps")) {
        int res = value_to_double(value, &config->default_eps);
        // Compute required precision given error
        config->default_precision = hll_precision_for_error(config->default_eps);

        // Compute error given precision. This is kinda strange but it is done
        // since its not possible to hit all epsilons perfectly, but we try to get
        // the eps provided to be the upper bound. This value is the actual eps.
        config->default_eps = hll_error_for_precision(config->default_precision);
        return res;

        // Copy the string values
    } else if (NAME_MATCH("data_dir")) {
        config->data_dir = strdup(value);
    } else if (NAME_MATCH("log_level")) {
        config->log_level = strdup(value);

        // Unknown parameter?
    } else {
        // Log it, but ignore
        syslog(LOG_NOTICE, "Unrecognized config parameter: %s", value);
    }

    // Success
    return 1;
}