Merge branch 'collectd-5.5'
[collectd.git] / src / table.c
1 /**
2  * collectd - src/table.c
3  * Copyright (C) 2009       Sebastian Harl
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Sebastian Harl <sh at tokkee.org>
25  **/
26
27 /*
28  * This module provides generic means to parse and dispatch tabular data.
29  */
30
31 #include "collectd.h"
32 #include "common.h"
33
34 #include "configfile.h"
35 #include "plugin.h"
36
37 #define log_err(...) ERROR ("table plugin: " __VA_ARGS__)
38 #define log_warn(...) WARNING ("table plugin: " __VA_ARGS__)
39
40 /*
41  * private data types
42  */
43
44 typedef struct {
45         char   *type;
46         char   *instance_prefix;
47         size_t *instances;
48         size_t  instances_num;
49         size_t *values;
50         size_t  values_num;
51
52         const data_set_t *ds;
53 } tbl_result_t;
54
55 typedef struct {
56         char *file;
57         char *sep;
58         char *instance;
59
60         tbl_result_t *results;
61         size_t        results_num;
62
63         size_t max_colnum;
64 } tbl_t;
65
66 static void tbl_result_setup (tbl_result_t *res)
67 {
68         res->type            = NULL;
69
70         res->instance_prefix = NULL;
71         res->instances       = NULL;
72         res->instances_num   = 0;
73
74         res->values          = NULL;
75         res->values_num      = 0;
76
77         res->ds              = NULL;
78 } /* tbl_result_setup */
79
80 static void tbl_result_clear (tbl_result_t *res)
81 {
82         sfree (res->type);
83
84         sfree (res->instance_prefix);
85         sfree (res->instances);
86         res->instances_num = 0;
87
88         sfree (res->values);
89         res->values_num = 0;
90
91         res->ds = NULL;
92 } /* tbl_result_clear */
93
94 static void tbl_setup (tbl_t *tbl, char *file)
95 {
96         tbl->file        = sstrdup (file);
97         tbl->sep         = NULL;
98         tbl->instance    = NULL;
99
100         tbl->results     = NULL;
101         tbl->results_num = 0;
102
103         tbl->max_colnum  = 0;
104 } /* tbl_setup */
105
106 static void tbl_clear (tbl_t *tbl)
107 {
108         size_t i;
109
110         sfree (tbl->file);
111         sfree (tbl->sep);
112         sfree (tbl->instance);
113
114         for (i = 0; i < tbl->results_num; ++i)
115                 tbl_result_clear (tbl->results + i);
116         sfree (tbl->results);
117         tbl->results_num = 0;
118
119         tbl->max_colnum  = 0;
120 } /* tbl_clear */
121
122 static tbl_t *tables;
123 static size_t tables_num;
124
125 /*
126  * configuration handling
127  */
128
129 static int tbl_config_set_s (char *name, char **var, oconfig_item_t *ci)
130 {
131         if ((1 != ci->values_num)
132                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
133                 log_err ("\"%s\" expects a single string argument.", name);
134                 return 1;
135         }
136
137         sfree (*var);
138         *var = sstrdup (ci->values[0].value.string);
139         return 0;
140 } /* tbl_config_set_separator */
141
142 static int tbl_config_append_array_i (char *name, size_t **var, size_t *len,
143                 oconfig_item_t *ci)
144 {
145         size_t *tmp;
146         size_t num;
147         size_t i;
148
149         if (1 > ci->values_num) {
150                 log_err ("\"%s\" expects at least one argument.", name);
151                 return 1;
152         }
153
154         num = (size_t) ci->values_num;
155         for (i = 0; i < num; ++i) {
156                 if (OCONFIG_TYPE_NUMBER != ci->values[i].type) {
157                         log_err ("\"%s\" expects numerical arguments only.", name);
158                         return 1;
159                 }
160         }
161
162         tmp = realloc (*var, ((*len) + num) * sizeof (**var));
163         if (NULL == tmp) {
164                 char errbuf[1024];
165                 log_err ("realloc failed: %s.",
166                                 sstrerror (errno, errbuf, sizeof (errbuf)));
167                 return -1;
168         }
169         *var = tmp;
170
171         for (i = 0; i < num; ++i) {
172                 (*var)[*len] = (size_t) ci->values[i].value.number;
173                 (*len)++;
174         }
175
176         return 0;
177 } /* tbl_config_append_array_s */
178
179 static int tbl_config_result (tbl_t *tbl, oconfig_item_t *ci)
180 {
181         tbl_result_t *res;
182
183         int status = 0;
184         int i;
185
186         if (0 != ci->values_num) {
187                 log_err ("<Result> does not expect any arguments.");
188                 return 1;
189         }
190
191         res = realloc (tbl->results,
192                         (tbl->results_num + 1) * sizeof (*tbl->results));
193         if (res == NULL) {
194                 char errbuf[1024];
195                 log_err ("realloc failed: %s.",
196                                 sstrerror (errno, errbuf, sizeof (errbuf)));
197                 return -1;
198         }
199
200         tbl->results = res;
201         ++tbl->results_num;
202
203         res = tbl->results + tbl->results_num - 1;
204         tbl_result_setup (res);
205
206         for (i = 0; i < ci->children_num; ++i) {
207                 oconfig_item_t *c = ci->children + i;
208
209                 if (0 == strcasecmp (c->key, "Type"))
210                         tbl_config_set_s (c->key, &res->type, c);
211                 else if (0 == strcasecmp (c->key, "InstancePrefix"))
212                         tbl_config_set_s (c->key, &res->instance_prefix, c);
213                 else if (0 == strcasecmp (c->key, "InstancesFrom"))
214                         tbl_config_append_array_i (c->key,
215                                         &res->instances, &res->instances_num, c);
216                 else if (0 == strcasecmp (c->key, "ValuesFrom"))
217                         tbl_config_append_array_i (c->key,
218                                         &res->values, &res->values_num, c);
219                 else
220                         log_warn ("Ignoring unknown config key \"%s\" "
221                                         " in <Result>.", c->key);
222         }
223
224         if (NULL == res->type) {
225                 log_err ("No \"Type\" option specified for <Result> "
226                                 "in table \"%s\".", tbl->file);
227                 status = 1;
228         }
229
230         if (NULL == res->values) {
231                 log_err ("No \"ValuesFrom\" option specified for <Result> "
232                                 "in table \"%s\".", tbl->file);
233                 status = 1;
234         }
235
236         if (0 != status) {
237                 tbl_result_clear (res);
238                 --tbl->results_num;
239                 return status;
240         }
241         return 0;
242 } /* tbl_config_result */
243
244 static int tbl_config_table (oconfig_item_t *ci)
245 {
246         tbl_t *tbl;
247
248         int status = 0;
249         size_t i;
250
251         if ((1 != ci->values_num)
252                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
253                 log_err ("<Table> expects a single string argument.");
254                 return 1;
255         }
256
257         tbl = realloc (tables, (tables_num + 1) * sizeof (*tables));
258         if (NULL == tbl) {
259                 char errbuf[1024];
260                 log_err ("realloc failed: %s.",
261                                 sstrerror (errno, errbuf, sizeof (errbuf)));
262                 return -1;
263         }
264
265         tables = tbl;
266         ++tables_num;
267
268         tbl = tables + tables_num - 1;
269         tbl_setup (tbl, ci->values[0].value.string);
270
271         for (i = 0; i < ((size_t) ci->children_num); ++i) {
272                 oconfig_item_t *c = ci->children + i;
273
274                 if (0 == strcasecmp (c->key, "Separator"))
275                         tbl_config_set_s (c->key, &tbl->sep, c);
276                 else if (0 == strcasecmp (c->key, "Instance"))
277                         tbl_config_set_s (c->key, &tbl->instance, c);
278                 else if (0 == strcasecmp (c->key, "Result"))
279                         tbl_config_result (tbl, c);
280                 else
281                         log_warn ("Ignoring unknown config key \"%s\" "
282                                         "in <Table %s>.", c->key, tbl->file);
283         }
284
285         if (NULL == tbl->sep) {
286                 log_err ("Table \"%s\" does not specify any separator.", tbl->file);
287                 status = 1;
288         } else {
289                 strunescape (tbl->sep, strlen (tbl->sep) + 1);
290         }
291
292         if (NULL == tbl->instance) {
293                 tbl->instance = sstrdup (tbl->file);
294                 replace_special (tbl->instance, strlen (tbl->instance));
295         }
296
297         if (NULL == tbl->results) {
298                 log_err ("Table \"%s\" does not specify any (valid) results.",
299                                 tbl->file);
300                 status = 1;
301         }
302
303         if (0 != status) {
304                 tbl_clear (tbl);
305                 --tables_num;
306                 return status;
307         }
308
309         for (i = 0; i < tbl->results_num; ++i) {
310                 tbl_result_t *res = tbl->results + i;
311                 size_t j;
312
313                 for (j = 0; j < res->instances_num; ++j)
314                         if (res->instances[j] > tbl->max_colnum)
315                                 tbl->max_colnum = res->instances[j];
316
317                 for (j = 0; j < res->values_num; ++j)
318                         if (res->values[j] > tbl->max_colnum)
319                                 tbl->max_colnum = res->values[j];
320         }
321         return 0;
322 } /* tbl_config_table */
323
324 static int tbl_config (oconfig_item_t *ci)
325 {
326         int i;
327
328         for (i = 0; i < ci->children_num; ++i) {
329                 oconfig_item_t *c = ci->children + i;
330
331                 if (0 == strcasecmp (c->key, "Table"))
332                         tbl_config_table (c);
333                 else
334                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
335         }
336         return 0;
337 } /* tbl_config */
338
339 /*
340  * result handling
341  */
342
343 static int tbl_prepare (tbl_t *tbl)
344 {
345         size_t i;
346
347         for (i = 0; i < tbl->results_num; ++i) {
348                 tbl_result_t *res = tbl->results + i;
349
350                 res->ds = plugin_get_ds (res->type);
351                 if (NULL == res->ds) {
352                         log_err ("Unknown type \"%s\". See types.db(5) for details.",
353                                         res->type);
354                         return -1;
355                 }
356
357                 if (res->values_num != res->ds->ds_num) {
358                         log_err ("Invalid type \"%s\". Expected %zu data source%s, "
359                                         "got %zu.", res->type, res->values_num,
360                                         (1 == res->values_num) ? "" : "s",
361                                         res->ds->ds_num);
362                         return -1;
363                 }
364         }
365         return 0;
366 } /* tbl_prepare */
367
368 static int tbl_finish (tbl_t *tbl)
369 {
370         size_t i;
371
372         for (i = 0; i < tbl->results_num; ++i)
373                 tbl->results[i].ds = NULL;
374         return 0;
375 } /* tbl_finish */
376
377 static int tbl_result_dispatch (tbl_t *tbl, tbl_result_t *res,
378                 char **fields, size_t fields_num)
379 {
380         value_list_t vl = VALUE_LIST_INIT;
381         value_t values[res->values_num];
382
383         size_t i;
384
385         assert (NULL != res->ds);
386         assert (res->values_num == res->ds->ds_num);
387
388         for (i = 0; i < res->values_num; ++i) {
389                 char *value;
390
391                 assert (res->values[i] < fields_num);
392                 value = fields[res->values[i]];
393
394                 if (0 != parse_value (value, &values[i], res->ds->ds[i].type))
395                         return -1;
396         }
397
398         vl.values     = values;
399         vl.values_len = STATIC_ARRAY_SIZE (values);
400
401         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
402         sstrncpy (vl.plugin, "table", sizeof (vl.plugin));
403         sstrncpy (vl.plugin_instance, tbl->instance, sizeof (vl.plugin_instance));
404         sstrncpy (vl.type, res->type, sizeof (vl.type));
405
406         if (0 == res->instances_num) {
407                 if (NULL != res->instance_prefix)
408                         sstrncpy (vl.type_instance, res->instance_prefix,
409                                         sizeof (vl.type_instance));
410         }
411         else {
412                 char *instances[res->instances_num];
413                 char  instances_str[DATA_MAX_NAME_LEN];
414
415                 for (i = 0; i < res->instances_num; ++i) {
416                         assert (res->instances[i] < fields_num);
417                         instances[i] = fields[res->instances[i]];
418                 }
419
420                 strjoin (instances_str, sizeof (instances_str),
421                                 instances, STATIC_ARRAY_SIZE (instances), "-");
422                 instances_str[sizeof (instances_str) - 1] = '\0';
423
424                 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
425                 if (NULL == res->instance_prefix)
426                         strncpy (vl.type_instance, instances_str,
427                                         sizeof (vl.type_instance));
428                 else
429                         snprintf (vl.type_instance, sizeof (vl.type_instance),
430                                         "%s-%s", res->instance_prefix, instances_str);
431
432                 if ('\0' != vl.type_instance[sizeof (vl.type_instance) - 1]) {
433                         vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
434                         log_warn ("Truncated type instance: %s.", vl.type_instance);
435                 }
436         }
437
438         plugin_dispatch_values (&vl);
439         return 0;
440 } /* tbl_result_dispatch */
441
442 static int tbl_parse_line (tbl_t *tbl, char *line, size_t len)
443 {
444         char *fields[tbl->max_colnum + 1];
445         char *ptr, *saveptr;
446
447         size_t i;
448
449         i = 0;
450         ptr = line;
451         saveptr = NULL;
452         while (NULL != (fields[i] = strtok_r (ptr, tbl->sep, &saveptr))) {
453                 ptr = NULL;
454                 ++i;
455
456                 if (i > tbl->max_colnum)
457                         break;
458         }
459
460         if (i <= tbl->max_colnum) {
461                 log_warn ("Not enough columns in line "
462                                 "(expected at least %zu, got %zu).",
463                                 tbl->max_colnum + 1, i);
464                 return -1;
465         }
466
467         for (i = 0; i < tbl->results_num; ++i)
468                 if (0 != tbl_result_dispatch (tbl, tbl->results + i,
469                                         fields, STATIC_ARRAY_SIZE (fields))) {
470                         log_err ("Failed to dispatch result.");
471                         continue;
472                 }
473         return 0;
474 } /* tbl_parse_line */
475
476 static int tbl_read_table (tbl_t *tbl)
477 {
478         FILE *fh;
479         char  buf[4096];
480
481         fh = fopen (tbl->file, "r");
482         if (NULL == fh) {
483                 char errbuf[1024];
484                 log_err ("Failed to open file \"%s\": %s.", tbl->file,
485                                 sstrerror (errno, errbuf, sizeof (errbuf)));
486                 return -1;
487         }
488
489         buf[sizeof (buf) - 1] = '\0';
490         while (NULL != fgets (buf, sizeof (buf), fh)) {
491                 if ('\0' != buf[sizeof (buf) - 1]) {
492                         buf[sizeof (buf) - 1] = '\0';
493                         log_warn ("Table %s: Truncated line: %s", tbl->file, buf);
494                 }
495
496                 if (0 != tbl_parse_line (tbl, buf, sizeof (buf))) {
497                         log_warn ("Table %s: Failed to parse line: %s", tbl->file, buf);
498                         continue;
499                 }
500         }
501
502         if (0 != ferror (fh)) {
503                 char errbuf[1024];
504                 log_err ("Failed to read from file \"%s\": %s.", tbl->file,
505                                 sstrerror (errno, errbuf, sizeof (errbuf)));
506                 fclose (fh);
507                 return -1;
508         }
509
510         fclose (fh);
511         return 0;
512 } /* tbl_read_table */
513
514 /*
515  * collectd callbacks
516  */
517
518 static int tbl_read (void)
519 {
520         int status = -1;
521         size_t i;
522
523         if (0 == tables_num)
524                 return 0;
525
526         for (i = 0; i < tables_num; ++i) {
527                 tbl_t *tbl = tables + i;
528
529                 if (0 != tbl_prepare (tbl)) {
530                         log_err ("Failed to prepare and parse table \"%s\".", tbl->file);
531                         continue;
532                 }
533
534                 if (0 == tbl_read_table (tbl))
535                         status = 0;
536
537                 tbl_finish (tbl);
538         }
539         return status;
540 } /* tbl_read */
541
542 static int tbl_shutdown (void)
543 {
544         size_t i;
545
546         for (i = 0; i < tables_num; ++i)
547                 tbl_clear (&tables[i]);
548         sfree (tables);
549         return 0;
550 } /* tbl_shutdown */
551
552 static int tbl_init (void)
553 {
554         if (0 == tables_num)
555                 return 0;
556
557         plugin_register_read ("table", tbl_read);
558         plugin_register_shutdown ("table", tbl_shutdown);
559         return 0;
560 } /* tbl_init */
561
562 void module_register (void)
563 {
564         plugin_register_complex_config ("table", tbl_config);
565         plugin_register_init ("table", tbl_init);
566 } /* module_register */
567
568 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */