a3bacc7ae6e73b2e7f7b587f25833a9cd9374e7a
1 /**
2 * collectd - src/table.c
3 * Copyright (C) 2009 Sebastian Harl
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Sebastian Harl <sh at tokkee.org>
20 **/
22 /*
23 * This module provides generic means to parse and dispatch tabular data.
24 */
26 #include "collectd.h"
27 #include "common.h"
29 #include "configfile.h"
30 #include "plugin.h"
32 #define log_err(...) ERROR ("table plugin: " __VA_ARGS__)
33 #define log_warn(...) WARNING ("table plugin: " __VA_ARGS__)
35 /*
36 * private data types
37 */
39 typedef struct {
40 char *type;
41 char *instance_prefix;
42 int *instances;
43 size_t instances_num;
44 int *values;
45 size_t values_num;
47 const data_set_t *ds;
48 } tbl_result_t;
50 typedef struct {
51 char *file;
52 char *sep;
53 char *instance;
55 tbl_result_t *results;
56 size_t results_num;
58 size_t max_colnum;
59 } tbl_t;
61 static void tbl_result_setup (tbl_result_t *res)
62 {
63 res->type = NULL;
65 res->instance_prefix = NULL;
66 res->instances = NULL;
67 res->instances_num = 0;
69 res->values = NULL;
70 res->values_num = 0;
72 res->ds = NULL;
73 } /* tbl_result_setup */
75 static void tbl_result_clear (tbl_result_t *res)
76 {
77 sfree (res->type);
79 sfree (res->instance_prefix);
80 sfree (res->instances);
81 res->instances_num = 0;
83 sfree (res->values);
84 res->values_num = 0;
86 res->ds = NULL;
87 } /* tbl_result_clear */
89 static void tbl_setup (tbl_t *tbl, char *file)
90 {
91 tbl->file = sstrdup (file);
92 tbl->sep = NULL;
93 tbl->instance = NULL;
95 tbl->results = NULL;
96 tbl->results_num = 0;
98 tbl->max_colnum = 0;
99 } /* tbl_setup */
101 static void tbl_clear (tbl_t *tbl)
102 {
103 size_t i;
105 sfree (tbl->file);
106 sfree (tbl->sep);
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;
114 tbl->max_colnum = 0;
115 } /* tbl_clear */
117 static tbl_t *tables;
118 static size_t tables_num;
120 /*
121 * configuration handling
122 */
124 static int tbl_config_set_s (char *name, char **var, oconfig_item_t *ci)
125 {
126 if ((1 != ci->values_num)
127 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
128 log_err ("\"%s\" expects a single string argument.", name);
129 return 1;
130 }
132 sfree (*var);
133 *var = sstrdup (ci->values[0].value.string);
134 return 0;
135 } /* tbl_config_set_separator */
137 static int tbl_config_append_array_i (char *name, int **var, size_t *len,
138 oconfig_item_t *ci)
139 {
140 int *tmp;
142 size_t i;
144 if (1 > ci->values_num) {
145 log_err ("\"%s\" expects at least one argument.", name);
146 return 1;
147 }
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);
152 return 1;
153 }
154 }
156 *len += ci->values_num;
157 tmp = (int *)realloc (*var, *len * sizeof (**var));
158 if (NULL == tmp) {
159 char errbuf[1024];
160 log_err ("realloc failed: %s.",
161 sstrerror (errno, errbuf, sizeof (errbuf)));
162 return -1;
163 }
165 *var = tmp;
167 for (i = *len - ci->values_num; i < *len; ++i)
168 (*var)[i] = (int)ci->values[i].value.number;
169 return 0;
170 } /* tbl_config_append_array_s */
172 static int tbl_config_result (tbl_t *tbl, oconfig_item_t *ci)
173 {
174 tbl_result_t *res;
176 int status = 0;
177 size_t i;
179 if (0 != ci->values_num) {
180 log_err ("<Result> does not expect any arguments.");
181 return 1;
182 }
184 res = (tbl_result_t *)realloc (tbl->results,
185 (tbl->results_num + 1) * sizeof (*tbl->results));
186 if (NULL == tbl) {
187 char errbuf[1024];
188 log_err ("realloc failed: %s.",
189 sstrerror (errno, errbuf, sizeof (errbuf)));
190 return -1;
191 }
193 tbl->results = res;
194 ++tbl->results_num;
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);
212 else
213 log_warn ("Ignoring unknown config key \"%s\" "
214 " in <Result>.", c->key);
215 }
217 if (NULL == res->type) {
218 log_err ("No \"Type\" option specified for <Result> "
219 "in table \"%s\".", tbl->file);
220 status = 1;
221 }
223 if (NULL == res->values) {
224 log_err ("No \"ValuesFrom\" option specified for <Result> "
225 "in table \"%s\".", tbl->file);
226 status = 1;
227 }
229 if (0 != status) {
230 tbl_result_clear (res);
231 --tbl->results_num;
232 return status;
233 }
234 return 0;
235 } /* tbl_config_result */
237 static int tbl_config_table (oconfig_item_t *ci)
238 {
239 tbl_t *tbl;
241 int status = 0;
242 size_t i;
244 if ((1 != ci->values_num)
245 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
246 log_err ("<Table> expects a single string argument.");
247 return 1;
248 }
250 tbl = (tbl_t *)realloc (tables, (tables_num + 1) * sizeof (*tables));
251 if (NULL == tbl) {
252 char errbuf[1024];
253 log_err ("realloc failed: %s.",
254 sstrerror (errno, errbuf, sizeof (errbuf)));
255 return -1;
256 }
258 tables = tbl;
259 ++tables_num;
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);
273 else
274 log_warn ("Ignoring unknown config key \"%s\" "
275 "in <Table %s>.", c->key, tbl->file);
276 }
278 if (NULL == tbl->sep) {
279 log_err ("Table \"%s\" does not specify any separator.", tbl->file);
280 status = 1;
281 } else {
282 strunescape (tbl->sep, strlen (tbl->sep) + 1);
283 }
285 if (NULL == tbl->instance) {
286 tbl->instance = sstrdup (tbl->file);
287 replace_special (tbl->instance, strlen (tbl->instance));
288 }
290 if (NULL == tbl->results) {
291 log_err ("Table \"%s\" does not specify any (valid) results.",
292 tbl->file);
293 status = 1;
294 }
296 if (0 != status) {
297 tbl_clear (tbl);
298 --tables_num;
299 return status;
300 }
302 for (i = 0; i < tbl->results_num; ++i) {
303 tbl_result_t *res = tbl->results + i;
304 size_t j;
306 for (j = 0; j < res->instances_num; ++j)
307 if (res->instances[j] > tbl->max_colnum)
308 tbl->max_colnum = res->instances[j];
310 for (j = 0; j < res->values_num; ++j)
311 if (res->values[j] > tbl->max_colnum)
312 tbl->max_colnum = res->values[j];
313 }
314 return 0;
315 } /* tbl_config_table */
317 static int tbl_config (oconfig_item_t *ci)
318 {
319 size_t i;
321 for (i = 0; i < ci->children_num; ++i) {
322 oconfig_item_t *c = ci->children + i;
324 if (0 == strcasecmp (c->key, "Table"))
325 tbl_config_table (c);
326 else
327 log_warn ("Ignoring unknown config key \"%s\".", c->key);
328 }
329 return 0;
330 } /* tbl_config */
332 /*
333 * result handling
334 */
336 static int tbl_prepare (tbl_t *tbl)
337 {
338 size_t i;
340 for (i = 0; i < tbl->results_num; ++i) {
341 tbl_result_t *res = tbl->results + i;
343 res->ds = plugin_get_ds (res->type);
344 if (NULL == res->ds) {
345 log_err ("Unknown type \"%s\". See types.db(5) for details.",
346 res->type);
347 return -1;
348 }
350 if (res->values_num != (size_t)res->ds->ds_num) {
351 log_err ("Invalid type \"%s\". Expected %zu data source%s, "
352 "got %i.", res->type, res->values_num,
353 (1 == res->values_num) ? "" : "s",
354 res->ds->ds_num);
355 return -1;
356 }
357 }
358 return 0;
359 } /* tbl_prepare */
361 static int tbl_finish (tbl_t *tbl)
362 {
363 size_t i;
365 for (i = 0; i < tbl->results_num; ++i)
366 tbl->results[i].ds = NULL;
367 return 0;
368 } /* tbl_finish */
370 static int tbl_result_dispatch (tbl_t *tbl, tbl_result_t *res,
371 char **fields, size_t fields_num)
372 {
373 value_list_t vl = VALUE_LIST_INIT;
374 value_t values[res->values_num];
376 size_t i;
378 assert (NULL != res->ds);
379 assert (res->values_num == res->ds->ds_num);
381 for (i = 0; i < res->values_num; ++i) {
382 char *value;
384 assert (res->values[i] < fields_num);
385 value = fields[res->values[i]];
387 if (0 != parse_value (value, &values[i], res->ds->ds[i].type))
388 return -1;
389 }
391 vl.values = values;
392 vl.values_len = STATIC_ARRAY_SIZE (values);
394 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
395 sstrncpy (vl.plugin, "table", sizeof (vl.plugin));
396 sstrncpy (vl.plugin_instance, tbl->instance, sizeof (vl.plugin_instance));
397 sstrncpy (vl.type, res->type, sizeof (vl.type));
399 if (0 == res->instances_num) {
400 if (NULL != res->instance_prefix)
401 sstrncpy (vl.type_instance, res->instance_prefix,
402 sizeof (vl.type_instance));
403 }
404 else {
405 char *instances[res->instances_num];
406 char instances_str[DATA_MAX_NAME_LEN];
408 for (i = 0; i < res->instances_num; ++i) {
409 assert (res->instances[i] < fields_num);
410 instances[i] = fields[res->instances[i]];
411 }
413 strjoin (instances_str, sizeof (instances_str),
414 instances, STATIC_ARRAY_SIZE (instances), "-");
415 instances_str[sizeof (instances_str) - 1] = '\0';
417 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
418 if (NULL == res->instance_prefix)
419 strncpy (vl.type_instance, instances_str,
420 sizeof (vl.type_instance));
421 else
422 snprintf (vl.type_instance, sizeof (vl.type_instance),
423 "%s-%s", res->instance_prefix, instances_str);
425 if ('\0' != vl.type_instance[sizeof (vl.type_instance) - 1]) {
426 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
427 log_warn ("Truncated type instance: %s.", vl.type_instance);
428 }
429 }
431 plugin_dispatch_values (&vl);
432 return 0;
433 } /* tbl_result_dispatch */
435 static int tbl_parse_line (tbl_t *tbl, char *line, size_t len)
436 {
437 char *fields[tbl->max_colnum + 1];
438 char *ptr, *saveptr;
440 size_t i;
442 i = 0;
443 ptr = line;
444 saveptr = NULL;
445 while (NULL != (fields[i] = strtok_r (ptr, tbl->sep, &saveptr))) {
446 ptr = NULL;
447 ++i;
449 if (i > tbl->max_colnum)
450 break;
451 }
453 if (i <= tbl->max_colnum) {
454 log_err ("Not enough columns in line "
455 "(expected at least %zu, got %zu).",
456 tbl->max_colnum + 1, i);
457 return -1;
458 }
460 for (i = 0; i < tbl->results_num; ++i)
461 if (0 != tbl_result_dispatch (tbl, tbl->results + i,
462 fields, STATIC_ARRAY_SIZE (fields))) {
463 log_err ("Failed to dispatch result.");
464 continue;
465 }
466 return 0;
467 } /* tbl_parse_line */
469 static int tbl_read_table (tbl_t *tbl)
470 {
471 FILE *fh;
472 char buf[4096];
474 fh = fopen (tbl->file, "r");
475 if (NULL == fh) {
476 char errbuf[1024];
477 log_err ("Failed to open file \"%s\": %s.", tbl->file,
478 sstrerror (errno, errbuf, sizeof (errbuf)));
479 return -1;
480 }
482 buf[sizeof (buf) - 1] = '\0';
483 while (NULL != fgets (buf, sizeof (buf), fh)) {
484 if ('\0' != buf[sizeof (buf) - 1]) {
485 buf[sizeof (buf) - 1] = '\0';
486 log_err ("Table %s: Truncated line: %s", tbl->file, buf);
487 }
489 if (0 != tbl_parse_line (tbl, buf, sizeof (buf))) {
490 log_err ("Table %s: Failed to parse line: %s", tbl->file, buf);
491 continue;
492 }
493 }
495 if (0 != ferror (fh)) {
496 char errbuf[1024];
497 log_err ("Failed to read from file \"%s\": %s.", tbl->file,
498 sstrerror (errno, errbuf, sizeof (errbuf)));
499 fclose (fh);
500 return -1;
501 }
503 fclose (fh);
504 return 0;
505 } /* tbl_read_table */
507 /*
508 * collectd callbacks
509 */
511 static int tbl_read (void)
512 {
513 int status = -1;
514 size_t i;
516 if (0 == tables_num)
517 return 0;
519 for (i = 0; i < tables_num; ++i) {
520 tbl_t *tbl = tables + i;
522 if (0 != tbl_prepare (tbl)) {
523 log_err ("Failed to prepare and parse table \"%s\".", tbl->file);
524 continue;
525 }
527 if (0 == tbl_read_table (tbl))
528 status = 0;
530 tbl_finish (tbl);
531 }
532 return status;
533 } /* tbl_read */
535 static int tbl_shutdown (void)
536 {
537 size_t i;
539 for (i = 0; i < tables_num; ++i)
540 tbl_clear (&tables[i]);
541 sfree (tables);
542 return 0;
543 } /* tbl_shutdown */
545 static int tbl_init (void)
546 {
547 if (0 == tables_num)
548 return 0;
550 plugin_register_read ("table", tbl_read);
551 plugin_register_shutdown ("table", tbl_shutdown);
552 return 0;
553 } /* tbl_init */
555 void module_register (void)
556 {
557 plugin_register_complex_config ("table", tbl_config);
558 plugin_register_init ("table", tbl_init);
559 } /* module_register */
561 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */