2 * collectd - src/table.c
3 * Copyright (C) 2009 Sebastian Harl
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Sebastian Harl <sh at tokkee.org>
28 * This module provides generic means to parse and dispatch tabular data.
35 #include "configfile.h"
38 #define log_err(...) ERROR ("table plugin: " __VA_ARGS__)
39 #define log_warn(...) WARNING ("table plugin: " __VA_ARGS__)
47 char *instance_prefix;
61 tbl_result_t *results;
67 static void tbl_result_setup (tbl_result_t *res)
71 res->instance_prefix = NULL;
72 res->instances = NULL;
73 res->instances_num = 0;
79 } /* tbl_result_setup */
81 static void tbl_result_clear (tbl_result_t *res)
85 sfree (res->instance_prefix);
86 sfree (res->instances);
87 res->instances_num = 0;
93 } /* tbl_result_clear */
95 static void tbl_setup (tbl_t *tbl, char *file)
97 tbl->file = sstrdup (file);
102 tbl->results_num = 0;
107 static void tbl_clear (tbl_t *tbl)
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;
121 static tbl_t *tables;
122 static size_t tables_num;
125 * configuration handling
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);
137 *var = sstrdup (ci->values[0].value.string);
139 } /* tbl_config_set_separator */
141 static int tbl_config_append_array_i (char *name, size_t **var, size_t *len,
147 if (1 > ci->values_num) {
148 log_err ("\"%s\" expects at least one argument.", name);
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);
160 tmp = realloc (*var, ((*len) + num) * sizeof (**var));
163 log_err ("realloc failed: %s.",
164 sstrerror (errno, errbuf, sizeof (errbuf)));
169 for (size_t i = 0; i < num; ++i) {
170 (*var)[*len] = (size_t) ci->values[i].value.number;
175 } /* tbl_config_append_array_s */
177 static int tbl_config_result (tbl_t *tbl, oconfig_item_t *ci)
183 if (0 != ci->values_num) {
184 log_err ("<Result> does not expect any arguments.");
188 res = realloc (tbl->results,
189 (tbl->results_num + 1) * sizeof (*tbl->results));
192 log_err ("realloc failed: %s.",
193 sstrerror (errno, errbuf, sizeof (errbuf)));
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);
217 log_warn ("Ignoring unknown config key \"%s\" "
218 " in <Result>.", c->key);
221 if (NULL == res->type) {
222 log_err ("No \"Type\" option specified for <Result> "
223 "in table \"%s\".", tbl->file);
227 if (NULL == res->values) {
228 log_err ("No \"ValuesFrom\" option specified for <Result> "
229 "in table \"%s\".", tbl->file);
234 tbl_result_clear (res);
239 } /* tbl_config_result */
241 static int tbl_config_table (oconfig_item_t *ci)
247 if ((1 != ci->values_num)
248 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
249 log_err ("<Table> expects a single string argument.");
253 tbl = realloc (tables, (tables_num + 1) * sizeof (*tables));
256 log_err ("realloc failed: %s.",
257 sstrerror (errno, errbuf, sizeof (errbuf)));
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);
277 log_warn ("Ignoring unknown config key \"%s\" "
278 "in <Table %s>.", c->key, tbl->file);
281 if (NULL == tbl->sep) {
282 log_err ("Table \"%s\" does not specify any separator.", tbl->file);
285 strunescape (tbl->sep, strlen (tbl->sep) + 1);
288 if (NULL == tbl->instance) {
289 tbl->instance = sstrdup (tbl->file);
290 replace_special (tbl->instance, strlen (tbl->instance));
293 if (NULL == tbl->results) {
294 log_err ("Table \"%s\" does not specify any (valid) results.",
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];
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);
327 log_warn ("Ignoring unknown config key \"%s\".", c->key);
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.",
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",
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;
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) {
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))
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));
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]];
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));
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);
425 plugin_dispatch_values (&vl);
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];
438 while (NULL != (fields[i] = strtok_r (ptr, tbl->sep, &saveptr))) {
442 if (i > tbl->max_colnum)
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);
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.");
460 } /* tbl_parse_line */
462 static int tbl_read_table (tbl_t *tbl)
467 fh = fopen (tbl->file, "r");
470 log_err ("Failed to open file \"%s\": %s.", tbl->file,
471 sstrerror (errno, errbuf, sizeof (errbuf)));
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);
482 if (0 != tbl_parse_line (tbl, buf, sizeof (buf))) {
483 log_warn ("Table %s: Failed to parse line: %s", tbl->file, buf);
488 if (0 != ferror (fh)) {
490 log_err ("Failed to read from file \"%s\": %s.", tbl->file,
491 sstrerror (errno, errbuf, sizeof (errbuf)));
498 } /* tbl_read_table */
504 static int tbl_read (void)
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);
519 if (0 == tbl_read_table (tbl))
527 static int tbl_shutdown (void)
529 for (size_t i = 0; i < tables_num; ++i)
530 tbl_clear (&tables[i]);
535 static int tbl_init (void)
540 plugin_register_read ("table", tbl_read);
541 plugin_register_shutdown ("table", tbl_shutdown);
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 : */