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_preparation_area_t **q_prep_areas;
50 udb_query_t **queries;
55 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
60 static udb_query_t **queries = NULL;
61 static size_t queries_num = 0;
62 static cdbi_database_t **databases = NULL;
63 static size_t databases_num = 0;
68 static const char *cdbi_strerror (dbi_conn conn, /* {{{ */
69 char *buffer, size_t buffer_size)
76 sstrncpy (buffer, "connection is NULL", buffer_size);
81 status = dbi_conn_error (conn, &msg);
82 if ((status >= 0) && (msg != NULL))
83 ssnprintf (buffer, buffer_size, "%s (status %i)", msg, status);
85 ssnprintf (buffer, buffer_size, "dbi_conn_error failed with status %i",
89 } /* }}} const char *cdbi_conn_error */
91 static int cdbi_result_get_field (dbi_result res, /* {{{ */
92 unsigned int index, char *buffer, size_t buffer_size)
94 unsigned short src_type;
96 src_type = dbi_result_get_field_type_idx (res, index);
97 if (src_type == DBI_TYPE_ERROR)
99 ERROR ("dbi plugin: cdbi_result_get: "
100 "dbi_result_get_field_type_idx failed.");
104 if (src_type == DBI_TYPE_INTEGER)
108 value = dbi_result_get_longlong_idx (res, index);
109 ssnprintf (buffer, buffer_size, "%lli", value);
111 else if (src_type == DBI_TYPE_DECIMAL)
115 value = dbi_result_get_double_idx (res, index);
116 ssnprintf (buffer, buffer_size, "%63.15g", value);
118 else if (src_type == DBI_TYPE_STRING)
122 value = dbi_result_get_string_idx (res, index);
124 sstrncpy (buffer, "", buffer_size);
125 else if (strcmp ("ERROR", value) == 0)
128 sstrncpy (buffer, value, buffer_size);
130 /* DBI_TYPE_BINARY */
131 /* DBI_TYPE_DATETIME */
134 const char *field_name;
136 field_name = dbi_result_get_field_name (res, index);
137 if (field_name == NULL)
138 field_name = "<unknown>";
140 ERROR ("dbi plugin: Column `%s': Don't know how to handle "
142 field_name, src_type);
147 } /* }}} int cdbi_result_get_field */
149 static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
159 for (i = 0; i < db->driver_options_num; i++)
161 sfree (db->driver_options[i].key);
162 sfree (db->driver_options[i].value);
164 sfree (db->driver_options);
166 if (db->q_prep_areas)
167 for (i = 0; i < db->queries_num; ++i)
168 udb_query_delete_preparation_area (db->q_prep_areas[i]);
169 free (db->q_prep_areas);
172 } /* }}} void cdbi_database_free */
174 /* Configuration handling functions {{{
177 * <Query "plugin_instance0">
178 * Statement "SELECT name, value FROM table"
181 * InstancesFrom "name"
187 * <Database "plugin_instance1">
189 * DriverOption "hostname" "localhost"
191 * Query "plugin_instance0"
196 static int cdbi_config_set_string (char **ret_string, /* {{{ */
201 if ((ci->values_num != 1)
202 || (ci->values[0].type != OCONFIG_TYPE_STRING))
204 WARNING ("dbi plugin: The `%s' config option "
205 "needs exactly one string argument.", ci->key);
209 string = strdup (ci->values[0].value.string);
212 ERROR ("dbi plugin: strdup failed.");
216 if (*ret_string != NULL)
218 *ret_string = string;
221 } /* }}} int cdbi_config_set_string */
223 static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */
226 cdbi_driver_option_t *option;
228 if ((ci->values_num != 2)
229 || (ci->values[0].type != OCONFIG_TYPE_STRING)
230 || (ci->values[1].type != OCONFIG_TYPE_STRING))
232 WARNING ("dbi plugin: The `DriverOption' config option "
233 "needs exactly two string arguments.");
237 option = (cdbi_driver_option_t *) realloc (db->driver_options,
238 sizeof (*option) * (db->driver_options_num + 1));
241 ERROR ("dbi plugin: realloc failed");
245 db->driver_options = option;
246 option = db->driver_options + db->driver_options_num;
248 option->key = strdup (ci->values[0].value.string);
249 if (option->key == NULL)
251 ERROR ("dbi plugin: strdup failed.");
255 option->value = strdup (ci->values[1].value.string);
256 if (option->value == NULL)
258 ERROR ("dbi plugin: strdup failed.");
263 db->driver_options_num++;
265 } /* }}} int cdbi_config_add_database_driver_option */
267 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
273 if ((ci->values_num != 1)
274 || (ci->values[0].type != OCONFIG_TYPE_STRING))
276 WARNING ("dbi plugin: The `Database' block "
277 "needs exactly one string argument.");
281 db = (cdbi_database_t *) malloc (sizeof (*db));
284 ERROR ("dbi plugin: malloc failed.");
287 memset (db, 0, sizeof (*db));
289 status = cdbi_config_set_string (&db->name, ci);
296 /* Fill the `cdbi_database_t' structure.. */
297 for (i = 0; i < ci->children_num; i++)
299 oconfig_item_t *child = ci->children + i;
301 if (strcasecmp ("Driver", child->key) == 0)
302 status = cdbi_config_set_string (&db->driver, child);
303 else if (strcasecmp ("DriverOption", child->key) == 0)
304 status = cdbi_config_add_database_driver_option (db, child);
305 else if (strcasecmp ("SelectDB", child->key) == 0)
306 status = cdbi_config_set_string (&db->select_db, child);
307 else if (strcasecmp ("Query", child->key) == 0)
308 status = udb_query_pick_from_list (child, queries, queries_num,
309 &db->queries, &db->queries_num);
312 WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
320 /* Check that all necessary options have been given. */
323 if (db->driver == NULL)
325 WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
328 if (db->driver_options_num == 0)
330 WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
331 "This will likely not work.", db->name);
335 } /* while (status == 0) */
337 while ((status == 0) && (db->queries_num > 0))
339 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
340 db->queries_num, sizeof (*db->q_prep_areas));
342 if (db->q_prep_areas == NULL)
344 WARNING ("dbi plugin: malloc failed");
349 for (i = 0; i < db->queries_num; ++i)
352 = udb_query_allocate_preparation_area (db->queries[i]);
354 if (db->q_prep_areas[i] == NULL)
356 WARNING ("dbi plugin: udb_query_allocate_preparation_area failed");
365 /* If all went well, add this database to the global list of databases. */
368 cdbi_database_t **temp;
370 temp = (cdbi_database_t **) realloc (databases,
371 sizeof (*databases) * (databases_num + 1));
374 ERROR ("dbi plugin: realloc failed");
380 databases[databases_num] = db;
387 cdbi_database_free (db);
392 } /* }}} int cdbi_config_add_database */
394 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
398 for (i = 0; i < ci->children_num; i++)
400 oconfig_item_t *child = ci->children + i;
401 if (strcasecmp ("Query", child->key) == 0)
402 udb_query_create (&queries, &queries_num, child,
403 /* callback = */ NULL, /* legacy mode = */ 0);
404 else if (strcasecmp ("Database", child->key) == 0)
405 cdbi_config_add_database (child);
408 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
410 } /* for (ci->children) */
413 } /* }}} int cdbi_config */
415 /* }}} End of configuration handling functions */
417 static int cdbi_init (void) /* {{{ */
419 static int did_init = 0;
425 if (queries_num == 0)
427 ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
428 "this plugin can't do anything useful, so we will returns an error.");
432 if (databases_num == 0)
434 ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
435 "this plugin can't do anything useful, so we will returns an error.");
439 status = dbi_initialize (NULL);
442 ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.",
446 else if (status == 0)
448 ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please "
449 "install at least one `DBD' or check your installation.");
452 DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.",
453 status, (status == 1) ? "" : "s");
456 } /* }}} int cdbi_init */
458 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
459 udb_query_t *q, udb_query_preparation_area_t *prep_area)
461 const char *statement;
465 char **column_values;
469 /* Macro that cleans up dynamically allocated memory and returns the
470 * specified status. */
471 #define BAIL_OUT(status) \
472 if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
473 if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
474 if (res != NULL) { dbi_result_free (res); res = NULL; } \
478 column_values = NULL;
481 statement = udb_query_get_statement (q);
482 assert (statement != NULL);
484 res = dbi_conn_query (db->connection, statement);
488 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
489 "dbi_conn_query failed: %s",
490 db->name, udb_query_get_name (q),
491 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
494 else /* Get the number of columns */
496 unsigned int db_status;
498 db_status = dbi_result_get_numfields (res);
499 if (db_status == DBI_FIELD_ERROR)
502 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
503 "dbi_result_get_numfields failed: %s",
504 db->name, udb_query_get_name (q),
505 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
509 column_num = (size_t) db_status;
510 DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
511 db->name, udb_query_get_name (q), column_num);
514 /* Allocate `column_names' and `column_values'. {{{ */
515 column_names = (char **) calloc (column_num, sizeof (char *));
516 if (column_names == NULL)
518 ERROR ("dbi plugin: malloc failed.");
522 column_names[0] = (char *) calloc (column_num,
523 DATA_MAX_NAME_LEN * sizeof (char));
524 if (column_names[0] == NULL)
526 ERROR ("dbi plugin: malloc failed.");
529 for (i = 1; i < column_num; i++)
530 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
532 column_values = (char **) calloc (column_num, sizeof (char *));
533 if (column_values == NULL)
535 ERROR ("dbi plugin: malloc failed.");
539 column_values[0] = (char *) calloc (column_num,
540 DATA_MAX_NAME_LEN * sizeof (char));
541 if (column_values[0] == NULL)
543 ERROR ("dbi plugin: malloc failed.");
546 for (i = 1; i < column_num; i++)
547 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
550 /* Copy the field names to `column_names' */
551 for (i = 0; i < column_num; i++) /* {{{ */
553 const char *column_name;
555 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
556 if (column_name == NULL)
558 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
559 "Cannot retrieve name of field %zu.",
560 db->name, udb_query_get_name (q), i + 1);
564 sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
565 } /* }}} for (i = 0; i < column_num; i++) */
567 udb_query_prepare_result (q, prep_area, hostname_g,
568 /* plugin = */ "dbi", db->name,
569 column_names, column_num, /* interval = */ -1);
571 /* 0 = error; 1 = success; */
572 status = dbi_result_first_row (res); /* {{{ */
576 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
577 "dbi_result_first_row failed: %s. Maybe the statement didn't "
579 db->name, udb_query_get_name (q),
580 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
581 udb_query_finish_result (q, prep_area);
585 /* Iterate over all rows and call `udb_query_handle_result' with each list of
590 /* Copy the value of the columns to `column_values' */
591 for (i = 0; i < column_num; i++) /* {{{ */
593 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
594 column_values[i], DATA_MAX_NAME_LEN);
598 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
599 "cdbi_result_get_field (%zu) failed.",
600 db->name, udb_query_get_name (q), i + 1);
604 } /* }}} for (i = 0; i < column_num; i++) */
606 /* If all values were copied successfully, call `udb_query_handle_result'
607 * to dispatch the row to the daemon. */
608 if (status == 0) /* {{{ */
610 status = udb_query_handle_result (q, prep_area, column_values);
613 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
614 "udb_query_handle_result failed.",
615 db->name, udb_query_get_name (q));
619 /* Get the next row from the database. */
620 status = dbi_result_next_row (res); /* {{{ */
623 if (dbi_conn_error (db->connection, NULL) != 0)
626 WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
627 "dbi_result_next_row failed: %s.",
628 db->name, udb_query_get_name (q),
629 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
633 } /* }}} while (42) */
635 /* Tell the db query interface that we're done with this query. */
636 udb_query_finish_result (q, prep_area);
638 /* Clean up and return `status = 0' (success) */
641 } /* }}} int cdbi_read_database_query */
643 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
650 if (db->connection != NULL)
652 status = dbi_conn_ping (db->connection);
653 if (status != 0) /* connection is alive */
656 dbi_conn_close (db->connection);
657 db->connection = NULL;
660 driver = dbi_driver_open (db->driver);
663 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.",
665 INFO ("dbi plugin: Maybe the driver isn't installed? "
666 "Known drivers are:");
667 for (driver = dbi_driver_list (NULL);
669 driver = dbi_driver_list (driver))
671 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
676 connection = dbi_conn_open (driver);
677 if (connection == NULL)
679 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
684 /* Set all the driver options. Because this is a very very very generic
685 * interface, the error handling is kind of long. If an invalid option is
686 * encountered, it will get a list of options understood by the driver and
687 * report that as `INFO'. This way, users hopefully don't have too much
688 * trouble finding out how to configure the plugin correctly.. */
689 for (i = 0; i < db->driver_options_num; i++)
691 DEBUG ("dbi plugin: cdbi_connect_database (%s): "
692 "key = %s; value = %s;",
694 db->driver_options[i].key,
695 db->driver_options[i].value);
697 status = dbi_conn_set_option (connection,
698 db->driver_options[i].key, db->driver_options[i].value);
704 ERROR ("dbi plugin: cdbi_connect_database (%s): "
705 "dbi_conn_set_option (%s, %s) failed: %s.",
707 db->driver_options[i].key, db->driver_options[i].value,
708 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
710 INFO ("dbi plugin: This is a list of all options understood "
711 "by the `%s' driver:", db->driver);
712 for (opt = dbi_conn_get_option_list (connection, NULL);
714 opt = dbi_conn_get_option_list (connection, opt))
716 INFO ("dbi plugin: * %s", opt);
719 dbi_conn_close (connection);
722 } /* for (i = 0; i < db->driver_options_num; i++) */
724 status = dbi_conn_connect (connection);
728 ERROR ("dbi plugin: cdbi_connect_database (%s): "
729 "dbi_conn_connect failed: %s",
730 db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
731 dbi_conn_close (connection);
735 if (db->select_db != NULL)
737 status = dbi_conn_select_db (connection, db->select_db);
741 WARNING ("dbi plugin: cdbi_connect_database (%s): "
742 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
743 db->name, db->select_db,
744 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
745 dbi_conn_close (connection);
750 db->connection = connection;
752 } /* }}} int cdbi_connect_database */
754 static int cdbi_read_database (cdbi_database_t *db) /* {{{ */
760 unsigned int db_version;
762 status = cdbi_connect_database (db);
765 assert (db->connection != NULL);
767 db_version = dbi_conn_get_engine_version (db->connection);
768 /* TODO: Complain if `db_version == 0' */
771 for (i = 0; i < db->queries_num; i++)
773 /* Check if we know the database's version and if so, if this query applies
774 * to that version. */
775 if ((db_version != 0)
776 && (udb_query_check_version (db->queries[i], db_version) == 0))
779 status = cdbi_read_database_query (db,
780 db->queries[i], db->q_prep_areas[i]);
787 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
792 } /* }}} int cdbi_read_database */
794 static int cdbi_read (void) /* {{{ */
800 for (i = 0; i < databases_num; i++)
802 status = cdbi_read_database (databases[i]);
809 ERROR ("dbi plugin: No database could be read. Will return an error so "
810 "the plugin will be delayed.");
815 } /* }}} int cdbi_read */
817 static int cdbi_shutdown (void) /* {{{ */
821 for (i = 0; i < databases_num; i++)
823 if (databases[i]->connection != NULL)
825 dbi_conn_close (databases[i]->connection);
826 databases[i]->connection = NULL;
828 cdbi_database_free (databases[i]);
833 udb_query_free (queries, queries_num);
838 } /* }}} int cdbi_shutdown */
840 void module_register (void) /* {{{ */
842 plugin_register_complex_config ("dbi", cdbi_config);
843 plugin_register_init ("dbi", cdbi_init);
844 plugin_register_read ("dbi", cdbi_read);
845 plugin_register_shutdown ("dbi", cdbi_shutdown);
846 } /* }}} void module_register */
849 * vim: shiftwidth=2 softtabstop=2 et fdm=marker