2 * collectd - src/tail_csv.c
3 * Copyright (C) 2013 Kris Nielander
4 * Copyright (C) 2013 Florian Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Kris Nielander <nielander at fox-it.com>
21 * Florian Forster <octo at collectd.org>
26 #include "common.h" /* auxiliary functions */
27 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
28 #include "utils_tail.h"
36 struct metric_definition_s {
42 struct metric_definition_s *next;
44 typedef struct metric_definition_s metric_definition_t;
46 struct instance_definition_s {
50 metric_definition_t **metric_list;
51 size_t metric_list_len;
54 struct instance_definition_s *next;
56 typedef struct instance_definition_s instance_definition_t;
59 static metric_definition_t *metric_head = NULL;
61 static int tcsv_submit(instance_definition_t *id, metric_definition_t *md,
62 value_t v, cdtime_t t) {
63 /* Registration variables */
64 value_list_t vl = VALUE_LIST_INIT;
70 sstrncpy(vl.plugin, "tail_csv", sizeof(vl.plugin));
71 if (id->instance != NULL)
72 sstrncpy(vl.plugin_instance, id->instance, sizeof(vl.plugin_instance));
73 sstrncpy(vl.type, md->type, sizeof(vl.type));
74 if (md->instance != NULL)
75 sstrncpy(vl.type_instance, md->instance, sizeof(vl.type_instance));
78 vl.interval = id->interval;
80 return (plugin_dispatch_values(&vl));
83 static cdtime_t parse_time(char const *tbuf) {
88 t = strtod(tbuf, &endptr);
89 if ((errno != 0) || (endptr == NULL) || (endptr[0] != 0))
92 return (DOUBLE_TO_CDTIME_T(t));
95 static int tcsv_read_metric(instance_definition_t *id, metric_definition_t *md,
96 char **fields, size_t fields_num) {
101 if (md->data_source_type == -1)
104 assert(md->value_from >= 0);
105 if (((size_t)md->value_from) >= fields_num)
108 status = parse_value(fields[md->value_from], &v, md->data_source_type);
112 if (id->time_from >= 0) {
113 if (((size_t)id->time_from) >= fields_num)
115 t = parse_time(fields[id->time_from]);
118 return (tcsv_submit(id, md, v, t));
121 static _Bool tcsv_check_index(ssize_t index, size_t fields_num,
125 else if (((size_t)index) < fields_num)
128 ERROR("tail_csv plugin: Metric \"%s\": Request for index %zd when "
129 "only %zu fields are available.",
130 name, index, fields_num);
134 static int tcsv_read_buffer(instance_definition_t *id, char *buffer,
135 size_t buffer_size) {
142 /* Remove newlines at the end of line. */
143 while (buffer_size > 0) {
144 if ((buffer[buffer_size - 1] == '\n') ||
145 (buffer[buffer_size - 1] == '\r')) {
146 buffer[buffer_size - 1] = 0;
153 /* Ignore empty lines. */
154 if ((buffer_size == 0) || (buffer[0] == '#'))
157 /* Count the number of fields. */
159 for (i = 0; i < buffer_size; i++) {
160 if (buffer[i] == ',')
164 if (metrics_num == 1) {
165 ERROR("tail_csv plugin: last line of `%s' does not contain "
171 /* Create a list of all values */
172 metrics = calloc(metrics_num, sizeof(*metrics));
173 if (metrics == NULL) {
174 ERROR("tail_csv plugin: calloc failed.");
181 for (ptr = buffer; *ptr != 0; ptr++) {
186 metrics[i] = ptr + 1;
189 assert(i == metrics_num);
191 /* Register values */
192 for (i = 0; i < id->metric_list_len; ++i) {
193 metric_definition_t *md = id->metric_list[i];
195 if (!tcsv_check_index(md->value_from, metrics_num, md->name) ||
196 !tcsv_check_index(id->time_from, metrics_num, md->name))
199 tcsv_read_metric(id, md, metrics, metrics_num);
202 /* Free up resources */
207 static int tcsv_read(user_data_t *ud) {
208 instance_definition_t *id;
211 if (id->tail == NULL) {
212 id->tail = cu_tail_create(id->path);
213 if (id->tail == NULL) {
214 ERROR("tail_csv plugin: cu_tail_create (\"%s\") failed.", id->path);
224 status = cu_tail_readline(id->tail, buffer, (int)sizeof(buffer));
226 ERROR("tail_csv plugin: File \"%s\": cu_tail_readline failed "
232 buffer_len = strlen(buffer);
236 tcsv_read_buffer(id, buffer, buffer_len);
242 static void tcsv_metric_definition_destroy(void *arg) {
243 metric_definition_t *md;
244 metric_definition_t *next;
258 tcsv_metric_definition_destroy(next);
261 static int tcsv_config_get_index(oconfig_item_t *ci, ssize_t *ret_index) {
262 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
263 WARNING("tail_csv plugin: The \"%s\" config option needs exactly one "
269 if (ci->values[0].value.number < 0) {
270 WARNING("tail_csv plugin: The \"%s\" config option must be positive "
276 *ret_index = (ssize_t)ci->values[0].value.number;
281 static int tcsv_config_add_metric(oconfig_item_t *ci) {
282 metric_definition_t *md;
285 md = calloc(1, sizeof(*md));
291 md->data_source_type = -1;
295 status = cf_util_get_string(ci, &md->name);
301 for (int i = 0; i < ci->children_num; ++i) {
302 oconfig_item_t *option = ci->children + i;
304 if (strcasecmp("Type", option->key) == 0)
305 status = cf_util_get_string(option, &md->type);
306 else if (strcasecmp("Instance", option->key) == 0)
307 status = cf_util_get_string(option, &md->instance);
308 else if (strcasecmp("ValueFrom", option->key) == 0)
309 status = tcsv_config_get_index(option, &md->value_from);
311 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
320 tcsv_metric_definition_destroy(md);
324 /* Verify all necessary options have been set. */
325 if (md->type == NULL) {
326 WARNING("tail_csv plugin: Option `Type' must be set.");
328 } else if (md->value_from < 0) {
329 WARNING("tail_csv plugin: Option `ValueFrom' must be set.");
333 tcsv_metric_definition_destroy(md);
337 if (metric_head == NULL)
340 metric_definition_t *last;
342 while (last->next != NULL)
350 static void tcsv_instance_definition_destroy(void *arg) {
351 instance_definition_t *id;
357 if (id->tail != NULL)
358 cu_tail_destroy(id->tail);
363 sfree(id->metric_list);
367 static int tcsv_config_add_instance_collect(instance_definition_t *id,
368 oconfig_item_t *ci) {
369 metric_definition_t *metric;
370 metric_definition_t **metric_list;
371 size_t metric_list_size;
373 if (ci->values_num < 1) {
374 WARNING("tail_csv plugin: The `Collect' config option needs at least one "
379 metric_list_size = id->metric_list_len + (size_t)ci->values_num;
381 realloc(id->metric_list, sizeof(*id->metric_list) * metric_list_size);
382 if (metric_list == NULL)
384 id->metric_list = metric_list;
386 for (int i = 0; i < ci->values_num; i++) {
389 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
390 WARNING("tail_csv plugin: All arguments to `Collect' must be strings.");
393 metric_name = ci->values[i].value.string;
395 for (metric = metric_head; metric != NULL; metric = metric->next)
396 if (strcasecmp(metric_name, metric->name) == 0)
399 if (metric == NULL) {
400 WARNING("tail_csv plugin: `Collect' argument not found `%s'.",
405 id->metric_list[id->metric_list_len] = metric;
406 id->metric_list_len++;
413 static int tcsv_config_add_file(oconfig_item_t *ci) {
414 instance_definition_t *id;
417 /* Registration variables */
418 char cb_name[DATA_MAX_NAME_LEN];
420 id = calloc(1, sizeof(*id));
425 id->metric_list = NULL;
429 status = cf_util_get_string(ci, &id->path);
435 /* Use default interval. */
436 id->interval = plugin_get_interval();
438 for (int i = 0; i < ci->children_num; ++i) {
439 oconfig_item_t *option = ci->children + i;
442 if (strcasecmp("Instance", option->key) == 0)
443 status = cf_util_get_string(option, &id->instance);
444 else if (strcasecmp("Collect", option->key) == 0)
445 status = tcsv_config_add_instance_collect(id, option);
446 else if (strcasecmp("Interval", option->key) == 0)
447 cf_util_get_cdtime(option, &id->interval);
448 else if (strcasecmp("TimeFrom", option->key) == 0)
449 status = tcsv_config_get_index(option, &id->time_from);
451 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
460 tcsv_instance_definition_destroy(id);
464 /* Verify all necessary options have been set. */
465 if (id->path == NULL) {
466 WARNING("tail_csv plugin: Option `Path' must be set.");
468 } else if (id->metric_list == NULL) {
469 WARNING("tail_csv plugin: Option `Collect' must be set.");
474 tcsv_instance_definition_destroy(id);
478 ssnprintf(cb_name, sizeof(cb_name), "tail_csv/%s", id->path);
480 status = plugin_register_complex_read(
481 NULL, cb_name, tcsv_read, id->interval,
483 .data = id, .free_func = tcsv_instance_definition_destroy,
486 ERROR("tail_csv plugin: Registering complex read function failed.");
487 tcsv_instance_definition_destroy(id);
495 static int tcsv_config(oconfig_item_t *ci) {
496 for (int i = 0; i < ci->children_num; ++i) {
497 oconfig_item_t *child = ci->children + i;
498 if (strcasecmp("Metric", child->key) == 0)
499 tcsv_config_add_metric(child);
500 else if (strcasecmp("File", child->key) == 0)
501 tcsv_config_add_file(child);
503 WARNING("tail_csv plugin: Ignore unknown config option `%s'.",
508 } /* int tcsv_config */
510 static int tcsv_init(void) { /* {{{ */
511 static _Bool have_init = 0;
512 metric_definition_t *md;
517 for (md = metric_head; md != NULL; md = md->next) {
518 data_set_t const *ds;
520 /* Retrieve the data source type from the types db. */
521 ds = plugin_get_ds(md->type);
523 ERROR("tail_csv plugin: Failed to look up type \"%s\" for "
524 "metric \"%s\". It may not be defined in the types.db "
525 "file. Please read the types.db(5) manual page for more "
529 } else if (ds->ds_num != 1) {
530 ERROR("tail_csv plugin: The type \"%s\" has %zu data sources. "
531 "Only types with a single data source are supported.",
532 ds->type, ds->ds_num);
536 md->data_source_type = ds->ds->type;
540 } /* }}} int tcsv_init */
542 static int tcsv_shutdown(void) {
543 tcsv_metric_definition_destroy(metric_head);
549 void module_register(void) {
550 plugin_register_complex_config("tail_csv", tcsv_config);
551 plugin_register_init("tail_csv", tcsv_init);
552 plugin_register_shutdown("tail_csv", tcsv_shutdown);