X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Ftail_csv.c;h=61eeee34c166a85923bb200e21a1a3aa3f0382b8;hb=cce136946b879557f91183e4de58e92b81e138c8;hp=ed79170add14069e35f777a81b1f6a690cb246a0;hpb=5e136b4d9ab110b0a23e48404ca00374063c0184;p=collectd.git diff --git a/src/tail_csv.c b/src/tail_csv.c index ed79170a..61eeee34 100644 --- a/src/tail_csv.c +++ b/src/tail_csv.c @@ -37,7 +37,7 @@ struct metric_definition_s { char *type; char *instance; int data_source_type; - int index; + int value_from; struct metric_definition_s *next; }; typedef struct metric_definition_s metric_definition_t; @@ -49,6 +49,7 @@ struct instance_definition_s { metric_definition_t **metric_list; size_t metric_list_len; cdtime_t interval; + int time_from; struct instance_definition_s *next; }; typedef struct instance_definition_s instance_definition_t; @@ -69,7 +70,8 @@ static int tcsv_submit (instance_definition_t *id, sstrncpy(vl.host, hostname_g, sizeof (vl.host)); sstrncpy(vl.plugin, "tail_csv", sizeof(vl.plugin)); - sstrncpy(vl.plugin_instance, id->instance, sizeof(vl.plugin_instance)); + if (id->instance != NULL) + sstrncpy(vl.plugin_instance, id->instance, sizeof(vl.plugin_instance)); sstrncpy(vl.type, md->type, sizeof(vl.type)); if (md->instance != NULL) sstrncpy(vl.type_instance, md->instance, sizeof(vl.type_instance)); @@ -77,10 +79,7 @@ static int tcsv_submit (instance_definition_t *id, vl.time = t; vl.interval = id->interval; - DEBUG("tail_csv plugin: -> plugin_dispatch_values (&vl);"); - plugin_dispatch_values(&vl); - - return (0); + return (plugin_dispatch_values(&vl)); } static cdtime_t parse_time (char const *tbuf) @@ -104,18 +103,36 @@ static int tcsv_read_metric (instance_definition_t *id, cdtime_t t; int status; - if (md->index >= fields_num) + if (md->data_source_type == -1) + return (EINVAL); + + if ((md->value_from >= fields_num) || (id->time_from >= fields_num)) return (EINVAL); - t = parse_time (fields[0]); + t = 0; + if (id->time_from >= 0) + t = parse_time (fields[id->time_from]); - status = parse_value (fields[md->index], &v, md->data_source_type); + status = parse_value (fields[md->value_from], &v, md->data_source_type); if (status != 0) return (status); return (tcsv_submit (id, md, v, t)); } +static _Bool tcsv_check_index (int index, size_t fields_num, char const *name) +{ + if (index < 0) + return 1; + else if (((size_t) index) < fields_num) + return 1; + + ERROR ("tail_csv plugin: Metric \"%s\": Request for index %i when " + "only %zu fields are available.", + name, index, fields_num); + return (0); +} + static int tcsv_read_buffer (instance_definition_t *id, char *buffer, size_t buffer_size) { @@ -177,12 +194,9 @@ static int tcsv_read_buffer (instance_definition_t *id, for (i = 0; i < id->metric_list_len; ++i){ metric_definition_t *md = id->metric_list[i]; - if (((size_t) md->index) >= metrics_num) { - ERROR ("tail_csv plugin: Metric \"%s\": Request for index %i when " - "only %zu fields are available.", - md->name, md->index, metrics_num); + if (!tcsv_check_index (md->value_from, metrics_num, md->name) + || !tcsv_check_index (id->time_from, metrics_num, md->name)) continue; - } tcsv_read_metric (id, md, metrics, metrics_num); } @@ -194,9 +208,7 @@ static int tcsv_read_buffer (instance_definition_t *id, static int tcsv_read (user_data_t *ud) { instance_definition_t *id; - id = ud->data; - DEBUG("tail_csv plugin: tcsv_read (instance = %s)", id->instance); if (id->tail == NULL) { @@ -218,8 +230,8 @@ static int tcsv_read (user_data_t *ud) { status = cu_tail_readline (id->tail, buffer, (int) sizeof (buffer)); if (status != 0) { - ERROR ("tail_csv plugin: Instance \"%s\": cu_tail_readline failed " - "with status %i.", id->instance, status); + ERROR ("tail_csv plugin: File \"%s\": cu_tail_readline failed " + "with status %i.", id->path, status); return (-1); } @@ -235,39 +247,46 @@ static int tcsv_read (user_data_t *ud) { static void tcsv_metric_definition_destroy(void *arg){ metric_definition_t *md; + metric_definition_t *next; md = arg; if (md == NULL) return; - if (md->name != NULL) - DEBUG("tail_csv plugin: Destroying metric definition `%s'.", md->name); + next = md->next; + md->next = NULL; sfree(md->name); sfree(md->type); sfree(md->instance); sfree(md); + + tcsv_metric_definition_destroy (next); } -static int tcsv_config_add_metric_index(metric_definition_t *md, oconfig_item_t *ci){ +static int tcsv_config_get_index(oconfig_item_t *ci, int *ret_index) { + int index; + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)){ - WARNING("tail_csv plugin: `Index' needs exactly one integer argument."); + WARNING("tail_csv plugin: The \"%s\" config option needs exactly one " + "integer argument.", ci->key); return (-1); } - md->index = (int)ci->values[0].value.number; - if (md->index <= 0){ - WARNING("tail_csv plugin: `Index' must be higher than 0."); + index = (int) ci->values[0].value.number; + if (index < 0) { + WARNING("tail_csv plugin: The \"%s\" config option must be positive " + "(or zero).", ci->key); return (-1); } + *ret_index = index; return (0); } /* Parse metric */ static int tcsv_config_add_metric(oconfig_item_t *ci){ metric_definition_t *md; - const data_set_t *ds; int status = 0; int i; @@ -278,6 +297,8 @@ static int tcsv_config_add_metric(oconfig_item_t *ci){ md->name = NULL; md->type = NULL; md->instance = NULL; + md->data_source_type = -1; + md->value_from = -1; md->next = NULL; status = cf_util_get_string (ci, &md->name); @@ -294,8 +315,8 @@ static int tcsv_config_add_metric(oconfig_item_t *ci){ status = cf_util_get_string(option, &md->type); else if (strcasecmp("Instance", option->key) == 0) status = cf_util_get_string(option, &md->instance); - else if (strcasecmp("Index", option->key) == 0) - status = tcsv_config_add_metric_index(md, option); + else if (strcasecmp("ValueFrom", option->key) == 0) + status = tcsv_config_get_index (option, &md->value_from); else { WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key); status = -1; @@ -311,40 +332,18 @@ static int tcsv_config_add_metric(oconfig_item_t *ci){ } /* Verify all necessary options have been set. */ - if (md->type == NULL){ + if (md->type == NULL) { WARNING("tail_csv plugin: Option `Type' must be set."); status = -1; - } else if (md->index == 0){ - WARNING("tail_csv plugin: Option `Index' must be set."); + } else if (md->value_from < 0) { + WARNING("tail_csv plugin: Option `ValueFrom' must be set."); status = -1; } - - if (status != 0){ - tcsv_metric_definition_destroy(md); - return (-1); - } - - /* Retrieve the data source type from the types db. */ - ds = plugin_get_ds(md->type); - if (ds == NULL){ - ERROR ("tail_csv plugin: Failed to look up type \"%s\". " - "It may not be defined in the types.db file. " - "Please read the types.db(5) manual page for more details.", - md->type); + if (status != 0) { tcsv_metric_definition_destroy(md); - return (-1); - } else if (ds->ds_num != 1) { - ERROR ("tail_csv plugin: The type \"%s\" has %i data sources. " - "Only types with a single data soure are supported.", - ds->type, ds->ds_num); - return (-1); - } else { - md->data_source_type = ds->ds->type; + return (status); } - DEBUG("tail_csv plugin: md = { name = %s, type = %s, data_source_type = %d, index = %d }", - md->name, md->type, md->data_source_type, md->index); - if (metric_head == NULL) metric_head = md; else { @@ -365,10 +364,8 @@ static void tcsv_instance_definition_destroy(void *arg){ if (id == NULL) return; - if (id->instance != NULL) - DEBUG("tail_csv plugin: Destroying instance definition `%s'.", id->instance); - - cu_tail_destroy (id->tail); + if (id->tail != NULL) + cu_tail_destroy (id->tail); id->tail = NULL; sfree(id->instance); @@ -407,8 +404,6 @@ static int tcsv_config_add_instance_collect(instance_definition_t *id, oconfig_i return (-1); } - DEBUG("tail_csv plugin: id { instance=%s md->name=%s }", id->instance, metric->name); - id->metric_list[i] = metric; id->metric_list_len++; } @@ -416,7 +411,7 @@ static int tcsv_config_add_instance_collect(instance_definition_t *id, oconfig_i return (0); } -/* Parse instance */ +/* block */ static int tcsv_config_add_file(oconfig_item_t *ci) { instance_definition_t* id; @@ -426,7 +421,6 @@ static int tcsv_config_add_file(oconfig_item_t *ci) /* Registration variables */ char cb_name[DATA_MAX_NAME_LEN]; user_data_t cb_data; - struct timespec cb_interval; id = malloc(sizeof(*id)); if (id == NULL) @@ -435,6 +429,7 @@ static int tcsv_config_add_file(oconfig_item_t *ci) id->instance = NULL; id->path = NULL; id->metric_list = NULL; + id->time_from = -1; id->next = NULL; status = cf_util_get_string (ci, &id->path); @@ -456,6 +451,8 @@ static int tcsv_config_add_file(oconfig_item_t *ci) status = tcsv_config_add_instance_collect(id, option); else if (strcasecmp("Interval", option->key) == 0) cf_util_get_cdtime(option, &id->interval); + else if (strcasecmp("TimeFrom", option->key) == 0) + status = tcsv_config_get_index (option, &id->time_from); else { WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key); status = -1; @@ -484,14 +481,11 @@ static int tcsv_config_add_file(oconfig_item_t *ci) return (-1); } - DEBUG("tail_csv plugin: id = { instance = %s, path = %s }", id->instance, id->path); - ssnprintf (cb_name, sizeof (cb_name), "tail_csv/%s", id->path); memset(&cb_data, 0, sizeof(cb_data)); cb_data.data = id; cb_data.free_func = tcsv_instance_definition_destroy; - CDTIME_T_TO_TIMESPEC(id->interval, &cb_interval); - status = plugin_register_complex_read(NULL, cb_name, tcsv_read, &cb_interval, &cb_data); + status = plugin_register_complex_read(NULL, cb_name, tcsv_read, id->interval, &cb_data); if (status != 0){ ERROR("tail_csv plugin: Registering complex read function failed."); @@ -518,24 +512,51 @@ static int tcsv_config(oconfig_item_t *ci){ return (0); } /* int tcsv_config */ -static int tcsv_shutdown(void){ - metric_definition_t *metric_this; - metric_definition_t *metric_next; +static int tcsv_init(void) { /* {{{ */ + static _Bool have_init = 0; + metric_definition_t *md; + + if (have_init) + return (0); - metric_this = metric_head; - metric_head = NULL; + for (md = metric_head; md != NULL; md = md->next) { + data_set_t const *ds; - while (metric_this != NULL){ - metric_next = metric_this->next; - tcsv_metric_definition_destroy(metric_this); - metric_this = metric_next; + /* Retrieve the data source type from the types db. */ + ds = plugin_get_ds(md->type); + if (ds == NULL) + { + ERROR ("tail_csv plugin: Failed to look up type \"%s\" for " + "metric \"%s\". It may not be defined in the types.db " + "file. Please read the types.db(5) manual page for more " + "details.", + md->type, md->name); + continue; + } + else if (ds->ds_num != 1) + { + ERROR ("tail_csv plugin: The type \"%s\" has %i data sources. " + "Only types with a single data soure are supported.", + ds->type, ds->ds_num); + continue; + } + + md->data_source_type = ds->ds->type; } return (0); +} /* }}} int tcsv_init */ + +static int tcsv_shutdown (void) { + tcsv_metric_definition_destroy (metric_head); + metric_head = NULL; + + return (0); } void module_register(void){ plugin_register_complex_config("tail_csv", tcsv_config); + plugin_register_init("tail_csv", tcsv_init); plugin_register_shutdown("tail_csv", tcsv_shutdown); }