1 /**
2 * collectd - src/table.c
3 * Copyright (C) 2009 Sebastian Harl
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Sebastian Harl <sh at tokkee.org>
25 **/
27 /*
28 * This module provides generic means to parse and dispatch tabular data.
29 */
31 #include "collectd.h"
33 #include "common.h"
35 #include "plugin.h"
37 #define log_err(...) ERROR ("table plugin: " __VA_ARGS__)
38 #define log_warn(...) WARNING ("table plugin: " __VA_ARGS__)
40 /*
41 * private data types
42 */
44 typedef struct {
45 char *type;
46 char *instance_prefix;
47 size_t *instances;
48 size_t instances_num;
49 size_t *values;
50 size_t values_num;
52 const data_set_t *ds;
53 } tbl_result_t;
55 typedef struct {
56 char *file;
57 char *sep;
58 char *instance;
60 tbl_result_t *results;
61 size_t results_num;
63 size_t max_colnum;
64 } tbl_t;
66 static void tbl_result_setup (tbl_result_t *res)
67 {
68 res->type = NULL;
70 res->instance_prefix = NULL;
71 res->instances = NULL;
72 res->instances_num = 0;
74 res->values = NULL;
75 res->values_num = 0;
77 res->ds = NULL;
78 } /* tbl_result_setup */
80 static void tbl_result_clear (tbl_result_t *res)
81 {
82 sfree (res->type);
84 sfree (res->instance_prefix);
85 sfree (res->instances);
86 res->instances_num = 0;
88 sfree (res->values);
89 res->values_num = 0;
91 res->ds = NULL;
92 } /* tbl_result_clear */
94 static void tbl_setup (tbl_t *tbl, char *file)
95 {
96 tbl->file = sstrdup (file);
97 tbl->sep = NULL;
98 tbl->instance = NULL;
100 tbl->results = NULL;
101 tbl->results_num = 0;
103 tbl->max_colnum = 0;
104 } /* tbl_setup */
106 static void tbl_clear (tbl_t *tbl)
107 {
108 sfree (tbl->file);
109 sfree (tbl->sep);
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;
117 tbl->max_colnum = 0;
118 } /* tbl_clear */
120 static tbl_t *tables;
121 static size_t tables_num;
123 /*
124 * configuration handling
125 */
127 static int tbl_config_set_s (char *name, char **var, oconfig_item_t *ci)
128 {
129 if ((1 != ci->values_num)
130 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
131 log_err ("\"%s\" expects a single string argument.", name);
132 return 1;
133 }
135 sfree (*var);
136 *var = sstrdup (ci->values[0].value.string);
137 return 0;
138 } /* tbl_config_set_separator */
140 static int tbl_config_append_array_i (char *name, size_t **var, size_t *len,
141 oconfig_item_t *ci)
142 {
143 size_t *tmp;
144 size_t num;
146 if (1 > ci->values_num) {
147 log_err ("\"%s\" expects at least one argument.", name);
148 return 1;
149 }
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);
155 return 1;
156 }
157 }
159 tmp = realloc (*var, ((*len) + num) * sizeof (**var));
160 if (NULL == tmp) {
161 char errbuf[1024];
162 log_err ("realloc failed: %s.",
163 sstrerror (errno, errbuf, sizeof (errbuf)));
164 return -1;
165 }
166 *var = tmp;
168 for (size_t i = 0; i < num; ++i) {
169 (*var)[*len] = (size_t) ci->values[i].value.number;
170 (*len)++;
171 }
173 return 0;
174 } /* tbl_config_append_array_s */
176 static int tbl_config_result (tbl_t *tbl, oconfig_item_t *ci)
177 {
178 tbl_result_t *res;
180 int status = 0;
182 if (0 != ci->values_num) {
183 log_err ("<Result> does not expect any arguments.");
184 return 1;
185 }
187 res = realloc (tbl->results,
188 (tbl->results_num + 1) * sizeof (*tbl->results));
189 if (res == NULL) {
190 char errbuf[1024];
191 log_err ("realloc failed: %s.",
192 sstrerror (errno, errbuf, sizeof (errbuf)));
193 return -1;
194 }
196 tbl->results = res;
197 ++tbl->results_num;
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);
215 else
216 log_warn ("Ignoring unknown config key \"%s\" "
217 " in <Result>.", c->key);
218 }
220 if (NULL == res->type) {
221 log_err ("No \"Type\" option specified for <Result> "
222 "in table \"%s\".", tbl->file);
223 status = 1;
224 }
226 if (NULL == res->values) {
227 log_err ("No \"ValuesFrom\" option specified for <Result> "
228 "in table \"%s\".", tbl->file);
229 status = 1;
230 }
232 if (0 != status) {
233 tbl_result_clear (res);
234 --tbl->results_num;
235 return status;
236 }
237 return 0;
238 } /* tbl_config_result */
240 static int tbl_config_table (oconfig_item_t *ci)
241 {
242 tbl_t *tbl;
244 int status = 0;
246 if ((1 != ci->values_num)
247 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
248 log_err ("<Table> expects a single string argument.");
249 return 1;
250 }
252 tbl = realloc (tables, (tables_num + 1) * sizeof (*tables));
253 if (NULL == tbl) {
254 char errbuf[1024];
255 log_err ("realloc failed: %s.",
256 sstrerror (errno, errbuf, sizeof (errbuf)));
257 return -1;
258 }
260 tables = tbl;
261 ++tables_num;
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);
275 else
276 log_warn ("Ignoring unknown config key \"%s\" "
277 "in <Table %s>.", c->key, tbl->file);
278 }
280 if (NULL == tbl->sep) {
281 log_err ("Table \"%s\" does not specify any separator.", tbl->file);
282 status = 1;
283 } else {
284 strunescape (tbl->sep, strlen (tbl->sep) + 1);
285 }
287 if (NULL == tbl->instance) {
288 tbl->instance = sstrdup (tbl->file);
289 replace_special (tbl->instance, strlen (tbl->instance));
290 }
292 if (NULL == tbl->results) {
293 log_err ("Table \"%s\" does not specify any (valid) results.",
294 tbl->file);
295 status = 1;
296 }
298 if (0 != status) {
299 tbl_clear (tbl);
300 --tables_num;
301 return status;
302 }
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];
314 }
315 return 0;
316 } /* tbl_config_table */
318 static int tbl_config (oconfig_item_t *ci)
319 {
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);
325 else
326 log_warn ("Ignoring unknown config key \"%s\".", c->key);
327 }
328 return 0;
329 } /* tbl_config */
331 /*
332 * result handling
333 */
335 static int tbl_prepare (tbl_t *tbl)
336 {
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.",
343 res->type);
344 return -1;
345 }
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",
351 res->ds->ds_num);
352 return -1;
353 }
354 }
355 return 0;
356 } /* tbl_prepare */
358 static int tbl_finish (tbl_t *tbl)
359 {
360 for (size_t i = 0; i < tbl->results_num; ++i)
361 tbl->results[i].ds = NULL;
362 return 0;
363 } /* tbl_finish */
365 static int tbl_result_dispatch (tbl_t *tbl, tbl_result_t *res,
366 char **fields, size_t fields_num)
367 {
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) {
375 char *value;
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))
381 return -1;
382 }
384 vl.values = values;
385 vl.values_len = STATIC_ARRAY_SIZE (values);
387 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
388 sstrncpy (vl.plugin, "table", sizeof (vl.plugin));
389 sstrncpy (vl.plugin_instance, tbl->instance, sizeof (vl.plugin_instance));
390 sstrncpy (vl.type, res->type, sizeof (vl.type));
392 if (0 == res->instances_num) {
393 if (NULL != res->instance_prefix)
394 sstrncpy (vl.type_instance, res->instance_prefix,
395 sizeof (vl.type_instance));
396 }
397 else {
398 char *instances[res->instances_num];
399 char instances_str[DATA_MAX_NAME_LEN];
401 for (size_t i = 0; i < res->instances_num; ++i) {
402 assert (res->instances[i] < fields_num);
403 instances[i] = fields[res->instances[i]];
404 }
406 strjoin (instances_str, sizeof (instances_str),
407 instances, STATIC_ARRAY_SIZE (instances), "-");
408 instances_str[sizeof (instances_str) - 1] = '\0';
410 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
411 if (NULL == res->instance_prefix)
412 strncpy (vl.type_instance, instances_str,
413 sizeof (vl.type_instance));
414 else
415 snprintf (vl.type_instance, sizeof (vl.type_instance),
416 "%s-%s", res->instance_prefix, instances_str);
418 if ('\0' != vl.type_instance[sizeof (vl.type_instance) - 1]) {
419 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
420 log_warn ("Truncated type instance: %s.", vl.type_instance);
421 }
422 }
424 plugin_dispatch_values (&vl);
425 return 0;
426 } /* tbl_result_dispatch */
428 static int tbl_parse_line (tbl_t *tbl, char *line, size_t len)
429 {
430 char *fields[tbl->max_colnum + 1];
431 char *ptr, *saveptr;
433 size_t i = 0;
435 ptr = line;
436 saveptr = NULL;
437 while (NULL != (fields[i] = strtok_r (ptr, tbl->sep, &saveptr))) {
438 ptr = NULL;
439 ++i;
441 if (i > tbl->max_colnum)
442 break;
443 }
445 if (i <= tbl->max_colnum) {
446 log_warn ("Not enough columns in line "
447 "(expected at least %zu, got %zu).",
448 tbl->max_colnum + 1, i);
449 return -1;
450 }
452 for (i = 0; i < tbl->results_num; ++i)
453 if (0 != tbl_result_dispatch (tbl, tbl->results + i,
454 fields, STATIC_ARRAY_SIZE (fields))) {
455 log_err ("Failed to dispatch result.");
456 continue;
457 }
458 return 0;
459 } /* tbl_parse_line */
461 static int tbl_read_table (tbl_t *tbl)
462 {
463 FILE *fh;
464 char buf[4096];
466 fh = fopen (tbl->file, "r");
467 if (NULL == fh) {
468 char errbuf[1024];
469 log_err ("Failed to open file \"%s\": %s.", tbl->file,
470 sstrerror (errno, errbuf, sizeof (errbuf)));
471 return -1;
472 }
474 buf[sizeof (buf) - 1] = '\0';
475 while (NULL != fgets (buf, sizeof (buf), fh)) {
476 if ('\0' != buf[sizeof (buf) - 1]) {
477 buf[sizeof (buf) - 1] = '\0';
478 log_warn ("Table %s: Truncated line: %s", tbl->file, buf);
479 }
481 if (0 != tbl_parse_line (tbl, buf, sizeof (buf))) {
482 log_warn ("Table %s: Failed to parse line: %s", tbl->file, buf);
483 continue;
484 }
485 }
487 if (0 != ferror (fh)) {
488 char errbuf[1024];
489 log_err ("Failed to read from file \"%s\": %s.", tbl->file,
490 sstrerror (errno, errbuf, sizeof (errbuf)));
491 fclose (fh);
492 return -1;
493 }
495 fclose (fh);
496 return 0;
497 } /* tbl_read_table */
499 /*
500 * collectd callbacks
501 */
503 static int tbl_read (void)
504 {
505 int status = -1;
507 if (0 == tables_num)
508 return 0;
510 for (size_t i = 0; i < tables_num; ++i) {
511 tbl_t *tbl = tables + i;
513 if (0 != tbl_prepare (tbl)) {
514 log_err ("Failed to prepare and parse table \"%s\".", tbl->file);
515 continue;
516 }
518 if (0 == tbl_read_table (tbl))
519 status = 0;
521 tbl_finish (tbl);
522 }
523 return status;
524 } /* tbl_read */
526 static int tbl_shutdown (void)
527 {
528 for (size_t i = 0; i < tables_num; ++i)
529 tbl_clear (&tables[i]);
530 sfree (tables);
531 return 0;
532 } /* tbl_shutdown */
534 static int tbl_init (void)
535 {
536 if (0 == tables_num)
537 return 0;
539 plugin_register_read ("table", tbl_read);
540 plugin_register_shutdown ("table", tbl_shutdown);
541 return 0;
542 } /* tbl_init */
544 void module_register (void)
545 {
546 plugin_register_complex_config ("table", tbl_config);
547 plugin_register_init ("table", tbl_init);
548 } /* module_register */
550 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */