1 /**
2 * collectd - src/tail_csv.c
3 * Copyright (C) 2013 Kris Nielander
4 * Copyright (C) 2013 Florian Forster
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Kris Nielander <nielander at fox-it.com>
21 * Florian Forster <octo at collectd.org>
22 **/
24 #include "collectd.h"
25 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
26 #include "common.h" /* auxiliary functions */
27 #include "utils_tail.h"
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <string.h>
35 struct metric_definition_s {
36 char *name;
37 char *type;
38 char *instance;
39 int data_source_type;
40 int value_from;
41 struct metric_definition_s *next;
42 };
43 typedef struct metric_definition_s metric_definition_t;
45 struct instance_definition_s {
46 char *instance;
47 char *path;
48 cu_tail_t *tail;
49 metric_definition_t **metric_list;
50 size_t metric_list_len;
51 cdtime_t interval;
52 int time_from;
53 struct instance_definition_s *next;
54 };
55 typedef struct instance_definition_s instance_definition_t;
57 /* Private */
58 static metric_definition_t *metric_head = NULL;
60 static int tcsv_submit (instance_definition_t *id,
61 metric_definition_t *md,
62 value_t v, cdtime_t t)
63 {
64 /* Registration variables */
65 value_list_t vl = VALUE_LIST_INIT;
67 /* Register */
68 vl.values_len = 1;
69 vl.values = &v;
71 sstrncpy(vl.host, hostname_g, sizeof (vl.host));
72 sstrncpy(vl.plugin, "tail_csv", sizeof(vl.plugin));
73 if (id->instance != NULL)
74 sstrncpy(vl.plugin_instance, id->instance, sizeof(vl.plugin_instance));
75 sstrncpy(vl.type, md->type, sizeof(vl.type));
76 if (md->instance != NULL)
77 sstrncpy(vl.type_instance, md->instance, sizeof(vl.type_instance));
79 vl.time = t;
80 vl.interval = id->interval;
82 return (plugin_dispatch_values(&vl));
83 }
85 static cdtime_t parse_time (char const *tbuf)
86 {
87 double t;
88 char *endptr = 0;
90 errno = 0;
91 t = strtod (tbuf, &endptr);
92 if ((errno != 0) || (endptr == NULL) || (endptr[0] != 0))
93 return (cdtime ());
95 return (DOUBLE_TO_CDTIME_T (t));
96 }
98 static int tcsv_read_metric (instance_definition_t *id,
99 metric_definition_t *md,
100 char **fields, size_t fields_num)
101 {
102 value_t v;
103 cdtime_t t;
104 int status;
106 if (md->data_source_type == -1)
107 return (EINVAL);
109 if ((md->value_from >= fields_num) || (id->time_from >= fields_num))
110 return (EINVAL);
112 t = 0;
113 if (id->time_from >= 0)
114 t = parse_time (fields[id->time_from]);
116 status = parse_value (fields[md->value_from], &v, md->data_source_type);
117 if (status != 0)
118 return (status);
120 return (tcsv_submit (id, md, v, t));
121 }
123 static _Bool tcsv_check_index (int index, size_t fields_num, char const *name)
124 {
125 if (index < 0)
126 return 1;
127 else if (((size_t) index) < fields_num)
128 return 1;
130 ERROR ("tail_csv plugin: Metric \"%s\": Request for index %i when "
131 "only %zu fields are available.",
132 name, index, fields_num);
133 return (0);
134 }
136 static int tcsv_read_buffer (instance_definition_t *id,
137 char *buffer, size_t buffer_size)
138 {
139 char **metrics;
140 size_t metrics_num;
142 char *ptr;
143 size_t i;
145 /* Remove newlines at the end of line. */
146 while (buffer_size > 0) {
147 if ((buffer[buffer_size - 1] == '\n')
148 || (buffer[buffer_size - 1] == '\r')) {
149 buffer[buffer_size - 1] = 0;
150 buffer_size--;
151 } else {
152 break;
153 }
154 }
156 /* Ignore empty lines. */
157 if ((buffer_size == 0) || (buffer[0] == '#'))
158 return (0);
160 /* Count the number of fields. */
161 metrics_num = 1;
162 for (i = 0; i < buffer_size; i++) {
163 if (buffer[i] == ',')
164 metrics_num++;
165 }
167 if (metrics_num == 1) {
168 ERROR("tail_csv plugin: last line of `%s' does not contain "
169 "enough values.", id->path);
170 return (-1);
171 }
173 /* Create a list of all values */
174 metrics = calloc (metrics_num, sizeof (*metrics));
175 if (metrics == NULL) {
176 ERROR ("tail_csv plugin: calloc failed.");
177 return (ENOMEM);
178 }
180 ptr = buffer;
181 metrics[0] = ptr;
182 i = 1;
183 for (ptr = buffer; *ptr != 0; ptr++) {
184 if (*ptr != ',')
185 continue;
187 *ptr = 0;
188 metrics[i] = ptr + 1;
189 i++;
190 }
191 assert (i == metrics_num);
193 /* Register values */
194 for (i = 0; i < id->metric_list_len; ++i){
195 metric_definition_t *md = id->metric_list[i];
197 if (!tcsv_check_index (md->value_from, metrics_num, md->name)
198 || !tcsv_check_index (id->time_from, metrics_num, md->name))
199 continue;
201 tcsv_read_metric (id, md, metrics, metrics_num);
202 }
204 /* Free up resources */
205 sfree (metrics);
206 return (0);
207 }
209 static int tcsv_read (user_data_t *ud) {
210 instance_definition_t *id;
211 id = ud->data;
213 if (id->tail == NULL)
214 {
215 id->tail = cu_tail_create (id->path);
216 if (id->tail == NULL)
217 {
218 ERROR ("tail_csv plugin: cu_tail_create (\"%s\") failed.",
219 id->path);
220 return (-1);
221 }
222 }
224 while (42)
225 {
226 char buffer[1024];
227 size_t buffer_len;
228 int status;
230 status = cu_tail_readline (id->tail, buffer, (int) sizeof (buffer));
231 if (status != 0)
232 {
233 ERROR ("tail_csv plugin: File \"%s\": cu_tail_readline failed "
234 "with status %i.", id->path, status);
235 return (-1);
236 }
238 buffer_len = strlen (buffer);
239 if (buffer_len == 0)
240 break;
242 tcsv_read_buffer (id, buffer, buffer_len);
243 }
245 return (0);
246 }
248 static void tcsv_metric_definition_destroy(void *arg){
249 metric_definition_t *md;
250 metric_definition_t *next;
252 md = arg;
253 if (md == NULL)
254 return;
256 next = md->next;
257 md->next = NULL;
259 sfree(md->name);
260 sfree(md->type);
261 sfree(md->instance);
262 sfree(md);
264 tcsv_metric_definition_destroy (next);
265 }
267 static int tcsv_config_get_index(oconfig_item_t *ci, int *ret_index) {
268 int index;
270 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)){
271 WARNING("tail_csv plugin: The \"%s\" config option needs exactly one "
272 "integer argument.", ci->key);
273 return (-1);
274 }
276 index = (int) ci->values[0].value.number;
277 if (index < 0) {
278 WARNING("tail_csv plugin: The \"%s\" config option must be positive "
279 "(or zero).", ci->key);
280 return (-1);
281 }
283 *ret_index = index;
284 return (0);
285 }
287 /* Parse metric */
288 static int tcsv_config_add_metric(oconfig_item_t *ci){
289 metric_definition_t *md;
290 int status = 0;
291 int i;
293 md = (metric_definition_t *)malloc(sizeof(*md));
294 if (md == NULL)
295 return (-1);
296 memset(md, 0, sizeof(*md));
297 md->name = NULL;
298 md->type = NULL;
299 md->instance = NULL;
300 md->data_source_type = -1;
301 md->value_from = -1;
302 md->next = NULL;
304 status = cf_util_get_string (ci, &md->name);
305 if (status != 0) {
306 sfree (md);
307 return (-1);
308 }
310 for (i = 0; i < ci->children_num; ++i){
311 oconfig_item_t *option = ci->children + i;
312 status = 0;
314 if (strcasecmp("Type", option->key) == 0)
315 status = cf_util_get_string(option, &md->type);
316 else if (strcasecmp("Instance", option->key) == 0)
317 status = cf_util_get_string(option, &md->instance);
318 else if (strcasecmp("ValueFrom", option->key) == 0)
319 status = tcsv_config_get_index (option, &md->value_from);
320 else {
321 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
322 status = -1;
323 }
325 if (status != 0)
326 break;
327 }
329 if (status != 0){
330 tcsv_metric_definition_destroy(md);
331 return (-1);
332 }
334 /* Verify all necessary options have been set. */
335 if (md->type == NULL) {
336 WARNING("tail_csv plugin: Option `Type' must be set.");
337 status = -1;
338 } else if (md->value_from < 0) {
339 WARNING("tail_csv plugin: Option `ValueFrom' must be set.");
340 status = -1;
341 }
342 if (status != 0) {
343 tcsv_metric_definition_destroy(md);
344 return (status);
345 }
347 if (metric_head == NULL)
348 metric_head = md;
349 else {
350 metric_definition_t *last;
351 last = metric_head;
352 while (last->next != NULL)
353 last = last->next;
354 last->next = md;
355 }
357 return (0);
358 }
360 static void tcsv_instance_definition_destroy(void *arg){
361 instance_definition_t *id;
363 id = arg;
364 if (id == NULL)
365 return;
367 if (id->tail != NULL)
368 cu_tail_destroy (id->tail);
369 id->tail = NULL;
371 sfree(id->instance);
372 sfree(id->path);
373 sfree(id->metric_list);
374 sfree(id);
375 }
377 static int tcsv_config_add_instance_collect(instance_definition_t *id, oconfig_item_t *ci){
378 metric_definition_t *metric;
379 int i;
381 if (ci->values_num < 1){
382 WARNING("tail_csv plugin: The `Collect' config option needs at least one argument.");
383 return (-1);
384 }
386 /* Verify string arguments */
387 for (i = 0; i < ci->values_num; ++i)
388 if (ci->values[i].type != OCONFIG_TYPE_STRING){
389 WARNING("tail_csv plugin: All arguments to `Collect' must be strings.");
390 return (-1);
391 }
393 id->metric_list = (metric_definition_t **)malloc(sizeof(metric_definition_t *) * ci->values_num);
394 if (id->metric_list == NULL)
395 return (-1);
397 for (i = 0; i < ci->values_num; ++i){
398 for (metric = metric_head; metric != NULL; metric = metric->next)
399 if (strcasecmp(ci->values[i].value.string, metric->name) == 0)
400 break;
402 if (metric == NULL){
403 WARNING("tail_csv plugin: `Collect' argument not found `%s'.", ci->values[i].value.string);
404 return (-1);
405 }
407 id->metric_list[i] = metric;
408 id->metric_list_len++;
409 }
411 return (0);
412 }
414 /* <File /> block */
415 static int tcsv_config_add_file(oconfig_item_t *ci)
416 {
417 instance_definition_t* id;
418 int status = 0;
419 int i;
421 /* Registration variables */
422 char cb_name[DATA_MAX_NAME_LEN];
423 user_data_t cb_data;
425 id = malloc(sizeof(*id));
426 if (id == NULL)
427 return (-1);
428 memset(id, 0, sizeof(*id));
429 id->instance = NULL;
430 id->path = NULL;
431 id->metric_list = NULL;
432 id->time_from = -1;
433 id->next = NULL;
435 status = cf_util_get_string (ci, &id->path);
436 if (status != 0) {
437 sfree (id);
438 return (status);
439 }
441 /* Use default interval. */
442 id->interval = plugin_get_interval();
444 for (i = 0; i < ci->children_num; ++i){
445 oconfig_item_t *option = ci->children + i;
446 status = 0;
448 if (strcasecmp("Instance", option->key) == 0)
449 status = cf_util_get_string(option, &id->instance);
450 else if (strcasecmp("Collect", option->key) == 0)
451 status = tcsv_config_add_instance_collect(id, option);
452 else if (strcasecmp("Interval", option->key) == 0)
453 cf_util_get_cdtime(option, &id->interval);
454 else if (strcasecmp("TimeFrom", option->key) == 0)
455 status = tcsv_config_get_index (option, &id->time_from);
456 else {
457 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
458 status = -1;
459 }
461 if (status != 0)
462 break;
463 }
465 if (status != 0){
466 tcsv_instance_definition_destroy(id);
467 return (-1);
468 }
470 /* Verify all necessary options have been set. */
471 if (id->path == NULL){
472 WARNING("tail_csv plugin: Option `Path' must be set.");
473 status = -1;
474 } else if (id->metric_list == NULL){
475 WARNING("tail_csv plugin: Option `Collect' must be set.");
476 status = -1;
477 }
479 if (status != 0){
480 tcsv_instance_definition_destroy(id);
481 return (-1);
482 }
484 ssnprintf (cb_name, sizeof (cb_name), "tail_csv/%s", id->path);
485 memset(&cb_data, 0, sizeof(cb_data));
486 cb_data.data = id;
487 cb_data.free_func = tcsv_instance_definition_destroy;
488 status = plugin_register_complex_read(NULL, cb_name, tcsv_read, id->interval, &cb_data);
490 if (status != 0){
491 ERROR("tail_csv plugin: Registering complex read function failed.");
492 tcsv_instance_definition_destroy(id);
493 return (-1);
494 }
496 return (0);
497 }
499 /* Parse blocks */
500 static int tcsv_config(oconfig_item_t *ci){
501 int i;
502 for (i = 0; i < ci->children_num; ++i){
503 oconfig_item_t *child = ci->children + i;
504 if (strcasecmp("Metric", child->key) == 0)
505 tcsv_config_add_metric(child);
506 else if (strcasecmp("File", child->key) == 0)
507 tcsv_config_add_file(child);
508 else
509 WARNING("tail_csv plugin: Ignore unknown config option `%s'.", child->key);
510 }
512 return (0);
513 } /* int tcsv_config */
515 static int tcsv_init(void) { /* {{{ */
516 static _Bool have_init = 0;
517 metric_definition_t *md;
519 if (have_init)
520 return (0);
522 for (md = metric_head; md != NULL; md = md->next) {
523 data_set_t const *ds;
525 /* Retrieve the data source type from the types db. */
526 ds = plugin_get_ds(md->type);
527 if (ds == NULL)
528 {
529 ERROR ("tail_csv plugin: Failed to look up type \"%s\" for "
530 "metric \"%s\". It may not be defined in the types.db "
531 "file. Please read the types.db(5) manual page for more "
532 "details.",
533 md->type, md->name);
534 continue;
535 }
536 else if (ds->ds_num != 1)
537 {
538 ERROR ("tail_csv plugin: The type \"%s\" has %i data sources. "
539 "Only types with a single data soure are supported.",
540 ds->type, ds->ds_num);
541 continue;
542 }
544 md->data_source_type = ds->ds->type;
545 }
547 return (0);
548 } /* }}} int tcsv_init */
550 static int tcsv_shutdown (void) {
551 tcsv_metric_definition_destroy (metric_head);
552 metric_head = NULL;
554 return (0);
555 }
557 void module_register(void){
558 plugin_register_complex_config("tail_csv", tcsv_config);
559 plugin_register_init("tail_csv", tcsv_init);
560 plugin_register_shutdown("tail_csv", tcsv_shutdown);
561 }
563 /* vim: set sw=4 sts=4 et : */