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_add_database_driver_option (cdbi_database_t *db, /* {{{ */
199 cdbi_driver_option_t *option;
201 if ((ci->values_num != 2)
202 || (ci->values[0].type != OCONFIG_TYPE_STRING)
203 || (ci->values[1].type != OCONFIG_TYPE_STRING))
205 WARNING ("dbi plugin: The `DriverOption' config option "
206 "needs exactly two string arguments.");
210 option = (cdbi_driver_option_t *) realloc (db->driver_options,
211 sizeof (*option) * (db->driver_options_num + 1));
214 ERROR ("dbi plugin: realloc failed");
218 db->driver_options = option;
219 option = db->driver_options + db->driver_options_num;
221 option->key = strdup (ci->values[0].value.string);
222 if (option->key == NULL)
224 ERROR ("dbi plugin: strdup failed.");
228 option->value = strdup (ci->values[1].value.string);
229 if (option->value == NULL)
231 ERROR ("dbi plugin: strdup failed.");
236 db->driver_options_num++;
238 } /* }}} int cdbi_config_add_database_driver_option */
240 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
246 if ((ci->values_num != 1)
247 || (ci->values[0].type != OCONFIG_TYPE_STRING))
249 WARNING ("dbi plugin: The `Database' block "
250 "needs exactly one string argument.");
254 db = (cdbi_database_t *) malloc (sizeof (*db));
257 ERROR ("dbi plugin: malloc failed.");
260 memset (db, 0, sizeof (*db));
262 status = cf_util_get_string (ci, &db->name);
269 /* Fill the `cdbi_database_t' structure.. */
270 for (i = 0; i < ci->children_num; i++)
272 oconfig_item_t *child = ci->children + i;
274 if (strcasecmp ("Driver", child->key) == 0)
275 status = cf_util_get_string (child, &db->driver);
276 else if (strcasecmp ("DriverOption", child->key) == 0)
277 status = cdbi_config_add_database_driver_option (db, child);
278 else if (strcasecmp ("SelectDB", child->key) == 0)
279 status = cf_util_get_string (child, &db->select_db);
280 else if (strcasecmp ("Query", child->key) == 0)
281 status = udb_query_pick_from_list (child, queries, queries_num,
282 &db->queries, &db->queries_num);
285 WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
293 /* Check that all necessary options have been given. */
296 if (db->driver == NULL)
298 WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
301 if (db->driver_options_num == 0)
303 WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
304 "This will likely not work.", db->name);
308 } /* while (status == 0) */
310 while ((status == 0) && (db->queries_num > 0))
312 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
313 db->queries_num, sizeof (*db->q_prep_areas));
315 if (db->q_prep_areas == NULL)
317 WARNING ("dbi plugin: malloc failed");
322 for (i = 0; i < db->queries_num; ++i)
325 = udb_query_allocate_preparation_area (db->queries[i]);
327 if (db->q_prep_areas[i] == NULL)
329 WARNING ("dbi plugin: udb_query_allocate_preparation_area failed");
338 /* If all went well, add this database to the global list of databases. */
341 cdbi_database_t **temp;
343 temp = (cdbi_database_t **) realloc (databases,
344 sizeof (*databases) * (databases_num + 1));
347 ERROR ("dbi plugin: realloc failed");
353 databases[databases_num] = db;
360 cdbi_database_free (db);
365 } /* }}} int cdbi_config_add_database */
367 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
371 for (i = 0; i < ci->children_num; i++)
373 oconfig_item_t *child = ci->children + i;
374 if (strcasecmp ("Query", child->key) == 0)
375 udb_query_create (&queries, &queries_num, child,
376 /* callback = */ NULL);
377 else if (strcasecmp ("Database", child->key) == 0)
378 cdbi_config_add_database (child);
381 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
383 } /* for (ci->children) */
386 } /* }}} int cdbi_config */
388 /* }}} End of configuration handling functions */
390 static int cdbi_init (void) /* {{{ */
392 static int did_init = 0;
398 if (queries_num == 0)
400 ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
401 "this plugin can't do anything useful, so we will returns an error.");
405 if (databases_num == 0)
407 ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
408 "this plugin can't do anything useful, so we will returns an error.");
412 status = dbi_initialize (NULL);
415 ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.",
419 else if (status == 0)
421 ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please "
422 "install at least one `DBD' or check your installation.");
425 DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.",
426 status, (status == 1) ? "" : "s");
429 } /* }}} int cdbi_init */
431 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
432 udb_query_t *q, udb_query_preparation_area_t *prep_area)
434 const char *statement;
438 char **column_values;
442 /* Macro that cleans up dynamically allocated memory and returns the
443 * specified status. */
444 #define BAIL_OUT(status) \
445 if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
446 if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
447 if (res != NULL) { dbi_result_free (res); res = NULL; } \
451 column_values = NULL;
454 statement = udb_query_get_statement (q);
455 assert (statement != NULL);
457 res = dbi_conn_query (db->connection, statement);
461 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
462 "dbi_conn_query failed: %s",
463 db->name, udb_query_get_name (q),
464 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
467 else /* Get the number of columns */
469 unsigned int db_status;
471 db_status = dbi_result_get_numfields (res);
472 if (db_status == DBI_FIELD_ERROR)
475 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
476 "dbi_result_get_numfields failed: %s",
477 db->name, udb_query_get_name (q),
478 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
482 column_num = (size_t) db_status;
483 DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
484 db->name, udb_query_get_name (q), column_num);
487 /* Allocate `column_names' and `column_values'. {{{ */
488 column_names = (char **) calloc (column_num, sizeof (char *));
489 if (column_names == NULL)
491 ERROR ("dbi plugin: malloc failed.");
495 column_names[0] = (char *) calloc (column_num,
496 DATA_MAX_NAME_LEN * sizeof (char));
497 if (column_names[0] == NULL)
499 ERROR ("dbi plugin: malloc failed.");
502 for (i = 1; i < column_num; i++)
503 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
505 column_values = (char **) calloc (column_num, sizeof (char *));
506 if (column_values == NULL)
508 ERROR ("dbi plugin: malloc failed.");
512 column_values[0] = (char *) calloc (column_num,
513 DATA_MAX_NAME_LEN * sizeof (char));
514 if (column_values[0] == NULL)
516 ERROR ("dbi plugin: malloc failed.");
519 for (i = 1; i < column_num; i++)
520 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
523 /* Copy the field names to `column_names' */
524 for (i = 0; i < column_num; i++) /* {{{ */
526 const char *column_name;
528 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
529 if (column_name == NULL)
531 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
532 "Cannot retrieve name of field %zu.",
533 db->name, udb_query_get_name (q), i + 1);
537 sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
538 } /* }}} for (i = 0; i < column_num; i++) */
540 udb_query_prepare_result (q, prep_area, hostname_g,
541 /* plugin = */ "dbi", db->name,
542 column_names, column_num, /* interval = */ 0);
544 /* 0 = error; 1 = success; */
545 status = dbi_result_first_row (res); /* {{{ */
549 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
550 "dbi_result_first_row failed: %s. Maybe the statement didn't "
552 db->name, udb_query_get_name (q),
553 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
554 udb_query_finish_result (q, prep_area);
558 /* Iterate over all rows and call `udb_query_handle_result' with each list of
563 /* Copy the value of the columns to `column_values' */
564 for (i = 0; i < column_num; i++) /* {{{ */
566 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
567 column_values[i], DATA_MAX_NAME_LEN);
571 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
572 "cdbi_result_get_field (%zu) failed.",
573 db->name, udb_query_get_name (q), i + 1);
577 } /* }}} for (i = 0; i < column_num; i++) */
579 /* If all values were copied successfully, call `udb_query_handle_result'
580 * to dispatch the row to the daemon. */
581 if (status == 0) /* {{{ */
583 status = udb_query_handle_result (q, prep_area, column_values);
586 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
587 "udb_query_handle_result failed.",
588 db->name, udb_query_get_name (q));
592 /* Get the next row from the database. */
593 status = dbi_result_next_row (res); /* {{{ */
596 if (dbi_conn_error (db->connection, NULL) != 0)
599 WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
600 "dbi_result_next_row failed: %s.",
601 db->name, udb_query_get_name (q),
602 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
606 } /* }}} while (42) */
608 /* Tell the db query interface that we're done with this query. */
609 udb_query_finish_result (q, prep_area);
611 /* Clean up and return `status = 0' (success) */
614 } /* }}} int cdbi_read_database_query */
616 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
623 if (db->connection != NULL)
625 status = dbi_conn_ping (db->connection);
626 if (status != 0) /* connection is alive */
629 dbi_conn_close (db->connection);
630 db->connection = NULL;
633 driver = dbi_driver_open (db->driver);
636 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.",
638 INFO ("dbi plugin: Maybe the driver isn't installed? "
639 "Known drivers are:");
640 for (driver = dbi_driver_list (NULL);
642 driver = dbi_driver_list (driver))
644 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
649 connection = dbi_conn_open (driver);
650 if (connection == NULL)
652 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
657 /* Set all the driver options. Because this is a very very very generic
658 * interface, the error handling is kind of long. If an invalid option is
659 * encountered, it will get a list of options understood by the driver and
660 * report that as `INFO'. This way, users hopefully don't have too much
661 * trouble finding out how to configure the plugin correctly.. */
662 for (i = 0; i < db->driver_options_num; i++)
664 DEBUG ("dbi plugin: cdbi_connect_database (%s): "
665 "key = %s; value = %s;",
667 db->driver_options[i].key,
668 db->driver_options[i].value);
670 status = dbi_conn_set_option (connection,
671 db->driver_options[i].key, db->driver_options[i].value);
677 ERROR ("dbi plugin: cdbi_connect_database (%s): "
678 "dbi_conn_set_option (%s, %s) failed: %s.",
680 db->driver_options[i].key, db->driver_options[i].value,
681 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
683 INFO ("dbi plugin: This is a list of all options understood "
684 "by the `%s' driver:", db->driver);
685 for (opt = dbi_conn_get_option_list (connection, NULL);
687 opt = dbi_conn_get_option_list (connection, opt))
689 INFO ("dbi plugin: * %s", opt);
692 dbi_conn_close (connection);
695 } /* for (i = 0; i < db->driver_options_num; i++) */
697 status = dbi_conn_connect (connection);
701 ERROR ("dbi plugin: cdbi_connect_database (%s): "
702 "dbi_conn_connect failed: %s",
703 db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
704 dbi_conn_close (connection);
708 if (db->select_db != NULL)
710 status = dbi_conn_select_db (connection, db->select_db);
714 WARNING ("dbi plugin: cdbi_connect_database (%s): "
715 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
716 db->name, db->select_db,
717 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
718 dbi_conn_close (connection);
723 db->connection = connection;
725 } /* }}} int cdbi_connect_database */
727 static int cdbi_read_database (cdbi_database_t *db) /* {{{ */
733 unsigned int db_version;
735 status = cdbi_connect_database (db);
738 assert (db->connection != NULL);
740 db_version = dbi_conn_get_engine_version (db->connection);
741 /* TODO: Complain if `db_version == 0' */
744 for (i = 0; i < db->queries_num; i++)
746 /* Check if we know the database's version and if so, if this query applies
747 * to that version. */
748 if ((db_version != 0)
749 && (udb_query_check_version (db->queries[i], db_version) == 0))
752 status = cdbi_read_database_query (db,
753 db->queries[i], db->q_prep_areas[i]);
760 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
765 } /* }}} int cdbi_read_database */
767 static int cdbi_read (void) /* {{{ */
773 for (i = 0; i < databases_num; i++)
775 status = cdbi_read_database (databases[i]);
782 ERROR ("dbi plugin: No database could be read. Will return an error so "
783 "the plugin will be delayed.");
788 } /* }}} int cdbi_read */
790 static int cdbi_shutdown (void) /* {{{ */
794 for (i = 0; i < databases_num; i++)
796 if (databases[i]->connection != NULL)
798 dbi_conn_close (databases[i]->connection);
799 databases[i]->connection = NULL;
801 cdbi_database_free (databases[i]);
806 udb_query_free (queries, queries_num);
811 } /* }}} int cdbi_shutdown */
813 void module_register (void) /* {{{ */
815 plugin_register_complex_config ("dbi", cdbi_config);
816 plugin_register_init ("dbi", cdbi_init);
817 plugin_register_read ("dbi", cdbi_read);
818 plugin_register_shutdown ("dbi", cdbi_shutdown);
819 } /* }}} void module_register */
822 * vim: shiftwidth=2 softtabstop=2 et fdm=marker