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;
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)
110 sfree (tbl->instance);
112 for (size_t i = 0; i < tbl->results_num; ++i)
113 tbl_result_clear (tbl->results + i);
114 sfree (tbl->results);
115 tbl->results_num = 0;
120 static tbl_t *tables;
121 static size_t tables_num;
124 * configuration handling
127 static int tbl_config_set_s (char *name, char **var, oconfig_item_t *ci)
129 if ((1 != ci->values_num)
130 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
131 log_err ("\"%s\" expects a single string argument.", name);
136 *var = sstrdup (ci->values[0].value.string);
138 } /* tbl_config_set_separator */
140 static int tbl_config_append_array_i (char *name, size_t **var, size_t *len,
146 if (1 > ci->values_num) {
147 log_err ("\"%s\" expects at least one argument.", name);
151 num = (size_t) ci->values_num;
152 for (size_t i = 0; i < num; ++i) {
153 if (OCONFIG_TYPE_NUMBER != ci->values[i].type) {
154 log_err ("\"%s\" expects numerical arguments only.", name);
159 tmp = realloc (*var, ((*len) + num) * sizeof (**var));
162 log_err ("realloc failed: %s.",
163 sstrerror (errno, errbuf, sizeof (errbuf)));
168 for (size_t i = 0; i < num; ++i) {
169 (*var)[*len] = (size_t) ci->values[i].value.number;
174 } /* tbl_config_append_array_s */
176 static int tbl_config_result (tbl_t *tbl, oconfig_item_t *ci)
182 if (0 != ci->values_num) {
183 log_err ("<Result> does not expect any arguments.");
187 res = realloc (tbl->results,
188 (tbl->results_num + 1) * sizeof (*tbl->results));
191 log_err ("realloc failed: %s.",
192 sstrerror (errno, errbuf, sizeof (errbuf)));
199 res = tbl->results + tbl->results_num - 1;
200 tbl_result_setup (res);
202 for (int i = 0; i < ci->children_num; ++i) {
203 oconfig_item_t *c = ci->children + i;
205 if (0 == strcasecmp (c->key, "Type"))
206 tbl_config_set_s (c->key, &res->type, c);
207 else if (0 == strcasecmp (c->key, "InstancePrefix"))
208 tbl_config_set_s (c->key, &res->instance_prefix, c);
209 else if (0 == strcasecmp (c->key, "InstancesFrom"))
210 tbl_config_append_array_i (c->key,
211 &res->instances, &res->instances_num, c);
212 else if (0 == strcasecmp (c->key, "ValuesFrom"))
213 tbl_config_append_array_i (c->key,
214 &res->values, &res->values_num, c);
216 log_warn ("Ignoring unknown config key \"%s\" "
217 " in <Result>.", c->key);
220 if (NULL == res->type) {
221 log_err ("No \"Type\" option specified for <Result> "
222 "in table \"%s\".", tbl->file);
226 if (NULL == res->values) {
227 log_err ("No \"ValuesFrom\" option specified for <Result> "
228 "in table \"%s\".", tbl->file);
233 tbl_result_clear (res);
238 } /* tbl_config_result */
240 static int tbl_config_table (oconfig_item_t *ci)
246 if ((1 != ci->values_num)
247 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
248 log_err ("<Table> expects a single string argument.");
252 tbl = realloc (tables, (tables_num + 1) * sizeof (*tables));
255 log_err ("realloc failed: %s.",
256 sstrerror (errno, errbuf, sizeof (errbuf)));
263 tbl = tables + tables_num - 1;
264 tbl_setup (tbl, ci->values[0].value.string);
266 for (size_t i = 0; i < ((size_t) ci->children_num); ++i) {
267 oconfig_item_t *c = ci->children + i;
269 if (0 == strcasecmp (c->key, "Separator"))
270 tbl_config_set_s (c->key, &tbl->sep, c);
271 else if (0 == strcasecmp (c->key, "Instance"))
272 tbl_config_set_s (c->key, &tbl->instance, c);
273 else if (0 == strcasecmp (c->key, "Result"))
274 tbl_config_result (tbl, c);
276 log_warn ("Ignoring unknown config key \"%s\" "
277 "in <Table %s>.", c->key, tbl->file);
280 if (NULL == tbl->sep) {
281 log_err ("Table \"%s\" does not specify any separator.", tbl->file);
284 strunescape (tbl->sep, strlen (tbl->sep) + 1);
287 if (NULL == tbl->instance) {
288 tbl->instance = sstrdup (tbl->file);
289 replace_special (tbl->instance, strlen (tbl->instance));
292 if (NULL == tbl->results) {
293 log_err ("Table \"%s\" does not specify any (valid) results.",
304 for (size_t i = 0; i < tbl->results_num; ++i) {
305 tbl_result_t *res = tbl->results + i;
307 for (size_t j = 0; j < res->instances_num; ++j)
308 if (res->instances[j] > tbl->max_colnum)
309 tbl->max_colnum = res->instances[j];
311 for (size_t j = 0; j < res->values_num; ++j)
312 if (res->values[j] > tbl->max_colnum)
313 tbl->max_colnum = res->values[j];
316 } /* tbl_config_table */
318 static int tbl_config (oconfig_item_t *ci)
320 for (int 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)
337 for (size_t i = 0; i < tbl->results_num; ++i) {
338 tbl_result_t *res = tbl->results + i;
340 res->ds = plugin_get_ds (res->type);
341 if (NULL == res->ds) {
342 log_err ("Unknown type \"%s\". See types.db(5) for details.",
347 if (res->values_num != res->ds->ds_num) {
348 log_err ("Invalid type \"%s\". Expected %zu data source%s, "
349 "got %zu.", res->type, res->values_num,
350 (1 == res->values_num) ? "" : "s",
358 static int tbl_finish (tbl_t *tbl)
360 for (size_t i = 0; i < tbl->results_num; ++i)
361 tbl->results[i].ds = NULL;
365 static int tbl_result_dispatch (tbl_t *tbl, tbl_result_t *res,
366 char **fields, size_t fields_num)
368 value_list_t vl = VALUE_LIST_INIT;
369 value_t values[res->values_num];
371 assert (NULL != res->ds);
372 assert (res->values_num == res->ds->ds_num);
374 for (size_t i = 0; i < res->values_num; ++i) {
377 assert (res->values[i] < fields_num);
378 value = fields[res->values[i]];
380 if (0 != parse_value (value, &values[i], res->ds->ds[i].type))
385 vl.values_len = STATIC_ARRAY_SIZE (values);
387 sstrncpy (vl.plugin, "table", sizeof (vl.plugin));
388 sstrncpy (vl.plugin_instance, tbl->instance, sizeof (vl.plugin_instance));
389 sstrncpy (vl.type, res->type, sizeof (vl.type));
391 if (0 == res->instances_num) {
392 if (NULL != res->instance_prefix)
393 sstrncpy (vl.type_instance, res->instance_prefix,
394 sizeof (vl.type_instance));
397 char *instances[res->instances_num];
398 char instances_str[DATA_MAX_NAME_LEN];
400 for (size_t i = 0; i < res->instances_num; ++i) {
401 assert (res->instances[i] < fields_num);
402 instances[i] = fields[res->instances[i]];
405 strjoin (instances_str, sizeof (instances_str),
406 instances, STATIC_ARRAY_SIZE (instances), "-");
407 instances_str[sizeof (instances_str) - 1] = '\0';
409 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
410 if (NULL == res->instance_prefix)
411 strncpy (vl.type_instance, instances_str,
412 sizeof (vl.type_instance));
414 snprintf (vl.type_instance, sizeof (vl.type_instance),
415 "%s-%s", res->instance_prefix, instances_str);
417 if ('\0' != vl.type_instance[sizeof (vl.type_instance) - 1]) {
418 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
419 log_warn ("Truncated type instance: %s.", vl.type_instance);
423 plugin_dispatch_values (&vl);
425 } /* tbl_result_dispatch */
427 static int tbl_parse_line (tbl_t *tbl, char *line, size_t len)
429 char *fields[tbl->max_colnum + 1];
436 while (NULL != (fields[i] = strtok_r (ptr, tbl->sep, &saveptr))) {
440 if (i > tbl->max_colnum)
444 if (i <= tbl->max_colnum) {
445 log_warn ("Not enough columns in line "
446 "(expected at least %zu, got %zu).",
447 tbl->max_colnum + 1, i);
451 for (i = 0; i < tbl->results_num; ++i)
452 if (0 != tbl_result_dispatch (tbl, tbl->results + i,
453 fields, STATIC_ARRAY_SIZE (fields))) {
454 log_err ("Failed to dispatch result.");
458 } /* tbl_parse_line */
460 static int tbl_read_table (tbl_t *tbl)
465 fh = fopen (tbl->file, "r");
468 log_err ("Failed to open file \"%s\": %s.", tbl->file,
469 sstrerror (errno, errbuf, sizeof (errbuf)));
473 buf[sizeof (buf) - 1] = '\0';
474 while (NULL != fgets (buf, sizeof (buf), fh)) {
475 if ('\0' != buf[sizeof (buf) - 1]) {
476 buf[sizeof (buf) - 1] = '\0';
477 log_warn ("Table %s: Truncated line: %s", tbl->file, buf);
480 if (0 != tbl_parse_line (tbl, buf, sizeof (buf))) {
481 log_warn ("Table %s: Failed to parse line: %s", tbl->file, buf);
486 if (0 != ferror (fh)) {
488 log_err ("Failed to read from file \"%s\": %s.", tbl->file,
489 sstrerror (errno, errbuf, sizeof (errbuf)));
496 } /* tbl_read_table */
502 static int tbl_read (void)
509 for (size_t i = 0; i < tables_num; ++i) {
510 tbl_t *tbl = tables + i;
512 if (0 != tbl_prepare (tbl)) {
513 log_err ("Failed to prepare and parse table \"%s\".", tbl->file);
517 if (0 == tbl_read_table (tbl))
525 static int tbl_shutdown (void)
527 for (size_t i = 0; i < tables_num; ++i)
528 tbl_clear (&tables[i]);
533 static int tbl_init (void)
538 plugin_register_read ("table", tbl_read);
539 plugin_register_shutdown ("table", tbl_shutdown);
543 void module_register (void)
545 plugin_register_complex_config ("table", tbl_config);
546 plugin_register_init ("table", tbl_init);
547 } /* module_register */
549 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */