X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Ftail.c;h=8c9dfbe539ad4f119dd3c6d34ef1e36ea5392a2a;hp=1b720b83af2fe8e52b1a58010ac29ebb9c2e6fdd;hb=54619dc85fd308b21ed09a0271e5c7383c7921b9;hpb=c144ae4659e129a929afb67706a54604220fef43 diff --git a/src/tail.c b/src/tail.c index 1b720b83..8c9dfbe5 100644 --- a/src/tail.c +++ b/src/tail.c @@ -26,15 +26,16 @@ #include "collectd.h" -#include "common.h" #include "plugin.h" -#include "utils_latency_config.h" +#include "utils/common/common.h" +#include "utils/latency/latency_config.h" #include "utils_tail_match.h" /* * * - * Instance "exim" + * Plugin "mail" + * Instance "exim" * Interval 60 * * Regex "S=([1-9][0-9]*)" @@ -53,14 +54,17 @@ struct ctail_config_match_s { int flags; char *type; char *type_instance; - cdtime_t interval; latency_config_t latency; }; typedef struct ctail_config_match_s ctail_config_match_t; -static cu_tail_match_t **tail_match_list = NULL; -static size_t tail_match_list_num = 0; -static cdtime_t tail_match_list_intervals[255]; +static size_t tail_file_num; + +static int ctail_read(user_data_t *ud); + +static void ctail_match_free(void *arg) { + tail_match_destroy((cu_tail_match_t *)arg); +} /* void ctail_match_free */ static int ctail_config_add_match_dstype(ctail_config_match_t *cm, oconfig_item_t *ci) { @@ -91,7 +95,7 @@ static int ctail_config_add_match_dstype(ctail_config_match_t *cm, } else if (strcasecmp("Distribution", ds_type) == 0) { cm->flags = UTILS_MATCH_DS_TYPE_GAUGE | UTILS_MATCH_CF_GAUGE_DIST; - int status = latency_config(&cm->latency, ci, "tail"); + int status = latency_config(&cm->latency, ci); if (status != 0) return status; } else if (strncasecmp("Counter", ds_type, strlen("Counter")) == 0) { @@ -133,9 +137,9 @@ static int ctail_config_add_match_dstype(ctail_config_match_t *cm, return 0; } /* int ctail_config_add_match_dstype */ -static int ctail_config_add_match(cu_tail_match_t *tm, +static int ctail_config_add_match(cu_tail_match_t *tm, const char *plugin_name, const char *plugin_instance, - oconfig_item_t *ci, cdtime_t interval) { + oconfig_item_t *ci) { ctail_config_match_t cm = {0}; int status; @@ -191,8 +195,9 @@ static int ctail_config_add_match(cu_tail_match_t *tm, if (status == 0) { // TODO(octo): there's nothing "simple" about the latency stuff … status = tail_match_add_match_simple( - tm, cm.regex, cm.excluderegex, cm.flags, "tail", plugin_instance, - cm.type, cm.type_instance, cm.latency, interval); + tm, cm.regex, cm.excluderegex, cm.flags, + (plugin_name != NULL) ? plugin_name : "tail", plugin_instance, cm.type, + cm.type_instance, cm.latency); if (status != 0) ERROR("tail plugin: tail_match_add_match_simple failed."); @@ -210,6 +215,7 @@ static int ctail_config_add_match(cu_tail_match_t *tm, static int ctail_config_add_file(oconfig_item_t *ci) { cu_tail_match_t *tm; cdtime_t interval = 0; + char *plugin_name = NULL; char *plugin_instance = NULL; int num_matches = 0; @@ -229,12 +235,14 @@ static int ctail_config_add_file(oconfig_item_t *ci) { oconfig_item_t *option = ci->children + i; int status = 0; - if (strcasecmp("Instance", option->key) == 0) + if (strcasecmp("Plugin", option->key) == 0) + status = cf_util_get_string(option, &plugin_name); + else if (strcasecmp("Instance", option->key) == 0) status = cf_util_get_string(option, &plugin_instance); else if (strcasecmp("Interval", option->key) == 0) cf_util_get_cdtime(option, &interval); else if (strcasecmp("Match", option->key) == 0) { - status = ctail_config_add_match(tm, plugin_instance, option, interval); + status = ctail_config_add_match(tm, plugin_name, plugin_instance, option); if (status == 0) num_matches++; /* Be mild with failed matches.. */ @@ -247,6 +255,7 @@ static int ctail_config_add_file(oconfig_item_t *ci) { break; } /* for (i = 0; i < ci->children_num; i++) */ + sfree(plugin_name); sfree(plugin_instance); if (num_matches == 0) { @@ -254,23 +263,15 @@ static int ctail_config_add_file(oconfig_item_t *ci) { ci->values[0].value.string); tail_match_destroy(tm); return -1; - } else { - cu_tail_match_t **temp; - - temp = realloc(tail_match_list, - sizeof(cu_tail_match_t *) * (tail_match_list_num + 1)); - if (temp == NULL) { - ERROR("tail plugin: realloc failed."); - tail_match_destroy(tm); - return -1; - } - - tail_match_list = temp; - tail_match_list[tail_match_list_num] = tm; - tail_match_list_intervals[tail_match_list_num] = interval; - tail_match_list_num++; } + char str[255]; + snprintf(str, sizeof(str), "tail-%zu", tail_file_num++); + + plugin_register_complex_read( + NULL, str, ctail_read, interval, + &(user_data_t){.data = tm, .free_func = ctail_match_free}); + return 0; } /* int ctail_config_add_file */ @@ -300,40 +301,6 @@ static int ctail_read(user_data_t *ud) { return 0; } /* int ctail_read */ -static int ctail_init(void) { - char str[255]; - - if (tail_match_list_num == 0) { - WARNING("tail plugin: File list is empty. Returning an error."); - return -1; - } - - for (size_t i = 0; i < tail_match_list_num; i++) { - ssnprintf(str, sizeof(str), "tail-%zu", i); - - plugin_register_complex_read(NULL, str, ctail_read, - tail_match_list_intervals[i], - &(user_data_t){ - .data = tail_match_list[i], - }); - } - - return 0; -} /* int ctail_init */ - -static int ctail_shutdown(void) { - for (size_t i = 0; i < tail_match_list_num; i++) { - tail_match_destroy(tail_match_list[i]); - tail_match_list[i] = NULL; - } - sfree(tail_match_list); - tail_match_list_num = 0; - - return 0; -} /* int ctail_shutdown */ - void module_register(void) { plugin_register_complex_config("tail", ctail_config); - plugin_register_init("tail", ctail_init); - plugin_register_shutdown("tail", ctail_shutdown); } /* void module_register */