dc89f0d70b01c10a8d2941d754c80326d4cf31f7
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 index;
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 struct instance_definition_s *next;
53 };
54 typedef struct instance_definition_s instance_definition_t;
56 /* Private */
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)
62 {
63 /* Registration variables */
64 value_list_t vl = VALUE_LIST_INIT;
66 /* Register */
67 vl.values_len = 1;
68 vl.values = &v;
70 sstrncpy(vl.host, hostname_g, sizeof (vl.host));
71 sstrncpy(vl.plugin, "tail_csv", sizeof(vl.plugin));
72 if (id->instance != NULL)
73 sstrncpy(vl.plugin_instance, id->instance, sizeof(vl.plugin_instance));
74 sstrncpy(vl.type, md->type, sizeof(vl.type));
75 if (md->instance != NULL)
76 sstrncpy(vl.type_instance, md->instance, sizeof(vl.type_instance));
78 vl.time = t;
79 vl.interval = id->interval;
81 return (plugin_dispatch_values(&vl));
82 }
84 static cdtime_t parse_time (char const *tbuf)
85 {
86 double t;
87 char *endptr = 0;
89 errno = 0;
90 t = strtod (tbuf, &endptr);
91 if ((errno != 0) || (endptr == NULL) || (endptr[0] != 0))
92 return (cdtime ());
94 return (DOUBLE_TO_CDTIME_T (t));
95 }
97 static int tcsv_read_metric (instance_definition_t *id,
98 metric_definition_t *md,
99 char **fields, size_t fields_num)
100 {
101 value_t v;
102 cdtime_t t;
103 int status;
105 if (md->data_source_type == -1)
106 return (EINVAL);
108 if (md->index >= fields_num)
109 return (EINVAL);
111 t = parse_time (fields[0]);
113 status = parse_value (fields[md->index], &v, md->data_source_type);
114 if (status != 0)
115 return (status);
117 return (tcsv_submit (id, md, v, t));
118 }
120 static int tcsv_read_buffer (instance_definition_t *id,
121 char *buffer, size_t buffer_size)
122 {
123 char **metrics;
124 size_t metrics_num;
126 char *ptr;
127 size_t i;
129 /* Remove newlines at the end of line. */
130 while (buffer_size > 0) {
131 if ((buffer[buffer_size - 1] == '\n')
132 || (buffer[buffer_size - 1] == '\r')) {
133 buffer[buffer_size - 1] = 0;
134 buffer_size--;
135 } else {
136 break;
137 }
138 }
140 /* Ignore empty lines. */
141 if ((buffer_size == 0) || (buffer[0] == '#'))
142 return (0);
144 /* Count the number of fields. */
145 metrics_num = 1;
146 for (i = 0; i < buffer_size; i++) {
147 if (buffer[i] == ',')
148 metrics_num++;
149 }
151 if (metrics_num == 1) {
152 ERROR("tail_csv plugin: last line of `%s' does not contain "
153 "enough values.", id->path);
154 return (-1);
155 }
157 /* Create a list of all values */
158 metrics = calloc (metrics_num, sizeof (*metrics));
159 if (metrics == NULL) {
160 ERROR ("tail_csv plugin: calloc failed.");
161 return (ENOMEM);
162 }
164 ptr = buffer;
165 metrics[0] = ptr;
166 i = 1;
167 for (ptr = buffer; *ptr != 0; ptr++) {
168 if (*ptr != ',')
169 continue;
171 *ptr = 0;
172 metrics[i] = ptr + 1;
173 i++;
174 }
175 assert (i == metrics_num);
177 /* Register values */
178 for (i = 0; i < id->metric_list_len; ++i){
179 metric_definition_t *md = id->metric_list[i];
181 if (((size_t) md->index) >= metrics_num) {
182 ERROR ("tail_csv plugin: Metric \"%s\": Request for index %i when "
183 "only %zu fields are available.",
184 md->name, md->index, metrics_num);
185 continue;
186 }
188 tcsv_read_metric (id, md, metrics, metrics_num);
189 }
191 /* Free up resources */
192 sfree (metrics);
193 return (0);
194 }
196 static int tcsv_read (user_data_t *ud) {
197 instance_definition_t *id;
198 id = ud->data;
200 if (id->tail == NULL)
201 {
202 id->tail = cu_tail_create (id->path);
203 if (id->tail == NULL)
204 {
205 ERROR ("tail_csv plugin: cu_tail_create (\"%s\") failed.",
206 id->path);
207 return (-1);
208 }
209 }
211 while (42)
212 {
213 char buffer[1024];
214 size_t buffer_len;
215 int status;
217 status = cu_tail_readline (id->tail, buffer, (int) sizeof (buffer));
218 if (status != 0)
219 {
220 ERROR ("tail_csv plugin: File \"%s\": cu_tail_readline failed "
221 "with status %i.", id->path, status);
222 return (-1);
223 }
225 buffer_len = strlen (buffer);
226 if (buffer_len == 0)
227 break;
229 tcsv_read_buffer (id, buffer, buffer_len);
230 }
232 return (0);
233 }
235 static void tcsv_metric_definition_destroy(void *arg){
236 metric_definition_t *md;
237 metric_definition_t *next;
239 md = arg;
240 if (md == NULL)
241 return;
243 next = md->next;
244 md->next = NULL;
246 sfree(md->name);
247 sfree(md->type);
248 sfree(md->instance);
249 sfree(md);
251 tcsv_metric_definition_destroy (next);
252 }
254 static int tcsv_config_add_metric_index(metric_definition_t *md, oconfig_item_t *ci){
255 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)){
256 WARNING("tail_csv plugin: `Index' needs exactly one integer argument.");
257 return (-1);
258 }
260 md->index = (int)ci->values[0].value.number;
261 if (md->index <= 0){
262 WARNING("tail_csv plugin: `Index' must be higher than 0.");
263 return (-1);
264 }
266 return (0);
267 }
269 /* Parse metric */
270 static int tcsv_config_add_metric(oconfig_item_t *ci){
271 metric_definition_t *md;
272 int status = 0;
273 int i;
275 md = (metric_definition_t *)malloc(sizeof(*md));
276 if (md == NULL)
277 return (-1);
278 memset(md, 0, sizeof(*md));
279 md->name = NULL;
280 md->type = NULL;
281 md->instance = NULL;
282 md->data_source_type = -1;
283 md->next = NULL;
285 status = cf_util_get_string (ci, &md->name);
286 if (status != 0) {
287 sfree (md);
288 return (-1);
289 }
291 for (i = 0; i < ci->children_num; ++i){
292 oconfig_item_t *option = ci->children + i;
293 status = 0;
295 if (strcasecmp("Type", option->key) == 0)
296 status = cf_util_get_string(option, &md->type);
297 else if (strcasecmp("Instance", option->key) == 0)
298 status = cf_util_get_string(option, &md->instance);
299 else if (strcasecmp("Index", option->key) == 0)
300 status = tcsv_config_add_metric_index(md, option);
301 else {
302 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
303 status = -1;
304 }
306 if (status != 0)
307 break;
308 }
310 if (status != 0){
311 tcsv_metric_definition_destroy(md);
312 return (-1);
313 }
315 /* Verify all necessary options have been set. */
316 if (md->type == NULL) {
317 WARNING("tail_csv plugin: Option `Type' must be set.");
318 status = -1;
319 } else if (md->index == 0) {
320 WARNING("tail_csv plugin: Option `Index' must be set.");
321 status = -1;
322 }
323 if (status != 0) {
324 tcsv_metric_definition_destroy(md);
325 return (status);
326 }
328 DEBUG ("tail_csv plugin: md = { name = %s, type = %s, index = %d }",
329 md->name, md->type, md->index);
331 if (metric_head == NULL)
332 metric_head = md;
333 else {
334 metric_definition_t *last;
335 last = metric_head;
336 while (last->next != NULL)
337 last = last->next;
338 last->next = md;
339 }
341 return (0);
342 }
344 static void tcsv_instance_definition_destroy(void *arg){
345 instance_definition_t *id;
347 id = arg;
348 if (id == NULL)
349 return;
351 cu_tail_destroy (id->tail);
352 id->tail = NULL;
354 sfree(id->instance);
355 sfree(id->path);
356 sfree(id->metric_list);
357 sfree(id);
358 }
360 static int tcsv_config_add_instance_collect(instance_definition_t *id, oconfig_item_t *ci){
361 metric_definition_t *metric;
362 int i;
364 if (ci->values_num < 1){
365 WARNING("tail_csv plugin: The `Collect' config option needs at least one argument.");
366 return (-1);
367 }
369 /* Verify string arguments */
370 for (i = 0; i < ci->values_num; ++i)
371 if (ci->values[i].type != OCONFIG_TYPE_STRING){
372 WARNING("tail_csv plugin: All arguments to `Collect' must be strings.");
373 return (-1);
374 }
376 id->metric_list = (metric_definition_t **)malloc(sizeof(metric_definition_t *) * ci->values_num);
377 if (id->metric_list == NULL)
378 return (-1);
380 for (i = 0; i < ci->values_num; ++i){
381 for (metric = metric_head; metric != NULL; metric = metric->next)
382 if (strcasecmp(ci->values[i].value.string, metric->name) == 0)
383 break;
385 if (metric == NULL){
386 WARNING("tail_csv plugin: `Collect' argument not found `%s'.", ci->values[i].value.string);
387 return (-1);
388 }
390 id->metric_list[i] = metric;
391 id->metric_list_len++;
392 }
394 return (0);
395 }
397 /* <File /> block */
398 static int tcsv_config_add_file(oconfig_item_t *ci)
399 {
400 instance_definition_t* id;
401 int status = 0;
402 int i;
404 /* Registration variables */
405 char cb_name[DATA_MAX_NAME_LEN];
406 user_data_t cb_data;
407 struct timespec cb_interval;
409 id = malloc(sizeof(*id));
410 if (id == NULL)
411 return (-1);
412 memset(id, 0, sizeof(*id));
413 id->instance = NULL;
414 id->path = NULL;
415 id->metric_list = NULL;
416 id->next = NULL;
418 status = cf_util_get_string (ci, &id->path);
419 if (status != 0) {
420 sfree (id);
421 return (status);
422 }
424 /* Use default interval. */
425 id->interval = plugin_get_interval();
427 for (i = 0; i < ci->children_num; ++i){
428 oconfig_item_t *option = ci->children + i;
429 status = 0;
431 if (strcasecmp("Instance", option->key) == 0)
432 status = cf_util_get_string(option, &id->instance);
433 else if (strcasecmp("Collect", option->key) == 0)
434 status = tcsv_config_add_instance_collect(id, option);
435 else if (strcasecmp("Interval", option->key) == 0)
436 cf_util_get_cdtime(option, &id->interval);
437 else {
438 WARNING("tail_csv plugin: Option `%s' not allowed here.", option->key);
439 status = -1;
440 }
442 if (status != 0)
443 break;
444 }
446 if (status != 0){
447 tcsv_instance_definition_destroy(id);
448 return (-1);
449 }
451 /* Verify all necessary options have been set. */
452 if (id->path == NULL){
453 WARNING("tail_csv plugin: Option `Path' must be set.");
454 status = -1;
455 } else if (id->metric_list == NULL){
456 WARNING("tail_csv plugin: Option `Collect' must be set.");
457 status = -1;
458 }
460 if (status != 0){
461 tcsv_instance_definition_destroy(id);
462 return (-1);
463 }
465 ssnprintf (cb_name, sizeof (cb_name), "tail_csv/%s", id->path);
466 memset(&cb_data, 0, sizeof(cb_data));
467 cb_data.data = id;
468 cb_data.free_func = tcsv_instance_definition_destroy;
469 CDTIME_T_TO_TIMESPEC(id->interval, &cb_interval);
470 status = plugin_register_complex_read(NULL, cb_name, tcsv_read, &cb_interval, &cb_data);
472 if (status != 0){
473 ERROR("tail_csv plugin: Registering complex read function failed.");
474 tcsv_instance_definition_destroy(id);
475 return (-1);
476 }
478 return (0);
479 }
481 /* Parse blocks */
482 static int tcsv_config(oconfig_item_t *ci){
483 int i;
484 for (i = 0; i < ci->children_num; ++i){
485 oconfig_item_t *child = ci->children + i;
486 if (strcasecmp("Metric", child->key) == 0)
487 tcsv_config_add_metric(child);
488 else if (strcasecmp("File", child->key) == 0)
489 tcsv_config_add_file(child);
490 else
491 WARNING("tail_csv plugin: Ignore unknown config option `%s'.", child->key);
492 }
494 return (0);
495 } /* int tcsv_config */
497 static int tcsv_init(void) { /* {{{ */
498 static _Bool have_init = 0;
499 metric_definition_t *md;
501 if (have_init)
502 return (0);
504 for (md = metric_head; md != NULL; md = md->next) {
505 data_set_t const *ds;
507 /* Retrieve the data source type from the types db. */
508 ds = plugin_get_ds(md->type);
509 if (ds == NULL)
510 {
511 ERROR ("tail_csv plugin: Failed to look up type \"%s\" for "
512 "metric \"%s\". It may not be defined in the types.db "
513 "file. Please read the types.db(5) manual page for more "
514 "details.",
515 md->type, md->name);
516 continue;
517 }
518 else if (ds->ds_num != 1)
519 {
520 ERROR ("tail_csv plugin: The type \"%s\" has %i data sources. "
521 "Only types with a single data soure are supported.",
522 ds->type, ds->ds_num);
523 continue;
524 }
526 md->data_source_type = ds->ds->type;
527 }
529 return (0);
530 } /* }}} int tcsv_init */
532 static int tcsv_shutdown (void) {
533 tcsv_metric_definition_destroy (metric_head);
534 metric_head = NULL;
536 return (0);
537 }
539 void module_register(void){
540 plugin_register_complex_config("tail_csv", tcsv_config);
541 plugin_register_init("tail_csv", tcsv_init);
542 plugin_register_shutdown("tail_csv", tcsv_shutdown);
543 }
545 /* vim: set sw=4 sts=4 et : */