3 * Copyright (C) 2008,2009 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 verplant.org>
25 #include "configfile.h"
26 #include "utils_db_query.h"
33 struct cdbi_driver_option_s /* {{{ */
38 typedef struct cdbi_driver_option_s cdbi_driver_option_t; /* }}} */
40 struct cdbi_database_s /* {{{ */
46 cdbi_driver_option_t *driver_options;
47 size_t driver_options_num;
49 udb_query_t **queries;
54 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
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;
67 static const char *cdbi_strerror (dbi_conn conn, /* {{{ */
68 char *buffer, size_t buffer_size)
75 sstrncpy (buffer, "connection is NULL", buffer_size);
80 status = dbi_conn_error (conn, &msg);
81 if ((status >= 0) && (msg != NULL))
82 ssnprintf (buffer, buffer_size, "%s (status %i)", msg, status);
84 ssnprintf (buffer, buffer_size, "dbi_conn_error failed with status %i",
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)
93 unsigned short src_type;
95 src_type = dbi_result_get_field_type_idx (res, index);
96 if (src_type == DBI_TYPE_ERROR)
98 ERROR ("dbi plugin: cdbi_result_get: "
99 "dbi_result_get_field_type_idx failed.");
103 if (src_type == DBI_TYPE_INTEGER)
107 value = dbi_result_get_longlong_idx (res, index);
108 ssnprintf (buffer, buffer_size, "%lli", value);
110 else if (src_type == DBI_TYPE_DECIMAL)
114 value = dbi_result_get_double_idx (res, index);
115 ssnprintf (buffer, buffer_size, "%63.15g", value);
117 else if (src_type == DBI_TYPE_STRING)
121 value = dbi_result_get_string_idx (res, index);
123 sstrncpy (buffer, "", buffer_size);
124 else if (strcmp ("ERROR", value) == 0)
127 sstrncpy (buffer, value, buffer_size);
131 ERROR ("dbi plugin: cdbi_result_get: Don't know how to handle "
132 "source type %hu.", src_type);
137 } /* }}} int cdbi_result_get_field */
139 static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
149 for (i = 0; i < db->driver_options_num; i++)
151 sfree (db->driver_options[i].key);
152 sfree (db->driver_options[i].value);
154 sfree (db->driver_options);
157 } /* }}} void cdbi_database_free */
159 /* Configuration handling functions {{{
162 * <Query "plugin_instance0">
163 * Statement "SELECT name, value FROM table"
166 * InstancesFrom "name"
172 * <Database "plugin_instance1">
174 * DriverOption "hostname" "localhost"
176 * Query "plugin_instance0"
181 static int cdbi_config_set_string (char **ret_string, /* {{{ */
186 if ((ci->values_num != 1)
187 || (ci->values[0].type != OCONFIG_TYPE_STRING))
189 WARNING ("dbi plugin: The `%s' config option "
190 "needs exactly one string argument.", ci->key);
194 string = strdup (ci->values[0].value.string);
197 ERROR ("dbi plugin: strdup failed.");
201 if (*ret_string != NULL)
203 *ret_string = string;
206 } /* }}} int cdbi_config_set_string */
208 static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */
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))
217 WARNING ("dbi plugin: The `DriverOption' config option "
218 "needs exactly two string arguments.");
222 option = (cdbi_driver_option_t *) realloc (db->driver_options,
223 sizeof (*option) * (db->driver_options_num + 1));
226 ERROR ("dbi plugin: realloc failed");
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)
236 ERROR ("dbi plugin: strdup failed.");
240 option->value = strdup (ci->values[1].value.string);
241 if (option->value == NULL)
243 ERROR ("dbi plugin: strdup failed.");
248 db->driver_options_num++;
250 } /* }}} int cdbi_config_add_database_driver_option */
252 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
258 if ((ci->values_num != 1)
259 || (ci->values[0].type != OCONFIG_TYPE_STRING))
261 WARNING ("dbi plugin: The `Database' block "
262 "needs exactly one string argument.");
266 db = (cdbi_database_t *) malloc (sizeof (*db));
269 ERROR ("dbi plugin: malloc failed.");
272 memset (db, 0, sizeof (*db));
274 status = cdbi_config_set_string (&db->name, ci);
281 /* Fill the `cdbi_database_t' structure.. */
282 for (i = 0; i < ci->children_num; i++)
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);
297 WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
305 /* Check that all necessary options have been given. */
308 if (db->driver == NULL)
310 WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
313 if (db->driver_options_num == 0)
315 WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
316 "This will likely not work.", db->name);
320 } /* while (status == 0) */
322 /* If all went well, add this database to the global list of databases. */
325 cdbi_database_t **temp;
327 temp = (cdbi_database_t **) realloc (databases,
328 sizeof (*databases) * (databases_num + 1));
331 ERROR ("dbi plugin: realloc failed");
337 databases[databases_num] = db;
344 cdbi_database_free (db);
349 } /* }}} int cdbi_config_add_database */
351 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
355 for (i = 0; i < ci->children_num; i++)
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);
364 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
366 } /* for (ci->children) */
369 } /* }}} int cdbi_config */
371 /* }}} End of configuration handling functions */
373 static int cdbi_init (void) /* {{{ */
375 static int did_init = 0;
381 if (queries_num == 0)
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.");
388 if (databases_num == 0)
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.");
395 status = dbi_initialize (NULL);
398 ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.",
402 else if (status == 0)
404 ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please "
405 "install at least one `DBD' or check your installation.");
408 DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.",
409 status, (status == 1) ? "" : "s");
412 } /* }}} int cdbi_init */
414 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
417 const char *statement;
421 char **column_values;
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; } \
434 column_values = NULL;
437 statement = udb_query_get_statement (q);
438 assert (statement != NULL);
440 res = dbi_conn_query (db->connection, statement);
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)));
450 else /* Get the number of columns */
452 unsigned int db_status;
454 db_status = dbi_result_get_numfields (res);
455 if (db_status == DBI_FIELD_ERROR)
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)));
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);
470 /* Allocate `column_names' and `column_values'. {{{ */
471 column_names = (char **) calloc (column_num, sizeof (char *));
472 if (column_names == NULL)
474 ERROR ("dbi plugin: malloc failed.");
478 column_names[0] = (char *) calloc (column_num,
479 DATA_MAX_NAME_LEN * sizeof (char));
480 if (column_names[0] == NULL)
482 ERROR ("dbi plugin: malloc failed.");
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)
491 ERROR ("dbi plugin: malloc failed.");
495 column_values[0] = (char *) calloc (column_num,
496 DATA_MAX_NAME_LEN * sizeof (char));
497 if (column_values[0] == NULL)
499 ERROR ("dbi plugin: malloc failed.");
502 for (i = 1; i < column_num; i++)
503 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
506 /* Copy the field names to `column_names' */
507 for (i = 0; i < column_num; i++) /* {{{ */
509 const char *column_name;
511 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
512 if (column_name == NULL)
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);
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); /* {{{ */
531 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
532 "dbi_result_first_row failed: %s. Maybe the statement didn't "
534 db->name, udb_query_get_name (q),
535 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
536 udb_query_finish_result (q);
540 /* Iterate over all rows and call `udb_query_handle_result' with each list of
545 /* Copy the value of the columns to `column_values' */
546 for (i = 0; i < column_num; i++) /* {{{ */
548 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
549 column_values[i], DATA_MAX_NAME_LEN);
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);
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) /* {{{ */
565 status = udb_query_handle_result (q, column_values);
568 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
569 "udb_query_handle_result failed.",
570 db->name, udb_query_get_name (q));
574 /* Get the next row from the database. */
575 status = dbi_result_next_row (res); /* {{{ */
578 if (dbi_conn_error (db->connection, NULL) != 0)
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)));
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) */
596 } /* }}} int cdbi_read_database_query */
598 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
605 if (db->connection != NULL)
607 status = dbi_conn_ping (db->connection);
608 if (status != 0) /* connection is alive */
611 dbi_conn_close (db->connection);
612 db->connection = NULL;
615 driver = dbi_driver_open (db->driver);
618 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.",
620 INFO ("dbi plugin: Maybe the driver isn't installed? "
621 "Known drivers are:");
622 for (driver = dbi_driver_list (NULL);
624 driver = dbi_driver_list (driver))
626 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
631 connection = dbi_conn_open (driver);
632 if (connection == NULL)
634 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
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++)
646 DEBUG ("dbi plugin: cdbi_connect_database (%s): "
647 "key = %s; value = %s;",
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);
659 ERROR ("dbi plugin: cdbi_connect_database (%s): "
660 "dbi_conn_set_option (%s, %s) failed: %s.",
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);
669 opt = dbi_conn_get_option_list (connection, opt))
671 INFO ("dbi plugin: * %s", opt);
674 dbi_conn_close (connection);
677 } /* for (i = 0; i < db->driver_options_num; i++) */
679 status = dbi_conn_connect (connection);
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);
690 if (db->select_db != NULL)
692 status = dbi_conn_select_db (connection, db->select_db);
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);
705 db->connection = connection;
707 } /* }}} int cdbi_connect_database */
709 static int cdbi_read_database (cdbi_database_t *db) /* {{{ */
715 unsigned int db_version;
717 status = cdbi_connect_database (db);
720 assert (db->connection != NULL);
722 db_version = dbi_conn_get_engine_version (db->connection);
723 /* TODO: Complain if `db_version == 0' */
726 for (i = 0; i < db->queries_num; i++)
728 /* Check if we know the database's version and if so, if this query applies
729 * to that version. */
730 if ((db_version != 0)
731 && (udb_query_check_version (db->queries[i], db_version) == 0))
734 status = cdbi_read_database_query (db, db->queries[i]);
741 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
746 } /* }}} int cdbi_read_database */
748 static int cdbi_read (void) /* {{{ */
754 for (i = 0; i < databases_num; i++)
756 status = cdbi_read_database (databases[i]);
763 ERROR ("dbi plugin: No database could be read. Will return an error so "
764 "the plugin will be delayed.");
769 } /* }}} int cdbi_read */
771 static int cdbi_shutdown (void) /* {{{ */
775 for (i = 0; i < databases_num; i++)
777 if (databases[i]->connection != NULL)
779 dbi_conn_close (databases[i]->connection);
780 databases[i]->connection = NULL;
782 cdbi_database_free (databases[i]);
787 udb_query_free (queries, queries_num);
792 } /* }}} int cdbi_shutdown */
794 void module_register (void) /* {{{ */
796 plugin_register_complex_config ("dbi", cdbi_config);
797 plugin_register_init ("dbi", cdbi_init);
798 plugin_register_read ("dbi", cdbi_read);
799 plugin_register_shutdown ("dbi", cdbi_shutdown);
800 } /* }}} void module_register */
803 * vim: shiftwidth=2 softtabstop=2 et fdm=marker