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>
25 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
26 #include "common.h" /* auxiliary functions */
27 #include "utils_tail.h"
35 struct metric_definition_s {
41 struct metric_definition_s *next;
43 typedef struct metric_definition_s metric_definition_t;
45 struct instance_definition_s {
49 metric_definition_t **metric_list;
50 size_t metric_list_len;
52 struct instance_definition_s *next;
54 typedef struct instance_definition_s instance_definition_t;
57 static metric_definition_t *metric_head = NULL;
59 static int tcsv_submit (instance_definition_t *id,
60 metric_definition_t *md,
61 value_t v, cdtime_t t)
63 /* Registration variables */
64 value_list_t vl = VALUE_LIST_INIT;
70 sstrncpy(vl.host, hostname_g, sizeof (vl.host));
71 sstrncpy(vl.plugin, "tail_csv", sizeof(vl.plugin));
72 sstrncpy(vl.plugin_instance, id->name, 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 DEBUG("tail_csv plugin: -> plugin_dispatch_values (&vl);");
81 plugin_dispatch_values(&vl);
86 static cdtime_t parse_time (char const *tbuf)
92 t = strtod (tbuf, &endptr);
93 if ((errno != 0) || (endptr == NULL) || (endptr[0] != 0))
96 return (DOUBLE_TO_CDTIME_T (t));
99 static int tcsv_read_metric (instance_definition_t *id,
100 metric_definition_t *md,
101 char **fields, size_t fields_num)
107 if (md->index >= fields_num)
110 t = parse_time (fields[0]);
112 status = parse_value (fields[md->index], &v, md->data_source_type);
116 return (tcsv_submit (id, md, v, t));
119 static int tcsv_read_buffer (instance_definition_t *id,
120 char *buffer, size_t buffer_size)
128 /* Remove newlines at the end of line. */
129 while (buffer_size > 0) {
130 if ((buffer[buffer_size - 1] == '\n')
131 || (buffer[buffer_size - 1] == '\r')) {
132 buffer[buffer_size - 1] = 0;
139 /* Ignore empty lines. */
140 if ((buffer_size == 0) || (buffer[0] == '#'))
143 /* Count the number of fields. */
145 for (i = 0; i < buffer_size; i++) {
146 if (buffer[i] == ',')
150 if (metrics_num == 1) {
151 ERROR("tail_csv plugin: last line of `%s' does not contain "
152 "enough values.", id->path);
156 /* Create a list of all values */
157 metrics = calloc (metrics_num, sizeof (*metrics));
158 if (metrics == NULL) {
159 ERROR ("tail_csv plugin: calloc failed.");
166 for (ptr = buffer; *ptr != 0; ptr++) {
171 metrics[i] = ptr + 1;
174 assert (i == metrics_num);
176 /* Register values */
177 for (i = 0; i < id->metric_list_len; ++i){
178 metric_definition_t *md = id->metric_list[i];
180 if (((size_t) md->index) >= metrics_num) {
181 ERROR ("tail_csv plugin: Metric \"%s\": Request for index %i when "
182 "only %zu fields are available.",
183 md->name, md->index, metrics_num);
187 tcsv_read_metric (id, md, metrics, metrics_num);
190 /* Free up resources */
195 static int tcsv_read (user_data_t *ud) {
196 instance_definition_t *id;
199 DEBUG("tail_csv plugin: tcsv_read (instance = %s)", id->name);
201 if (id->tail == NULL)
203 id->tail = cu_tail_create (id->path);
204 if (id->tail == NULL)
206 ERROR ("tail_csv plugin: cu_tail_create (\"%s\") failed.",
218 status = cu_tail_readline (id->tail, buffer, (int) sizeof (buffer));
221 ERROR ("tail_csv plugin: Instance \"%s\": cu_tail_readline failed "
222 "with status %i.", id->name, status);
226 buffer_len = strlen (buffer);
230 tcsv_read_buffer (id, buffer, buffer_len);
236 static void tcsv_metric_definition_destroy(void *arg){
237 metric_definition_t *md;
243 if (md->name != NULL)
244 DEBUG("tail_csv plugin: Destroying metric definition `%s'.", md->name);
252 static int tcsv_config_add_metric_index(metric_definition_t *md, oconfig_item_t *ci){
253 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)){
254 WARNING("tail_csv plugin: `Index' needs exactly one integer argument.");
258 md->index = (int)ci->values[0].value.number;
260 WARNING("tail_csv plugin: `Index' must be higher than 0.");
268 static int tcsv_config_add_metric(oconfig_item_t *ci){
269 metric_definition_t *md;
270 const data_set_t *ds;
274 md = (metric_definition_t *)malloc(sizeof(*md));
277 memset(md, 0, sizeof(*md));
283 status = cf_util_get_string (ci, &md->name);
289 for (i = 0; i < ci->children_num; ++i){
290 oconfig_item_t *option = ci->children + i;
293 if (strcasecmp("Type", option->key) == 0)
294 status = cf_util_get_string(option, &md->type);
295 else if (strcasecmp("Instance", option->key) == 0)
296 status = cf_util_get_string(option, &md->instance);
297 else if (strcasecmp("Index", option->key) == 0)
298 status = tcsv_config_add_metric_index(md, option);
300 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
309 tcsv_metric_definition_destroy(md);
313 /* Verify all necessary options have been set. */
314 if (md->type == NULL){
315 WARNING("tail_csv plugin: Option `Type' must be set.");
317 } else if (md->index == 0){
318 WARNING("tail_csv plugin: Option `Index' must be set.");
323 tcsv_metric_definition_destroy(md);
327 /* Retrieve the data source type from the types db. */
328 ds = plugin_get_ds(md->type);
330 ERROR ("tail_csv plugin: Failed to look up type \"%s\". "
331 "It may not be defined in the types.db file. "
332 "Please read the types.db(5) manual page for more details.",
334 tcsv_metric_definition_destroy(md);
336 } else if (ds->ds_num != 1) {
337 ERROR ("tail_csv plugin: The type \"%s\" has %i data sources. "
338 "Only types with a single data soure are supported.",
339 ds->type, ds->ds_num);
342 md->data_source_type = ds->ds->type;
345 DEBUG("tail_csv plugin: md = { name = %s, type = %s, data_source_type = %d, index = %d }",
346 md->name, md->type, md->data_source_type, md->index);
348 if (metric_head == NULL)
351 metric_definition_t *last;
353 while (last->next != NULL)
361 static void tcsv_instance_definition_destroy(void *arg){
362 instance_definition_t *id;
368 if (id->name != NULL)
369 DEBUG("tail_csv plugin: Destroying instance definition `%s'.", id->name);
371 cu_tail_destroy (id->tail);
376 sfree(id->metric_list);
380 static int tcsv_config_add_instance_collect(instance_definition_t *id, oconfig_item_t *ci){
381 metric_definition_t *metric;
384 if (ci->values_num < 1){
385 WARNING("tail_csv plugin: The `Collect' config option needs at least one argument.");
389 /* Verify string arguments */
390 for (i = 0; i < ci->values_num; ++i)
391 if (ci->values[i].type != OCONFIG_TYPE_STRING){
392 WARNING("tail_csv plugin: All arguments to `Collect' must be strings.");
396 id->metric_list = (metric_definition_t **)malloc(sizeof(metric_definition_t *) * ci->values_num);
397 if (id->metric_list == NULL)
400 for (i = 0; i < ci->values_num; ++i){
401 for (metric = metric_head; metric != NULL; metric = metric->next)
402 if (strcasecmp(ci->values[i].value.string, metric->name) == 0)
406 WARNING("tail_csv plugin: `Collect' argument not found `%s'.", ci->values[i].value.string);
410 DEBUG("tail_csv plugin: id { name=%s md->name=%s }", id->name, metric->name);
412 id->metric_list[i] = metric;
413 id->metric_list_len++;
420 static int tcsv_config_add_instance(oconfig_item_t *ci){
422 instance_definition_t* id;
426 /* Registration variables */
427 char cb_name[DATA_MAX_NAME_LEN];
429 struct timespec cb_interval;
431 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)){
432 WARNING("tail_csv plugin: The `Instance' config option needs exactly one string argument.");
436 id = (instance_definition_t *)malloc(sizeof(*id));
439 memset(id, 0, sizeof(*id));
441 id->name = strdup(ci->values[0].value.string);
442 if (id->name == NULL){
447 /* Use default interval. */
448 id->interval = plugin_get_interval();
450 for (i = 0; i < ci->children_num; ++i){
451 oconfig_item_t *option = ci->children + i;
454 if (strcasecmp("Path", option->key) == 0)
455 status = cf_util_get_string(option, &id->path);
456 else if (strcasecmp("Collect", option->key) == 0)
457 status = tcsv_config_add_instance_collect(id, option);
458 else if (strcasecmp("Interval", option->key) == 0)
459 cf_util_get_cdtime(option, &id->interval);
461 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
470 tcsv_instance_definition_destroy(id);
474 /* Verify all necessary options have been set. */
475 if (id->path == NULL){
476 WARNING("tail_csv plugin: Option `Path' must be set.");
478 } else if (id->metric_list == NULL){
479 WARNING("tail_csv plugin: Option `Collect' must be set.");
484 tcsv_instance_definition_destroy(id);
488 DEBUG("tail_csv plugin: id = { name = %s, path = %s }", id->name, id->path);
490 ssnprintf (cb_name, sizeof (cb_name), "tail_csv/%s", id->name);
491 memset(&cb_data, 0, sizeof(cb_data));
493 cb_data.free_func = tcsv_instance_definition_destroy;
494 CDTIME_T_TO_TIMESPEC(id->interval, &cb_interval);
495 status = plugin_register_complex_read(NULL, cb_name, tcsv_read, &cb_interval, &cb_data);
498 ERROR("tail_csv plugin: Registering complex read function failed.");
499 tcsv_instance_definition_destroy(id);
507 static int tcsv_config(oconfig_item_t *ci){
509 for (i = 0; i < ci->children_num; ++i){
510 oconfig_item_t *child = ci->children + i;
511 if (strcasecmp("Metric", child->key) == 0)
512 tcsv_config_add_metric(child);
513 else if (strcasecmp("Instance", child->key) == 0)
514 tcsv_config_add_instance(child);
516 WARNING("tail_csv plugin: Ignore unknown config option `%s'.", child->key);
520 } /* int tcsv_config */
522 static int tcsv_shutdown(void){
523 metric_definition_t *metric_this;
524 metric_definition_t *metric_next;
526 metric_this = metric_head;
529 while (metric_this != NULL){
530 metric_next = metric_this->next;
531 tcsv_metric_definition_destroy(metric_this);
532 metric_this = metric_next;
538 void module_register(void){
539 plugin_register_complex_config("tail_csv", tcsv_config);
540 plugin_register_shutdown("tail_csv", tcsv_shutdown);
543 /* vim: set sw=4 sts=4 et : */