1 /**
2 * collectd - src/dbi.c
3 * Copyright (C) 2008,2009 Florian octo Forster
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 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_db_query.h"
28 #include <dbi/dbi.h>
30 /*
31 * Data types
32 */
33 struct cdbi_driver_option_s /* {{{ */
34 {
35 char *key;
36 char *value;
37 };
38 typedef struct cdbi_driver_option_s cdbi_driver_option_t; /* }}} */
40 struct cdbi_database_s /* {{{ */
41 {
42 char *name;
43 char *select_db;
45 char *driver;
46 cdbi_driver_option_t *driver_options;
47 size_t driver_options_num;
49 udb_query_t **queries;
50 size_t queries_num;
52 dbi_conn connection;
53 };
54 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
56 /*
57 * Global variables
58 */
59 static udb_query_t **queries = NULL;
60 static size_t queries_num = 0;
61 static cdbi_database_t **databases = NULL;
62 static size_t databases_num = 0;
64 /*
65 * Functions
66 */
67 static const char *cdbi_strerror (dbi_conn conn, /* {{{ */
68 char *buffer, size_t buffer_size)
69 {
70 const char *msg;
71 int status;
73 if (conn == NULL)
74 {
75 sstrncpy (buffer, "connection is NULL", buffer_size);
76 return (buffer);
77 }
79 msg = NULL;
80 status = dbi_conn_error (conn, &msg);
81 if ((status >= 0) && (msg != NULL))
82 ssnprintf (buffer, buffer_size, "%s (status %i)", msg, status);
83 else
84 ssnprintf (buffer, buffer_size, "dbi_conn_error failed with status %i",
85 status);
87 return (buffer);
88 } /* }}} const char *cdbi_conn_error */
90 static int cdbi_result_get_field (dbi_result res, /* {{{ */
91 unsigned int index, char *buffer, size_t buffer_size)
92 {
93 unsigned short src_type;
95 src_type = dbi_result_get_field_type_idx (res, index);
96 if (src_type == DBI_TYPE_ERROR)
97 {
98 ERROR ("dbi plugin: cdbi_result_get: "
99 "dbi_result_get_field_type_idx failed.");
100 return (-1);
101 }
103 if (src_type == DBI_TYPE_INTEGER)
104 {
105 long long value;
107 value = dbi_result_get_longlong_idx (res, index);
108 ssnprintf (buffer, buffer_size, "%lli", value);
109 }
110 else if (src_type == DBI_TYPE_DECIMAL)
111 {
112 double value;
114 value = dbi_result_get_double_idx (res, index);
115 ssnprintf (buffer, buffer_size, "%63.15g", value);
116 }
117 else if (src_type == DBI_TYPE_STRING)
118 {
119 const char *value;
121 value = dbi_result_get_string_idx (res, index);
122 if (value == NULL)
123 sstrncpy (buffer, "", buffer_size);
124 else if (strcmp ("ERROR", value) == 0)
125 return (-1);
126 else
127 sstrncpy (buffer, value, buffer_size);
128 }
129 else
130 {
131 ERROR ("dbi plugin: cdbi_result_get: Don't know how to handle "
132 "source type %hu.", src_type);
133 return (-1);
134 }
136 return (0);
137 } /* }}} int cdbi_result_get_field */
139 static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
140 {
141 size_t i;
143 if (db == NULL)
144 return;
146 sfree (db->name);
147 sfree (db->driver);
149 for (i = 0; i < db->driver_options_num; i++)
150 {
151 sfree (db->driver_options[i].key);
152 sfree (db->driver_options[i].value);
153 }
154 sfree (db->driver_options);
156 sfree (db);
157 } /* }}} void cdbi_database_free */
159 /* Configuration handling functions {{{
160 *
161 * <Plugin dbi>
162 * <Query "plugin_instance0">
163 * Statement "SELECT name, value FROM table"
164 * <Result>
165 * Type "gauge"
166 * InstancesFrom "name"
167 * ValuesFrom "value"
168 * </Result>
169 * ...
170 * </Query>
171 *
172 * <Database "plugin_instance1">
173 * Driver "mysql"
174 * DriverOption "hostname" "localhost"
175 * ...
176 * Query "plugin_instance0"
177 * </Database>
178 * </Plugin>
179 */
181 static int cdbi_config_set_string (char **ret_string, /* {{{ */
182 oconfig_item_t *ci)
183 {
184 char *string;
186 if ((ci->values_num != 1)
187 || (ci->values[0].type != OCONFIG_TYPE_STRING))
188 {
189 WARNING ("dbi plugin: The `%s' config option "
190 "needs exactly one string argument.", ci->key);
191 return (-1);
192 }
194 string = strdup (ci->values[0].value.string);
195 if (string == NULL)
196 {
197 ERROR ("dbi plugin: strdup failed.");
198 return (-1);
199 }
201 if (*ret_string != NULL)
202 free (*ret_string);
203 *ret_string = string;
205 return (0);
206 } /* }}} int cdbi_config_set_string */
208 static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */
209 oconfig_item_t *ci)
210 {
211 cdbi_driver_option_t *option;
213 if ((ci->values_num != 2)
214 || (ci->values[0].type != OCONFIG_TYPE_STRING)
215 || (ci->values[1].type != OCONFIG_TYPE_STRING))
216 {
217 WARNING ("dbi plugin: The `DriverOption' config option "
218 "needs exactly two string arguments.");
219 return (-1);
220 }
222 option = (cdbi_driver_option_t *) realloc (db->driver_options,
223 sizeof (*option) * (db->driver_options_num + 1));
224 if (option == NULL)
225 {
226 ERROR ("dbi plugin: realloc failed");
227 return (-1);
228 }
230 db->driver_options = option;
231 option = db->driver_options + db->driver_options_num;
233 option->key = strdup (ci->values[0].value.string);
234 if (option->key == NULL)
235 {
236 ERROR ("dbi plugin: strdup failed.");
237 return (-1);
238 }
240 option->value = strdup (ci->values[1].value.string);
241 if (option->value == NULL)
242 {
243 ERROR ("dbi plugin: strdup failed.");
244 sfree (option->key);
245 return (-1);
246 }
248 db->driver_options_num++;
249 return (0);
250 } /* }}} int cdbi_config_add_database_driver_option */
252 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
253 {
254 cdbi_database_t *db;
255 int status;
256 int i;
258 if ((ci->values_num != 1)
259 || (ci->values[0].type != OCONFIG_TYPE_STRING))
260 {
261 WARNING ("dbi plugin: The `Database' block "
262 "needs exactly one string argument.");
263 return (-1);
264 }
266 db = (cdbi_database_t *) malloc (sizeof (*db));
267 if (db == NULL)
268 {
269 ERROR ("dbi plugin: malloc failed.");
270 return (-1);
271 }
272 memset (db, 0, sizeof (*db));
274 status = cdbi_config_set_string (&db->name, ci);
275 if (status != 0)
276 {
277 sfree (db);
278 return (status);
279 }
281 /* Fill the `cdbi_database_t' structure.. */
282 for (i = 0; i < ci->children_num; i++)
283 {
284 oconfig_item_t *child = ci->children + i;
286 if (strcasecmp ("Driver", child->key) == 0)
287 status = cdbi_config_set_string (&db->driver, child);
288 else if (strcasecmp ("DriverOption", child->key) == 0)
289 status = cdbi_config_add_database_driver_option (db, child);
290 else if (strcasecmp ("SelectDB", child->key) == 0)
291 status = cdbi_config_set_string (&db->select_db, child);
292 else if (strcasecmp ("Query", child->key) == 0)
293 status = udb_query_pick_from_list (child, queries, queries_num,
294 &db->queries, &db->queries_num);
295 else
296 {
297 WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
298 status = -1;
299 }
301 if (status != 0)
302 break;
303 }
305 /* Check that all necessary options have been given. */
306 while (status == 0)
307 {
308 if (db->driver == NULL)
309 {
310 WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
311 status = -1;
312 }
313 if (db->driver_options_num == 0)
314 {
315 WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
316 "This will likely not work.", db->name);
317 }
319 break;
320 } /* while (status == 0) */
322 /* If all went well, add this database to the global list of databases. */
323 if (status == 0)
324 {
325 cdbi_database_t **temp;
327 temp = (cdbi_database_t **) realloc (databases,
328 sizeof (*databases) * (databases_num + 1));
329 if (temp == NULL)
330 {
331 ERROR ("dbi plugin: realloc failed");
332 status = -1;
333 }
334 else
335 {
336 databases = temp;
337 databases[databases_num] = db;
338 databases_num++;
339 }
340 }
342 if (status != 0)
343 {
344 cdbi_database_free (db);
345 return (-1);
346 }
348 return (0);
349 } /* }}} int cdbi_config_add_database */
351 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
352 {
353 int i;
355 for (i = 0; i < ci->children_num; i++)
356 {
357 oconfig_item_t *child = ci->children + i;
358 if (strcasecmp ("Query", child->key) == 0)
359 udb_query_create (&queries, &queries_num, child);
360 else if (strcasecmp ("Database", child->key) == 0)
361 cdbi_config_add_database (child);
362 else
363 {
364 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
365 }
366 } /* for (ci->children) */
368 return (0);
369 } /* }}} int cdbi_config */
371 /* }}} End of configuration handling functions */
373 static int cdbi_init (void) /* {{{ */
374 {
375 static int did_init = 0;
376 int status;
378 if (did_init != 0)
379 return (0);
381 if (queries_num == 0)
382 {
383 ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
384 "this plugin can't do anything useful, so we will returns an error.");
385 return (-1);
386 }
388 if (databases_num == 0)
389 {
390 ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
391 "this plugin can't do anything useful, so we will returns an error.");
392 return (-1);
393 }
395 status = dbi_initialize (NULL);
396 if (status < 0)
397 {
398 ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.",
399 status);
400 return (-1);
401 }
402 else if (status == 0)
403 {
404 ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please "
405 "install at least one `DBD' or check your installation.");
406 return (-1);
407 }
408 DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.",
409 status, (status == 1) ? "" : "s");
411 return (0);
412 } /* }}} int cdbi_init */
414 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
415 udb_query_t *q)
416 {
417 const char *statement;
418 dbi_result res;
419 size_t column_num;
420 char **column_names;
421 char **column_values;
422 int status;
423 size_t i;
425 /* Macro that cleans up dynamically allocated memory and returns the
426 * specified status. */
427 #define BAIL_OUT(status) \
428 if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
429 if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
430 if (res != NULL) { dbi_result_free (res); res = NULL; } \
431 return (status)
433 column_names = NULL;
434 column_values = NULL;
435 res = NULL;
437 statement = udb_query_get_statement (q);
438 assert (statement != NULL);
440 res = dbi_conn_query (db->connection, statement);
441 if (res == NULL)
442 {
443 char errbuf[1024];
444 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
445 "dbi_conn_query failed: %s",
446 db->name, udb_query_get_name (q),
447 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
448 BAIL_OUT (-1);
449 }
450 else /* Get the number of columns */
451 {
452 unsigned int db_status;
454 db_status = dbi_result_get_numfields (res);
455 if (db_status == DBI_FIELD_ERROR)
456 {
457 char errbuf[1024];
458 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
459 "dbi_result_get_numfields failed: %s",
460 db->name, udb_query_get_name (q),
461 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
462 BAIL_OUT (-1);
463 }
465 column_num = (size_t) db_status;
466 DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
467 db->name, udb_query_get_name (q), column_num);
468 }
470 /* Allocate `column_names' and `column_values'. {{{ */
471 column_names = (char **) calloc (column_num, sizeof (char *));
472 if (column_names == NULL)
473 {
474 ERROR ("dbi plugin: malloc failed.");
475 BAIL_OUT (-1);
476 }
478 column_names[0] = (char *) calloc (column_num,
479 DATA_MAX_NAME_LEN * sizeof (char));
480 if (column_names[0] == NULL)
481 {
482 ERROR ("dbi plugin: malloc failed.");
483 BAIL_OUT (-1);
484 }
485 for (i = 1; i < column_num; i++)
486 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
488 column_values = (char **) calloc (column_num, sizeof (char *));
489 if (column_values == NULL)
490 {
491 ERROR ("dbi plugin: malloc failed.");
492 BAIL_OUT (-1);
493 }
495 column_values[0] = (char *) calloc (column_num,
496 DATA_MAX_NAME_LEN * sizeof (char));
497 if (column_values[0] == NULL)
498 {
499 ERROR ("dbi plugin: malloc failed.");
500 BAIL_OUT (-1);
501 }
502 for (i = 1; i < column_num; i++)
503 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
504 /* }}} */
506 /* Copy the field names to `column_names' */
507 for (i = 0; i < column_num; i++) /* {{{ */
508 {
509 const char *column_name;
511 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
512 if (column_name == NULL)
513 {
514 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
515 "Cannot retrieve name of field %zu.",
516 db->name, udb_query_get_name (q), i + 1);
517 BAIL_OUT (-1);
518 }
520 sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
521 } /* }}} for (i = 0; i < column_num; i++) */
523 udb_query_prepare_result (q, hostname_g, /* plugin = */ "dbi", db->name,
524 column_names, column_num);
526 /* 0 = error; 1 = success; */
527 status = dbi_result_first_row (res); /* {{{ */
528 if (status != 1)
529 {
530 char errbuf[1024];
531 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
532 "dbi_result_first_row failed: %s. Maybe the statement didn't "
533 "return any rows?",
534 db->name, udb_query_get_name (q),
535 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
536 udb_query_finish_result (q);
537 BAIL_OUT (-1);
538 } /* }}} */
540 /* Iterate over all rows and call `udb_query_handle_result' with each list of
541 * values. */
542 while (42) /* {{{ */
543 {
544 status = 0;
545 /* Copy the value of the columns to `column_values' */
546 for (i = 0; i < column_num; i++) /* {{{ */
547 {
548 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
549 column_values[i], DATA_MAX_NAME_LEN);
551 if (status != 0)
552 {
553 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
554 "cdbi_result_get_field (%zu) failed.",
555 db->name, udb_query_get_name (q), i + 1);
556 status = -1;
557 break;
558 }
559 } /* }}} for (i = 0; i < column_num; i++) */
561 /* If all values were copied successfully, call `udb_query_handle_result'
562 * to dispatch the row to the daemon. */
563 if (status == 0) /* {{{ */
564 {
565 status = udb_query_handle_result (q, column_values);
566 if (status != 0)
567 {
568 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
569 "udb_query_handle_result failed.",
570 db->name, udb_query_get_name (q));
571 }
572 } /* }}} */
574 /* Get the next row from the database. */
575 status = dbi_result_next_row (res); /* {{{ */
576 if (status != 1)
577 {
578 if (dbi_conn_error (db->connection, NULL) != 0)
579 {
580 char errbuf[1024];
581 WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
582 "dbi_result_next_row failed: %s.",
583 db->name, udb_query_get_name (q),
584 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
585 }
586 break;
587 } /* }}} */
588 } /* }}} while (42) */
590 /* Tell the db query interface that we're done with this query. */
591 udb_query_finish_result (q);
593 /* Clean up and return `status = 0' (success) */
594 BAIL_OUT (0);
595 #undef BAIL_OUT
596 } /* }}} int cdbi_read_database_query */
598 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
599 {
600 dbi_driver driver;
601 dbi_conn connection;
602 size_t i;
603 int status;
605 if (db->connection != NULL)
606 {
607 status = dbi_conn_ping (db->connection);
608 if (status != 0) /* connection is alive */
609 return (0);
611 dbi_conn_close (db->connection);
612 db->connection = NULL;
613 }
615 driver = dbi_driver_open (db->driver);
616 if (driver == NULL)
617 {
618 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.",
619 db->driver);
620 INFO ("dbi plugin: Maybe the driver isn't installed? "
621 "Known drivers are:");
622 for (driver = dbi_driver_list (NULL);
623 driver != NULL;
624 driver = dbi_driver_list (driver))
625 {
626 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
627 }
628 return (-1);
629 }
631 connection = dbi_conn_open (driver);
632 if (connection == NULL)
633 {
634 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
635 db->driver);
636 return (-1);
637 }
639 /* Set all the driver options. Because this is a very very very generic
640 * interface, the error handling is kind of long. If an invalid option is
641 * encountered, it will get a list of options understood by the driver and
642 * report that as `INFO'. This way, users hopefully don't have too much
643 * trouble finding out how to configure the plugin correctly.. */
644 for (i = 0; i < db->driver_options_num; i++)
645 {
646 DEBUG ("dbi plugin: cdbi_connect_database (%s): "
647 "key = %s; value = %s;",
648 db->name,
649 db->driver_options[i].key,
650 db->driver_options[i].value);
652 status = dbi_conn_set_option (connection,
653 db->driver_options[i].key, db->driver_options[i].value);
654 if (status != 0)
655 {
656 char errbuf[1024];
657 const char *opt;
659 ERROR ("dbi plugin: cdbi_connect_database (%s): "
660 "dbi_conn_set_option (%s, %s) failed: %s.",
661 db->name,
662 db->driver_options[i].key, db->driver_options[i].value,
663 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
665 INFO ("dbi plugin: This is a list of all options understood "
666 "by the `%s' driver:", db->driver);
667 for (opt = dbi_conn_get_option_list (connection, NULL);
668 opt != NULL;
669 opt = dbi_conn_get_option_list (connection, opt))
670 {
671 INFO ("dbi plugin: * %s", opt);
672 }
674 dbi_conn_close (connection);
675 return (-1);
676 }
677 } /* for (i = 0; i < db->driver_options_num; i++) */
679 status = dbi_conn_connect (connection);
680 if (status != 0)
681 {
682 char errbuf[1024];
683 ERROR ("dbi plugin: cdbi_connect_database (%s): "
684 "dbi_conn_connect failed: %s",
685 db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
686 dbi_conn_close (connection);
687 return (-1);
688 }
690 if (db->select_db != NULL)
691 {
692 status = dbi_conn_select_db (connection, db->select_db);
693 if (status != 0)
694 {
695 char errbuf[1024];
696 WARNING ("dbi plugin: cdbi_connect_database (%s): "
697 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
698 db->name, db->select_db,
699 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
700 dbi_conn_close (connection);
701 return (-1);
702 }
703 }
705 db->connection = connection;
706 return (0);
707 } /* }}} int cdbi_connect_database */
709 static int cdbi_read_database (cdbi_database_t *db) /* {{{ */
710 {
711 size_t i;
712 int success;
713 int status;
715 status = cdbi_connect_database (db);
716 if (status != 0)
717 return (status);
718 assert (db->connection != NULL);
720 success = 0;
721 for (i = 0; i < db->queries_num; i++)
722 {
723 status = cdbi_read_database_query (db, db->queries[i]);
724 if (status == 0)
725 success++;
726 }
728 if (success == 0)
729 {
730 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
731 return (-1);
732 }
734 return (0);
735 } /* }}} int cdbi_read_database */
737 static int cdbi_read (void) /* {{{ */
738 {
739 size_t i;
740 int success = 0;
741 int status;
743 for (i = 0; i < databases_num; i++)
744 {
745 status = cdbi_read_database (databases[i]);
746 if (status == 0)
747 success++;
748 }
750 if (success == 0)
751 {
752 ERROR ("dbi plugin: No database could be read. Will return an error so "
753 "the plugin will be delayed.");
754 return (-1);
755 }
757 return (0);
758 } /* }}} int cdbi_read */
760 static int cdbi_shutdown (void) /* {{{ */
761 {
762 size_t i;
764 for (i = 0; i < databases_num; i++)
765 {
766 if (databases[i]->connection != NULL)
767 {
768 dbi_conn_close (databases[i]->connection);
769 databases[i]->connection = NULL;
770 }
771 cdbi_database_free (databases[i]);
772 }
773 sfree (databases);
774 databases_num = 0;
776 udb_query_free (queries, queries_num);
777 queries = NULL;
778 queries_num = 0;
780 return (0);
781 } /* }}} int cdbi_shutdown */
783 void module_register (void) /* {{{ */
784 {
785 plugin_register_complex_config ("dbi", cdbi_config);
786 plugin_register_init ("dbi", cdbi_init);
787 plugin_register_read ("dbi", cdbi_read);
788 plugin_register_shutdown ("dbi", cdbi_shutdown);
789 } /* }}} void module_register */
791 /*
792 * vim: shiftwidth=2 softtabstop=2 et fdm=marker
793 */