3 * Copyright (C) 2008-2013 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"
33 struct cdbi_driver_option_s /* {{{ */
43 typedef struct cdbi_driver_option_s cdbi_driver_option_t; /* }}} */
45 struct cdbi_database_s /* {{{ */
52 cdbi_driver_option_t *driver_options;
53 size_t driver_options_num;
55 udb_query_preparation_area_t **q_prep_areas;
56 udb_query_t **queries;
61 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
66 static udb_query_t **queries = NULL;
67 static size_t queries_num = 0;
68 static cdbi_database_t **databases = NULL;
69 static size_t databases_num = 0;
74 static const char *cdbi_strerror (dbi_conn conn, /* {{{ */
75 char *buffer, size_t buffer_size)
82 sstrncpy (buffer, "connection is NULL", buffer_size);
87 status = dbi_conn_error (conn, &msg);
88 if ((status >= 0) && (msg != NULL))
89 ssnprintf (buffer, buffer_size, "%s (status %i)", msg, status);
91 ssnprintf (buffer, buffer_size, "dbi_conn_error failed with status %i",
95 } /* }}} const char *cdbi_conn_error */
97 static int cdbi_result_get_field (dbi_result res, /* {{{ */
98 unsigned int index, char *buffer, size_t buffer_size)
100 unsigned short src_type;
102 src_type = dbi_result_get_field_type_idx (res, index);
103 if (src_type == DBI_TYPE_ERROR)
105 ERROR ("dbi plugin: cdbi_result_get: "
106 "dbi_result_get_field_type_idx failed.");
110 if (src_type == DBI_TYPE_INTEGER)
114 value = dbi_result_get_longlong_idx (res, index);
115 ssnprintf (buffer, buffer_size, "%lli", value);
117 else if (src_type == DBI_TYPE_DECIMAL)
121 value = dbi_result_get_double_idx (res, index);
122 ssnprintf (buffer, buffer_size, "%63.15g", value);
124 else if (src_type == DBI_TYPE_STRING)
128 value = dbi_result_get_string_idx (res, index);
130 sstrncpy (buffer, "", buffer_size);
131 else if (strcmp ("ERROR", value) == 0)
134 sstrncpy (buffer, value, buffer_size);
136 /* DBI_TYPE_BINARY */
137 /* DBI_TYPE_DATETIME */
140 const char *field_name;
142 field_name = dbi_result_get_field_name (res, index);
143 if (field_name == NULL)
144 field_name = "<unknown>";
146 ERROR ("dbi plugin: Column `%s': Don't know how to handle "
148 field_name, src_type);
153 } /* }}} int cdbi_result_get_field */
155 static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
165 for (i = 0; i < db->driver_options_num; i++)
167 sfree (db->driver_options[i].key);
168 if (!db->driver_options[i].is_numeric)
169 sfree (db->driver_options[i].value.string);
171 sfree (db->driver_options);
173 if (db->q_prep_areas)
174 for (i = 0; i < db->queries_num; ++i)
175 udb_query_delete_preparation_area (db->q_prep_areas[i]);
176 free (db->q_prep_areas);
179 } /* }}} void cdbi_database_free */
181 /* Configuration handling functions {{{
184 * <Query "plugin_instance0">
185 * Statement "SELECT name, value FROM table"
188 * InstancesFrom "name"
194 * <Database "plugin_instance1">
196 * DriverOption "hostname" "localhost"
198 * Query "plugin_instance0"
203 static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */
206 cdbi_driver_option_t *option;
208 if ((ci->values_num != 2)
209 || (ci->values[0].type != OCONFIG_TYPE_STRING)
210 || ((ci->values[1].type != OCONFIG_TYPE_STRING)
211 && (ci->values[1].type != OCONFIG_TYPE_NUMBER)))
213 WARNING ("dbi plugin: The `DriverOption' config option "
214 "needs exactly two arguments.");
218 option = (cdbi_driver_option_t *) realloc (db->driver_options,
219 sizeof (*option) * (db->driver_options_num + 1));
222 ERROR ("dbi plugin: realloc failed");
226 db->driver_options = option;
227 option = db->driver_options + db->driver_options_num;
228 memset (option, 0, sizeof (*option));
230 option->key = strdup (ci->values[0].value.string);
231 if (option->key == NULL)
233 ERROR ("dbi plugin: strdup failed.");
237 if (ci->values[1].type == OCONFIG_TYPE_STRING)
239 option->value.string = strdup (ci->values[1].value.string);
240 if (option->value.string == NULL)
242 ERROR ("dbi plugin: strdup failed.");
249 assert (ci->values[1].type == OCONFIG_TYPE_NUMBER);
250 option->value.numeric = (int) (ci->values[1].value.number + .5);
251 option->is_numeric = 1;
254 db->driver_options_num++;
256 } /* }}} int cdbi_config_add_database_driver_option */
258 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
264 if ((ci->values_num != 1)
265 || (ci->values[0].type != OCONFIG_TYPE_STRING))
267 WARNING ("dbi plugin: The `Database' block "
268 "needs exactly one string argument.");
272 db = (cdbi_database_t *) malloc (sizeof (*db));
275 ERROR ("dbi plugin: malloc failed.");
278 memset (db, 0, sizeof (*db));
280 status = cf_util_get_string (ci, &db->name);
287 /* Fill the `cdbi_database_t' structure.. */
288 for (i = 0; i < ci->children_num; i++)
290 oconfig_item_t *child = ci->children + i;
292 if (strcasecmp ("Driver", child->key) == 0)
293 status = cf_util_get_string (child, &db->driver);
294 else if (strcasecmp ("DriverOption", child->key) == 0)
295 status = cdbi_config_add_database_driver_option (db, child);
296 else if (strcasecmp ("SelectDB", child->key) == 0)
297 status = cf_util_get_string (child, &db->select_db);
298 else if (strcasecmp ("Query", child->key) == 0)
299 status = udb_query_pick_from_list (child, queries, queries_num,
300 &db->queries, &db->queries_num);
301 else if (strcasecmp ("Hostname", child->key) == 0)
302 db->hostname = strdup(child->key);
306 WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
314 /* Check that all necessary options have been given. */
317 if (db->driver == NULL)
319 WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
322 if (db->driver_options_num == 0)
324 WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
325 "This will likely not work.", db->name);
329 } /* while (status == 0) */
331 while ((status == 0) && (db->queries_num > 0))
333 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
334 db->queries_num, sizeof (*db->q_prep_areas));
336 if (db->q_prep_areas == NULL)
338 WARNING ("dbi plugin: malloc failed");
343 for (i = 0; i < db->queries_num; ++i)
346 = udb_query_allocate_preparation_area (db->queries[i]);
348 if (db->q_prep_areas[i] == NULL)
350 WARNING ("dbi plugin: udb_query_allocate_preparation_area failed");
359 /* If all went well, add this database to the global list of databases. */
362 cdbi_database_t **temp;
364 temp = (cdbi_database_t **) realloc (databases,
365 sizeof (*databases) * (databases_num + 1));
368 ERROR ("dbi plugin: realloc failed");
374 databases[databases_num] = db;
381 cdbi_database_free (db);
386 } /* }}} int cdbi_config_add_database */
388 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
392 for (i = 0; i < ci->children_num; i++)
394 oconfig_item_t *child = ci->children + i;
395 if (strcasecmp ("Query", child->key) == 0)
396 udb_query_create (&queries, &queries_num, child,
397 /* callback = */ NULL);
398 else if (strcasecmp ("Database", child->key) == 0)
399 cdbi_config_add_database (child);
402 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
404 } /* for (ci->children) */
407 } /* }}} int cdbi_config */
409 /* }}} End of configuration handling functions */
411 static int cdbi_init (void) /* {{{ */
413 static int did_init = 0;
419 if (queries_num == 0)
421 ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
422 "this plugin can't do anything useful, so we will returns an error.");
426 if (databases_num == 0)
428 ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
429 "this plugin can't do anything useful, so we will returns an error.");
433 status = dbi_initialize (NULL);
436 ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.",
440 else if (status == 0)
442 ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please "
443 "install at least one `DBD' or check your installation.");
446 DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.",
447 status, (status == 1) ? "" : "s");
450 } /* }}} int cdbi_init */
452 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
453 udb_query_t *q, udb_query_preparation_area_t *prep_area)
455 const char *statement;
459 char **column_values;
463 /* Macro that cleans up dynamically allocated memory and returns the
464 * specified status. */
465 #define BAIL_OUT(status) \
466 if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
467 if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
468 if (res != NULL) { dbi_result_free (res); res = NULL; } \
472 column_values = NULL;
475 statement = udb_query_get_statement (q);
476 assert (statement != NULL);
478 res = dbi_conn_query (db->connection, statement);
482 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
483 "dbi_conn_query failed: %s",
484 db->name, udb_query_get_name (q),
485 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
488 else /* Get the number of columns */
490 unsigned int db_status;
492 db_status = dbi_result_get_numfields (res);
493 if (db_status == DBI_FIELD_ERROR)
496 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
497 "dbi_result_get_numfields failed: %s",
498 db->name, udb_query_get_name (q),
499 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
503 column_num = (size_t) db_status;
504 DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
505 db->name, udb_query_get_name (q), column_num);
508 /* Allocate `column_names' and `column_values'. {{{ */
509 column_names = (char **) calloc (column_num, sizeof (char *));
510 if (column_names == NULL)
512 ERROR ("dbi plugin: malloc failed.");
516 column_names[0] = (char *) calloc (column_num,
517 DATA_MAX_NAME_LEN * sizeof (char));
518 if (column_names[0] == NULL)
520 ERROR ("dbi plugin: malloc failed.");
523 for (i = 1; i < column_num; i++)
524 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
526 column_values = (char **) calloc (column_num, sizeof (char *));
527 if (column_values == NULL)
529 ERROR ("dbi plugin: malloc failed.");
533 column_values[0] = (char *) calloc (column_num,
534 DATA_MAX_NAME_LEN * sizeof (char));
535 if (column_values[0] == NULL)
537 ERROR ("dbi plugin: malloc failed.");
540 for (i = 1; i < column_num; i++)
541 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
544 /* Copy the field names to `column_names' */
545 for (i = 0; i < column_num; i++) /* {{{ */
547 const char *column_name;
549 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
550 if (column_name == NULL)
552 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
553 "Cannot retrieve name of field %zu.",
554 db->name, udb_query_get_name (q), i + 1);
558 sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
559 } /* }}} for (i = 0; i < column_num; i++) */
561 udb_query_prepare_result (q, prep_area, hostname_g,
562 /* plugin = */ "dbi", db->name,
563 column_names, column_num, /* interval = */ 0);
565 /* 0 = error; 1 = success; */
566 status = dbi_result_first_row (res); /* {{{ */
570 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
571 "dbi_result_first_row failed: %s. Maybe the statement didn't "
573 db->name, udb_query_get_name (q),
574 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
575 udb_query_finish_result (q, prep_area);
579 /* Iterate over all rows and call `udb_query_handle_result' with each list of
584 /* Copy the value of the columns to `column_values' */
585 for (i = 0; i < column_num; i++) /* {{{ */
587 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
588 column_values[i], DATA_MAX_NAME_LEN);
592 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
593 "cdbi_result_get_field (%zu) failed.",
594 db->name, udb_query_get_name (q), i + 1);
598 } /* }}} for (i = 0; i < column_num; i++) */
600 /* If all values were copied successfully, call `udb_query_handle_result'
601 * to dispatch the row to the daemon. */
602 if (status == 0) /* {{{ */
604 status = udb_query_handle_result (q, prep_area, column_values);
607 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
608 "udb_query_handle_result failed.",
609 db->name, udb_query_get_name (q));
613 /* Get the next row from the database. */
614 status = dbi_result_next_row (res); /* {{{ */
617 if (dbi_conn_error (db->connection, NULL) != 0)
620 WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
621 "dbi_result_next_row failed: %s.",
622 db->name, udb_query_get_name (q),
623 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
627 } /* }}} while (42) */
629 /* Tell the db query interface that we're done with this query. */
630 udb_query_finish_result (q, prep_area);
632 /* Clean up and return `status = 0' (success) */
635 } /* }}} int cdbi_read_database_query */
637 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
644 if (db->connection != NULL)
646 status = dbi_conn_ping (db->connection);
647 if (status != 0) /* connection is alive */
650 dbi_conn_close (db->connection);
651 db->connection = NULL;
654 driver = dbi_driver_open (db->driver);
657 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.",
659 INFO ("dbi plugin: Maybe the driver isn't installed? "
660 "Known drivers are:");
661 for (driver = dbi_driver_list (NULL);
663 driver = dbi_driver_list (driver))
665 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
670 connection = dbi_conn_open (driver);
671 if (connection == NULL)
673 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
678 /* Set all the driver options. Because this is a very very very generic
679 * interface, the error handling is kind of long. If an invalid option is
680 * encountered, it will get a list of options understood by the driver and
681 * report that as `INFO'. This way, users hopefully don't have too much
682 * trouble finding out how to configure the plugin correctly.. */
683 for (i = 0; i < db->driver_options_num; i++)
685 if (db->driver_options[i].is_numeric)
687 status = dbi_conn_set_option_numeric (connection,
688 db->driver_options[i].key, db->driver_options[i].value.numeric);
692 ERROR ("dbi plugin: cdbi_connect_database (%s): "
693 "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.",
695 db->driver_options[i].key, db->driver_options[i].value.numeric,
696 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
701 status = dbi_conn_set_option (connection,
702 db->driver_options[i].key, db->driver_options[i].value.string);
706 ERROR ("dbi plugin: cdbi_connect_database (%s): "
707 "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.",
709 db->driver_options[i].key, db->driver_options[i].value.string,
710 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
718 INFO ("dbi plugin: This is a list of all options understood "
719 "by the `%s' driver:", db->driver);
720 for (opt = dbi_conn_get_option_list (connection, NULL);
722 opt = dbi_conn_get_option_list (connection, opt))
724 INFO ("dbi plugin: * %s", opt);
727 dbi_conn_close (connection);
730 } /* for (i = 0; i < db->driver_options_num; i++) */
732 status = dbi_conn_connect (connection);
736 ERROR ("dbi plugin: cdbi_connect_database (%s): "
737 "dbi_conn_connect failed: %s",
738 db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
739 dbi_conn_close (connection);
743 if (db->select_db != NULL)
745 status = dbi_conn_select_db (connection, db->select_db);
749 WARNING ("dbi plugin: cdbi_connect_database (%s): "
750 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
751 db->name, db->select_db,
752 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
753 dbi_conn_close (connection);
758 db->connection = connection;
760 } /* }}} int cdbi_connect_database */
762 static int cdbi_read_database (cdbi_database_t *db) /* {{{ */
768 unsigned int db_version;
770 status = cdbi_connect_database (db);
773 assert (db->connection != NULL);
775 db_version = dbi_conn_get_engine_version (db->connection);
776 /* TODO: Complain if `db_version == 0' */
779 for (i = 0; i < db->queries_num; i++)
781 /* Check if we know the database's version and if so, if this query applies
782 * to that version. */
783 if ((db_version != 0)
784 && (udb_query_check_version (db->queries[i], db_version) == 0))
787 status = cdbi_read_database_query (db,
788 db->queries[i], db->q_prep_areas[i]);
795 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
800 } /* }}} int cdbi_read_database */
802 static int cdbi_read (void) /* {{{ */
808 for (i = 0; i < databases_num; i++)
810 status = cdbi_read_database (databases[i]);
817 ERROR ("dbi plugin: No database could be read. Will return an error so "
818 "the plugin will be delayed.");
823 } /* }}} int cdbi_read */
825 static int cdbi_shutdown (void) /* {{{ */
829 for (i = 0; i < databases_num; i++)
831 if (databases[i]->connection != NULL)
833 dbi_conn_close (databases[i]->connection);
834 databases[i]->connection = NULL;
836 cdbi_database_free (databases[i]);
841 udb_query_free (queries, queries_num);
846 } /* }}} int cdbi_shutdown */
848 void module_register (void) /* {{{ */
850 plugin_register_complex_config ("dbi", cdbi_config);
851 plugin_register_init ("dbi", cdbi_init);
852 plugin_register_read ("dbi", cdbi_read);
853 plugin_register_shutdown ("dbi", cdbi_shutdown);
854 } /* }}} void module_register */
857 * vim: shiftwidth=2 softtabstop=2 et fdm=marker