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.
37 #define log_err(...) ERROR("table plugin: " __VA_ARGS__)
38 #define log_warn(...) WARNING("table plugin: " __VA_ARGS__)
46 char *instance_prefix;
61 tbl_result_t *results;
67 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) {
83 sfree(res->instance_prefix);
84 sfree(res->instances);
85 res->instances_num = 0;
91 } /* tbl_result_clear */
93 static void tbl_setup(tbl_t *tbl, char *file) {
94 tbl->file = sstrdup(file);
96 tbl->plugin_name = NULL;
100 tbl->results_num = 0;
105 static void tbl_clear(tbl_t *tbl) {
108 sfree(tbl->plugin_name);
109 sfree(tbl->instance);
111 for (size_t i = 0; i < tbl->results_num; ++i)
112 tbl_result_clear(tbl->results + i);
114 tbl->results_num = 0;
119 static tbl_t *tables;
120 static size_t tables_num;
123 * configuration handling
126 static int tbl_config_set_s(char *name, char **var, oconfig_item_t *ci) {
127 if ((1 != ci->values_num) || (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, size_t **var, size_t *len,
138 oconfig_item_t *ci) {
142 if (1 > ci->values_num) {
143 log_err("\"%s\" expects at least one argument.", name);
147 num = (size_t)ci->values_num;
148 for (size_t i = 0; i < num; ++i) {
149 if (OCONFIG_TYPE_NUMBER != ci->values[i].type) {
150 log_err("\"%s\" expects numerical arguments only.", name);
155 tmp = realloc(*var, ((*len) + num) * sizeof(**var));
158 log_err("realloc failed: %s.", sstrerror(errno, errbuf, sizeof(errbuf)));
163 for (size_t i = 0; i < num; ++i) {
164 (*var)[*len] = (size_t)ci->values[i].value.number;
169 } /* tbl_config_append_array_s */
171 static int tbl_config_result(tbl_t *tbl, oconfig_item_t *ci) {
176 if (0 != ci->values_num) {
177 log_err("<Result> does not expect any arguments.");
181 res = realloc(tbl->results, (tbl->results_num + 1) * sizeof(*tbl->results));
184 log_err("realloc failed: %s.", sstrerror(errno, errbuf, sizeof(errbuf)));
191 res = tbl->results + tbl->results_num - 1;
192 tbl_result_setup(res);
194 for (int i = 0; i < ci->children_num; ++i) {
195 oconfig_item_t *c = ci->children + i;
197 if (0 == strcasecmp(c->key, "Type"))
198 tbl_config_set_s(c->key, &res->type, c);
199 else if (0 == strcasecmp(c->key, "InstancePrefix"))
200 tbl_config_set_s(c->key, &res->instance_prefix, c);
201 else if (0 == strcasecmp(c->key, "InstancesFrom"))
202 tbl_config_append_array_i(c->key, &res->instances, &res->instances_num,
204 else if (0 == strcasecmp(c->key, "ValuesFrom"))
205 tbl_config_append_array_i(c->key, &res->values, &res->values_num, c);
207 log_warn("Ignoring unknown config key \"%s\" "
212 if (NULL == res->type) {
213 log_err("No \"Type\" option specified for <Result> "
219 if (NULL == res->values) {
220 log_err("No \"ValuesFrom\" option specified for <Result> "
227 tbl_result_clear(res);
232 } /* tbl_config_result */
234 static int tbl_config_table(oconfig_item_t *ci) {
239 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
240 log_err("<Table> expects a single string argument.");
244 tbl = realloc(tables, (tables_num + 1) * sizeof(*tables));
247 log_err("realloc failed: %s.", sstrerror(errno, errbuf, sizeof(errbuf)));
254 tbl = tables + tables_num - 1;
255 tbl_setup(tbl, ci->values[0].value.string);
257 for (size_t i = 0; i < ((size_t)ci->children_num); ++i) {
258 oconfig_item_t *c = ci->children + i;
260 if (0 == strcasecmp(c->key, "Separator"))
261 tbl_config_set_s(c->key, &tbl->sep, c);
262 else if (0 == strcasecmp(c->key, "Plugin"))
263 tbl_config_set_s(c->key, &tbl->plugin_name, c);
264 else if (0 == strcasecmp(c->key, "Instance"))
265 tbl_config_set_s(c->key, &tbl->instance, c);
266 else if (0 == strcasecmp(c->key, "Result"))
267 tbl_config_result(tbl, c);
269 log_warn("Ignoring unknown config key \"%s\" "
274 if (NULL == tbl->sep) {
275 log_err("Table \"%s\" does not specify any separator.", tbl->file);
278 strunescape(tbl->sep, strlen(tbl->sep) + 1);
281 if (NULL == tbl->instance) {
282 tbl->instance = sstrdup(tbl->file);
283 replace_special(tbl->instance, strlen(tbl->instance));
286 if (NULL == tbl->results) {
287 log_err("Table \"%s\" does not specify any (valid) results.", tbl->file);
297 for (size_t i = 0; i < tbl->results_num; ++i) {
298 tbl_result_t *res = tbl->results + i;
300 for (size_t j = 0; j < res->instances_num; ++j)
301 if (res->instances[j] > tbl->max_colnum)
302 tbl->max_colnum = res->instances[j];
304 for (size_t j = 0; j < res->values_num; ++j)
305 if (res->values[j] > tbl->max_colnum)
306 tbl->max_colnum = res->values[j];
309 } /* tbl_config_table */
311 static int tbl_config(oconfig_item_t *ci) {
312 for (int i = 0; i < ci->children_num; ++i) {
313 oconfig_item_t *c = ci->children + i;
315 if (0 == strcasecmp(c->key, "Table"))
318 log_warn("Ignoring unknown config key \"%s\".", c->key);
327 static int tbl_prepare(tbl_t *tbl) {
328 for (size_t i = 0; i < tbl->results_num; ++i) {
329 tbl_result_t *res = tbl->results + i;
331 res->ds = plugin_get_ds(res->type);
332 if (NULL == res->ds) {
333 log_err("Unknown type \"%s\". See types.db(5) for details.", res->type);
337 if (res->values_num != res->ds->ds_num) {
338 log_err("Invalid type \"%s\". Expected %zu data source%s, "
340 res->type, res->values_num, (1 == res->values_num) ? "" : "s",
348 static int tbl_finish(tbl_t *tbl) {
349 for (size_t i = 0; i < tbl->results_num; ++i)
350 tbl->results[i].ds = NULL;
354 static int tbl_result_dispatch(tbl_t *tbl, tbl_result_t *res, char **fields,
356 value_list_t vl = VALUE_LIST_INIT;
357 value_t values[res->values_num];
359 assert(NULL != res->ds);
360 assert(res->values_num == res->ds->ds_num);
362 for (size_t i = 0; i < res->values_num; ++i) {
365 assert(res->values[i] < fields_num);
366 value = fields[res->values[i]];
368 if (0 != parse_value(value, &values[i], res->ds->ds[i].type))
373 vl.values_len = STATIC_ARRAY_SIZE(values);
375 sstrncpy(vl.plugin, (tbl->plugin_name != NULL) ? tbl->plugin_name : "table",
377 sstrncpy(vl.plugin_instance, tbl->instance, sizeof(vl.plugin_instance));
378 sstrncpy(vl.type, res->type, sizeof(vl.type));
380 if (0 == res->instances_num) {
381 if (NULL != res->instance_prefix)
382 sstrncpy(vl.type_instance, res->instance_prefix,
383 sizeof(vl.type_instance));
385 char *instances[res->instances_num];
386 char instances_str[DATA_MAX_NAME_LEN];
388 for (size_t i = 0; i < res->instances_num; ++i) {
389 assert(res->instances[i] < fields_num);
390 instances[i] = fields[res->instances[i]];
393 strjoin(instances_str, sizeof(instances_str), instances,
394 STATIC_ARRAY_SIZE(instances), "-");
395 instances_str[sizeof(instances_str) - 1] = '\0';
397 vl.type_instance[sizeof(vl.type_instance) - 1] = '\0';
398 if (NULL == res->instance_prefix)
399 strncpy(vl.type_instance, instances_str, sizeof(vl.type_instance));
401 snprintf(vl.type_instance, sizeof(vl.type_instance), "%s-%s",
402 res->instance_prefix, instances_str);
404 if ('\0' != vl.type_instance[sizeof(vl.type_instance) - 1]) {
405 vl.type_instance[sizeof(vl.type_instance) - 1] = '\0';
406 log_warn("Truncated type instance: %s.", vl.type_instance);
410 plugin_dispatch_values(&vl);
412 } /* tbl_result_dispatch */
414 static int tbl_parse_line(tbl_t *tbl, char *line, size_t len) {
415 char *fields[tbl->max_colnum + 1];
422 while (NULL != (fields[i] = strtok_r(ptr, tbl->sep, &saveptr))) {
426 if (i > tbl->max_colnum)
430 if (i <= tbl->max_colnum) {
431 log_warn("Not enough columns in line "
432 "(expected at least %zu, got %zu).",
433 tbl->max_colnum + 1, i);
437 for (i = 0; i < tbl->results_num; ++i)
438 if (0 != tbl_result_dispatch(tbl, tbl->results + i, fields,
439 STATIC_ARRAY_SIZE(fields))) {
440 log_err("Failed to dispatch result.");
444 } /* tbl_parse_line */
446 static int tbl_read_table(tbl_t *tbl) {
450 fh = fopen(tbl->file, "r");
453 log_err("Failed to open file \"%s\": %s.", tbl->file,
454 sstrerror(errno, errbuf, sizeof(errbuf)));
458 buf[sizeof(buf) - 1] = '\0';
459 while (NULL != fgets(buf, sizeof(buf), fh)) {
460 if ('\0' != buf[sizeof(buf) - 1]) {
461 buf[sizeof(buf) - 1] = '\0';
462 log_warn("Table %s: Truncated line: %s", tbl->file, buf);
465 if (0 != tbl_parse_line(tbl, buf, sizeof(buf))) {
466 log_warn("Table %s: Failed to parse line: %s", tbl->file, buf);
471 if (0 != ferror(fh)) {
473 log_err("Failed to read from file \"%s\": %s.", tbl->file,
474 sstrerror(errno, errbuf, sizeof(errbuf)));
481 } /* tbl_read_table */
487 static int tbl_read(void) {
493 for (size_t i = 0; i < tables_num; ++i) {
494 tbl_t *tbl = tables + i;
496 if (0 != tbl_prepare(tbl)) {
497 log_err("Failed to prepare and parse table \"%s\".", tbl->file);
501 if (0 == tbl_read_table(tbl))
509 static int tbl_shutdown(void) {
510 for (size_t i = 0; i < tables_num; ++i)
511 tbl_clear(&tables[i]);
516 static int tbl_init(void) {
520 plugin_register_read("table", tbl_read);
521 plugin_register_shutdown("table", tbl_shutdown);
525 void module_register(void) {
526 plugin_register_complex_config("table", tbl_config);
527 plugin_register_init("table", tbl_init);
528 } /* module_register */