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 /* callback = */ NULL, /* legacy mode = */ 0);
361 else if (strcasecmp ("Database", child->key) == 0)
362 cdbi_config_add_database (child);
365 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
367 } /* for (ci->children) */
370 } /* }}} int cdbi_config */
372 /* }}} End of configuration handling functions */
374 static int cdbi_init (void) /* {{{ */
376 static int did_init = 0;
382 if (queries_num == 0)
384 ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
385 "this plugin can't do anything useful, so we will returns an error.");
389 if (databases_num == 0)
391 ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
392 "this plugin can't do anything useful, so we will returns an error.");
396 status = dbi_initialize (NULL);
399 ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.",
403 else if (status == 0)
405 ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please "
406 "install at least one `DBD' or check your installation.");
409 DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.",
410 status, (status == 1) ? "" : "s");
413 } /* }}} int cdbi_init */
415 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
418 const char *statement;
422 char **column_values;
426 /* Macro that cleans up dynamically allocated memory and returns the
427 * specified status. */
428 #define BAIL_OUT(status) \
429 if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
430 if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
431 if (res != NULL) { dbi_result_free (res); res = NULL; } \
435 column_values = NULL;
438 statement = udb_query_get_statement (q);
439 assert (statement != NULL);
441 res = dbi_conn_query (db->connection, statement);
445 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
446 "dbi_conn_query failed: %s",
447 db->name, udb_query_get_name (q),
448 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
451 else /* Get the number of columns */
453 unsigned int db_status;
455 db_status = dbi_result_get_numfields (res);
456 if (db_status == DBI_FIELD_ERROR)
459 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
460 "dbi_result_get_numfields failed: %s",
461 db->name, udb_query_get_name (q),
462 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
466 column_num = (size_t) db_status;
467 DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
468 db->name, udb_query_get_name (q), column_num);
471 /* Allocate `column_names' and `column_values'. {{{ */
472 column_names = (char **) calloc (column_num, sizeof (char *));
473 if (column_names == NULL)
475 ERROR ("dbi plugin: malloc failed.");
479 column_names[0] = (char *) calloc (column_num,
480 DATA_MAX_NAME_LEN * sizeof (char));
481 if (column_names[0] == NULL)
483 ERROR ("dbi plugin: malloc failed.");
486 for (i = 1; i < column_num; i++)
487 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
489 column_values = (char **) calloc (column_num, sizeof (char *));
490 if (column_values == NULL)
492 ERROR ("dbi plugin: malloc failed.");
496 column_values[0] = (char *) calloc (column_num,
497 DATA_MAX_NAME_LEN * sizeof (char));
498 if (column_values[0] == NULL)
500 ERROR ("dbi plugin: malloc failed.");
503 for (i = 1; i < column_num; i++)
504 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
507 /* Copy the field names to `column_names' */
508 for (i = 0; i < column_num; i++) /* {{{ */
510 const char *column_name;
512 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
513 if (column_name == NULL)
515 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
516 "Cannot retrieve name of field %zu.",
517 db->name, udb_query_get_name (q), i + 1);
521 sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
522 } /* }}} for (i = 0; i < column_num; i++) */
524 udb_query_prepare_result (q, hostname_g, /* plugin = */ "dbi", db->name,
525 column_names, column_num);
527 /* 0 = error; 1 = success; */
528 status = dbi_result_first_row (res); /* {{{ */
532 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
533 "dbi_result_first_row failed: %s. Maybe the statement didn't "
535 db->name, udb_query_get_name (q),
536 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
537 udb_query_finish_result (q);
541 /* Iterate over all rows and call `udb_query_handle_result' with each list of
546 /* Copy the value of the columns to `column_values' */
547 for (i = 0; i < column_num; i++) /* {{{ */
549 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
550 column_values[i], DATA_MAX_NAME_LEN);
554 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
555 "cdbi_result_get_field (%zu) failed.",
556 db->name, udb_query_get_name (q), i + 1);
560 } /* }}} for (i = 0; i < column_num; i++) */
562 /* If all values were copied successfully, call `udb_query_handle_result'
563 * to dispatch the row to the daemon. */
564 if (status == 0) /* {{{ */
566 status = udb_query_handle_result (q, column_values);
569 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
570 "udb_query_handle_result failed.",
571 db->name, udb_query_get_name (q));
575 /* Get the next row from the database. */
576 status = dbi_result_next_row (res); /* {{{ */
579 if (dbi_conn_error (db->connection, NULL) != 0)
582 WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
583 "dbi_result_next_row failed: %s.",
584 db->name, udb_query_get_name (q),
585 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
589 } /* }}} while (42) */
591 /* Tell the db query interface that we're done with this query. */
592 udb_query_finish_result (q);
594 /* Clean up and return `status = 0' (success) */
597 } /* }}} int cdbi_read_database_query */
599 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
606 if (db->connection != NULL)
608 status = dbi_conn_ping (db->connection);
609 if (status != 0) /* connection is alive */
612 dbi_conn_close (db->connection);
613 db->connection = NULL;
616 driver = dbi_driver_open (db->driver);
619 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.",
621 INFO ("dbi plugin: Maybe the driver isn't installed? "
622 "Known drivers are:");
623 for (driver = dbi_driver_list (NULL);
625 driver = dbi_driver_list (driver))
627 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
632 connection = dbi_conn_open (driver);
633 if (connection == NULL)
635 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
640 /* Set all the driver options. Because this is a very very very generic
641 * interface, the error handling is kind of long. If an invalid option is
642 * encountered, it will get a list of options understood by the driver and
643 * report that as `INFO'. This way, users hopefully don't have too much
644 * trouble finding out how to configure the plugin correctly.. */
645 for (i = 0; i < db->driver_options_num; i++)
647 DEBUG ("dbi plugin: cdbi_connect_database (%s): "
648 "key = %s; value = %s;",
650 db->driver_options[i].key,
651 db->driver_options[i].value);
653 status = dbi_conn_set_option (connection,
654 db->driver_options[i].key, db->driver_options[i].value);
660 ERROR ("dbi plugin: cdbi_connect_database (%s): "
661 "dbi_conn_set_option (%s, %s) failed: %s.",
663 db->driver_options[i].key, db->driver_options[i].value,
664 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
666 INFO ("dbi plugin: This is a list of all options understood "
667 "by the `%s' driver:", db->driver);
668 for (opt = dbi_conn_get_option_list (connection, NULL);
670 opt = dbi_conn_get_option_list (connection, opt))
672 INFO ("dbi plugin: * %s", opt);
675 dbi_conn_close (connection);
678 } /* for (i = 0; i < db->driver_options_num; i++) */
680 status = dbi_conn_connect (connection);
684 ERROR ("dbi plugin: cdbi_connect_database (%s): "
685 "dbi_conn_connect failed: %s",
686 db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
687 dbi_conn_close (connection);
691 if (db->select_db != NULL)
693 status = dbi_conn_select_db (connection, db->select_db);
697 WARNING ("dbi plugin: cdbi_connect_database (%s): "
698 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
699 db->name, db->select_db,
700 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
701 dbi_conn_close (connection);
706 db->connection = connection;
708 } /* }}} int cdbi_connect_database */
710 static int cdbi_read_database (cdbi_database_t *db) /* {{{ */
716 unsigned int db_version;
718 status = cdbi_connect_database (db);
721 assert (db->connection != NULL);
723 db_version = dbi_conn_get_engine_version (db->connection);
724 /* TODO: Complain if `db_version == 0' */
727 for (i = 0; i < db->queries_num; i++)
729 /* Check if we know the database's version and if so, if this query applies
730 * to that version. */
731 if ((db_version != 0)
732 && (udb_query_check_version (db->queries[i], db_version) == 0))
735 status = cdbi_read_database_query (db, db->queries[i]);
742 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
747 } /* }}} int cdbi_read_database */
749 static int cdbi_read (void) /* {{{ */
755 for (i = 0; i < databases_num; i++)
757 status = cdbi_read_database (databases[i]);
764 ERROR ("dbi plugin: No database could be read. Will return an error so "
765 "the plugin will be delayed.");
770 } /* }}} int cdbi_read */
772 static int cdbi_shutdown (void) /* {{{ */
776 for (i = 0; i < databases_num; i++)
778 if (databases[i]->connection != NULL)
780 dbi_conn_close (databases[i]->connection);
781 databases[i]->connection = NULL;
783 cdbi_database_free (databases[i]);
788 udb_query_free (queries, queries_num);
793 } /* }}} int cdbi_shutdown */
795 void module_register (void) /* {{{ */
797 plugin_register_complex_config ("dbi", cdbi_config);
798 plugin_register_init ("dbi", cdbi_init);
799 plugin_register_read ("dbi", cdbi_read);
800 plugin_register_shutdown ("dbi", cdbi_shutdown);
801 } /* }}} void module_register */
804 * vim: shiftwidth=2 softtabstop=2 et fdm=marker