From 3da36d67b99b4bcfd124c5c1d0877307ce7d8c24 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sat, 23 Mar 2013 09:27:24 +0100 Subject: [PATCH] tail_csv plugin: Implement the "TimeFrom" option. --- src/collectd.conf.in | 1 + src/collectd.conf.pod | 12 +++++++++--- src/tail_csv.c | 33 +++++++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/collectd.conf.in b/src/collectd.conf.in index 75e2291b..4d491c81 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -980,6 +980,7 @@ # Instance "snort-eth0" # Interval 600 # Collect "dropped" "mbps" "alerts" "kpps" +# TimeFrom 0 # # diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index c740808a..25717de2 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -5172,9 +5172,9 @@ created value lists. Otherwise, no type instance is used. =item B I -Each line in the statistics file is broken into many fields with the first -field, the timestamp of the line, is index with zero. This option configures to -read the value from the field with index I. +Configure to read the value from the field with the zero-based index I. +If the value is parsed as signed integer, unsigned integer or double depends on +the B setting, see above. =back @@ -5200,6 +5200,12 @@ metric to be extracted from this statistic file. Configures the interval in which to read values from this instance / file. Defaults to the plugin's default interval. +=item B I + +Rather than using the local time when dispatching a value, read the timestamp +from the field with the zero-based index I. The value is interpreted as +seconds since epoch. The value is parsed as a double and may be factional. + =back =back diff --git a/src/tail_csv.c b/src/tail_csv.c index 3a8304a6..a70b6658 100644 --- a/src/tail_csv.c +++ b/src/tail_csv.c @@ -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; @@ -105,10 +106,12 @@ static int tcsv_read_metric (instance_definition_t *id, if (md->data_source_type == -1) return (EINVAL); - if (md->value_from >= fields_num) + 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->value_from], &v, md->data_source_type); if (status != 0) @@ -117,6 +120,19 @@ static int tcsv_read_metric (instance_definition_t *id, 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) { @@ -178,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->value_from) >= metrics_num) { - ERROR ("tail_csv plugin: Metric \"%s\": Request for index %i when " - "only %zu fields are available.", - md->name, md->value_from, 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); } @@ -351,7 +364,8 @@ static void tcsv_instance_definition_destroy(void *arg){ if (id == NULL) return; - cu_tail_destroy (id->tail); + if (id->tail != NULL) + cu_tail_destroy (id->tail); id->tail = NULL; sfree(id->instance); @@ -416,6 +430,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); @@ -437,6 +452,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; -- 2.30.2