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