86c4fa88705e096b45d96ff9cebfaab32d21e8d2
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.host, hostname_g, sizeof (vl.host));
73 sstrncpy(vl.plugin, "tail_csv", sizeof(vl.plugin));
74 if (id->instance != NULL)
75 sstrncpy(vl.plugin_instance, id->instance, sizeof(vl.plugin_instance));
76 sstrncpy(vl.type, md->type, sizeof(vl.type));
77 if (md->instance != NULL)
78 sstrncpy(vl.type_instance, md->instance, sizeof(vl.type_instance));
80 vl.time = t;
81 vl.interval = id->interval;
83 return (plugin_dispatch_values(&vl));
84 }
86 static cdtime_t parse_time (char const *tbuf)
87 {
88 double t;
89 char *endptr = NULL;
91 errno = 0;
92 t = strtod (tbuf, &endptr);
93 if ((errno != 0) || (endptr == NULL) || (endptr[0] != 0))
94 return (cdtime ());
96 return (DOUBLE_TO_CDTIME_T (t));
97 }
99 static int tcsv_read_metric (instance_definition_t *id,
100 metric_definition_t *md,
101 char **fields, size_t fields_num)
102 {
103 value_t v;
104 cdtime_t t = 0;
105 int status;
107 if (md->data_source_type == -1)
108 return (EINVAL);
110 assert (md->value_from >= 0);
111 if (((size_t) md->value_from) >= fields_num)
112 return (EINVAL);
114 status = parse_value (fields[md->value_from], &v, md->data_source_type);
115 if (status != 0)
116 return (status);
118 if (id->time_from >= 0) {
119 if (((size_t) id->time_from) >= fields_num)
120 return (EINVAL);
121 t = parse_time (fields[id->time_from]);
122 }
124 return (tcsv_submit (id, md, v, t));
125 }
127 static _Bool tcsv_check_index (ssize_t index, size_t fields_num, char const *name)
128 {
129 if (index < 0)
130 return 1;
131 else if (((size_t) index) < fields_num)
132 return 1;
134 ERROR ("tail_csv plugin: Metric \"%s\": Request for index %zd when "
135 "only %zu fields are available.",
136 name, index, fields_num);
137 return (0);
138 }
140 static int tcsv_read_buffer (instance_definition_t *id,
141 char *buffer, size_t buffer_size)
142 {
143 char **metrics;
144 size_t metrics_num;
146 char *ptr;
147 size_t i;
149 /* Remove newlines at the end of line. */
150 while (buffer_size > 0) {
151 if ((buffer[buffer_size - 1] == '\n')
152 || (buffer[buffer_size - 1] == '\r')) {
153 buffer[buffer_size - 1] = 0;
154 buffer_size--;
155 } else {
156 break;
157 }
158 }
160 /* Ignore empty lines. */
161 if ((buffer_size == 0) || (buffer[0] == '#'))
162 return (0);
164 /* Count the number of fields. */
165 metrics_num = 1;
166 for (i = 0; i < buffer_size; i++) {
167 if (buffer[i] == ',')
168 metrics_num++;
169 }
171 if (metrics_num == 1) {
172 ERROR("tail_csv plugin: last line of `%s' does not contain "
173 "enough values.", id->path);
174 return (-1);
175 }
177 /* Create a list of all values */
178 metrics = calloc (metrics_num, sizeof (*metrics));
179 if (metrics == NULL) {
180 ERROR ("tail_csv plugin: calloc failed.");
181 return (ENOMEM);
182 }
184 ptr = buffer;
185 metrics[0] = ptr;
186 i = 1;
187 for (ptr = buffer; *ptr != 0; ptr++) {
188 if (*ptr != ',')
189 continue;
191 *ptr = 0;
192 metrics[i] = ptr + 1;
193 i++;
194 }
195 assert (i == metrics_num);
197 /* Register values */
198 for (i = 0; i < id->metric_list_len; ++i){
199 metric_definition_t *md = id->metric_list[i];
201 if (!tcsv_check_index (md->value_from, metrics_num, md->name)
202 || !tcsv_check_index (id->time_from, metrics_num, md->name))
203 continue;
205 tcsv_read_metric (id, md, metrics, metrics_num);
206 }
208 /* Free up resources */
209 sfree (metrics);
210 return (0);
211 }
213 static int tcsv_read (user_data_t *ud) {
214 instance_definition_t *id;
215 id = ud->data;
217 if (id->tail == NULL)
218 {
219 id->tail = cu_tail_create (id->path);
220 if (id->tail == NULL)
221 {
222 ERROR ("tail_csv plugin: cu_tail_create (\"%s\") failed.",
223 id->path);
224 return (-1);
225 }
226 }
228 while (42)
229 {
230 char buffer[1024];
231 size_t buffer_len;
232 int status;
234 status = cu_tail_readline (id->tail, buffer, (int) sizeof (buffer));
235 if (status != 0)
236 {
237 ERROR ("tail_csv plugin: File \"%s\": cu_tail_readline failed "
238 "with status %i.", id->path, status);
239 return (-1);
240 }
242 buffer_len = strlen (buffer);
243 if (buffer_len == 0)
244 break;
246 tcsv_read_buffer (id, buffer, buffer_len);
247 }
249 return (0);
250 }
252 static void tcsv_metric_definition_destroy(void *arg){
253 metric_definition_t *md;
254 metric_definition_t *next;
256 md = arg;
257 if (md == NULL)
258 return;
260 next = md->next;
261 md->next = NULL;
263 sfree(md->name);
264 sfree(md->type);
265 sfree(md->instance);
266 sfree(md);
268 tcsv_metric_definition_destroy (next);
269 }
271 static int tcsv_config_get_index(oconfig_item_t *ci, ssize_t *ret_index) {
272 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)){
273 WARNING("tail_csv plugin: The \"%s\" config option needs exactly one "
274 "integer argument.", ci->key);
275 return (-1);
276 }
278 if (ci->values[0].value.number < 0) {
279 WARNING("tail_csv plugin: The \"%s\" config option must be positive "
280 "(or zero).", ci->key);
281 return (-1);
282 }
284 *ret_index = (ssize_t) ci->values[0].value.number;
285 return (0);
286 }
288 /* Parse metric */
289 static int tcsv_config_add_metric(oconfig_item_t *ci){
290 metric_definition_t *md;
291 int status;
292 int i;
294 md = calloc(1, sizeof(*md));
295 if (md == NULL)
296 return (-1);
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;
313 if (strcasecmp("Type", option->key) == 0)
314 status = cf_util_get_string(option, &md->type);
315 else if (strcasecmp("Instance", option->key) == 0)
316 status = cf_util_get_string(option, &md->instance);
317 else if (strcasecmp("ValueFrom", option->key) == 0)
318 status = tcsv_config_get_index (option, &md->value_from);
319 else {
320 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
321 status = -1;
322 }
324 if (status != 0)
325 break;
326 }
328 if (status != 0){
329 tcsv_metric_definition_destroy(md);
330 return (-1);
331 }
333 /* Verify all necessary options have been set. */
334 if (md->type == NULL) {
335 WARNING("tail_csv plugin: Option `Type' must be set.");
336 status = -1;
337 } else if (md->value_from < 0) {
338 WARNING("tail_csv plugin: Option `ValueFrom' must be set.");
339 status = -1;
340 }
341 if (status != 0) {
342 tcsv_metric_definition_destroy(md);
343 return (status);
344 }
346 if (metric_head == NULL)
347 metric_head = md;
348 else {
349 metric_definition_t *last;
350 last = metric_head;
351 while (last->next != NULL)
352 last = last->next;
353 last->next = md;
354 }
356 return (0);
357 }
359 static void tcsv_instance_definition_destroy(void *arg){
360 instance_definition_t *id;
362 id = arg;
363 if (id == NULL)
364 return;
366 if (id->tail != NULL)
367 cu_tail_destroy (id->tail);
368 id->tail = NULL;
370 sfree(id->instance);
371 sfree(id->path);
372 sfree(id->metric_list);
373 sfree(id);
374 }
376 static int tcsv_config_add_instance_collect(instance_definition_t *id, oconfig_item_t *ci) {
377 metric_definition_t *metric;
378 metric_definition_t **metric_list;
379 size_t metric_list_size;
380 int i;
382 if (ci->values_num < 1) {
383 WARNING("tail_csv plugin: The `Collect' config option needs at least one argument.");
384 return (-1);
385 }
387 metric_list_size = id->metric_list_len + (size_t) ci->values_num;
388 metric_list = realloc (id->metric_list, sizeof (*id->metric_list) * metric_list_size);
389 if (metric_list == NULL)
390 return (-1);
391 id->metric_list = metric_list;
393 for (i = 0; i < ci->values_num; i++) {
394 char *metric_name;
396 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
397 WARNING("tail_csv plugin: All arguments to `Collect' must be strings.");
398 continue;
399 }
400 metric_name = ci->values[i].value.string;
402 for (metric = metric_head; metric != NULL; metric = metric->next)
403 if (strcasecmp(metric_name, metric->name) == 0)
404 break;
406 if (metric == NULL) {
407 WARNING ("tail_csv plugin: `Collect' argument not found `%s'.", metric_name);
408 continue;
409 }
411 id->metric_list[id->metric_list_len] = metric;
412 id->metric_list_len++;
413 }
415 return (0);
416 }
418 /* <File /> block */
419 static int tcsv_config_add_file(oconfig_item_t *ci)
420 {
421 instance_definition_t* id;
422 int status = 0;
423 int i;
425 /* Registration variables */
426 char cb_name[DATA_MAX_NAME_LEN];
427 user_data_t cb_data = { 0 };
429 id = calloc(1, sizeof(*id));
430 if (id == NULL)
431 return (-1);
432 id->instance = NULL;
433 id->path = NULL;
434 id->metric_list = NULL;
435 id->time_from = -1;
436 id->next = NULL;
438 status = cf_util_get_string (ci, &id->path);
439 if (status != 0) {
440 sfree (id);
441 return (status);
442 }
444 /* Use default interval. */
445 id->interval = plugin_get_interval();
447 for (i = 0; i < ci->children_num; ++i){
448 oconfig_item_t *option = ci->children + i;
449 status = 0;
451 if (strcasecmp("Instance", option->key) == 0)
452 status = cf_util_get_string(option, &id->instance);
453 else if (strcasecmp("Collect", option->key) == 0)
454 status = tcsv_config_add_instance_collect(id, option);
455 else if (strcasecmp("Interval", option->key) == 0)
456 cf_util_get_cdtime(option, &id->interval);
457 else if (strcasecmp("TimeFrom", option->key) == 0)
458 status = tcsv_config_get_index (option, &id->time_from);
459 else {
460 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
461 status = -1;
462 }
464 if (status != 0)
465 break;
466 }
468 if (status != 0){
469 tcsv_instance_definition_destroy(id);
470 return (-1);
471 }
473 /* Verify all necessary options have been set. */
474 if (id->path == NULL){
475 WARNING("tail_csv plugin: Option `Path' must be set.");
476 status = -1;
477 } else if (id->metric_list == NULL){
478 WARNING("tail_csv plugin: Option `Collect' must be set.");
479 status = -1;
480 }
482 if (status != 0){
483 tcsv_instance_definition_destroy(id);
484 return (-1);
485 }
487 ssnprintf (cb_name, sizeof (cb_name), "tail_csv/%s", id->path);
488 cb_data.data = id;
489 cb_data.free_func = tcsv_instance_definition_destroy;
490 status = plugin_register_complex_read(NULL, cb_name, tcsv_read, id->interval, &cb_data);
492 if (status != 0){
493 ERROR("tail_csv plugin: Registering complex read function failed.");
494 tcsv_instance_definition_destroy(id);
495 return (-1);
496 }
498 return (0);
499 }
501 /* Parse blocks */
502 static int tcsv_config(oconfig_item_t *ci){
503 int i;
504 for (i = 0; i < ci->children_num; ++i){
505 oconfig_item_t *child = ci->children + i;
506 if (strcasecmp("Metric", child->key) == 0)
507 tcsv_config_add_metric(child);
508 else if (strcasecmp("File", child->key) == 0)
509 tcsv_config_add_file(child);
510 else
511 WARNING("tail_csv plugin: Ignore unknown config option `%s'.", child->key);
512 }
514 return (0);
515 } /* int tcsv_config */
517 static int tcsv_init(void) { /* {{{ */
518 static _Bool have_init = 0;
519 metric_definition_t *md;
521 if (have_init)
522 return (0);
524 for (md = metric_head; md != NULL; md = md->next) {
525 data_set_t const *ds;
527 /* Retrieve the data source type from the types db. */
528 ds = plugin_get_ds(md->type);
529 if (ds == NULL)
530 {
531 ERROR ("tail_csv plugin: Failed to look up type \"%s\" for "
532 "metric \"%s\". It may not be defined in the types.db "
533 "file. Please read the types.db(5) manual page for more "
534 "details.",
535 md->type, md->name);
536 continue;
537 }
538 else if (ds->ds_num != 1)
539 {
540 ERROR ("tail_csv plugin: The type \"%s\" has %zu data sources. "
541 "Only types with a single data soure are supported.",
542 ds->type, ds->ds_num);
543 continue;
544 }
546 md->data_source_type = ds->ds->type;
547 }
549 return (0);
550 } /* }}} int tcsv_init */
552 static int tcsv_shutdown (void) {
553 tcsv_metric_definition_destroy (metric_head);
554 metric_head = NULL;
556 return (0);
557 }
559 void module_register(void){
560 plugin_register_complex_config("tail_csv", tcsv_config);
561 plugin_register_init("tail_csv", tcsv_init);
562 plugin_register_shutdown("tail_csv", tcsv_shutdown);
563 }
565 /* vim: set sw=4 sts=4 et : */