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);
290 if (NULL == tbl->instance) {
291 tbl->instance = sstrdup (tbl->file);
292 replace_special (tbl->instance, strlen (tbl->instance));
295 if (NULL == tbl->results) {
296 log_err ("Table \"%s\" does not specify any (valid) results.",
307 for (i = 0; i < tbl->results_num; ++i) {
308 tbl_result_t *res = tbl->results + i;
311 for (j = 0; j < res->instances_num; ++j)
312 if (res->instances[j] > tbl->max_colnum)
313 tbl->max_colnum = res->instances[j];
315 for (j = 0; j < res->values_num; ++j)
316 if (res->values[j] > tbl->max_colnum)
317 tbl->max_colnum = res->values[j];
320 } /* tbl_config_table */
322 static int tbl_config (oconfig_item_t *ci)
326 for (i = 0; i < ci->children_num; ++i) {
327 oconfig_item_t *c = ci->children + i;
329 if (0 == strcasecmp (c->key, "Table"))
330 tbl_config_table (c);
332 log_warn ("Ignoring unknown config key \"%s\".", c->key);
341 static int tbl_prepare (tbl_t *tbl)
345 for (i = 0; i < tbl->results_num; ++i) {
346 tbl_result_t *res = tbl->results + i;
348 res->ds = plugin_get_ds (res->type);
349 if (NULL == res->ds) {
350 log_err ("Unknown type \"%s\". See types.db(5) for details.",
355 if (res->values_num != (size_t)res->ds->ds_num) {
356 log_err ("Invalid type \"%s\". Expected %zu data source%s, "
357 "got %i.", res->type, res->values_num,
358 (1 == res->values_num) ? "" : "s",
366 static int tbl_finish (tbl_t *tbl)
370 for (i = 0; i < tbl->results_num; ++i)
371 tbl->results[i].ds = NULL;
375 static int tbl_result_dispatch (tbl_t *tbl, tbl_result_t *res,
376 char **fields, size_t fields_num)
378 value_list_t vl = VALUE_LIST_INIT;
379 value_t values[res->values_num];
383 assert (NULL != res->ds);
384 assert (res->values_num == res->ds->ds_num);
386 for (i = 0; i < res->values_num; ++i) {
389 assert (res->values[i] < fields_num);
390 value = fields[res->values[i]];
392 if (0 != parse_value (value, &values[i], res->ds->ds[i].type))
397 vl.values_len = STATIC_ARRAY_SIZE (values);
399 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
400 sstrncpy (vl.plugin, "table", sizeof (vl.plugin));
401 sstrncpy (vl.plugin_instance, tbl->instance, sizeof (vl.plugin_instance));
402 sstrncpy (vl.type, res->type, sizeof (vl.type));
404 if (0 == res->instances_num) {
405 if (NULL != res->instance_prefix)
406 sstrncpy (vl.type_instance, res->instance_prefix,
407 sizeof (vl.type_instance));
410 char *instances[res->instances_num];
411 char instances_str[DATA_MAX_NAME_LEN];
413 for (i = 0; i < res->instances_num; ++i) {
414 assert (res->instances[i] < fields_num);
415 instances[i] = fields[res->instances[i]];
418 strjoin (instances_str, sizeof (instances_str),
419 instances, STATIC_ARRAY_SIZE (instances), "-");
420 instances_str[sizeof (instances_str) - 1] = '\0';
422 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
423 if (NULL == res->instance_prefix)
424 strncpy (vl.type_instance, instances_str,
425 sizeof (vl.type_instance));
427 snprintf (vl.type_instance, sizeof (vl.type_instance),
428 "%s-%s", res->instance_prefix, instances_str);
430 if ('\0' != vl.type_instance[sizeof (vl.type_instance) - 1]) {
431 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
432 log_warn ("Truncated type instance: %s.", vl.type_instance);
436 plugin_dispatch_values (&vl);
438 } /* tbl_result_dispatch */
440 static int tbl_parse_line (tbl_t *tbl, char *line, size_t len)
442 char *fields[tbl->max_colnum + 1];
450 while (NULL != (fields[i] = strtok_r (ptr, tbl->sep, &saveptr))) {
454 if (i > tbl->max_colnum)
458 if (i <= tbl->max_colnum) {
459 log_err ("Not enough columns in line "
460 "(expected at least %zu, got %zu).",
461 tbl->max_colnum + 1, i);
465 for (i = 0; i < tbl->results_num; ++i)
466 if (0 != tbl_result_dispatch (tbl, tbl->results + i,
467 fields, STATIC_ARRAY_SIZE (fields))) {
468 log_err ("Failed to dispatch result.");
472 } /* tbl_parse_line */
474 static int tbl_read_table (tbl_t *tbl)
479 fh = fopen (tbl->file, "r");
482 log_err ("Failed to open file \"%s\": %s.", tbl->file,
483 sstrerror (errno, errbuf, sizeof (errbuf)));
487 buf[sizeof (buf) - 1] = '\0';
488 while (NULL != fgets (buf, sizeof (buf), fh)) {
489 if ('\0' != buf[sizeof (buf) - 1]) {
490 buf[sizeof (buf) - 1] = '\0';
491 log_err ("Table %s: Truncated line: %s", tbl->file, buf);
494 if (0 != tbl_parse_line (tbl, buf, sizeof (buf))) {
495 log_err ("Table %s: Failed to parse line: %s", tbl->file, buf);
500 if (0 != ferror (fh)) {
502 log_err ("Failed to read from file \"%s\": %s.", tbl->file,
503 sstrerror (errno, errbuf, sizeof (errbuf)));
510 } /* tbl_read_table */
516 static int tbl_read (void)
524 for (i = 0; i < tables_num; ++i) {
525 tbl_t *tbl = tables + i;
527 if (0 != tbl_prepare (tbl)) {
528 log_err ("Failed to prepare and parse table \"%s\".", tbl->file);
532 if (0 == tbl_read_table (tbl))
540 static int tbl_shutdown (void)
544 for (i = 0; i < tables_num; ++i)
545 tbl_clear (&tables[i]);
550 static int tbl_init (void)
555 plugin_register_read ("table", tbl_read);
556 plugin_register_shutdown ("table", tbl_shutdown);
560 void module_register (void)
562 plugin_register_complex_config ("table", tbl_config);
563 plugin_register_init ("table", tbl_init);
564 } /* module_register */
566 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */