3 * Copyright (C) 2008-2015 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at collectd.org>
25 #include "configfile.h"
26 #include "utils_db_query.h"
30 /* libdbi 0.9.0 introduced a new thread-safe interface and marked the old
31 * functions "deprecated". These macros convert the new functions to their old
32 * counterparts for backwards compatibility. */
33 #if !defined(LIBDBI_VERSION) || (LIBDBI_VERSION < 900)
34 # define HAVE_LEGACY_LIBDBI 1
35 # define dbi_initialize_r(a,inst) dbi_initialize(a)
36 # define dbi_shutdown_r(inst) dbi_shutdown()
37 # define dbi_set_verbosity_r(a,inst) dbi_set_verbosity(a)
38 # define dbi_driver_list_r(a,inst) dbi_driver_list(a)
39 # define dbi_driver_open_r(a,inst) dbi_driver_open(a)
45 struct cdbi_driver_option_s /* {{{ */
55 typedef struct cdbi_driver_option_s cdbi_driver_option_t; /* }}} */
57 struct cdbi_database_s /* {{{ */
64 cdbi_driver_option_t *driver_options;
65 size_t driver_options_num;
67 udb_query_preparation_area_t **q_prep_areas;
68 udb_query_t **queries;
73 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
78 #if !defined(HAVE_LEGACY_LIBDBI) || !HAVE_LEGACY_LIBDBI
79 static dbi_inst dbi_instance = 0;
81 static udb_query_t **queries = NULL;
82 static size_t queries_num = 0;
83 static cdbi_database_t **databases = NULL;
84 static size_t databases_num = 0;
89 static const char *cdbi_strerror (dbi_conn conn, /* {{{ */
90 char *buffer, size_t buffer_size)
97 sstrncpy (buffer, "connection is NULL", buffer_size);
102 status = dbi_conn_error (conn, &msg);
103 if ((status >= 0) && (msg != NULL))
104 ssnprintf (buffer, buffer_size, "%s (status %i)", msg, status);
106 ssnprintf (buffer, buffer_size, "dbi_conn_error failed with status %i",
110 } /* }}} const char *cdbi_conn_error */
112 static int cdbi_result_get_field (dbi_result res, /* {{{ */
113 unsigned int index, char *buffer, size_t buffer_size)
115 unsigned short src_type;
117 src_type = dbi_result_get_field_type_idx (res, index);
118 if (src_type == DBI_TYPE_ERROR)
120 ERROR ("dbi plugin: cdbi_result_get: "
121 "dbi_result_get_field_type_idx failed.");
125 if (src_type == DBI_TYPE_INTEGER)
129 value = dbi_result_get_longlong_idx (res, index);
130 ssnprintf (buffer, buffer_size, "%lli", value);
132 else if (src_type == DBI_TYPE_DECIMAL)
136 value = dbi_result_get_double_idx (res, index);
137 ssnprintf (buffer, buffer_size, "%63.15g", value);
139 else if (src_type == DBI_TYPE_STRING)
143 value = dbi_result_get_string_idx (res, index);
145 sstrncpy (buffer, "", buffer_size);
146 else if (strcmp ("ERROR", value) == 0)
149 sstrncpy (buffer, value, buffer_size);
151 /* DBI_TYPE_BINARY */
152 /* DBI_TYPE_DATETIME */
155 const char *field_name;
157 field_name = dbi_result_get_field_name (res, index);
158 if (field_name == NULL)
159 field_name = "<unknown>";
161 ERROR ("dbi plugin: Column `%s': Don't know how to handle "
163 field_name, src_type);
168 } /* }}} int cdbi_result_get_field */
170 static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
180 for (i = 0; i < db->driver_options_num; i++)
182 sfree (db->driver_options[i].key);
183 if (!db->driver_options[i].is_numeric)
184 sfree (db->driver_options[i].value.string);
186 sfree (db->driver_options);
188 if (db->q_prep_areas)
189 for (i = 0; i < db->queries_num; ++i)
190 udb_query_delete_preparation_area (db->q_prep_areas[i]);
191 free (db->q_prep_areas);
194 } /* }}} void cdbi_database_free */
196 /* Configuration handling functions {{{
199 * <Query "plugin_instance0">
200 * Statement "SELECT name, value FROM table"
203 * InstancesFrom "name"
209 * <Database "plugin_instance1">
211 * DriverOption "hostname" "localhost"
213 * Query "plugin_instance0"
218 static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */
221 cdbi_driver_option_t *option;
223 if ((ci->values_num != 2)
224 || (ci->values[0].type != OCONFIG_TYPE_STRING)
225 || ((ci->values[1].type != OCONFIG_TYPE_STRING)
226 && (ci->values[1].type != OCONFIG_TYPE_NUMBER)))
228 WARNING ("dbi plugin: The `DriverOption' config option "
229 "needs exactly two arguments.");
233 option = (cdbi_driver_option_t *) realloc (db->driver_options,
234 sizeof (*option) * (db->driver_options_num + 1));
237 ERROR ("dbi plugin: realloc failed");
241 db->driver_options = option;
242 option = db->driver_options + db->driver_options_num;
243 memset (option, 0, sizeof (*option));
245 option->key = strdup (ci->values[0].value.string);
246 if (option->key == NULL)
248 ERROR ("dbi plugin: strdup failed.");
252 if (ci->values[1].type == OCONFIG_TYPE_STRING)
254 option->value.string = strdup (ci->values[1].value.string);
255 if (option->value.string == NULL)
257 ERROR ("dbi plugin: strdup failed.");
264 assert (ci->values[1].type == OCONFIG_TYPE_NUMBER);
265 option->value.numeric = (int) (ci->values[1].value.number + .5);
266 option->is_numeric = 1;
269 db->driver_options_num++;
271 } /* }}} int cdbi_config_add_database_driver_option */
273 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
279 if ((ci->values_num != 1)
280 || (ci->values[0].type != OCONFIG_TYPE_STRING))
282 WARNING ("dbi plugin: The `Database' block "
283 "needs exactly one string argument.");
287 db = (cdbi_database_t *) malloc (sizeof (*db));
290 ERROR ("dbi plugin: malloc failed.");
293 memset (db, 0, sizeof (*db));
295 status = cf_util_get_string (ci, &db->name);
302 /* Fill the `cdbi_database_t' structure.. */
303 for (i = 0; i < ci->children_num; i++)
305 oconfig_item_t *child = ci->children + i;
307 if (strcasecmp ("Driver", child->key) == 0)
308 status = cf_util_get_string (child, &db->driver);
309 else if (strcasecmp ("DriverOption", child->key) == 0)
310 status = cdbi_config_add_database_driver_option (db, child);
311 else if (strcasecmp ("SelectDB", child->key) == 0)
312 status = cf_util_get_string (child, &db->select_db);
313 else if (strcasecmp ("Query", child->key) == 0)
314 status = udb_query_pick_from_list (child, queries, queries_num,
315 &db->queries, &db->queries_num);
316 else if (strcasecmp ("Host", child->key) == 0)
317 status = cf_util_get_string (child, &db->host);
320 WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
328 /* Check that all necessary options have been given. */
331 if (db->driver == NULL)
333 WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
336 if (db->driver_options_num == 0)
338 WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
339 "This will likely not work.", db->name);
343 } /* while (status == 0) */
345 while ((status == 0) && (db->queries_num > 0))
347 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
348 db->queries_num, sizeof (*db->q_prep_areas));
350 if (db->q_prep_areas == NULL)
352 WARNING ("dbi plugin: malloc failed");
357 for (i = 0; i < db->queries_num; ++i)
360 = udb_query_allocate_preparation_area (db->queries[i]);
362 if (db->q_prep_areas[i] == NULL)
364 WARNING ("dbi plugin: udb_query_allocate_preparation_area failed");
373 /* If all went well, add this database to the global list of databases. */
376 cdbi_database_t **temp;
378 temp = (cdbi_database_t **) realloc (databases,
379 sizeof (*databases) * (databases_num + 1));
382 ERROR ("dbi plugin: realloc failed");
388 databases[databases_num] = db;
395 cdbi_database_free (db);
400 } /* }}} int cdbi_config_add_database */
402 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
406 for (i = 0; i < ci->children_num; i++)
408 oconfig_item_t *child = ci->children + i;
409 if (strcasecmp ("Query", child->key) == 0)
410 udb_query_create (&queries, &queries_num, child,
411 /* callback = */ NULL);
412 else if (strcasecmp ("Database", child->key) == 0)
413 cdbi_config_add_database (child);
416 WARNING ("dbi plugin: Ignoring unknown config option `%s'.", child->key);
418 } /* for (ci->children) */
421 } /* }}} int cdbi_config */
423 /* }}} End of configuration handling functions */
425 static int cdbi_init (void) /* {{{ */
427 static int did_init = 0;
433 if (queries_num == 0)
435 ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
436 "this plugin can't do anything useful, so we will returns an error.");
440 if (databases_num == 0)
442 ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
443 "this plugin can't do anything useful, so we will returns an error.");
447 status = dbi_initialize_r (/* driverdir = */ NULL, &dbi_instance);
450 ERROR ("dbi plugin: cdbi_init: dbi_initialize_r failed with status %i.",
454 else if (status == 0)
456 ERROR ("dbi plugin: `dbi_initialize_r' could not load any drivers. Please "
457 "install at least one `DBD' or check your installation.");
460 DEBUG ("dbi plugin: cdbi_init: dbi_initialize_r reports %i driver%s.",
461 status, (status == 1) ? "" : "s");
464 } /* }}} int cdbi_init */
466 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
467 udb_query_t *q, udb_query_preparation_area_t *prep_area)
469 const char *statement;
473 char **column_values;
477 /* Macro that cleans up dynamically allocated memory and returns the
478 * specified status. */
479 #define BAIL_OUT(status) \
480 if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
481 if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
482 if (res != NULL) { dbi_result_free (res); res = NULL; } \
486 column_values = NULL;
489 statement = udb_query_get_statement (q);
490 assert (statement != NULL);
492 res = dbi_conn_query (db->connection, statement);
496 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
497 "dbi_conn_query failed: %s",
498 db->name, udb_query_get_name (q),
499 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
502 else /* Get the number of columns */
504 unsigned int db_status;
506 db_status = dbi_result_get_numfields (res);
507 if (db_status == DBI_FIELD_ERROR)
510 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
511 "dbi_result_get_numfields failed: %s",
512 db->name, udb_query_get_name (q),
513 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
517 column_num = (size_t) db_status;
518 DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
519 db->name, udb_query_get_name (q), column_num);
522 /* Allocate `column_names' and `column_values'. {{{ */
523 column_names = (char **) calloc (column_num, sizeof (char *));
524 if (column_names == NULL)
526 ERROR ("dbi plugin: malloc failed.");
530 column_names[0] = (char *) calloc (column_num,
531 DATA_MAX_NAME_LEN * sizeof (char));
532 if (column_names[0] == NULL)
534 ERROR ("dbi plugin: malloc failed.");
537 for (i = 1; i < column_num; i++)
538 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
540 column_values = (char **) calloc (column_num, sizeof (char *));
541 if (column_values == NULL)
543 ERROR ("dbi plugin: malloc failed.");
547 column_values[0] = (char *) calloc (column_num,
548 DATA_MAX_NAME_LEN * sizeof (char));
549 if (column_values[0] == NULL)
551 ERROR ("dbi plugin: malloc failed.");
554 for (i = 1; i < column_num; i++)
555 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
558 /* Copy the field names to `column_names' */
559 for (i = 0; i < column_num; i++) /* {{{ */
561 const char *column_name;
563 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
564 if (column_name == NULL)
566 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
567 "Cannot retrieve name of field %zu.",
568 db->name, udb_query_get_name (q), i + 1);
572 sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
573 } /* }}} for (i = 0; i < column_num; i++) */
575 udb_query_prepare_result (q, prep_area, (db->host ? db->host : hostname_g),
576 /* plugin = */ "dbi", db->name,
577 column_names, column_num, /* interval = */ 0);
579 /* 0 = error; 1 = success; */
580 status = dbi_result_first_row (res); /* {{{ */
584 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
585 "dbi_result_first_row failed: %s. Maybe the statement didn't "
587 db->name, udb_query_get_name (q),
588 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
589 udb_query_finish_result (q, prep_area);
593 /* Iterate over all rows and call `udb_query_handle_result' with each list of
598 /* Copy the value of the columns to `column_values' */
599 for (i = 0; i < column_num; i++) /* {{{ */
601 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
602 column_values[i], DATA_MAX_NAME_LEN);
606 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
607 "cdbi_result_get_field (%zu) failed.",
608 db->name, udb_query_get_name (q), i + 1);
612 } /* }}} for (i = 0; i < column_num; i++) */
614 /* If all values were copied successfully, call `udb_query_handle_result'
615 * to dispatch the row to the daemon. */
616 if (status == 0) /* {{{ */
618 status = udb_query_handle_result (q, prep_area, column_values);
621 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
622 "udb_query_handle_result failed.",
623 db->name, udb_query_get_name (q));
627 /* Get the next row from the database. */
628 status = dbi_result_next_row (res); /* {{{ */
631 if (dbi_conn_error (db->connection, NULL) != 0)
634 WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
635 "dbi_result_next_row failed: %s.",
636 db->name, udb_query_get_name (q),
637 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
641 } /* }}} while (42) */
643 /* Tell the db query interface that we're done with this query. */
644 udb_query_finish_result (q, prep_area);
646 /* Clean up and return `status = 0' (success) */
649 } /* }}} int cdbi_read_database_query */
651 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
658 if (db->connection != NULL)
660 status = dbi_conn_ping (db->connection);
661 if (status != 0) /* connection is alive */
664 dbi_conn_close (db->connection);
665 db->connection = NULL;
668 driver = dbi_driver_open_r (db->driver, dbi_instance);
671 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open_r (%s) failed.",
673 INFO ("dbi plugin: Maybe the driver isn't installed? "
674 "Known drivers are:");
675 for (driver = dbi_driver_list_r (NULL, dbi_instance);
677 driver = dbi_driver_list_r (driver, dbi_instance))
679 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
684 connection = dbi_conn_open (driver);
685 if (connection == NULL)
687 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
692 /* Set all the driver options. Because this is a very very very generic
693 * interface, the error handling is kind of long. If an invalid option is
694 * encountered, it will get a list of options understood by the driver and
695 * report that as `INFO'. This way, users hopefully don't have too much
696 * trouble finding out how to configure the plugin correctly.. */
697 for (i = 0; i < db->driver_options_num; i++)
699 if (db->driver_options[i].is_numeric)
701 status = dbi_conn_set_option_numeric (connection,
702 db->driver_options[i].key, db->driver_options[i].value.numeric);
706 ERROR ("dbi plugin: cdbi_connect_database (%s): "
707 "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.",
709 db->driver_options[i].key, db->driver_options[i].value.numeric,
710 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
715 status = dbi_conn_set_option (connection,
716 db->driver_options[i].key, db->driver_options[i].value.string);
720 ERROR ("dbi plugin: cdbi_connect_database (%s): "
721 "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.",
723 db->driver_options[i].key, db->driver_options[i].value.string,
724 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
732 INFO ("dbi plugin: This is a list of all options understood "
733 "by the `%s' driver:", db->driver);
734 for (opt = dbi_conn_get_option_list (connection, NULL);
736 opt = dbi_conn_get_option_list (connection, opt))
738 INFO ("dbi plugin: * %s", opt);
741 dbi_conn_close (connection);
744 } /* for (i = 0; i < db->driver_options_num; i++) */
746 status = dbi_conn_connect (connection);
750 ERROR ("dbi plugin: cdbi_connect_database (%s): "
751 "dbi_conn_connect failed: %s",
752 db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
753 dbi_conn_close (connection);
757 if (db->select_db != NULL)
759 status = dbi_conn_select_db (connection, db->select_db);
763 WARNING ("dbi plugin: cdbi_connect_database (%s): "
764 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
765 db->name, db->select_db,
766 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
767 dbi_conn_close (connection);
772 db->connection = connection;
774 } /* }}} int cdbi_connect_database */
776 static int cdbi_read_database (cdbi_database_t *db) /* {{{ */
782 unsigned int db_version;
784 status = cdbi_connect_database (db);
787 assert (db->connection != NULL);
789 db_version = dbi_conn_get_engine_version (db->connection);
790 /* TODO: Complain if `db_version == 0' */
793 for (i = 0; i < db->queries_num; i++)
795 /* Check if we know the database's version and if so, if this query applies
796 * to that version. */
797 if ((db_version != 0)
798 && (udb_query_check_version (db->queries[i], db_version) == 0))
801 status = cdbi_read_database_query (db,
802 db->queries[i], db->q_prep_areas[i]);
809 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
814 } /* }}} int cdbi_read_database */
816 static int cdbi_read (void) /* {{{ */
822 /* TODO(octo): Starting with libdbi 0.9.0, there is an "instance" argument to
823 * the *_r-functions. We should probably have multiple read callbacks instead
825 for (i = 0; i < databases_num; i++)
827 status = cdbi_read_database (databases[i]);
834 ERROR ("dbi plugin: No database could be read. Will return an error so "
835 "the plugin will be delayed.");
840 } /* }}} int cdbi_read */
842 static int cdbi_shutdown (void) /* {{{ */
846 for (i = 0; i < databases_num; i++)
848 if (databases[i]->connection != NULL)
850 dbi_conn_close (databases[i]->connection);
851 databases[i]->connection = NULL;
853 cdbi_database_free (databases[i]);
858 udb_query_free (queries, queries_num);
863 } /* }}} int cdbi_shutdown */
865 void module_register (void) /* {{{ */
867 plugin_register_complex_config ("dbi", cdbi_config);
868 plugin_register_init ("dbi", cdbi_init);
869 plugin_register_read ("dbi", cdbi_read);
870 plugin_register_shutdown ("dbi", cdbi_shutdown);
871 } /* }}} void module_register */
874 * vim: shiftwidth=2 softtabstop=2 et fdm=marker