3 * Copyright (C) 2008-2015 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
31 #include "utils_db_query.h"
35 /* libdbi 0.9.0 introduced a new thread-safe interface and marked the old
36 * functions "deprecated". These macros convert the new functions to their old
37 * counterparts for backwards compatibility. */
38 #if !defined(LIBDBI_VERSION) || (LIBDBI_VERSION < 900)
39 #define HAVE_LEGACY_LIBDBI 1
40 #define dbi_initialize_r(a, inst) dbi_initialize(a)
41 #define dbi_shutdown_r(inst) dbi_shutdown()
42 #define dbi_set_verbosity_r(a, inst) dbi_set_verbosity(a)
43 #define dbi_driver_list_r(a, inst) dbi_driver_list(a)
44 #define dbi_driver_open_r(a, inst) dbi_driver_open(a)
50 struct cdbi_driver_option_s /* {{{ */
59 typedef struct cdbi_driver_option_s cdbi_driver_option_t; /* }}} */
61 struct cdbi_database_s /* {{{ */
71 cdbi_driver_option_t *driver_options;
72 size_t driver_options_num;
74 udb_query_preparation_area_t **q_prep_areas;
75 udb_query_t **queries;
80 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
85 #if !defined(HAVE_LEGACY_LIBDBI) || !HAVE_LEGACY_LIBDBI
86 static dbi_inst dbi_instance = 0;
88 static udb_query_t **queries = NULL;
89 static size_t queries_num = 0;
90 static cdbi_database_t **databases = NULL;
91 static size_t databases_num = 0;
93 static int cdbi_read_database(user_data_t *ud);
98 static const char *cdbi_strerror(dbi_conn conn, /* {{{ */
99 char *buffer, size_t buffer_size) {
104 sstrncpy(buffer, "connection is NULL", buffer_size);
109 status = dbi_conn_error(conn, &msg);
110 if ((status >= 0) && (msg != NULL))
111 snprintf(buffer, buffer_size, "%s (status %i)", msg, status);
113 snprintf(buffer, buffer_size, "dbi_conn_error failed with status %i",
117 } /* }}} const char *cdbi_conn_error */
119 static int cdbi_result_get_field(dbi_result res, /* {{{ */
120 unsigned int index, char *buffer,
121 size_t buffer_size) {
122 unsigned short src_type;
124 src_type = dbi_result_get_field_type_idx(res, index);
125 if (src_type == DBI_TYPE_ERROR) {
126 ERROR("dbi plugin: cdbi_result_get: "
127 "dbi_result_get_field_type_idx failed.");
131 if (src_type == DBI_TYPE_INTEGER) {
134 value = dbi_result_get_longlong_idx(res, index);
135 snprintf(buffer, buffer_size, "%lli", value);
136 } else if (src_type == DBI_TYPE_DECIMAL) {
139 value = dbi_result_get_double_idx(res, index);
140 snprintf(buffer, buffer_size, "%63.15g", value);
141 } else if (src_type == DBI_TYPE_STRING) {
144 value = dbi_result_get_string_idx(res, index);
146 sstrncpy(buffer, "", buffer_size);
147 else if (strcmp("ERROR", value) == 0)
150 sstrncpy(buffer, value, buffer_size);
152 /* DBI_TYPE_BINARY */
153 /* 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) /* {{{ */
176 sfree(db->select_db);
177 sfree(db->plugin_name);
181 for (size_t 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 (size_t i = 0; i < db->queries_num; ++i)
190 udb_query_delete_preparation_area(db->q_prep_areas[i]);
191 sfree(db->q_prep_areas);
192 /* N.B.: db->queries references objects "owned" by the global queries
193 * variable. Free the array here, but not the content. */
197 } /* }}} void cdbi_database_free */
199 /* Configuration handling functions {{{
202 * <Query "plugin_instance0">
203 * Statement "SELECT name, value FROM table"
206 * InstancesFrom "name"
212 * <Database "plugin_instance1">
215 * DriverOption "hostname" "localhost"
217 * Query "plugin_instance0"
222 static int cdbi_config_add_database_driver_option(cdbi_database_t *db, /* {{{ */
223 oconfig_item_t *ci) {
224 cdbi_driver_option_t *option;
226 if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
227 ((ci->values[1].type != OCONFIG_TYPE_STRING) &&
228 (ci->values[1].type != OCONFIG_TYPE_NUMBER))) {
229 WARNING("dbi plugin: The `DriverOption' config option "
230 "needs exactly two arguments.");
234 option = realloc(db->driver_options,
235 sizeof(*option) * (db->driver_options_num + 1));
236 if (option == NULL) {
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) {
247 ERROR("dbi plugin: strdup failed.");
251 if (ci->values[1].type == OCONFIG_TYPE_STRING) {
252 option->value.string = strdup(ci->values[1].value.string);
253 if (option->value.string == NULL) {
254 ERROR("dbi plugin: strdup failed.");
259 assert(ci->values[1].type == OCONFIG_TYPE_NUMBER);
260 option->value.numeric = (int)(ci->values[1].value.number + .5);
261 option->is_numeric = 1;
264 db->driver_options_num++;
266 } /* }}} int cdbi_config_add_database_driver_option */
268 static int cdbi_config_add_database(oconfig_item_t *ci) /* {{{ */
273 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
274 WARNING("dbi plugin: The `Database' block "
275 "needs exactly one string argument.");
279 db = calloc(1, sizeof(*db));
281 ERROR("dbi plugin: calloc failed.");
285 status = cf_util_get_string(ci, &db->name);
291 /* Fill the `cdbi_database_t' structure.. */
292 for (int i = 0; i < ci->children_num; i++) {
293 oconfig_item_t *child = ci->children + i;
295 if (strcasecmp("Driver", child->key) == 0)
296 status = cf_util_get_string(child, &db->driver);
297 else if (strcasecmp("DriverOption", child->key) == 0)
298 status = cdbi_config_add_database_driver_option(db, child);
299 else if (strcasecmp("SelectDB", child->key) == 0)
300 status = cf_util_get_string(child, &db->select_db);
301 else if (strcasecmp("Query", child->key) == 0)
302 status = udb_query_pick_from_list(child, queries, queries_num,
303 &db->queries, &db->queries_num);
304 else if (strcasecmp("Host", child->key) == 0)
305 status = cf_util_get_string(child, &db->host);
306 else if (strcasecmp("Interval", child->key) == 0)
307 status = cf_util_get_cdtime(child, &db->interval);
308 else if (strcasecmp("Plugin", child->key) == 0)
309 status = cf_util_get_string(child, &db->plugin_name);
311 WARNING("dbi plugin: Option `%s' not allowed here.", child->key);
319 /* Check that all necessary options have been given. */
320 while (status == 0) {
321 if (db->driver == NULL) {
322 WARNING("dbi plugin: `Driver' not given for database `%s'", db->name);
325 if (db->driver_options_num == 0) {
326 WARNING("dbi plugin: No `DriverOption' given for database `%s'. "
327 "This will likely not work.",
332 } /* while (status == 0) */
334 while ((status == 0) && (db->queries_num > 0)) {
335 db->q_prep_areas = calloc(db->queries_num, sizeof(*db->q_prep_areas));
336 if (db->q_prep_areas == NULL) {
337 WARNING("dbi plugin: calloc failed");
342 for (size_t i = 0; i < db->queries_num; ++i) {
343 db->q_prep_areas[i] = udb_query_allocate_preparation_area(db->queries[i]);
345 if (db->q_prep_areas[i] == NULL) {
346 WARNING("dbi plugin: udb_query_allocate_preparation_area failed");
355 /* If all went well, add this database to the global list of databases. */
357 cdbi_database_t **temp;
359 temp = realloc(databases, sizeof(*databases) * (databases_num + 1));
361 ERROR("dbi plugin: realloc failed");
365 databases[databases_num] = db;
368 char *name = ssnprintf_alloc("dbi:%s", db->name);
369 plugin_register_complex_read(
371 /* name = */ name ? name : db->name,
372 /* callback = */ cdbi_read_database,
373 /* interval = */ (db->interval > 0) ? db->interval : 0,
382 cdbi_database_free(db);
387 } /* }}} int cdbi_config_add_database */
389 static int cdbi_config(oconfig_item_t *ci) /* {{{ */
391 for (int i = 0; i < ci->children_num; i++) {
392 oconfig_item_t *child = ci->children + i;
393 if (strcasecmp("Query", child->key) == 0)
394 udb_query_create(&queries, &queries_num, child,
395 /* callback = */ NULL);
396 else if (strcasecmp("Database", child->key) == 0)
397 cdbi_config_add_database(child);
399 WARNING("dbi plugin: Ignoring unknown config option `%s'.", child->key);
401 } /* for (ci->children) */
404 } /* }}} int cdbi_config */
406 /* }}} End of configuration handling functions */
408 static int cdbi_init(void) /* {{{ */
410 static int did_init = 0;
416 if (queries_num == 0) {
417 ERROR("dbi plugin: No <Query> blocks have been found. Without them, "
418 "this plugin can't do anything useful, so we will return an error.");
422 if (databases_num == 0) {
423 ERROR("dbi plugin: No <Database> blocks have been found. Without them, "
424 "this plugin can't do anything useful, so we will return an error.");
428 status = dbi_initialize_r(/* driverdir = */ NULL, &dbi_instance);
430 ERROR("dbi plugin: cdbi_init: dbi_initialize_r failed with status %i.",
433 } else if (status == 0) {
434 ERROR("dbi plugin: `dbi_initialize_r' could not load any drivers. Please "
435 "install at least one `DBD' or check your installation.");
438 DEBUG("dbi plugin: cdbi_init: dbi_initialize_r reports %i driver%s.", status,
439 (status == 1) ? "" : "s");
442 } /* }}} int cdbi_init */
444 static int cdbi_read_database_query(cdbi_database_t *db, /* {{{ */
446 udb_query_preparation_area_t *prep_area) {
447 const char *statement;
451 char **column_values;
454 /* Macro that cleans up dynamically allocated memory and returns the
455 * specified status. */
456 #define BAIL_OUT(status) \
457 if (column_names != NULL) { \
458 sfree(column_names[0]); \
459 sfree(column_names); \
461 if (column_values != NULL) { \
462 sfree(column_values[0]); \
463 sfree(column_values); \
466 dbi_result_free(res); \
472 column_values = NULL;
474 statement = udb_query_get_statement(q);
475 assert(statement != NULL);
477 res = dbi_conn_query(db->connection, statement);
480 ERROR("dbi plugin: cdbi_read_database_query (%s, %s): "
481 "dbi_conn_query failed: %s",
482 db->name, udb_query_get_name(q),
483 cdbi_strerror(db->connection, errbuf, sizeof(errbuf)));
485 } else /* Get the number of columns */
487 unsigned int db_status;
489 db_status = dbi_result_get_numfields(res);
490 if (db_status == DBI_FIELD_ERROR) {
492 ERROR("dbi plugin: cdbi_read_database_query (%s, %s): "
493 "dbi_result_get_numfields failed: %s",
494 db->name, udb_query_get_name(q),
495 cdbi_strerror(db->connection, errbuf, sizeof(errbuf)));
499 column_num = (size_t)db_status;
500 DEBUG("cdbi_read_database_query (%s, %s): There are %" PRIsz " columns.",
501 db->name, udb_query_get_name(q), column_num);
504 /* Allocate `column_names' and `column_values'. {{{ */
505 column_names = calloc(column_num, sizeof(*column_names));
506 if (column_names == NULL) {
507 ERROR("dbi plugin: calloc failed.");
511 column_names[0] = calloc(column_num, DATA_MAX_NAME_LEN);
512 if (column_names[0] == NULL) {
513 ERROR("dbi plugin: calloc failed.");
516 for (size_t i = 1; i < column_num; i++)
517 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
519 column_values = calloc(column_num, sizeof(*column_values));
520 if (column_values == NULL) {
521 ERROR("dbi plugin: calloc failed.");
525 column_values[0] = calloc(column_num, DATA_MAX_NAME_LEN);
526 if (column_values[0] == NULL) {
527 ERROR("dbi plugin: calloc failed.");
530 for (size_t i = 1; i < column_num; i++)
531 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
534 /* Copy the field names to `column_names' */
535 for (size_t i = 0; i < column_num; i++) /* {{{ */
537 const char *column_name;
539 column_name = dbi_result_get_field_name(res, (unsigned int)(i + 1));
540 if (column_name == NULL) {
541 ERROR("dbi plugin: cdbi_read_database_query (%s, %s): "
542 "Cannot retrieve name of field %" PRIsz ".",
543 db->name, udb_query_get_name(q), i + 1);
547 sstrncpy(column_names[i], column_name, DATA_MAX_NAME_LEN);
548 } /* }}} for (i = 0; i < column_num; i++) */
550 udb_query_prepare_result(
551 q, prep_area, (db->host ? db->host : hostname_g),
552 /* plugin = */ (db->plugin_name != NULL) ? db->plugin_name : "dbi",
553 db->name, column_names, column_num,
554 /* interval = */ (db->interval > 0) ? db->interval : 0);
556 /* 0 = error; 1 = success; */
557 status = dbi_result_first_row(res); /* {{{ */
560 ERROR("dbi plugin: cdbi_read_database_query (%s, %s): "
561 "dbi_result_first_row failed: %s. Maybe the statement didn't "
563 db->name, udb_query_get_name(q),
564 cdbi_strerror(db->connection, errbuf, sizeof(errbuf)));
565 udb_query_finish_result(q, prep_area);
569 /* Iterate over all rows and call `udb_query_handle_result' with each list of
574 /* Copy the value of the columns to `column_values' */
575 for (size_t i = 0; i < column_num; i++) /* {{{ */
577 status = cdbi_result_get_field(res, (unsigned int)(i + 1),
578 column_values[i], DATA_MAX_NAME_LEN);
581 ERROR("dbi plugin: cdbi_read_database_query (%s, %s): "
582 "cdbi_result_get_field (%" PRIsz ") failed.",
583 db->name, udb_query_get_name(q), i + 1);
587 } /* }}} for (i = 0; i < column_num; i++) */
589 /* If all values were copied successfully, call `udb_query_handle_result'
590 * to dispatch the row to the daemon. */
591 if (status == 0) /* {{{ */
593 status = udb_query_handle_result(q, prep_area, column_values);
595 ERROR("dbi plugin: cdbi_read_database_query (%s, %s): "
596 "udb_query_handle_result failed.",
597 db->name, udb_query_get_name(q));
601 /* Get the next row from the database. */
602 status = dbi_result_next_row(res); /* {{{ */
604 if (dbi_conn_error(db->connection, NULL) != 0) {
606 WARNING("dbi plugin: cdbi_read_database_query (%s, %s): "
607 "dbi_result_next_row failed: %s.",
608 db->name, udb_query_get_name(q),
609 cdbi_strerror(db->connection, errbuf, sizeof(errbuf)));
613 } /* }}} while (42) */
615 /* Tell the db query interface that we're done with this query. */
616 udb_query_finish_result(q, prep_area);
618 /* Clean up and return `status = 0' (success) */
621 } /* }}} int cdbi_read_database_query */
623 static int cdbi_connect_database(cdbi_database_t *db) /* {{{ */
629 if (db->connection != NULL) {
630 status = dbi_conn_ping(db->connection);
631 if (status != 0) /* connection is alive */
634 dbi_conn_close(db->connection);
635 db->connection = NULL;
638 driver = dbi_driver_open_r(db->driver, dbi_instance);
639 if (driver == NULL) {
640 ERROR("dbi plugin: cdbi_connect_database: dbi_driver_open_r (%s) failed.",
642 INFO("dbi plugin: Maybe the driver isn't installed? "
643 "Known drivers are:");
644 for (driver = dbi_driver_list_r(NULL, dbi_instance); driver != NULL;
645 driver = dbi_driver_list_r(driver, dbi_instance)) {
646 INFO("dbi plugin: * %s", dbi_driver_get_name(driver));
651 connection = dbi_conn_open(driver);
652 if (connection == NULL) {
653 ERROR("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
658 /* Set all the driver options. Because this is a very very very generic
659 * interface, the error handling is kind of long. If an invalid option is
660 * encountered, it will get a list of options understood by the driver and
661 * report that as `INFO'. This way, users hopefully don't have too much
662 * trouble finding out how to configure the plugin correctly.. */
663 for (size_t i = 0; i < db->driver_options_num; i++) {
664 if (db->driver_options[i].is_numeric) {
666 dbi_conn_set_option_numeric(connection, db->driver_options[i].key,
667 db->driver_options[i].value.numeric);
670 ERROR("dbi plugin: cdbi_connect_database (%s): "
671 "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.",
672 db->name, db->driver_options[i].key,
673 db->driver_options[i].value.numeric,
674 cdbi_strerror(connection, errbuf, sizeof(errbuf)));
677 status = dbi_conn_set_option(connection, db->driver_options[i].key,
678 db->driver_options[i].value.string);
681 ERROR("dbi plugin: cdbi_connect_database (%s): "
682 "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.",
683 db->name, db->driver_options[i].key,
684 db->driver_options[i].value.string,
685 cdbi_strerror(connection, errbuf, sizeof(errbuf)));
690 INFO("dbi plugin: This is a list of all options understood "
691 "by the `%s' driver:",
693 for (const char *opt = dbi_conn_get_option_list(connection, NULL);
694 opt != NULL; opt = dbi_conn_get_option_list(connection, opt)) {
695 INFO("dbi plugin: * %s", opt);
698 dbi_conn_close(connection);
701 } /* for (i = 0; i < db->driver_options_num; i++) */
703 status = dbi_conn_connect(connection);
706 ERROR("dbi plugin: cdbi_connect_database (%s): "
707 "dbi_conn_connect failed: %s",
708 db->name, cdbi_strerror(connection, errbuf, sizeof(errbuf)));
709 dbi_conn_close(connection);
713 if (db->select_db != NULL) {
714 status = dbi_conn_select_db(connection, db->select_db);
718 "dbi plugin: cdbi_connect_database (%s): "
719 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
720 db->name, db->select_db,
721 cdbi_strerror(connection, errbuf, sizeof(errbuf)));
722 dbi_conn_close(connection);
727 db->connection = connection;
729 } /* }}} int cdbi_connect_database */
731 static int cdbi_read_database(user_data_t *ud) /* {{{ */
733 cdbi_database_t *db = (cdbi_database_t *)ud->data;
737 unsigned int db_version;
739 status = cdbi_connect_database(db);
742 assert(db->connection != NULL);
744 db_version = dbi_conn_get_engine_version(db->connection);
745 /* TODO: Complain if `db_version == 0' */
748 for (size_t i = 0; i < db->queries_num; i++) {
749 /* Check if we know the database's version and if so, if this query applies
750 * to that version. */
751 if ((db_version != 0) &&
752 (udb_query_check_version(db->queries[i], db_version) == 0))
755 status = cdbi_read_database_query(db, db->queries[i], db->q_prep_areas[i]);
761 ERROR("dbi plugin: All queries failed for database `%s'.", db->name);
766 } /* }}} int cdbi_read_database */
768 static int cdbi_shutdown(void) /* {{{ */
770 for (size_t i = 0; i < databases_num; i++) {
771 if (databases[i]->connection != NULL) {
772 dbi_conn_close(databases[i]->connection);
773 databases[i]->connection = NULL;
775 cdbi_database_free(databases[i]);
780 udb_query_free(queries, queries_num);
785 } /* }}} int cdbi_shutdown */
787 void module_register(void) /* {{{ */
789 plugin_register_complex_config("dbi", cdbi_config);
790 plugin_register_init("dbi", cdbi_init);
791 plugin_register_shutdown("dbi", cdbi_shutdown);
792 } /* }}} void module_register */