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 "configfile.h"
36 #include "plugin.h"
38 #define log_err(...) ERROR ("table plugin: " __VA_ARGS__)
39 #define log_warn(...) WARNING ("table plugin: " __VA_ARGS__)
41 /*
42 * private data types
43 */
45 typedef struct {
46 char *type;
47 char *instance_prefix;
48 size_t *instances;
49 size_t instances_num;
50 size_t *values;
51 size_t values_num;
53 const data_set_t *ds;
54 } tbl_result_t;
56 typedef struct {
57 char *file;
58 char *sep;
59 char *instance;
61 tbl_result_t *results;
62 size_t results_num;
64 size_t max_colnum;
65 } tbl_t;
67 static void tbl_result_setup (tbl_result_t *res)
68 {
69 res->type = NULL;
71 res->instance_prefix = NULL;
72 res->instances = NULL;
73 res->instances_num = 0;
75 res->values = NULL;
76 res->values_num = 0;
78 res->ds = NULL;
79 } /* tbl_result_setup */
81 static void tbl_result_clear (tbl_result_t *res)
82 {
83 sfree (res->type);
85 sfree (res->instance_prefix);
86 sfree (res->instances);
87 res->instances_num = 0;
89 sfree (res->values);
90 res->values_num = 0;
92 res->ds = NULL;
93 } /* tbl_result_clear */
95 static void tbl_setup (tbl_t *tbl, char *file)
96 {
97 tbl->file = sstrdup (file);
98 tbl->sep = NULL;
99 tbl->instance = NULL;
101 tbl->results = NULL;
102 tbl->results_num = 0;
104 tbl->max_colnum = 0;
105 } /* tbl_setup */
107 static void tbl_clear (tbl_t *tbl)
108 {
109 size_t i;
111 sfree (tbl->file);
112 sfree (tbl->sep);
113 sfree (tbl->instance);
115 for (i = 0; i < tbl->results_num; ++i)
116 tbl_result_clear (tbl->results + i);
117 sfree (tbl->results);
118 tbl->results_num = 0;
120 tbl->max_colnum = 0;
121 } /* tbl_clear */
123 static tbl_t *tables;
124 static size_t tables_num;
126 /*
127 * configuration handling
128 */
130 static int tbl_config_set_s (char *name, char **var, oconfig_item_t *ci)
131 {
132 if ((1 != ci->values_num)
133 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
134 log_err ("\"%s\" expects a single string argument.", name);
135 return 1;
136 }
138 sfree (*var);
139 *var = sstrdup (ci->values[0].value.string);
140 return 0;
141 } /* tbl_config_set_separator */
143 static int tbl_config_append_array_i (char *name, size_t **var, size_t *len,
144 oconfig_item_t *ci)
145 {
146 size_t *tmp;
147 size_t num;
148 size_t i;
150 if (1 > ci->values_num) {
151 log_err ("\"%s\" expects at least one argument.", name);
152 return 1;
153 }
155 num = (size_t) ci->values_num;
156 for (i = 0; i < num; ++i) {
157 if (OCONFIG_TYPE_NUMBER != ci->values[i].type) {
158 log_err ("\"%s\" expects numerical arguments only.", name);
159 return 1;
160 }
161 }
163 tmp = realloc (*var, ((*len) + num) * sizeof (**var));
164 if (NULL == tmp) {
165 char errbuf[1024];
166 log_err ("realloc failed: %s.",
167 sstrerror (errno, errbuf, sizeof (errbuf)));
168 return -1;
169 }
170 *var = tmp;
172 for (i = 0; i < num; ++i) {
173 (*var)[*len] = (size_t) ci->values[i].value.number;
174 (*len)++;
175 }
177 return 0;
178 } /* tbl_config_append_array_s */
180 static int tbl_config_result (tbl_t *tbl, oconfig_item_t *ci)
181 {
182 tbl_result_t *res;
184 int status = 0;
185 int i;
187 if (0 != ci->values_num) {
188 log_err ("<Result> does not expect any arguments.");
189 return 1;
190 }
192 res = realloc (tbl->results,
193 (tbl->results_num + 1) * sizeof (*tbl->results));
194 if (res == NULL) {
195 char errbuf[1024];
196 log_err ("realloc failed: %s.",
197 sstrerror (errno, errbuf, sizeof (errbuf)));
198 return -1;
199 }
201 tbl->results = res;
202 ++tbl->results_num;
204 res = tbl->results + tbl->results_num - 1;
205 tbl_result_setup (res);
207 for (i = 0; i < ci->children_num; ++i) {
208 oconfig_item_t *c = ci->children + i;
210 if (0 == strcasecmp (c->key, "Type"))
211 tbl_config_set_s (c->key, &res->type, c);
212 else if (0 == strcasecmp (c->key, "InstancePrefix"))
213 tbl_config_set_s (c->key, &res->instance_prefix, c);
214 else if (0 == strcasecmp (c->key, "InstancesFrom"))
215 tbl_config_append_array_i (c->key,
216 &res->instances, &res->instances_num, c);
217 else if (0 == strcasecmp (c->key, "ValuesFrom"))
218 tbl_config_append_array_i (c->key,
219 &res->values, &res->values_num, c);
220 else
221 log_warn ("Ignoring unknown config key \"%s\" "
222 " in <Result>.", c->key);
223 }
225 if (NULL == res->type) {
226 log_err ("No \"Type\" option specified for <Result> "
227 "in table \"%s\".", tbl->file);
228 status = 1;
229 }
231 if (NULL == res->values) {
232 log_err ("No \"ValuesFrom\" option specified for <Result> "
233 "in table \"%s\".", tbl->file);
234 status = 1;
235 }
237 if (0 != status) {
238 tbl_result_clear (res);
239 --tbl->results_num;
240 return status;
241 }
242 return 0;
243 } /* tbl_config_result */
245 static int tbl_config_table (oconfig_item_t *ci)
246 {
247 tbl_t *tbl;
249 int status = 0;
250 size_t i;
252 if ((1 != ci->values_num)
253 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
254 log_err ("<Table> expects a single string argument.");
255 return 1;
256 }
258 tbl = realloc (tables, (tables_num + 1) * sizeof (*tables));
259 if (NULL == tbl) {
260 char errbuf[1024];
261 log_err ("realloc failed: %s.",
262 sstrerror (errno, errbuf, sizeof (errbuf)));
263 return -1;
264 }
266 tables = tbl;
267 ++tables_num;
269 tbl = tables + tables_num - 1;
270 tbl_setup (tbl, ci->values[0].value.string);
272 for (i = 0; i < ((size_t) ci->children_num); ++i) {
273 oconfig_item_t *c = ci->children + i;
275 if (0 == strcasecmp (c->key, "Separator"))
276 tbl_config_set_s (c->key, &tbl->sep, c);
277 else if (0 == strcasecmp (c->key, "Instance"))
278 tbl_config_set_s (c->key, &tbl->instance, c);
279 else if (0 == strcasecmp (c->key, "Result"))
280 tbl_config_result (tbl, c);
281 else
282 log_warn ("Ignoring unknown config key \"%s\" "
283 "in <Table %s>.", c->key, tbl->file);
284 }
286 if (NULL == tbl->sep) {
287 log_err ("Table \"%s\" does not specify any separator.", tbl->file);
288 status = 1;
289 } else {
290 strunescape (tbl->sep, strlen (tbl->sep) + 1);
291 }
293 if (NULL == tbl->instance) {
294 tbl->instance = sstrdup (tbl->file);
295 replace_special (tbl->instance, strlen (tbl->instance));
296 }
298 if (NULL == tbl->results) {
299 log_err ("Table \"%s\" does not specify any (valid) results.",
300 tbl->file);
301 status = 1;
302 }
304 if (0 != status) {
305 tbl_clear (tbl);
306 --tables_num;
307 return status;
308 }
310 for (i = 0; i < tbl->results_num; ++i) {
311 tbl_result_t *res = tbl->results + i;
312 size_t j;
314 for (j = 0; j < res->instances_num; ++j)
315 if (res->instances[j] > tbl->max_colnum)
316 tbl->max_colnum = res->instances[j];
318 for (j = 0; j < res->values_num; ++j)
319 if (res->values[j] > tbl->max_colnum)
320 tbl->max_colnum = res->values[j];
321 }
322 return 0;
323 } /* tbl_config_table */
325 static int tbl_config (oconfig_item_t *ci)
326 {
327 int i;
329 for (i = 0; i < ci->children_num; ++i) {
330 oconfig_item_t *c = ci->children + i;
332 if (0 == strcasecmp (c->key, "Table"))
333 tbl_config_table (c);
334 else
335 log_warn ("Ignoring unknown config key \"%s\".", c->key);
336 }
337 return 0;
338 } /* tbl_config */
340 /*
341 * result handling
342 */
344 static int tbl_prepare (tbl_t *tbl)
345 {
346 size_t i;
348 for (i = 0; i < tbl->results_num; ++i) {
349 tbl_result_t *res = tbl->results + i;
351 res->ds = plugin_get_ds (res->type);
352 if (NULL == res->ds) {
353 log_err ("Unknown type \"%s\". See types.db(5) for details.",
354 res->type);
355 return -1;
356 }
358 if (res->values_num != res->ds->ds_num) {
359 log_err ("Invalid type \"%s\". Expected %zu data source%s, "
360 "got %zu.", res->type, res->values_num,
361 (1 == res->values_num) ? "" : "s",
362 res->ds->ds_num);
363 return -1;
364 }
365 }
366 return 0;
367 } /* tbl_prepare */
369 static int tbl_finish (tbl_t *tbl)
370 {
371 size_t i;
373 for (i = 0; i < tbl->results_num; ++i)
374 tbl->results[i].ds = NULL;
375 return 0;
376 } /* tbl_finish */
378 static int tbl_result_dispatch (tbl_t *tbl, tbl_result_t *res,
379 char **fields, size_t fields_num)
380 {
381 value_list_t vl = VALUE_LIST_INIT;
382 value_t values[res->values_num];
384 size_t i;
386 assert (NULL != res->ds);
387 assert (res->values_num == res->ds->ds_num);
389 for (i = 0; i < res->values_num; ++i) {
390 char *value;
392 assert (res->values[i] < fields_num);
393 value = fields[res->values[i]];
395 if (0 != parse_value (value, &values[i], res->ds->ds[i].type))
396 return -1;
397 }
399 vl.values = values;
400 vl.values_len = STATIC_ARRAY_SIZE (values);
402 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
403 sstrncpy (vl.plugin, "table", sizeof (vl.plugin));
404 sstrncpy (vl.plugin_instance, tbl->instance, sizeof (vl.plugin_instance));
405 sstrncpy (vl.type, res->type, sizeof (vl.type));
407 if (0 == res->instances_num) {
408 if (NULL != res->instance_prefix)
409 sstrncpy (vl.type_instance, res->instance_prefix,
410 sizeof (vl.type_instance));
411 }
412 else {
413 char *instances[res->instances_num];
414 char instances_str[DATA_MAX_NAME_LEN];
416 for (i = 0; i < res->instances_num; ++i) {
417 assert (res->instances[i] < fields_num);
418 instances[i] = fields[res->instances[i]];
419 }
421 strjoin (instances_str, sizeof (instances_str),
422 instances, STATIC_ARRAY_SIZE (instances), "-");
423 instances_str[sizeof (instances_str) - 1] = '\0';
425 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
426 if (NULL == res->instance_prefix)
427 strncpy (vl.type_instance, instances_str,
428 sizeof (vl.type_instance));
429 else
430 snprintf (vl.type_instance, sizeof (vl.type_instance),
431 "%s-%s", res->instance_prefix, instances_str);
433 if ('\0' != vl.type_instance[sizeof (vl.type_instance) - 1]) {
434 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
435 log_warn ("Truncated type instance: %s.", vl.type_instance);
436 }
437 }
439 plugin_dispatch_values (&vl);
440 return 0;
441 } /* tbl_result_dispatch */
443 static int tbl_parse_line (tbl_t *tbl, char *line, size_t len)
444 {
445 char *fields[tbl->max_colnum + 1];
446 char *ptr, *saveptr;
448 size_t i;
450 i = 0;
451 ptr = line;
452 saveptr = NULL;
453 while (NULL != (fields[i] = strtok_r (ptr, tbl->sep, &saveptr))) {
454 ptr = NULL;
455 ++i;
457 if (i > tbl->max_colnum)
458 break;
459 }
461 if (i <= tbl->max_colnum) {
462 log_warn ("Not enough columns in line "
463 "(expected at least %zu, got %zu).",
464 tbl->max_colnum + 1, i);
465 return -1;
466 }
468 for (i = 0; i < tbl->results_num; ++i)
469 if (0 != tbl_result_dispatch (tbl, tbl->results + i,
470 fields, STATIC_ARRAY_SIZE (fields))) {
471 log_err ("Failed to dispatch result.");
472 continue;
473 }
474 return 0;
475 } /* tbl_parse_line */
477 static int tbl_read_table (tbl_t *tbl)
478 {
479 FILE *fh;
480 char buf[4096];
482 fh = fopen (tbl->file, "r");
483 if (NULL == fh) {
484 char errbuf[1024];
485 log_err ("Failed to open file \"%s\": %s.", tbl->file,
486 sstrerror (errno, errbuf, sizeof (errbuf)));
487 return -1;
488 }
490 buf[sizeof (buf) - 1] = '\0';
491 while (NULL != fgets (buf, sizeof (buf), fh)) {
492 if ('\0' != buf[sizeof (buf) - 1]) {
493 buf[sizeof (buf) - 1] = '\0';
494 log_warn ("Table %s: Truncated line: %s", tbl->file, buf);
495 }
497 if (0 != tbl_parse_line (tbl, buf, sizeof (buf))) {
498 log_warn ("Table %s: Failed to parse line: %s", tbl->file, buf);
499 continue;
500 }
501 }
503 if (0 != ferror (fh)) {
504 char errbuf[1024];
505 log_err ("Failed to read from file \"%s\": %s.", tbl->file,
506 sstrerror (errno, errbuf, sizeof (errbuf)));
507 fclose (fh);
508 return -1;
509 }
511 fclose (fh);
512 return 0;
513 } /* tbl_read_table */
515 /*
516 * collectd callbacks
517 */
519 static int tbl_read (void)
520 {
521 int status = -1;
522 size_t i;
524 if (0 == tables_num)
525 return 0;
527 for (i = 0; i < tables_num; ++i) {
528 tbl_t *tbl = tables + i;
530 if (0 != tbl_prepare (tbl)) {
531 log_err ("Failed to prepare and parse table \"%s\".", tbl->file);
532 continue;
533 }
535 if (0 == tbl_read_table (tbl))
536 status = 0;
538 tbl_finish (tbl);
539 }
540 return status;
541 } /* tbl_read */
543 static int tbl_shutdown (void)
544 {
545 size_t i;
547 for (i = 0; i < tables_num; ++i)
548 tbl_clear (&tables[i]);
549 sfree (tables);
550 return 0;
551 } /* tbl_shutdown */
553 static int tbl_init (void)
554 {
555 if (0 == tables_num)
556 return 0;
558 plugin_register_read ("table", tbl_read);
559 plugin_register_shutdown ("table", tbl_shutdown);
560 return 0;
561 } /* tbl_init */
563 void module_register (void)
564 {
565 plugin_register_complex_config ("table", tbl_config);
566 plugin_register_init ("table", tbl_init);
567 } /* module_register */
569 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */