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"
26 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
27 #include "common.h" /* auxiliary functions */
28 #include "utils_tail.h"
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <stdlib.h>
34 #include <string.h>
36 struct metric_definition_s {
37 char *name;
38 char *type;
39 char *instance;
40 int data_source_type;
41 ssize_t value_from;
42 struct metric_definition_s *next;
43 };
44 typedef struct metric_definition_s metric_definition_t;
46 struct instance_definition_s {
47 char *instance;
48 char *path;
49 cu_tail_t *tail;
50 metric_definition_t **metric_list;
51 size_t metric_list_len;
52 cdtime_t interval;
53 ssize_t time_from;
54 struct instance_definition_s *next;
55 };
56 typedef struct instance_definition_s instance_definition_t;
58 /* Private */
59 static metric_definition_t *metric_head = NULL;
61 static int tcsv_submit (instance_definition_t *id,
62 metric_definition_t *md,
63 value_t v, cdtime_t t)
64 {
65 /* Registration variables */
66 value_list_t vl = VALUE_LIST_INIT;
68 /* Register */
69 vl.values_len = 1;
70 vl.values = &v;
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 = NULL;
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 = 0;
104 int status;
106 if (md->data_source_type == -1)
107 return (EINVAL);
109 assert (md->value_from >= 0);
110 if (((size_t) md->value_from) >= fields_num)
111 return (EINVAL);
113 status = parse_value (fields[md->value_from], &v, md->data_source_type);
114 if (status != 0)
115 return (status);
117 if (id->time_from >= 0) {
118 if (((size_t) id->time_from) >= fields_num)
119 return (EINVAL);
120 t = parse_time (fields[id->time_from]);
121 }
123 return (tcsv_submit (id, md, v, t));
124 }
126 static _Bool tcsv_check_index (ssize_t index, size_t fields_num, char const *name)
127 {
128 if (index < 0)
129 return 1;
130 else if (((size_t) index) < fields_num)
131 return 1;
133 ERROR ("tail_csv plugin: Metric \"%s\": Request for index %zd when "
134 "only %zu fields are available.",
135 name, index, fields_num);
136 return (0);
137 }
139 static int tcsv_read_buffer (instance_definition_t *id,
140 char *buffer, size_t buffer_size)
141 {
142 char **metrics;
143 size_t metrics_num;
145 char *ptr;
146 size_t i;
148 /* Remove newlines at the end of line. */
149 while (buffer_size > 0) {
150 if ((buffer[buffer_size - 1] == '\n')
151 || (buffer[buffer_size - 1] == '\r')) {
152 buffer[buffer_size - 1] = 0;
153 buffer_size--;
154 } else {
155 break;
156 }
157 }
159 /* Ignore empty lines. */
160 if ((buffer_size == 0) || (buffer[0] == '#'))
161 return (0);
163 /* Count the number of fields. */
164 metrics_num = 1;
165 for (i = 0; i < buffer_size; i++) {
166 if (buffer[i] == ',')
167 metrics_num++;
168 }
170 if (metrics_num == 1) {
171 ERROR("tail_csv plugin: last line of `%s' does not contain "
172 "enough values.", id->path);
173 return (-1);
174 }
176 /* Create a list of all values */
177 metrics = calloc (metrics_num, sizeof (*metrics));
178 if (metrics == NULL) {
179 ERROR ("tail_csv plugin: calloc failed.");
180 return (ENOMEM);
181 }
183 ptr = buffer;
184 metrics[0] = ptr;
185 i = 1;
186 for (ptr = buffer; *ptr != 0; ptr++) {
187 if (*ptr != ',')
188 continue;
190 *ptr = 0;
191 metrics[i] = ptr + 1;
192 i++;
193 }
194 assert (i == metrics_num);
196 /* Register values */
197 for (i = 0; i < id->metric_list_len; ++i){
198 metric_definition_t *md = id->metric_list[i];
200 if (!tcsv_check_index (md->value_from, metrics_num, md->name)
201 || !tcsv_check_index (id->time_from, metrics_num, md->name))
202 continue;
204 tcsv_read_metric (id, md, metrics, metrics_num);
205 }
207 /* Free up resources */
208 sfree (metrics);
209 return (0);
210 }
212 static int tcsv_read (user_data_t *ud) {
213 instance_definition_t *id;
214 id = ud->data;
216 if (id->tail == NULL)
217 {
218 id->tail = cu_tail_create (id->path);
219 if (id->tail == NULL)
220 {
221 ERROR ("tail_csv plugin: cu_tail_create (\"%s\") failed.",
222 id->path);
223 return (-1);
224 }
225 }
227 while (42)
228 {
229 char buffer[1024];
230 size_t buffer_len;
231 int status;
233 status = cu_tail_readline (id->tail, buffer, (int) sizeof (buffer));
234 if (status != 0)
235 {
236 ERROR ("tail_csv plugin: File \"%s\": cu_tail_readline failed "
237 "with status %i.", id->path, status);
238 return (-1);
239 }
241 buffer_len = strlen (buffer);
242 if (buffer_len == 0)
243 break;
245 tcsv_read_buffer (id, buffer, buffer_len);
246 }
248 return (0);
249 }
251 static void tcsv_metric_definition_destroy(void *arg){
252 metric_definition_t *md;
253 metric_definition_t *next;
255 md = arg;
256 if (md == NULL)
257 return;
259 next = md->next;
260 md->next = NULL;
262 sfree(md->name);
263 sfree(md->type);
264 sfree(md->instance);
265 sfree(md);
267 tcsv_metric_definition_destroy (next);
268 }
270 static int tcsv_config_get_index(oconfig_item_t *ci, ssize_t *ret_index) {
271 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)){
272 WARNING("tail_csv plugin: The \"%s\" config option needs exactly one "
273 "integer argument.", ci->key);
274 return (-1);
275 }
277 if (ci->values[0].value.number < 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 = (ssize_t) ci->values[0].value.number;
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;
292 md = calloc(1, sizeof(*md));
293 if (md == NULL)
294 return (-1);
295 md->name = NULL;
296 md->type = NULL;
297 md->instance = NULL;
298 md->data_source_type = -1;
299 md->value_from = -1;
300 md->next = NULL;
302 status = cf_util_get_string (ci, &md->name);
303 if (status != 0) {
304 sfree (md);
305 return (-1);
306 }
308 for (int i = 0; i < ci->children_num; ++i){
309 oconfig_item_t *option = ci->children + i;
311 if (strcasecmp("Type", option->key) == 0)
312 status = cf_util_get_string(option, &md->type);
313 else if (strcasecmp("Instance", option->key) == 0)
314 status = cf_util_get_string(option, &md->instance);
315 else if (strcasecmp("ValueFrom", option->key) == 0)
316 status = tcsv_config_get_index (option, &md->value_from);
317 else {
318 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
319 status = -1;
320 }
322 if (status != 0)
323 break;
324 }
326 if (status != 0){
327 tcsv_metric_definition_destroy(md);
328 return (-1);
329 }
331 /* Verify all necessary options have been set. */
332 if (md->type == NULL) {
333 WARNING("tail_csv plugin: Option `Type' must be set.");
334 status = -1;
335 } else if (md->value_from < 0) {
336 WARNING("tail_csv plugin: Option `ValueFrom' must be set.");
337 status = -1;
338 }
339 if (status != 0) {
340 tcsv_metric_definition_destroy(md);
341 return (status);
342 }
344 if (metric_head == NULL)
345 metric_head = md;
346 else {
347 metric_definition_t *last;
348 last = metric_head;
349 while (last->next != NULL)
350 last = last->next;
351 last->next = md;
352 }
354 return (0);
355 }
357 static void tcsv_instance_definition_destroy(void *arg){
358 instance_definition_t *id;
360 id = arg;
361 if (id == NULL)
362 return;
364 if (id->tail != NULL)
365 cu_tail_destroy (id->tail);
366 id->tail = NULL;
368 sfree(id->instance);
369 sfree(id->path);
370 sfree(id->metric_list);
371 sfree(id);
372 }
374 static int tcsv_config_add_instance_collect(instance_definition_t *id, oconfig_item_t *ci) {
375 metric_definition_t *metric;
376 metric_definition_t **metric_list;
377 size_t metric_list_size;
379 if (ci->values_num < 1) {
380 WARNING("tail_csv plugin: The `Collect' config option needs at least one argument.");
381 return (-1);
382 }
384 metric_list_size = id->metric_list_len + (size_t) ci->values_num;
385 metric_list = realloc (id->metric_list, sizeof (*id->metric_list) * metric_list_size);
386 if (metric_list == NULL)
387 return (-1);
388 id->metric_list = metric_list;
390 for (int i = 0; i < ci->values_num; i++) {
391 char *metric_name;
393 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
394 WARNING("tail_csv plugin: All arguments to `Collect' must be strings.");
395 continue;
396 }
397 metric_name = ci->values[i].value.string;
399 for (metric = metric_head; metric != NULL; metric = metric->next)
400 if (strcasecmp(metric_name, metric->name) == 0)
401 break;
403 if (metric == NULL) {
404 WARNING ("tail_csv plugin: `Collect' argument not found `%s'.", metric_name);
405 continue;
406 }
408 id->metric_list[id->metric_list_len] = metric;
409 id->metric_list_len++;
410 }
412 return (0);
413 }
415 /* <File /> block */
416 static int tcsv_config_add_file(oconfig_item_t *ci)
417 {
418 instance_definition_t* id;
419 int status = 0;
421 /* Registration variables */
422 char cb_name[DATA_MAX_NAME_LEN];
424 id = calloc(1, sizeof(*id));
425 if (id == NULL)
426 return (-1);
427 id->instance = NULL;
428 id->path = NULL;
429 id->metric_list = NULL;
430 id->time_from = -1;
431 id->next = NULL;
433 status = cf_util_get_string (ci, &id->path);
434 if (status != 0) {
435 sfree (id);
436 return (status);
437 }
439 /* Use default interval. */
440 id->interval = plugin_get_interval();
442 for (int i = 0; i < ci->children_num; ++i){
443 oconfig_item_t *option = ci->children + i;
444 status = 0;
446 if (strcasecmp("Instance", option->key) == 0)
447 status = cf_util_get_string(option, &id->instance);
448 else if (strcasecmp("Collect", option->key) == 0)
449 status = tcsv_config_add_instance_collect(id, option);
450 else if (strcasecmp("Interval", option->key) == 0)
451 cf_util_get_cdtime(option, &id->interval);
452 else if (strcasecmp("TimeFrom", option->key) == 0)
453 status = tcsv_config_get_index (option, &id->time_from);
454 else {
455 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
456 status = -1;
457 }
459 if (status != 0)
460 break;
461 }
463 if (status != 0){
464 tcsv_instance_definition_destroy(id);
465 return (-1);
466 }
468 /* Verify all necessary options have been set. */
469 if (id->path == NULL){
470 WARNING("tail_csv plugin: Option `Path' must be set.");
471 status = -1;
472 } else if (id->metric_list == NULL){
473 WARNING("tail_csv plugin: Option `Collect' must be set.");
474 status = -1;
475 }
477 if (status != 0){
478 tcsv_instance_definition_destroy(id);
479 return (-1);
480 }
482 ssnprintf (cb_name, sizeof (cb_name), "tail_csv/%s", id->path);
484 status = plugin_register_complex_read(NULL, cb_name, tcsv_read, id->interval,
485 &(user_data_t) {
486 .data = id,
487 .free_func = tcsv_instance_definition_destroy,
488 });
489 if (status != 0){
490 ERROR("tail_csv plugin: Registering complex read function failed.");
491 tcsv_instance_definition_destroy(id);
492 return (-1);
493 }
495 return (0);
496 }
498 /* Parse blocks */
499 static int tcsv_config(oconfig_item_t *ci){
500 for (int i = 0; i < ci->children_num; ++i){
501 oconfig_item_t *child = ci->children + i;
502 if (strcasecmp("Metric", child->key) == 0)
503 tcsv_config_add_metric(child);
504 else if (strcasecmp("File", child->key) == 0)
505 tcsv_config_add_file(child);
506 else
507 WARNING("tail_csv plugin: Ignore unknown config option `%s'.", child->key);
508 }
510 return (0);
511 } /* int tcsv_config */
513 static int tcsv_init(void) { /* {{{ */
514 static _Bool have_init = 0;
515 metric_definition_t *md;
517 if (have_init)
518 return (0);
520 for (md = metric_head; md != NULL; md = md->next) {
521 data_set_t const *ds;
523 /* Retrieve the data source type from the types db. */
524 ds = plugin_get_ds(md->type);
525 if (ds == NULL)
526 {
527 ERROR ("tail_csv plugin: Failed to look up type \"%s\" for "
528 "metric \"%s\". It may not be defined in the types.db "
529 "file. Please read the types.db(5) manual page for more "
530 "details.",
531 md->type, md->name);
532 continue;
533 }
534 else if (ds->ds_num != 1)
535 {
536 ERROR ("tail_csv plugin: The type \"%s\" has %zu data sources. "
537 "Only types with a single data source are supported.",
538 ds->type, ds->ds_num);
539 continue;
540 }
542 md->data_source_type = ds->ds->type;
543 }
545 return (0);
546 } /* }}} int tcsv_init */
548 static int tcsv_shutdown (void) {
549 tcsv_metric_definition_destroy (metric_head);
550 metric_head = NULL;
552 return (0);
553 }
555 void module_register(void){
556 plugin_register_complex_config("tail_csv", tcsv_config);
557 plugin_register_init("tail_csv", tcsv_init);
558 plugin_register_shutdown("tail_csv", tcsv_shutdown);
559 }
561 /* vim: set sw=4 sts=4 et : */