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.
34 #include "configfile.h"
37 #define log_err(...) ERROR ("table plugin: " __VA_ARGS__)
38 #define log_warn(...) WARNING ("table plugin: " __VA_ARGS__)
46 char *instance_prefix;
60 tbl_result_t *results;
66 static void tbl_result_setup (tbl_result_t *res)
70 res->instance_prefix = NULL;
71 res->instances = NULL;
72 res->instances_num = 0;
78 } /* tbl_result_setup */
80 static void tbl_result_clear (tbl_result_t *res)
84 sfree (res->instance_prefix);
85 sfree (res->instances);
86 res->instances_num = 0;
92 } /* tbl_result_clear */
94 static void tbl_setup (tbl_t *tbl, char *file)
96 tbl->file = sstrdup (file);
101 tbl->results_num = 0;
106 static void tbl_clear (tbl_t *tbl)
112 sfree (tbl->instance);
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;
122 static tbl_t *tables;
123 static size_t tables_num;
126 * configuration handling
129 static int tbl_config_set_s (char *name, char **var, oconfig_item_t *ci)
131 if ((1 != ci->values_num)
132 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
133 log_err ("\"%s\" expects a single string argument.", name);
138 *var = sstrdup (ci->values[0].value.string);
140 } /* tbl_config_set_separator */
142 static int tbl_config_append_array_i (char *name, int **var, size_t *len,
149 if (1 > ci->values_num) {
150 log_err ("\"%s\" expects at least one argument.", name);
154 for (i = 0; i < ci->values_num; ++i) {
155 if (OCONFIG_TYPE_NUMBER != ci->values[i].type) {
156 log_err ("\"%s\" expects numerical arguments only.", name);
161 *len += ci->values_num;
162 tmp = (int *)realloc (*var, *len * sizeof (**var));
165 log_err ("realloc failed: %s.",
166 sstrerror (errno, errbuf, sizeof (errbuf)));
172 for (i = *len - ci->values_num; i < *len; ++i)
173 (*var)[i] = (int)ci->values[i].value.number;
175 } /* tbl_config_append_array_s */
177 static int tbl_config_result (tbl_t *tbl, oconfig_item_t *ci)
184 if (0 != ci->values_num) {
185 log_err ("<Result> does not expect any arguments.");
189 res = (tbl_result_t *)realloc (tbl->results,
190 (tbl->results_num + 1) * sizeof (*tbl->results));
193 log_err ("realloc failed: %s.",
194 sstrerror (errno, errbuf, sizeof (errbuf)));
201 res = tbl->results + tbl->results_num - 1;
202 tbl_result_setup (res);
204 for (i = 0; i < ci->children_num; ++i) {
205 oconfig_item_t *c = ci->children + i;
207 if (0 == strcasecmp (c->key, "Type"))
208 tbl_config_set_s (c->key, &res->type, c);
209 else if (0 == strcasecmp (c->key, "InstancePrefix"))
210 tbl_config_set_s (c->key, &res->instance_prefix, c);
211 else if (0 == strcasecmp (c->key, "InstancesFrom"))
212 tbl_config_append_array_i (c->key,
213 &res->instances, &res->instances_num, c);
214 else if (0 == strcasecmp (c->key, "ValuesFrom"))
215 tbl_config_append_array_i (c->key,
216 &res->values, &res->values_num, c);
218 log_warn ("Ignoring unknown config key \"%s\" "
219 " in <Result>.", c->key);
222 if (NULL == res->type) {
223 log_err ("No \"Type\" option specified for <Result> "
224 "in table \"%s\".", tbl->file);
228 if (NULL == res->values) {
229 log_err ("No \"ValuesFrom\" option specified for <Result> "
230 "in table \"%s\".", tbl->file);
235 tbl_result_clear (res);
240 } /* tbl_config_result */
242 static int tbl_config_table (oconfig_item_t *ci)
249 if ((1 != ci->values_num)
250 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
251 log_err ("<Table> expects a single string argument.");
255 tbl = (tbl_t *)realloc (tables, (tables_num + 1) * sizeof (*tables));
258 log_err ("realloc failed: %s.",
259 sstrerror (errno, errbuf, sizeof (errbuf)));
266 tbl = tables + tables_num - 1;
267 tbl_setup (tbl, ci->values[0].value.string);
269 for (i = 0; i < ci->children_num; ++i) {
270 oconfig_item_t *c = ci->children + i;
272 if (0 == strcasecmp (c->key, "Separator"))
273 tbl_config_set_s (c->key, &tbl->sep, c);
274 else if (0 == strcasecmp (c->key, "Instance"))
275 tbl_config_set_s (c->key, &tbl->instance, c);
276 else if (0 == strcasecmp (c->key, "Result"))
277 tbl_config_result (tbl, c);
279 log_warn ("Ignoring unknown config key \"%s\" "
280 "in <Table %s>.", c->key, tbl->file);
283 if (NULL == tbl->sep) {
284 log_err ("Table \"%s\" does not specify any separator.", tbl->file);
287 strunescape (tbl->sep, strlen (tbl->sep) + 1);
289 if (NULL == tbl->instance) {
290 tbl->instance = sstrdup (tbl->file);
291 replace_special (tbl->instance, strlen (tbl->instance));
294 if (NULL == tbl->results) {
295 log_err ("Table \"%s\" does not specify any (valid) results.",
306 for (i = 0; i < tbl->results_num; ++i) {
307 tbl_result_t *res = tbl->results + i;
310 for (j = 0; j < res->instances_num; ++j)
311 if (res->instances[j] > tbl->max_colnum)
312 tbl->max_colnum = res->instances[j];
314 for (j = 0; j < res->values_num; ++j)
315 if (res->values[j] > tbl->max_colnum)
316 tbl->max_colnum = res->values[j];
319 } /* tbl_config_table */
321 static int tbl_config (oconfig_item_t *ci)
325 for (i = 0; i < ci->children_num; ++i) {
326 oconfig_item_t *c = ci->children + i;
328 if (0 == strcasecmp (c->key, "Table"))
329 tbl_config_table (c);
331 log_warn ("Ignoring unknown config key \"%s\".", c->key);
340 static int tbl_prepare (tbl_t *tbl)
344 for (i = 0; i < tbl->results_num; ++i) {
345 tbl_result_t *res = tbl->results + i;
347 res->ds = plugin_get_ds (res->type);
348 if (NULL == res->ds) {
349 log_err ("Unknown type \"%s\". See types.db(5) for details.",
354 if (res->values_num != (size_t)res->ds->ds_num) {
355 log_err ("Invalid type \"%s\". Expected %zu data source%s, "
356 "got %i.", res->type, res->values_num,
357 (1 == res->values_num) ? "" : "s",
365 static int tbl_finish (tbl_t *tbl)
369 for (i = 0; i < tbl->results_num; ++i)
370 tbl->results[i].ds = NULL;
374 static int tbl_result_dispatch (tbl_t *tbl, tbl_result_t *res,
375 char **fields, size_t fields_num)
377 value_list_t vl = VALUE_LIST_INIT;
378 value_t values[res->values_num];
382 assert (NULL != res->ds);
383 assert (res->values_num == res->ds->ds_num);
385 for (i = 0; i < res->values_num; ++i) {
388 assert (res->values[i] < fields_num);
389 value = fields[res->values[i]];
391 if (0 != parse_value (value, &values[i], res->ds->ds[i].type))
396 vl.values_len = STATIC_ARRAY_SIZE (values);
398 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
399 sstrncpy (vl.plugin, "table", sizeof (vl.plugin));
400 sstrncpy (vl.plugin_instance, tbl->instance, sizeof (vl.plugin_instance));
401 sstrncpy (vl.type, res->type, sizeof (vl.type));
403 if (0 == res->instances_num) {
404 if (NULL != res->instance_prefix)
405 sstrncpy (vl.type_instance, res->instance_prefix,
406 sizeof (vl.type_instance));
409 char *instances[res->instances_num];
410 char instances_str[DATA_MAX_NAME_LEN];
412 for (i = 0; i < res->instances_num; ++i) {
413 assert (res->instances[i] < fields_num);
414 instances[i] = fields[res->instances[i]];
417 strjoin (instances_str, sizeof (instances_str),
418 instances, STATIC_ARRAY_SIZE (instances), "-");
419 instances_str[sizeof (instances_str) - 1] = '\0';
421 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
422 if (NULL == res->instance_prefix)
423 strncpy (vl.type_instance, instances_str,
424 sizeof (vl.type_instance));
426 snprintf (vl.type_instance, sizeof (vl.type_instance),
427 "%s-%s", res->instance_prefix, instances_str);
429 if ('\0' != vl.type_instance[sizeof (vl.type_instance) - 1]) {
430 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
431 log_warn ("Truncated type instance: %s.", vl.type_instance);
435 plugin_dispatch_values (&vl);
437 } /* tbl_result_dispatch */
439 static int tbl_parse_line (tbl_t *tbl, char *line, size_t len)
441 char *fields[tbl->max_colnum + 1];
449 while (NULL != (fields[i] = strtok_r (ptr, tbl->sep, &saveptr))) {
453 if (i > tbl->max_colnum)
457 if (i <= tbl->max_colnum) {
458 log_err ("Not enough columns in line "
459 "(expected at least %zu, got %zu).",
460 tbl->max_colnum + 1, i);
464 for (i = 0; i < tbl->results_num; ++i)
465 if (0 != tbl_result_dispatch (tbl, tbl->results + i,
466 fields, STATIC_ARRAY_SIZE (fields))) {
467 log_err ("Failed to dispatch result.");
471 } /* tbl_parse_line */
473 static int tbl_read_table (tbl_t *tbl)
478 fh = fopen (tbl->file, "r");
481 log_err ("Failed to open file \"%s\": %s.", tbl->file,
482 sstrerror (errno, errbuf, sizeof (errbuf)));
486 buf[sizeof (buf) - 1] = '\0';
487 while (NULL != fgets (buf, sizeof (buf), fh)) {
488 if ('\0' != buf[sizeof (buf) - 1]) {
489 buf[sizeof (buf) - 1] = '\0';
490 log_err ("Table %s: Truncated line: %s", tbl->file, buf);
493 if (0 != tbl_parse_line (tbl, buf, sizeof (buf))) {
494 log_err ("Table %s: Failed to parse line: %s", tbl->file, buf);
499 if (0 != ferror (fh)) {
501 log_err ("Failed to read from file \"%s\": %s.", tbl->file,
502 sstrerror (errno, errbuf, sizeof (errbuf)));
509 } /* tbl_read_table */
515 static int tbl_read (void)
523 for (i = 0; i < tables_num; ++i) {
524 tbl_t *tbl = tables + i;
526 if (0 != tbl_prepare (tbl)) {
527 log_err ("Failed to prepare and parse table \"%s\".", tbl->file);
531 if (0 == tbl_read_table (tbl))
539 static int tbl_shutdown (void)
543 for (i = 0; i < tables_num; ++i)
544 tbl_clear (&tables[i]);
549 static int tbl_init (void)
554 plugin_register_read ("table", tbl_read);
555 plugin_register_shutdown ("table", tbl_shutdown);
559 void module_register (void)
561 plugin_register_complex_config ("table", tbl_config);
562 plugin_register_init ("table", tbl_init);
563 } /* module_register */
565 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */