Code

treewide: declare loop variable in loop expression
[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  **/
27 /*
28  * This module provides generic means to parse and dispatch tabular data.
29  */
31 #include "collectd.h"
33 #include "common.h"
35 #include "configfile.h"
36 #include "plugin.h"
38 #define log_err(...) ERROR ("table plugin: " __VA_ARGS__)
39 #define log_warn(...) WARNING ("table plugin: " __VA_ARGS__)
41 /*
42  * private data types
43  */
45 typedef struct {
46         char   *type;
47         char   *instance_prefix;
48         size_t *instances;
49         size_t  instances_num;
50         size_t *values;
51         size_t  values_num;
53         const data_set_t *ds;
54 } tbl_result_t;
56 typedef struct {
57         char *file;
58         char *sep;
59         char *instance;
61         tbl_result_t *results;
62         size_t        results_num;
64         size_t max_colnum;
65 } tbl_t;
67 static void tbl_result_setup (tbl_result_t *res)
68 {
69         res->type            = NULL;
71         res->instance_prefix = NULL;
72         res->instances       = NULL;
73         res->instances_num   = 0;
75         res->values          = NULL;
76         res->values_num      = 0;
78         res->ds              = NULL;
79 } /* tbl_result_setup */
81 static void tbl_result_clear (tbl_result_t *res)
82 {
83         sfree (res->type);
85         sfree (res->instance_prefix);
86         sfree (res->instances);
87         res->instances_num = 0;
89         sfree (res->values);
90         res->values_num = 0;
92         res->ds = NULL;
93 } /* tbl_result_clear */
95 static void tbl_setup (tbl_t *tbl, char *file)
96 {
97         tbl->file        = sstrdup (file);
98         tbl->sep         = NULL;
99         tbl->instance    = NULL;
101         tbl->results     = NULL;
102         tbl->results_num = 0;
104         tbl->max_colnum  = 0;
105 } /* tbl_setup */
107 static void tbl_clear (tbl_t *tbl)
109         sfree (tbl->file);
110         sfree (tbl->sep);
111         sfree (tbl->instance);
113         for (size_t i = 0; i < tbl->results_num; ++i)
114                 tbl_result_clear (tbl->results + i);
115         sfree (tbl->results);
116         tbl->results_num = 0;
118         tbl->max_colnum  = 0;
119 } /* tbl_clear */
121 static tbl_t *tables;
122 static size_t tables_num;
124 /*
125  * configuration handling
126  */
128 static int tbl_config_set_s (char *name, char **var, oconfig_item_t *ci)
130         if ((1 != ci->values_num)
131                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
132                 log_err ("\"%s\" expects a single string argument.", name);
133                 return 1;
134         }
136         sfree (*var);
137         *var = sstrdup (ci->values[0].value.string);
138         return 0;
139 } /* tbl_config_set_separator */
141 static int tbl_config_append_array_i (char *name, size_t **var, size_t *len,
142                 oconfig_item_t *ci)
144         size_t *tmp;
145         size_t num;
147         if (1 > ci->values_num) {
148                 log_err ("\"%s\" expects at least one argument.", name);
149                 return 1;
150         }
152         num = (size_t) ci->values_num;
153         for (size_t i = 0; i < num; ++i) {
154                 if (OCONFIG_TYPE_NUMBER != ci->values[i].type) {
155                         log_err ("\"%s\" expects numerical arguments only.", name);
156                         return 1;
157                 }
158         }
160         tmp = realloc (*var, ((*len) + num) * sizeof (**var));
161         if (NULL == tmp) {
162                 char errbuf[1024];
163                 log_err ("realloc failed: %s.",
164                                 sstrerror (errno, errbuf, sizeof (errbuf)));
165                 return -1;
166         }
167         *var = tmp;
169         for (size_t i = 0; i < num; ++i) {
170                 (*var)[*len] = (size_t) ci->values[i].value.number;
171                 (*len)++;
172         }
174         return 0;
175 } /* tbl_config_append_array_s */
177 static int tbl_config_result (tbl_t *tbl, oconfig_item_t *ci)
179         tbl_result_t *res;
181         int status = 0;
183         if (0 != ci->values_num) {
184                 log_err ("<Result> does not expect any arguments.");
185                 return 1;
186         }
188         res = realloc (tbl->results,
189                         (tbl->results_num + 1) * sizeof (*tbl->results));
190         if (res == NULL) {
191                 char errbuf[1024];
192                 log_err ("realloc failed: %s.",
193                                 sstrerror (errno, errbuf, sizeof (errbuf)));
194                 return -1;
195         }
197         tbl->results = res;
198         ++tbl->results_num;
200         res = tbl->results + tbl->results_num - 1;
201         tbl_result_setup (res);
203         for (int i = 0; i < ci->children_num; ++i) {
204                 oconfig_item_t *c = ci->children + i;
206                 if (0 == strcasecmp (c->key, "Type"))
207                         tbl_config_set_s (c->key, &res->type, c);
208                 else if (0 == strcasecmp (c->key, "InstancePrefix"))
209                         tbl_config_set_s (c->key, &res->instance_prefix, c);
210                 else if (0 == strcasecmp (c->key, "InstancesFrom"))
211                         tbl_config_append_array_i (c->key,
212                                         &res->instances, &res->instances_num, c);
213                 else if (0 == strcasecmp (c->key, "ValuesFrom"))
214                         tbl_config_append_array_i (c->key,
215                                         &res->values, &res->values_num, c);
216                 else
217                         log_warn ("Ignoring unknown config key \"%s\" "
218                                         " in <Result>.", c->key);
219         }
221         if (NULL == res->type) {
222                 log_err ("No \"Type\" option specified for <Result> "
223                                 "in table \"%s\".", tbl->file);
224                 status = 1;
225         }
227         if (NULL == res->values) {
228                 log_err ("No \"ValuesFrom\" option specified for <Result> "
229                                 "in table \"%s\".", tbl->file);
230                 status = 1;
231         }
233         if (0 != status) {
234                 tbl_result_clear (res);
235                 --tbl->results_num;
236                 return status;
237         }
238         return 0;
239 } /* tbl_config_result */
241 static int tbl_config_table (oconfig_item_t *ci)
243         tbl_t *tbl;
245         int status = 0;
247         if ((1 != ci->values_num)
248                         || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
249                 log_err ("<Table> expects a single string argument.");
250                 return 1;
251         }
253         tbl = realloc (tables, (tables_num + 1) * sizeof (*tables));
254         if (NULL == tbl) {
255                 char errbuf[1024];
256                 log_err ("realloc failed: %s.",
257                                 sstrerror (errno, errbuf, sizeof (errbuf)));
258                 return -1;
259         }
261         tables = tbl;
262         ++tables_num;
264         tbl = tables + tables_num - 1;
265         tbl_setup (tbl, ci->values[0].value.string);
267         for (size_t i = 0; i < ((size_t) ci->children_num); ++i) {
268                 oconfig_item_t *c = ci->children + i;
270                 if (0 == strcasecmp (c->key, "Separator"))
271                         tbl_config_set_s (c->key, &tbl->sep, c);
272                 else if (0 == strcasecmp (c->key, "Instance"))
273                         tbl_config_set_s (c->key, &tbl->instance, c);
274                 else if (0 == strcasecmp (c->key, "Result"))
275                         tbl_config_result (tbl, c);
276                 else
277                         log_warn ("Ignoring unknown config key \"%s\" "
278                                         "in <Table %s>.", c->key, tbl->file);
279         }
281         if (NULL == tbl->sep) {
282                 log_err ("Table \"%s\" does not specify any separator.", tbl->file);
283                 status = 1;
284         } else {
285                 strunescape (tbl->sep, strlen (tbl->sep) + 1);
286         }
288         if (NULL == tbl->instance) {
289                 tbl->instance = sstrdup (tbl->file);
290                 replace_special (tbl->instance, strlen (tbl->instance));
291         }
293         if (NULL == tbl->results) {
294                 log_err ("Table \"%s\" does not specify any (valid) results.",
295                                 tbl->file);
296                 status = 1;
297         }
299         if (0 != status) {
300                 tbl_clear (tbl);
301                 --tables_num;
302                 return status;
303         }
305         for (size_t i = 0; i < tbl->results_num; ++i) {
306                 tbl_result_t *res = tbl->results + i;
308                 for (size_t j = 0; j < res->instances_num; ++j)
309                         if (res->instances[j] > tbl->max_colnum)
310                                 tbl->max_colnum = res->instances[j];
312                 for (size_t j = 0; j < res->values_num; ++j)
313                         if (res->values[j] > tbl->max_colnum)
314                                 tbl->max_colnum = res->values[j];
315         }
316         return 0;
317 } /* tbl_config_table */
319 static int tbl_config (oconfig_item_t *ci)
321         for (int i = 0; i < ci->children_num; ++i) {
322                 oconfig_item_t *c = ci->children + i;
324                 if (0 == strcasecmp (c->key, "Table"))
325                         tbl_config_table (c);
326                 else
327                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
328         }
329         return 0;
330 } /* tbl_config */
332 /*
333  * result handling
334  */
336 static int tbl_prepare (tbl_t *tbl)
338         for (size_t i = 0; i < tbl->results_num; ++i) {
339                 tbl_result_t *res = tbl->results + i;
341                 res->ds = plugin_get_ds (res->type);
342                 if (NULL == res->ds) {
343                         log_err ("Unknown type \"%s\". See types.db(5) for details.",
344                                         res->type);
345                         return -1;
346                 }
348                 if (res->values_num != res->ds->ds_num) {
349                         log_err ("Invalid type \"%s\". Expected %zu data source%s, "
350                                         "got %zu.", res->type, res->values_num,
351                                         (1 == res->values_num) ? "" : "s",
352                                         res->ds->ds_num);
353                         return -1;
354                 }
355         }
356         return 0;
357 } /* tbl_prepare */
359 static int tbl_finish (tbl_t *tbl)
361         for (size_t i = 0; i < tbl->results_num; ++i)
362                 tbl->results[i].ds = NULL;
363         return 0;
364 } /* tbl_finish */
366 static int tbl_result_dispatch (tbl_t *tbl, tbl_result_t *res,
367                 char **fields, size_t fields_num)
369         value_list_t vl = VALUE_LIST_INIT;
370         value_t values[res->values_num];
372         assert (NULL != res->ds);
373         assert (res->values_num == res->ds->ds_num);
375         for (size_t i = 0; i < res->values_num; ++i) {
376                 char *value;
378                 assert (res->values[i] < fields_num);
379                 value = fields[res->values[i]];
381                 if (0 != parse_value (value, &values[i], res->ds->ds[i].type))
382                         return -1;
383         }
385         vl.values     = values;
386         vl.values_len = STATIC_ARRAY_SIZE (values);
388         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
389         sstrncpy (vl.plugin, "table", sizeof (vl.plugin));
390         sstrncpy (vl.plugin_instance, tbl->instance, sizeof (vl.plugin_instance));
391         sstrncpy (vl.type, res->type, sizeof (vl.type));
393         if (0 == res->instances_num) {
394                 if (NULL != res->instance_prefix)
395                         sstrncpy (vl.type_instance, res->instance_prefix,
396                                         sizeof (vl.type_instance));
397         }
398         else {
399                 char *instances[res->instances_num];
400                 char  instances_str[DATA_MAX_NAME_LEN];
402                 for (size_t i = 0; i < res->instances_num; ++i) {
403                         assert (res->instances[i] < fields_num);
404                         instances[i] = fields[res->instances[i]];
405                 }
407                 strjoin (instances_str, sizeof (instances_str),
408                                 instances, STATIC_ARRAY_SIZE (instances), "-");
409                 instances_str[sizeof (instances_str) - 1] = '\0';
411                 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
412                 if (NULL == res->instance_prefix)
413                         strncpy (vl.type_instance, instances_str,
414                                         sizeof (vl.type_instance));
415                 else
416                         snprintf (vl.type_instance, sizeof (vl.type_instance),
417                                         "%s-%s", res->instance_prefix, instances_str);
419                 if ('\0' != vl.type_instance[sizeof (vl.type_instance) - 1]) {
420                         vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
421                         log_warn ("Truncated type instance: %s.", vl.type_instance);
422                 }
423         }
425         plugin_dispatch_values (&vl);
426         return 0;
427 } /* tbl_result_dispatch */
429 static int tbl_parse_line (tbl_t *tbl, char *line, size_t len)
431         char *fields[tbl->max_colnum + 1];
432         char *ptr, *saveptr;
434         size_t i = 0;
436         ptr = line;
437         saveptr = NULL;
438         while (NULL != (fields[i] = strtok_r (ptr, tbl->sep, &saveptr))) {
439                 ptr = NULL;
440                 ++i;
442                 if (i > tbl->max_colnum)
443                         break;
444         }
446         if (i <= tbl->max_colnum) {
447                 log_warn ("Not enough columns in line "
448                                 "(expected at least %zu, got %zu).",
449                                 tbl->max_colnum + 1, i);
450                 return -1;
451         }
453         for (i = 0; i < tbl->results_num; ++i)
454                 if (0 != tbl_result_dispatch (tbl, tbl->results + i,
455                                         fields, STATIC_ARRAY_SIZE (fields))) {
456                         log_err ("Failed to dispatch result.");
457                         continue;
458                 }
459         return 0;
460 } /* tbl_parse_line */
462 static int tbl_read_table (tbl_t *tbl)
464         FILE *fh;
465         char  buf[4096];
467         fh = fopen (tbl->file, "r");
468         if (NULL == fh) {
469                 char errbuf[1024];
470                 log_err ("Failed to open file \"%s\": %s.", tbl->file,
471                                 sstrerror (errno, errbuf, sizeof (errbuf)));
472                 return -1;
473         }
475         buf[sizeof (buf) - 1] = '\0';
476         while (NULL != fgets (buf, sizeof (buf), fh)) {
477                 if ('\0' != buf[sizeof (buf) - 1]) {
478                         buf[sizeof (buf) - 1] = '\0';
479                         log_warn ("Table %s: Truncated line: %s", tbl->file, buf);
480                 }
482                 if (0 != tbl_parse_line (tbl, buf, sizeof (buf))) {
483                         log_warn ("Table %s: Failed to parse line: %s", tbl->file, buf);
484                         continue;
485                 }
486         }
488         if (0 != ferror (fh)) {
489                 char errbuf[1024];
490                 log_err ("Failed to read from file \"%s\": %s.", tbl->file,
491                                 sstrerror (errno, errbuf, sizeof (errbuf)));
492                 fclose (fh);
493                 return -1;
494         }
496         fclose (fh);
497         return 0;
498 } /* tbl_read_table */
500 /*
501  * collectd callbacks
502  */
504 static int tbl_read (void)
506         int status = -1;
508         if (0 == tables_num)
509                 return 0;
511         for (size_t i = 0; i < tables_num; ++i) {
512                 tbl_t *tbl = tables + i;
514                 if (0 != tbl_prepare (tbl)) {
515                         log_err ("Failed to prepare and parse table \"%s\".", tbl->file);
516                         continue;
517                 }
519                 if (0 == tbl_read_table (tbl))
520                         status = 0;
522                 tbl_finish (tbl);
523         }
524         return status;
525 } /* tbl_read */
527 static int tbl_shutdown (void)
529         for (size_t i = 0; i < tables_num; ++i)
530                 tbl_clear (&tables[i]);
531         sfree (tables);
532         return 0;
533 } /* tbl_shutdown */
535 static int tbl_init (void)
537         if (0 == tables_num)
538                 return 0;
540         plugin_register_read ("table", tbl_read);
541         plugin_register_shutdown ("table", tbl_shutdown);
542         return 0;
543 } /* tbl_init */
545 void module_register (void)
547         plugin_register_complex_config ("table", tbl_config);
548         plugin_register_init ("table", tbl_init);
549 } /* module_register */
551 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */