Debian Codegraph @

src/tail_csv.c

raw
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 **/
23
24 #include "collectd.h"
25
26 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
27 #include "utils/common/common.h" /* auxiliary functions */
28 #include "utils/tail/tail.h"
29
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35
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;
45
46 struct instance_definition_s {
47 char *plugin_name;
48 char *instance;
49 char *path;
50 char field_separator;
51 cu_tail_t *tail;
52 metric_definition_t **metric_list;
53 size_t metric_list_len;
54 ssize_t time_from;
55 struct instance_definition_s *next;
56 };
57 typedef struct instance_definition_s instance_definition_t;
58
59 /* Private */
60 static metric_definition_t *metric_head;
61
62 static int tcsv_submit(instance_definition_t *id, metric_definition_t *md,
63 value_t v, cdtime_t t) {
64 /* Registration variables */
65 value_list_t vl = VALUE_LIST_INIT;
66
67 /* Register */
68 vl.values_len = 1;
69 vl.values = &v;
70
71 sstrncpy(vl.plugin, (id->plugin_name != NULL) ? id->plugin_name : "tail_csv",
72 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));
78
79 vl.time = t;
80
81 return plugin_dispatch_values(&vl);
82 }
83
84 static cdtime_t parse_time(char const *tbuf) {
85 double t;
86 char *endptr = NULL;
87
88 errno = 0;
89 t = strtod(tbuf, &endptr);
90 if ((errno != 0) || (endptr == NULL) || (endptr[0] != 0))
91 return cdtime();
92
93 return DOUBLE_TO_CDTIME_T(t);
94 }
95
96 static int tcsv_read_metric(instance_definition_t *id, metric_definition_t *md,
97 char **fields, size_t fields_num) {
98 value_t v;
99 cdtime_t t = 0;
100 int status;
101
102 if (md->data_source_type == -1)
103 return EINVAL;
104
105 assert(md->value_from >= 0);
106 if (((size_t)md->value_from) >= fields_num)
107 return EINVAL;
108
109 status = parse_value(fields[md->value_from], &v, md->data_source_type);
110 if (status != 0)
111 return status;
112
113 if (id->time_from >= 0) {
114 if (((size_t)id->time_from) >= fields_num)
115 return EINVAL;
116 t = parse_time(fields[id->time_from]);
117 }
118
119 return tcsv_submit(id, md, v, t);
120 }
121
122 static bool tcsv_check_index(ssize_t index, size_t fields_num,
123 char const *name) {
124 if (index < 0)
125 return true;
126 else if (((size_t)index) < fields_num)
127 return true;
128
129 ERROR("tail_csv plugin: Metric \"%s\": Request for index %zd when "
130 "only %" PRIsz " fields are available.",
131 name, index, fields_num);
132 return false;
133 }
134
135 static int tcsv_read_buffer(instance_definition_t *id, char *buffer,
136 size_t buffer_size) {
137 char **metrics;
138 size_t metrics_num;
139
140 char *ptr;
141 size_t i;
142
143 /* Remove newlines at the end of line. */
144 while (buffer_size > 0) {
145 if ((buffer[buffer_size - 1] == '\n') ||
146 (buffer[buffer_size - 1] == '\r')) {
147 buffer[buffer_size - 1] = '\0';
148 buffer_size--;
149 } else {
150 break;
151 }
152 }
153
154 /* Ignore empty lines. */
155 if ((buffer_size == 0) || (buffer[0] == '#'))
156 return 0;
157
158 /* Count the number of fields. */
159 metrics_num = 1;
160 for (i = 0; i < buffer_size; i++) {
161 if (buffer[i] == id->field_separator)
162 metrics_num++;
163 }
164
165 if (metrics_num == 1) {
166 ERROR("tail_csv plugin: last line of `%s' does not contain "
167 "enough values.",
168 id->path);
169 return -1;
170 }
171
172 /* Create a list of all values */
173 metrics = calloc(metrics_num, sizeof(*metrics));
174 if (metrics == NULL) {
175 ERROR("tail_csv plugin: calloc failed.");
176 return ENOMEM;
177 }
178
179 ptr = buffer;
180 metrics[0] = ptr;
181 i = 1;
182 for (ptr = buffer; *ptr != 0; ptr++) {
183 if (*ptr != id->field_separator)
184 continue;
185
186 *ptr = 0;
187 metrics[i] = ptr + 1;
188 i++;
189 }
190 assert(i == metrics_num);
191
192 /* Register values */
193 for (i = 0; i < id->metric_list_len; ++i) {
194 metric_definition_t *md = id->metric_list[i];
195
196 if (!tcsv_check_index(md->value_from, metrics_num, md->name) ||
197 !tcsv_check_index(id->time_from, metrics_num, md->name))
198 continue;
199
200 tcsv_read_metric(id, md, metrics, metrics_num);
201 }
202
203 /* Free up resources */
204 sfree(metrics);
205 return 0;
206 }
207
208 static int tcsv_read(user_data_t *ud) {
209 instance_definition_t *id;
210 id = ud->data;
211
212 if (id->tail == NULL) {
213 id->tail = cu_tail_create(id->path);
214 if (id->tail == NULL) {
215 ERROR("tail_csv plugin: cu_tail_create (\"%s\") failed.", id->path);
216 return -1;
217 }
218 }
219
220 while (42) {
221 char buffer[1024];
222 size_t buffer_len;
223 int status;
224
225 status = cu_tail_readline(id->tail, buffer, (int)sizeof(buffer), 0);
226 if (status != 0) {
227 ERROR("tail_csv plugin: File \"%s\": cu_tail_readline failed "
228 "with status %i.",
229 id->path, status);
230 return -1;
231 }
232
233 buffer_len = strlen(buffer);
234 if (buffer_len == 0)
235 break;
236
237 tcsv_read_buffer(id, buffer, buffer_len);
238 }
239
240 return 0;
241 }
242
243 static void tcsv_metric_definition_destroy(void *arg) {
244 metric_definition_t *md;
245 metric_definition_t *next;
246
247 md = arg;
248 if (md == NULL)
249 return;
250
251 next = md->next;
252 md->next = NULL;
253
254 sfree(md->name);
255 sfree(md->type);
256 sfree(md->instance);
257 sfree(md);
258
259 tcsv_metric_definition_destroy(next);
260 }
261
262 static int tcsv_config_get_index(oconfig_item_t *ci, ssize_t *ret_index) {
263 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
264 WARNING("tail_csv plugin: The \"%s\" config option needs exactly one "
265 "integer argument.",
266 ci->key);
267 return -1;
268 }
269
270 if (ci->values[0].value.number < 0) {
271 WARNING("tail_csv plugin: The \"%s\" config option must be positive "
272 "(or zero).",
273 ci->key);
274 return -1;
275 }
276
277 *ret_index = (ssize_t)ci->values[0].value.number;
278 return 0;
279 }
280
281 static int tcsv_config_get_separator(oconfig_item_t *ci, char *ret_char) {
282 size_t len_opt;
283
284 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
285 WARNING("tail_csv plugin: The \"%s\" config option needs exactly one "
286 "string argument.",
287 ci->key);
288 return -1;
289 }
290
291 len_opt = strlen(ci->values[0].value.string);
292 if (len_opt != 1) {
293 WARNING("tail_csv plugin: The \"%s\" config option must be a "
294 "single character",
295 ci->key);
296 return -1;
297 }
298
299 *ret_char = ci->values[0].value.string[0];
300 return 0;
301 }
302
303 /* Parse metric */
304 static int tcsv_config_add_metric(oconfig_item_t *ci) {
305 metric_definition_t *md;
306 int status;
307
308 md = calloc(1, sizeof(*md));
309 if (md == NULL)
310 return -1;
311 md->name = NULL;
312 md->type = NULL;
313 md->instance = NULL;
314 md->data_source_type = -1;
315 md->value_from = -1;
316 md->next = NULL;
317
318 status = cf_util_get_string(ci, &md->name);
319 if (status != 0) {
320 sfree(md);
321 return -1;
322 }
323
324 for (int i = 0; i < ci->children_num; ++i) {
325 oconfig_item_t *option = ci->children + i;
326
327 if (strcasecmp("Type", option->key) == 0)
328 status = cf_util_get_string(option, &md->type);
329 else if (strcasecmp("Instance", option->key) == 0)
330 status = cf_util_get_string(option, &md->instance);
331 else if (strcasecmp("ValueFrom", option->key) == 0)
332 status = tcsv_config_get_index(option, &md->value_from);
333 else {
334 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
335 status = -1;
336 }
337
338 if (status != 0)
339 break;
340 }
341
342 if (status != 0) {
343 tcsv_metric_definition_destroy(md);
344 return -1;
345 }
346
347 /* Verify all necessary options have been set. */
348 if (md->type == NULL) {
349 WARNING("tail_csv plugin: Option `Type' must be set.");
350 status = -1;
351 } else if (md->value_from < 0) {
352 WARNING("tail_csv plugin: Option `ValueFrom' must be set.");
353 status = -1;
354 }
355 if (status != 0) {
356 tcsv_metric_definition_destroy(md);
357 return status;
358 }
359
360 if (metric_head == NULL)
361 metric_head = md;
362 else {
363 metric_definition_t *last;
364 last = metric_head;
365 while (last->next != NULL)
366 last = last->next;
367 last->next = md;
368 }
369
370 return 0;
371 }
372
373 static void tcsv_instance_definition_destroy(void *arg) {
374 instance_definition_t *id;
375
376 id = arg;
377 if (id == NULL)
378 return;
379
380 if (id->tail != NULL)
381 cu_tail_destroy(id->tail);
382 id->tail = NULL;
383
384 sfree(id->plugin_name);
385 sfree(id->instance);
386 sfree(id->path);
387 sfree(id->metric_list);
388 sfree(id);
389 }
390
391 static int tcsv_config_add_instance_collect(instance_definition_t *id,
392 oconfig_item_t *ci) {
393 metric_definition_t *metric;
394 metric_definition_t **metric_list;
395 size_t metric_list_size;
396
397 if (ci->values_num < 1) {
398 WARNING("tail_csv plugin: The `Collect' config option needs at least one "
399 "argument.");
400 return -1;
401 }
402
403 metric_list_size = id->metric_list_len + (size_t)ci->values_num;
404 metric_list =
405 realloc(id->metric_list, sizeof(*id->metric_list) * metric_list_size);
406 if (metric_list == NULL)
407 return -1;
408 id->metric_list = metric_list;
409
410 for (int i = 0; i < ci->values_num; i++) {
411 char *metric_name;
412
413 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
414 WARNING("tail_csv plugin: All arguments to `Collect' must be strings.");
415 continue;
416 }
417 metric_name = ci->values[i].value.string;
418
419 for (metric = metric_head; metric != NULL; metric = metric->next)
420 if (strcasecmp(metric_name, metric->name) == 0)
421 break;
422
423 if (metric == NULL) {
424 WARNING("tail_csv plugin: `Collect' argument not found `%s'.",
425 metric_name);
426 continue;
427 }
428
429 id->metric_list[id->metric_list_len] = metric;
430 id->metric_list_len++;
431 }
432
433 return 0;
434 }
435
436 /* <File /> block */
437 static int tcsv_config_add_file(oconfig_item_t *ci) {
438 instance_definition_t *id;
439 int status = 0;
440
441 /* Registration variables */
442 cdtime_t interval = 0;
443 char cb_name[DATA_MAX_NAME_LEN];
444
445 id = calloc(1, sizeof(*id));
446 if (id == NULL)
447 return -1;
448 id->plugin_name = NULL;
449 id->instance = NULL;
450 id->path = NULL;
451 id->metric_list = NULL;
452 id->time_from = -1;
453 id->field_separator = ',';
454 id->next = NULL;
455
456 status = cf_util_get_string(ci, &id->path);
457 if (status != 0) {
458 sfree(id);
459 return status;
460 }
461
462 for (int i = 0; i < ci->children_num; ++i) {
463 oconfig_item_t *option = ci->children + i;
464 status = 0;
465
466 if (strcasecmp("Instance", option->key) == 0)
467 status = cf_util_get_string(option, &id->instance);
468 else if (strcasecmp("Collect", option->key) == 0)
469 status = tcsv_config_add_instance_collect(id, option);
470 else if (strcasecmp("Interval", option->key) == 0)
471 cf_util_get_cdtime(option, &interval);
472 else if (strcasecmp("TimeFrom", option->key) == 0)
473 status = tcsv_config_get_index(option, &id->time_from);
474 else if (strcasecmp("Plugin", option->key) == 0)
475 status = cf_util_get_string(option, &id->plugin_name);
476 else if (strcasecmp("FieldSeparator", option->key) == 0)
477 status = tcsv_config_get_separator(option, &id->field_separator);
478 else {
479 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
480 status = -1;
481 }
482
483 if (status != 0)
484 break;
485 }
486
487 if (status != 0) {
488 tcsv_instance_definition_destroy(id);
489 return -1;
490 }
491
492 /* Verify all necessary options have been set. */
493 if (id->path == NULL) {
494 WARNING("tail_csv plugin: Option `Path' must be set.");
495 status = -1;
496 } else if (id->metric_list == NULL) {
497 WARNING("tail_csv plugin: Option `Collect' must be set.");
498 status = -1;
499 }
500
501 if (status != 0) {
502 tcsv_instance_definition_destroy(id);
503 return -1;
504 }
505
506 snprintf(cb_name, sizeof(cb_name), "tail_csv/%s", id->path);
507
508 status = plugin_register_complex_read(
509 NULL, cb_name, tcsv_read, interval,
510 &(user_data_t){
511 .data = id,
512 .free_func = tcsv_instance_definition_destroy,
513 });
514 if (status != 0) {
515 ERROR("tail_csv plugin: Registering complex read function failed.");
516 return -1;
517 }
518
519 return 0;
520 }
521
522 /* Parse blocks */
523 static int tcsv_config(oconfig_item_t *ci) {
524 for (int i = 0; i < ci->children_num; ++i) {
525 oconfig_item_t *child = ci->children + i;
526 if (strcasecmp("Metric", child->key) == 0)
527 tcsv_config_add_metric(child);
528 else if (strcasecmp("File", child->key) == 0)
529 tcsv_config_add_file(child);
530 else
531 WARNING("tail_csv plugin: Ignore unknown config option `%s'.",
532 child->key);
533 }
534
535 return 0;
536 } /* int tcsv_config */
537
538 static int tcsv_init(void) { /* {{{ */
539 static bool have_init;
540 metric_definition_t *md;
541
542 if (have_init)
543 return 0;
544
545 for (md = metric_head; md != NULL; md = md->next) {
546 data_set_t const *ds;
547
548 /* Retrieve the data source type from the types db. */
549 ds = plugin_get_ds(md->type);
550 if (ds == NULL) {
551 ERROR("tail_csv plugin: Failed to look up type \"%s\" for "
552 "metric \"%s\". It may not be defined in the types.db "
553 "file. Please read the types.db(5) manual page for more "
554 "details.",
555 md->type, md->name);
556 continue;
557 } else if (ds->ds_num != 1) {
558 ERROR("tail_csv plugin: The type \"%s\" has %" PRIsz " data sources. "
559 "Only types with a single data source are supported.",
560 ds->type, ds->ds_num);
561 continue;
562 }
563
564 md->data_source_type = ds->ds->type;
565 }
566
567 return 0;
568 } /* }}} int tcsv_init */
569
570 static int tcsv_shutdown(void) {
571 tcsv_metric_definition_destroy(metric_head);
572 metric_head = NULL;
573
574 return 0;
575 }
576
577 void module_register(void) {
578 plugin_register_complex_config("tail_csv", tcsv_config);
579 plugin_register_init("tail_csv", tcsv_init);
580 plugin_register_shutdown("tail_csv", tcsv_shutdown);
581 }