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)
113 sfree (tbl->instance);
115 for (i = 0; i < tbl->results_num; ++i)
116 tbl_result_clear (tbl->results + i);
117 sfree (tbl->results);
118 tbl->results_num = 0;
123 static tbl_t *tables;
124 static size_t tables_num;
127 * configuration handling
130 static int tbl_config_set_s (char *name, char **var, oconfig_item_t *ci)
132 if ((1 != ci->values_num)
133 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
134 log_err ("\"%s\" expects a single string argument.", name);
139 *var = sstrdup (ci->values[0].value.string);
141 } /* tbl_config_set_separator */
143 static int tbl_config_append_array_i (char *name, size_t **var, size_t *len,
150 if (1 > ci->values_num) {
151 log_err ("\"%s\" expects at least one argument.", name);
155 num = (size_t) ci->values_num;
156 for (i = 0; i < num; ++i) {
157 if (OCONFIG_TYPE_NUMBER != ci->values[i].type) {
158 log_err ("\"%s\" expects numerical arguments only.", name);
163 tmp = realloc (*var, ((*len) + num) * sizeof (**var));
166 log_err ("realloc failed: %s.",
167 sstrerror (errno, errbuf, sizeof (errbuf)));
172 for (i = 0; i < num; ++i) {
173 (*var)[*len] = (size_t) ci->values[i].value.number;
178 } /* tbl_config_append_array_s */
180 static int tbl_config_result (tbl_t *tbl, oconfig_item_t *ci)
187 if (0 != ci->values_num) {
188 log_err ("<Result> does not expect any arguments.");
192 res = realloc (tbl->results,
193 (tbl->results_num + 1) * sizeof (*tbl->results));
196 log_err ("realloc failed: %s.",
197 sstrerror (errno, errbuf, sizeof (errbuf)));
204 res = tbl->results + tbl->results_num - 1;
205 tbl_result_setup (res);
207 for (i = 0; i < ci->children_num; ++i) {
208 oconfig_item_t *c = ci->children + i;
210 if (0 == strcasecmp (c->key, "Type"))
211 tbl_config_set_s (c->key, &res->type, c);
212 else if (0 == strcasecmp (c->key, "InstancePrefix"))
213 tbl_config_set_s (c->key, &res->instance_prefix, c);
214 else if (0 == strcasecmp (c->key, "InstancesFrom"))
215 tbl_config_append_array_i (c->key,
216 &res->instances, &res->instances_num, c);
217 else if (0 == strcasecmp (c->key, "ValuesFrom"))
218 tbl_config_append_array_i (c->key,
219 &res->values, &res->values_num, c);
221 log_warn ("Ignoring unknown config key \"%s\" "
222 " in <Result>.", c->key);
225 if (NULL == res->type) {
226 log_err ("No \"Type\" option specified for <Result> "
227 "in table \"%s\".", tbl->file);
231 if (NULL == res->values) {
232 log_err ("No \"ValuesFrom\" option specified for <Result> "
233 "in table \"%s\".", tbl->file);
238 tbl_result_clear (res);
243 } /* tbl_config_result */
245 static int tbl_config_table (oconfig_item_t *ci)
252 if ((1 != ci->values_num)
253 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
254 log_err ("<Table> expects a single string argument.");
258 tbl = realloc (tables, (tables_num + 1) * sizeof (*tables));
261 log_err ("realloc failed: %s.",
262 sstrerror (errno, errbuf, sizeof (errbuf)));
269 tbl = tables + tables_num - 1;
270 tbl_setup (tbl, ci->values[0].value.string);
272 for (i = 0; i < ((size_t) ci->children_num); ++i) {
273 oconfig_item_t *c = ci->children + i;
275 if (0 == strcasecmp (c->key, "Separator"))
276 tbl_config_set_s (c->key, &tbl->sep, c);
277 else if (0 == strcasecmp (c->key, "Instance"))
278 tbl_config_set_s (c->key, &tbl->instance, c);
279 else if (0 == strcasecmp (c->key, "Result"))
280 tbl_config_result (tbl, c);
282 log_warn ("Ignoring unknown config key \"%s\" "
283 "in <Table %s>.", c->key, tbl->file);
286 if (NULL == tbl->sep) {
287 log_err ("Table \"%s\" does not specify any separator.", tbl->file);
290 strunescape (tbl->sep, strlen (tbl->sep) + 1);
293 if (NULL == tbl->instance) {
294 tbl->instance = sstrdup (tbl->file);
295 replace_special (tbl->instance, strlen (tbl->instance));
298 if (NULL == tbl->results) {
299 log_err ("Table \"%s\" does not specify any (valid) results.",
310 for (i = 0; i < tbl->results_num; ++i) {
311 tbl_result_t *res = tbl->results + i;
314 for (j = 0; j < res->instances_num; ++j)
315 if (res->instances[j] > tbl->max_colnum)
316 tbl->max_colnum = res->instances[j];
318 for (j = 0; j < res->values_num; ++j)
319 if (res->values[j] > tbl->max_colnum)
320 tbl->max_colnum = res->values[j];
323 } /* tbl_config_table */
325 static int tbl_config (oconfig_item_t *ci)
329 for (i = 0; i < ci->children_num; ++i) {
330 oconfig_item_t *c = ci->children + i;
332 if (0 == strcasecmp (c->key, "Table"))
333 tbl_config_table (c);
335 log_warn ("Ignoring unknown config key \"%s\".", c->key);
344 static int tbl_prepare (tbl_t *tbl)
348 for (i = 0; i < tbl->results_num; ++i) {
349 tbl_result_t *res = tbl->results + i;
351 res->ds = plugin_get_ds (res->type);
352 if (NULL == res->ds) {
353 log_err ("Unknown type \"%s\". See types.db(5) for details.",
358 if (res->values_num != res->ds->ds_num) {
359 log_err ("Invalid type \"%s\". Expected %zu data source%s, "
360 "got %zu.", res->type, res->values_num,
361 (1 == res->values_num) ? "" : "s",
369 static int tbl_finish (tbl_t *tbl)
373 for (i = 0; i < tbl->results_num; ++i)
374 tbl->results[i].ds = NULL;
378 static int tbl_result_dispatch (tbl_t *tbl, tbl_result_t *res,
379 char **fields, size_t fields_num)
381 value_list_t vl = VALUE_LIST_INIT;
382 value_t values[res->values_num];
386 assert (NULL != res->ds);
387 assert (res->values_num == res->ds->ds_num);
389 for (i = 0; i < res->values_num; ++i) {
392 assert (res->values[i] < fields_num);
393 value = fields[res->values[i]];
395 if (0 != parse_value (value, &values[i], res->ds->ds[i].type))
400 vl.values_len = STATIC_ARRAY_SIZE (values);
402 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
403 sstrncpy (vl.plugin, "table", sizeof (vl.plugin));
404 sstrncpy (vl.plugin_instance, tbl->instance, sizeof (vl.plugin_instance));
405 sstrncpy (vl.type, res->type, sizeof (vl.type));
407 if (0 == res->instances_num) {
408 if (NULL != res->instance_prefix)
409 sstrncpy (vl.type_instance, res->instance_prefix,
410 sizeof (vl.type_instance));
413 char *instances[res->instances_num];
414 char instances_str[DATA_MAX_NAME_LEN];
416 for (i = 0; i < res->instances_num; ++i) {
417 assert (res->instances[i] < fields_num);
418 instances[i] = fields[res->instances[i]];
421 strjoin (instances_str, sizeof (instances_str),
422 instances, STATIC_ARRAY_SIZE (instances), "-");
423 instances_str[sizeof (instances_str) - 1] = '\0';
425 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
426 if (NULL == res->instance_prefix)
427 strncpy (vl.type_instance, instances_str,
428 sizeof (vl.type_instance));
430 snprintf (vl.type_instance, sizeof (vl.type_instance),
431 "%s-%s", res->instance_prefix, instances_str);
433 if ('\0' != vl.type_instance[sizeof (vl.type_instance) - 1]) {
434 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
435 log_warn ("Truncated type instance: %s.", vl.type_instance);
439 plugin_dispatch_values (&vl);
441 } /* tbl_result_dispatch */
443 static int tbl_parse_line (tbl_t *tbl, char *line, size_t len)
445 char *fields[tbl->max_colnum + 1];
453 while (NULL != (fields[i] = strtok_r (ptr, tbl->sep, &saveptr))) {
457 if (i > tbl->max_colnum)
461 if (i <= tbl->max_colnum) {
462 log_warn ("Not enough columns in line "
463 "(expected at least %zu, got %zu).",
464 tbl->max_colnum + 1, i);
468 for (i = 0; i < tbl->results_num; ++i)
469 if (0 != tbl_result_dispatch (tbl, tbl->results + i,
470 fields, STATIC_ARRAY_SIZE (fields))) {
471 log_err ("Failed to dispatch result.");
475 } /* tbl_parse_line */
477 static int tbl_read_table (tbl_t *tbl)
482 fh = fopen (tbl->file, "r");
485 log_err ("Failed to open file \"%s\": %s.", tbl->file,
486 sstrerror (errno, errbuf, sizeof (errbuf)));
490 buf[sizeof (buf) - 1] = '\0';
491 while (NULL != fgets (buf, sizeof (buf), fh)) {
492 if ('\0' != buf[sizeof (buf) - 1]) {
493 buf[sizeof (buf) - 1] = '\0';
494 log_warn ("Table %s: Truncated line: %s", tbl->file, buf);
497 if (0 != tbl_parse_line (tbl, buf, sizeof (buf))) {
498 log_warn ("Table %s: Failed to parse line: %s", tbl->file, buf);
503 if (0 != ferror (fh)) {
505 log_err ("Failed to read from file \"%s\": %s.", tbl->file,
506 sstrerror (errno, errbuf, sizeof (errbuf)));
513 } /* tbl_read_table */
519 static int tbl_read (void)
527 for (i = 0; i < tables_num; ++i) {
528 tbl_t *tbl = tables + i;
530 if (0 != tbl_prepare (tbl)) {
531 log_err ("Failed to prepare and parse table \"%s\".", tbl->file);
535 if (0 == tbl_read_table (tbl))
543 static int tbl_shutdown (void)
547 for (i = 0; i < tables_num; ++i)
548 tbl_clear (&tables[i]);
553 static int tbl_init (void)
558 plugin_register_read ("table", tbl_read);
559 plugin_register_shutdown ("table", tbl_shutdown);
563 void module_register (void)
565 plugin_register_complex_config ("table", tbl_config);
566 plugin_register_init ("table", tbl_init);
567 } /* module_register */
569 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */