2 * collectd - src/table.c
3 * Copyright (C) 2009 Sebastian Harl
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Sebastian Harl <sh at tokkee.org>
23 * This module provides generic means to parse and dispatch tabular data.
29 #include "configfile.h"
32 #define log_err(...) ERROR ("table plugin: " __VA_ARGS__)
33 #define log_warn(...) WARNING ("table plugin: " __VA_ARGS__)
41 char *instance_prefix;
55 tbl_result_t *results;
61 static void tbl_result_setup (tbl_result_t *res)
65 res->instance_prefix = NULL;
66 res->instances = NULL;
67 res->instances_num = 0;
73 } /* tbl_result_setup */
75 static void tbl_result_clear (tbl_result_t *res)
79 sfree (res->instance_prefix);
80 sfree (res->instances);
81 res->instances_num = 0;
87 } /* tbl_result_clear */
89 static void tbl_setup (tbl_t *tbl, char *file)
91 tbl->file = sstrdup (file);
101 static void tbl_clear (tbl_t *tbl)
107 sfree (tbl->instance);
109 for (i = 0; i < tbl->results_num; ++i)
110 tbl_result_clear (tbl->results + i);
111 sfree (tbl->results);
112 tbl->results_num = 0;
117 static tbl_t *tables;
118 static size_t tables_num;
121 * configuration handling
124 static int tbl_config_set_s (char *name, char **var, oconfig_item_t *ci)
126 if ((1 != ci->values_num)
127 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
128 log_err ("\"%s\" expects a single string argument.", name);
133 *var = sstrdup (ci->values[0].value.string);
135 } /* tbl_config_set_separator */
137 static int tbl_config_append_array_i (char *name, int **var, size_t *len,
144 if (1 > ci->values_num) {
145 log_err ("\"%s\" expects at least one argument.", name);
149 for (i = 0; i < ci->values_num; ++i) {
150 if (OCONFIG_TYPE_NUMBER != ci->values[i].type) {
151 log_err ("\"%s\" expects numerical arguments only.", name);
156 *len += ci->values_num;
157 tmp = (int *)realloc (*var, *len * sizeof (**var));
160 log_err ("realloc failed: %s.",
161 sstrerror (errno, errbuf, sizeof (errbuf)));
167 for (i = *len - ci->values_num; i < *len; ++i)
168 (*var)[i] = (int)ci->values[i].value.number;
170 } /* tbl_config_append_array_s */
172 static int tbl_config_result (tbl_t *tbl, oconfig_item_t *ci)
179 if (0 != ci->values_num) {
180 log_err ("<Result> does not expect any arguments.");
184 res = (tbl_result_t *)realloc (tbl->results,
185 (tbl->results_num + 1) * sizeof (*tbl->results));
188 log_err ("realloc failed: %s.",
189 sstrerror (errno, errbuf, sizeof (errbuf)));
196 res = tbl->results + tbl->results_num - 1;
197 tbl_result_setup (res);
199 for (i = 0; i < ci->children_num; ++i) {
200 oconfig_item_t *c = ci->children + i;
202 if (0 == strcasecmp (c->key, "Type"))
203 tbl_config_set_s (c->key, &res->type, c);
204 else if (0 == strcasecmp (c->key, "InstancePrefix"))
205 tbl_config_set_s (c->key, &res->instance_prefix, c);
206 else if (0 == strcasecmp (c->key, "InstancesFrom"))
207 tbl_config_append_array_i (c->key,
208 &res->instances, &res->instances_num, c);
209 else if (0 == strcasecmp (c->key, "ValuesFrom"))
210 tbl_config_append_array_i (c->key,
211 &res->values, &res->values_num, c);
213 log_warn ("Ignoring unknown config key \"%s\" "
214 " in <Result>.", c->key);
217 if (NULL == res->type) {
218 log_err ("No \"Type\" option specified for <Result> "
219 "in table \"%s\".", tbl->file);
223 if (NULL == res->values) {
224 log_err ("No \"ValuesFrom\" option specified for <Result> "
225 "in table \"%s\".", tbl->file);
230 tbl_result_clear (res);
235 } /* tbl_config_result */
237 static int tbl_config_table (oconfig_item_t *ci)
244 if ((1 != ci->values_num)
245 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
246 log_err ("<Table> expects a single string argument.");
250 tbl = (tbl_t *)realloc (tables, (tables_num + 1) * sizeof (*tables));
253 log_err ("realloc failed: %s.",
254 sstrerror (errno, errbuf, sizeof (errbuf)));
261 tbl = tables + tables_num - 1;
262 tbl_setup (tbl, ci->values[0].value.string);
264 for (i = 0; i < ci->children_num; ++i) {
265 oconfig_item_t *c = ci->children + i;
267 if (0 == strcasecmp (c->key, "Separator"))
268 tbl_config_set_s (c->key, &tbl->sep, c);
269 else if (0 == strcasecmp (c->key, "Instance"))
270 tbl_config_set_s (c->key, &tbl->instance, c);
271 else if (0 == strcasecmp (c->key, "Result"))
272 tbl_config_result (tbl, c);
274 log_warn ("Ignoring unknown config key \"%s\" "
275 "in <Table %s>.", c->key, tbl->file);
278 if (NULL == tbl->sep) {
279 log_err ("Table \"%s\" does not specify any separator.", tbl->file);
282 strunescape (tbl->sep, strlen (tbl->sep) + 1);
284 if (NULL == tbl->instance) {
285 tbl->instance = sstrdup (tbl->file);
286 replace_special (tbl->instance, strlen (tbl->instance));
289 if (NULL == tbl->results) {
290 log_err ("Table \"%s\" does not specify any (valid) results.",
301 for (i = 0; i < tbl->results_num; ++i) {
302 tbl_result_t *res = tbl->results + i;
305 for (j = 0; j < res->instances_num; ++j)
306 if (res->instances[j] > tbl->max_colnum)
307 tbl->max_colnum = res->instances[j];
309 for (j = 0; j < res->values_num; ++j)
310 if (res->values[j] > tbl->max_colnum)
311 tbl->max_colnum = res->values[j];
314 } /* tbl_config_table */
316 static int tbl_config (oconfig_item_t *ci)
320 for (i = 0; i < ci->children_num; ++i) {
321 oconfig_item_t *c = ci->children + i;
323 if (0 == strcasecmp (c->key, "Table"))
324 tbl_config_table (c);
326 log_warn ("Ignoring unknown config key \"%s\".", c->key);
335 static int tbl_prepare (tbl_t *tbl)
339 for (i = 0; i < tbl->results_num; ++i) {
340 tbl_result_t *res = tbl->results + i;
342 res->ds = plugin_get_ds (res->type);
343 if (NULL == res->ds) {
344 log_err ("Unknown type \"%s\". See types.db(5) for details.",
349 if (res->values_num != (size_t)res->ds->ds_num) {
350 log_err ("Invalid type \"%s\". Expected %zu data source%s, "
351 "got %i.", res->type, res->values_num,
352 (1 == res->values_num) ? "" : "s",
360 static int tbl_finish (tbl_t *tbl)
364 for (i = 0; i < tbl->results_num; ++i)
365 tbl->results[i].ds = NULL;
369 static int tbl_result_dispatch (tbl_t *tbl, tbl_result_t *res,
370 char **fields, size_t fields_num)
372 value_list_t vl = VALUE_LIST_INIT;
373 value_t values[res->values_num];
377 assert (NULL != res->ds);
378 assert (res->values_num == res->ds->ds_num);
380 for (i = 0; i < res->values_num; ++i) {
383 assert (res->values[i] < fields_num);
384 value = fields[res->values[i]];
386 if (0 != parse_value (value, &values[i], res->ds->ds[i].type))
391 vl.values_len = STATIC_ARRAY_SIZE (values);
393 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
394 sstrncpy (vl.plugin, "table", sizeof (vl.plugin));
395 sstrncpy (vl.plugin_instance, tbl->instance, sizeof (vl.plugin_instance));
396 sstrncpy (vl.type, res->type, sizeof (vl.type));
398 if (0 == res->instances_num) {
399 if (NULL != res->instance_prefix)
400 sstrncpy (vl.type_instance, res->instance_prefix,
401 sizeof (vl.type_instance));
404 char *instances[res->instances_num];
405 char instances_str[DATA_MAX_NAME_LEN];
407 for (i = 0; i < res->instances_num; ++i) {
408 assert (res->instances[i] < fields_num);
409 instances[i] = fields[res->instances[i]];
412 strjoin (instances_str, sizeof (instances_str),
413 instances, STATIC_ARRAY_SIZE (instances), "-");
414 instances_str[sizeof (instances_str) - 1] = '\0';
416 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
417 if (NULL == res->instance_prefix)
418 strncpy (vl.type_instance, instances_str,
419 sizeof (vl.type_instance));
421 snprintf (vl.type_instance, sizeof (vl.type_instance),
422 "%s-%s", res->instance_prefix, instances_str);
424 if ('\0' != vl.type_instance[sizeof (vl.type_instance) - 1]) {
425 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
426 log_warn ("Truncated type instance: %s.", vl.type_instance);
430 plugin_dispatch_values (&vl);
432 } /* tbl_result_dispatch */
434 static int tbl_parse_line (tbl_t *tbl, char *line, size_t len)
436 char *fields[tbl->max_colnum + 1];
444 while (NULL != (fields[i] = strtok_r (ptr, tbl->sep, &saveptr))) {
448 if (i > tbl->max_colnum)
452 if (i <= tbl->max_colnum) {
453 log_err ("Not enough columns in line "
454 "(expected at least %zu, got %zu).",
455 tbl->max_colnum + 1, i);
459 for (i = 0; i < tbl->results_num; ++i)
460 if (0 != tbl_result_dispatch (tbl, tbl->results + i,
461 fields, STATIC_ARRAY_SIZE (fields))) {
462 log_err ("Failed to dispatch result.");
466 } /* tbl_parse_line */
468 static int tbl_read_table (tbl_t *tbl)
473 fh = fopen (tbl->file, "r");
476 log_err ("Failed to open file \"%s\": %s.", tbl->file,
477 sstrerror (errno, errbuf, sizeof (errbuf)));
481 buf[sizeof (buf) - 1] = '\0';
482 while (NULL != fgets (buf, sizeof (buf), fh)) {
483 if ('\0' != buf[sizeof (buf) - 1]) {
484 buf[sizeof (buf) - 1] = '\0';
485 log_err ("Table %s: Truncated line: %s", tbl->file, buf);
488 if (0 != tbl_parse_line (tbl, buf, sizeof (buf))) {
489 log_err ("Table %s: Failed to parse line: %s", tbl->file, buf);
494 if (0 != ferror (fh)) {
496 log_err ("Failed to read from file \"%s\": %s.", tbl->file,
497 sstrerror (errno, errbuf, sizeof (errbuf)));
504 } /* tbl_read_table */
510 static int tbl_read (void)
518 for (i = 0; i < tables_num; ++i) {
519 tbl_t *tbl = tables + i;
521 if (0 != tbl_prepare (tbl)) {
522 log_err ("Failed to prepare and parse table \"%s\".", tbl->file);
526 if (0 == tbl_read_table (tbl))
534 static int tbl_shutdown (void)
538 for (i = 0; i < tables_num; ++i)
539 tbl_clear (&tables[i]);
544 static int tbl_init (void)
549 plugin_register_read ("table", tbl_read);
550 plugin_register_shutdown ("table", tbl_shutdown);
554 void module_register (void)
556 plugin_register_complex_config ("table", tbl_config);
557 plugin_register_init ("table", tbl_init);
558 } /* module_register */
560 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */