From: Dheeraj Gupta Date: Tue, 3 Mar 2020 12:20:52 +0000 (+0530) Subject: Add a FieldSeaparator option to tail_csv plugin X-Git-Tag: collectd-5.11.0~4^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8976efd14ef7ccb46d86d0258b0c1f1ed4ef38b8;p=thirdparty%2Fcollectd.git Add a FieldSeaparator option to tail_csv plugin --- diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 18fda8ed4..fe6967a85 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -9328,6 +9328,7 @@ B Instance "eth0" Interval 600 Collect "snort-dropped" + FieldSeparator "," #TimeFrom 0 @@ -9404,6 +9405,11 @@ 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. +=item B I + +Specify the character to use as field separator while parsing the CSV. +Defaults to ',' if not specified. The value can only be a single character. + =back =back diff --git a/src/tail_csv.c b/src/tail_csv.c index 4e92eade3..3879df7b1 100644 --- a/src/tail_csv.c +++ b/src/tail_csv.c @@ -47,6 +47,7 @@ struct instance_definition_s { char *plugin_name; char *instance; char *path; + char field_separator; cu_tail_t *tail; metric_definition_t **metric_list; size_t metric_list_len; @@ -157,7 +158,7 @@ static int tcsv_read_buffer(instance_definition_t *id, char *buffer, /* Count the number of fields. */ metrics_num = 1; for (i = 0; i < buffer_size; i++) { - if (buffer[i] == ',') + if (buffer[i] == id->field_separator) metrics_num++; } @@ -179,7 +180,7 @@ static int tcsv_read_buffer(instance_definition_t *id, char *buffer, metrics[0] = ptr; i = 1; for (ptr = buffer; *ptr != 0; ptr++) { - if (*ptr != ',') + if (*ptr != id->field_separator) continue; *ptr = 0; @@ -277,6 +278,28 @@ static int tcsv_config_get_index(oconfig_item_t *ci, ssize_t *ret_index) { return 0; } +static int tcsv_config_get_separator(oconfig_item_t *ci, char *ret_char) { + size_t len_opt; + + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { + WARNING("tail_csv plugin: The \"%s\" config option needs exactly one " + "string argument.", + ci->key); + return -1; + } + + len_opt = strlen(ci->values[0].value.string); + if (len_opt != 1) { + WARNING("tail_csv plugin: The \"%s\" config option must be a " + "single character", + ci->key); + return -1; + } + + *ret_char = ci->values[0].value.string[0]; + return 0; +} + /* Parse metric */ static int tcsv_config_add_metric(oconfig_item_t *ci) { metric_definition_t *md; @@ -427,6 +450,7 @@ static int tcsv_config_add_file(oconfig_item_t *ci) { id->path = NULL; id->metric_list = NULL; id->time_from = -1; + id->field_separator = ','; id->next = NULL; status = cf_util_get_string(ci, &id->path); @@ -449,6 +473,8 @@ static int tcsv_config_add_file(oconfig_item_t *ci) { status = tcsv_config_get_index(option, &id->time_from); else if (strcasecmp("Plugin", option->key) == 0) status = cf_util_get_string(option, &id->plugin_name); + else if (strcasecmp("FieldSeparator", option->key) == 0) + status = tcsv_config_get_separator(option, &id->field_separator); else { WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key); status = -1;