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);
129 /* DBI_TYPE_BINARY */
130 /* DBI_TYPE_DATETIME */
133 const char *field_name;
135 field_name = dbi_result_get_field_name (res, index);
136 if (field_name == NULL)
137 field_name = "<unknown>";
139 ERROR ("dbi plugin: Column `%s': Don't know how to handle "
141 field_name, src_type);
146 } /* }}} int cdbi_result_get_field */
148 static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
158 for (i = 0; i < db->driver_options_num; i++)
160 sfree (db->driver_options[i].key);
161 sfree (db->driver_options[i].value);
163 sfree (db->driver_options);
166 } /* }}} void cdbi_database_free */
168 /* Configuration handling functions {{{
171 * <Query "plugin_instance0">
172 * Statement "SELECT name, value FROM table"
175 * InstancesFrom "name"
181 * <Database "plugin_instance1">
183 * DriverOption "hostname" "localhost"
185 * Query "plugin_instance0"
190 static int cdbi_config_set_string (char **ret_string, /* {{{ */
195 if ((ci->values_num != 1)
196 || (ci->values[0].type != OCONFIG_TYPE_STRING))
198 WARNING ("dbi plugin: The `%s' config option "
199 "needs exactly one string argument.", ci->key);
203 string = strdup (ci->values[0].value.string);
206 ERROR ("dbi plugin: strdup failed.");
210 if (*ret_string != NULL)
212 *ret_string = string;
215 } /* }}} int cdbi_config_set_string */
217 static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */
220 cdbi_driver_option_t *option;
222 if ((ci->values_num != 2)
223 || (ci->values[0].type != OCONFIG_TYPE_STRING)
224 || (ci->values[1].type != OCONFIG_TYPE_STRING))
226 WARNING ("dbi plugin: The `DriverOption' config option "
227 "needs exactly two string arguments.");
231 option = (cdbi_driver_option_t *) realloc (db->driver_options,
232 sizeof (*option) * (db->driver_options_num + 1));
235 ERROR ("dbi plugin: realloc failed");
239 db->driver_options = option;
240 option = db->driver_options + db->driver_options_num;
242 option->key = strdup (ci->values[0].value.string);
243 if (option->key == NULL)
245 ERROR ("dbi plugin: strdup failed.");
249 option->value = strdup (ci->values[1].value.string);
250 if (option->value == NULL)
252 ERROR ("dbi plugin: strdup failed.");
257 db->driver_options_num++;
259 } /* }}} int cdbi_config_add_database_driver_option */
261 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
267 if ((ci->values_num != 1)
268 || (ci->values[0].type != OCONFIG_TYPE_STRING))
270 WARNING ("dbi plugin: The `Database' block "
271 "needs exactly one string argument.");
275 db = (cdbi_database_t *) malloc (sizeof (*db));
278 ERROR ("dbi plugin: malloc failed.");
281 memset (db, 0, sizeof (*db));
283 status = cdbi_config_set_string (&db->name, ci);
290 /* Fill the `cdbi_database_t' structure.. */
291 for (i = 0; i < ci->children_num; i++)
293 oconfig_item_t *child = ci->children + i;
295 if (strcasecmp ("Driver", child->key) == 0)
296 status = cdbi_config_set_string (&db->driver, child);
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 = cdbi_config_set_string (&db->select_db, child);
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);
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 /* If all went well, add this database to the global list of databases. */
334 cdbi_database_t **temp;
336 temp = (cdbi_database_t **) realloc (databases,
337 sizeof (*databases) * (databases_num + 1));
340 ERROR ("dbi plugin: realloc failed");
346 databases[databases_num] = db;
353 cdbi_database_free (db);
358 } /* }}} int cdbi_config_add_database */
360 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
364 for (i = 0; i < ci->children_num; i++)
366 oconfig_item_t *child = ci->children + i;
367 if (strcasecmp ("Query", child->key) == 0)
368 udb_query_create (&queries, &queries_num, child,
369 /* callback = */ NULL, /* legacy mode = */ 0);
370 else if (strcasecmp ("Database", child->key) == 0)
371 cdbi_config_add_database (child);
374 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
376 } /* for (ci->children) */
379 } /* }}} int cdbi_config */
381 /* }}} End of configuration handling functions */
383 static int cdbi_init (void) /* {{{ */
385 static int did_init = 0;
391 if (queries_num == 0)
393 ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
394 "this plugin can't do anything useful, so we will returns an error.");
398 if (databases_num == 0)
400 ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
401 "this plugin can't do anything useful, so we will returns an error.");
405 status = dbi_initialize (NULL);
408 ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.",
412 else if (status == 0)
414 ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please "
415 "install at least one `DBD' or check your installation.");
418 DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.",
419 status, (status == 1) ? "" : "s");
422 } /* }}} int cdbi_init */
424 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
427 const char *statement;
431 char **column_values;
435 /* Macro that cleans up dynamically allocated memory and returns the
436 * specified status. */
437 #define BAIL_OUT(status) \
438 if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
439 if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
440 if (res != NULL) { dbi_result_free (res); res = NULL; } \
444 column_values = NULL;
447 statement = udb_query_get_statement (q);
448 assert (statement != NULL);
450 res = dbi_conn_query (db->connection, statement);
454 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
455 "dbi_conn_query failed: %s",
456 db->name, udb_query_get_name (q),
457 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
460 else /* Get the number of columns */
462 unsigned int db_status;
464 db_status = dbi_result_get_numfields (res);
465 if (db_status == DBI_FIELD_ERROR)
468 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
469 "dbi_result_get_numfields failed: %s",
470 db->name, udb_query_get_name (q),
471 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
475 column_num = (size_t) db_status;
476 DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
477 db->name, udb_query_get_name (q), column_num);
480 /* Allocate `column_names' and `column_values'. {{{ */
481 column_names = (char **) calloc (column_num, sizeof (char *));
482 if (column_names == NULL)
484 ERROR ("dbi plugin: malloc failed.");
488 column_names[0] = (char *) calloc (column_num,
489 DATA_MAX_NAME_LEN * sizeof (char));
490 if (column_names[0] == NULL)
492 ERROR ("dbi plugin: malloc failed.");
495 for (i = 1; i < column_num; i++)
496 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
498 column_values = (char **) calloc (column_num, sizeof (char *));
499 if (column_values == NULL)
501 ERROR ("dbi plugin: malloc failed.");
505 column_values[0] = (char *) calloc (column_num,
506 DATA_MAX_NAME_LEN * sizeof (char));
507 if (column_values[0] == NULL)
509 ERROR ("dbi plugin: malloc failed.");
512 for (i = 1; i < column_num; i++)
513 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
516 /* Copy the field names to `column_names' */
517 for (i = 0; i < column_num; i++) /* {{{ */
519 const char *column_name;
521 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
522 if (column_name == NULL)
524 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
525 "Cannot retrieve name of field %zu.",
526 db->name, udb_query_get_name (q), i + 1);
530 sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
531 } /* }}} for (i = 0; i < column_num; i++) */
533 udb_query_prepare_result (q, hostname_g, /* plugin = */ "dbi", db->name,
534 column_names, column_num);
536 /* 0 = error; 1 = success; */
537 status = dbi_result_first_row (res); /* {{{ */
541 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
542 "dbi_result_first_row failed: %s. Maybe the statement didn't "
544 db->name, udb_query_get_name (q),
545 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
546 udb_query_finish_result (q);
550 /* Iterate over all rows and call `udb_query_handle_result' with each list of
555 /* Copy the value of the columns to `column_values' */
556 for (i = 0; i < column_num; i++) /* {{{ */
558 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
559 column_values[i], DATA_MAX_NAME_LEN);
563 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
564 "cdbi_result_get_field (%zu) failed.",
565 db->name, udb_query_get_name (q), i + 1);
569 } /* }}} for (i = 0; i < column_num; i++) */
571 /* If all values were copied successfully, call `udb_query_handle_result'
572 * to dispatch the row to the daemon. */
573 if (status == 0) /* {{{ */
575 status = udb_query_handle_result (q, column_values);
578 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
579 "udb_query_handle_result failed.",
580 db->name, udb_query_get_name (q));
584 /* Get the next row from the database. */
585 status = dbi_result_next_row (res); /* {{{ */
588 if (dbi_conn_error (db->connection, NULL) != 0)
591 WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
592 "dbi_result_next_row failed: %s.",
593 db->name, udb_query_get_name (q),
594 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
598 } /* }}} while (42) */
600 /* Tell the db query interface that we're done with this query. */
601 udb_query_finish_result (q);
603 /* Clean up and return `status = 0' (success) */
606 } /* }}} int cdbi_read_database_query */
608 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
615 if (db->connection != NULL)
617 status = dbi_conn_ping (db->connection);
618 if (status != 0) /* connection is alive */
621 dbi_conn_close (db->connection);
622 db->connection = NULL;
625 driver = dbi_driver_open (db->driver);
628 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.",
630 INFO ("dbi plugin: Maybe the driver isn't installed? "
631 "Known drivers are:");
632 for (driver = dbi_driver_list (NULL);
634 driver = dbi_driver_list (driver))
636 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
641 connection = dbi_conn_open (driver);
642 if (connection == NULL)
644 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
649 /* Set all the driver options. Because this is a very very very generic
650 * interface, the error handling is kind of long. If an invalid option is
651 * encountered, it will get a list of options understood by the driver and
652 * report that as `INFO'. This way, users hopefully don't have too much
653 * trouble finding out how to configure the plugin correctly.. */
654 for (i = 0; i < db->driver_options_num; i++)
656 DEBUG ("dbi plugin: cdbi_connect_database (%s): "
657 "key = %s; value = %s;",
659 db->driver_options[i].key,
660 db->driver_options[i].value);
662 status = dbi_conn_set_option (connection,
663 db->driver_options[i].key, db->driver_options[i].value);
669 ERROR ("dbi plugin: cdbi_connect_database (%s): "
670 "dbi_conn_set_option (%s, %s) failed: %s.",
672 db->driver_options[i].key, db->driver_options[i].value,
673 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
675 INFO ("dbi plugin: This is a list of all options understood "
676 "by the `%s' driver:", db->driver);
677 for (opt = dbi_conn_get_option_list (connection, NULL);
679 opt = dbi_conn_get_option_list (connection, opt))
681 INFO ("dbi plugin: * %s", opt);
684 dbi_conn_close (connection);
687 } /* for (i = 0; i < db->driver_options_num; i++) */
689 status = dbi_conn_connect (connection);
693 ERROR ("dbi plugin: cdbi_connect_database (%s): "
694 "dbi_conn_connect failed: %s",
695 db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
696 dbi_conn_close (connection);
700 if (db->select_db != NULL)
702 status = dbi_conn_select_db (connection, db->select_db);
706 WARNING ("dbi plugin: cdbi_connect_database (%s): "
707 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
708 db->name, db->select_db,
709 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
710 dbi_conn_close (connection);
715 db->connection = connection;
717 } /* }}} int cdbi_connect_database */
719 static int cdbi_read_database (cdbi_database_t *db) /* {{{ */
725 unsigned int db_version;
727 status = cdbi_connect_database (db);
730 assert (db->connection != NULL);
732 db_version = dbi_conn_get_engine_version (db->connection);
733 /* TODO: Complain if `db_version == 0' */
736 for (i = 0; i < db->queries_num; i++)
738 /* Check if we know the database's version and if so, if this query applies
739 * to that version. */
740 if ((db_version != 0)
741 && (udb_query_check_version (db->queries[i], db_version) == 0))
744 status = cdbi_read_database_query (db, db->queries[i]);
751 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
756 } /* }}} int cdbi_read_database */
758 static int cdbi_read (void) /* {{{ */
764 for (i = 0; i < databases_num; i++)
766 status = cdbi_read_database (databases[i]);
773 ERROR ("dbi plugin: No database could be read. Will return an error so "
774 "the plugin will be delayed.");
779 } /* }}} int cdbi_read */
781 static int cdbi_shutdown (void) /* {{{ */
785 for (i = 0; i < databases_num; i++)
787 if (databases[i]->connection != NULL)
789 dbi_conn_close (databases[i]->connection);
790 databases[i]->connection = NULL;
792 cdbi_database_free (databases[i]);
797 udb_query_free (queries, queries_num);
802 } /* }}} int cdbi_shutdown */
804 void module_register (void) /* {{{ */
806 plugin_register_complex_config ("dbi", cdbi_config);
807 plugin_register_init ("dbi", cdbi_init);
808 plugin_register_read ("dbi", cdbi_read);
809 plugin_register_shutdown ("dbi", cdbi_shutdown);
810 } /* }}} void module_register */
813 * vim: shiftwidth=2 softtabstop=2 et fdm=marker