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