3 * Copyright (C) 2008-2013 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 collectd.org>
25 #include "configfile.h"
26 #include "utils_db_query.h"
33 struct cdbi_driver_option_s /* {{{ */
43 typedef struct cdbi_driver_option_s cdbi_driver_option_t; /* }}} */
45 struct cdbi_database_s /* {{{ */
52 cdbi_driver_option_t *driver_options;
53 size_t driver_options_num;
55 udb_query_preparation_area_t **q_prep_areas;
56 udb_query_t **queries;
61 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
66 static udb_query_t **queries = NULL;
67 static size_t queries_num = 0;
68 static cdbi_database_t **databases = NULL;
69 static size_t databases_num = 0;
74 static const char *cdbi_strerror (dbi_conn conn, /* {{{ */
75 char *buffer, size_t buffer_size)
82 sstrncpy (buffer, "connection is NULL", buffer_size);
87 status = dbi_conn_error (conn, &msg);
88 if ((status >= 0) && (msg != NULL))
89 ssnprintf (buffer, buffer_size, "%s (status %i)", msg, status);
91 ssnprintf (buffer, buffer_size, "dbi_conn_error failed with status %i",
95 } /* }}} const char *cdbi_conn_error */
97 static int cdbi_result_get_field (dbi_result res, /* {{{ */
98 unsigned int index, char *buffer, size_t buffer_size)
100 unsigned short src_type;
102 src_type = dbi_result_get_field_type_idx (res, index);
103 if (src_type == DBI_TYPE_ERROR)
105 ERROR ("dbi plugin: cdbi_result_get: "
106 "dbi_result_get_field_type_idx failed.");
110 if (src_type == DBI_TYPE_INTEGER)
114 value = dbi_result_get_longlong_idx (res, index);
115 ssnprintf (buffer, buffer_size, "%lli", value);
117 else if (src_type == DBI_TYPE_DECIMAL)
121 value = dbi_result_get_double_idx (res, index);
122 ssnprintf (buffer, buffer_size, "%63.15g", value);
124 else if (src_type == DBI_TYPE_STRING)
128 value = dbi_result_get_string_idx (res, index);
130 sstrncpy (buffer, "", buffer_size);
131 else if (strcmp ("ERROR", value) == 0)
134 sstrncpy (buffer, value, buffer_size);
136 /* DBI_TYPE_BINARY */
137 /* DBI_TYPE_DATETIME */
140 const char *field_name;
142 field_name = dbi_result_get_field_name (res, index);
143 if (field_name == NULL)
144 field_name = "<unknown>";
146 ERROR ("dbi plugin: Column `%s': Don't know how to handle "
148 field_name, src_type);
153 } /* }}} int cdbi_result_get_field */
155 static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
165 for (i = 0; i < db->driver_options_num; i++)
167 sfree (db->driver_options[i].key);
168 if (!db->driver_options[i].is_numeric)
169 sfree (db->driver_options[i].value.string);
171 sfree (db->driver_options);
173 if (db->q_prep_areas)
174 for (i = 0; i < db->queries_num; ++i)
175 udb_query_delete_preparation_area (db->q_prep_areas[i]);
176 free (db->q_prep_areas);
179 } /* }}} void cdbi_database_free */
181 /* Configuration handling functions {{{
184 * <Query "plugin_instance0">
185 * Statement "SELECT name, value FROM table"
188 * InstancesFrom "name"
194 * <Database "plugin_instance1">
196 * DriverOption "hostname" "localhost"
198 * Query "plugin_instance0"
203 static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */
206 cdbi_driver_option_t *option;
208 if ((ci->values_num != 2)
209 || (ci->values[0].type != OCONFIG_TYPE_STRING)
210 || ((ci->values[1].type != OCONFIG_TYPE_STRING)
211 && (ci->values[1].type != OCONFIG_TYPE_NUMBER)))
213 WARNING ("dbi plugin: The `DriverOption' config option "
214 "needs exactly two arguments.");
218 option = (cdbi_driver_option_t *) realloc (db->driver_options,
219 sizeof (*option) * (db->driver_options_num + 1));
222 ERROR ("dbi plugin: realloc failed");
226 db->driver_options = option;
227 option = db->driver_options + db->driver_options_num;
228 memset (option, 0, sizeof (*option));
230 option->key = strdup (ci->values[0].value.string);
231 if (option->key == NULL)
233 ERROR ("dbi plugin: strdup failed.");
237 if (ci->values[1].type == OCONFIG_TYPE_STRING)
239 option->value.string = strdup (ci->values[1].value.string);
240 if (option->value.string == NULL)
242 ERROR ("dbi plugin: strdup failed.");
249 assert (ci->values[1].type == OCONFIG_TYPE_NUMBER);
250 option->value.numeric = (int) (ci->values[1].value.number + .5);
251 option->is_numeric = 1;
254 db->driver_options_num++;
256 } /* }}} int cdbi_config_add_database_driver_option */
258 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
264 if ((ci->values_num != 1)
265 || (ci->values[0].type != OCONFIG_TYPE_STRING))
267 WARNING ("dbi plugin: The `Database' block "
268 "needs exactly one string argument.");
272 db = (cdbi_database_t *) malloc (sizeof (*db));
275 ERROR ("dbi plugin: malloc failed.");
278 memset (db, 0, sizeof (*db));
280 status = cf_util_get_string (ci, &db->name);
287 /* Fill the `cdbi_database_t' structure.. */
288 for (i = 0; i < ci->children_num; i++)
290 oconfig_item_t *child = ci->children + i;
292 if (strcasecmp ("Driver", child->key) == 0)
293 status = cf_util_get_string (child, &db->driver);
294 else if (strcasecmp ("DriverOption", child->key) == 0)
295 status = cdbi_config_add_database_driver_option (db, child);
296 else if (strcasecmp ("SelectDB", child->key) == 0)
297 status = cf_util_get_string (child, &db->select_db);
298 else if (strcasecmp ("Query", child->key) == 0)
299 status = udb_query_pick_from_list (child, queries, queries_num,
300 &db->queries, &db->queries_num);
301 else if (strcasecmp ("Host", child->key) == 0)
302 status = cf_util_get_string (child, &db->host);
305 WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
313 /* Check that all necessary options have been given. */
316 if (db->driver == NULL)
318 WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
321 if (db->driver_options_num == 0)
323 WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
324 "This will likely not work.", db->name);
328 } /* while (status == 0) */
330 while ((status == 0) && (db->queries_num > 0))
332 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
333 db->queries_num, sizeof (*db->q_prep_areas));
335 if (db->q_prep_areas == NULL)
337 WARNING ("dbi plugin: malloc failed");
342 for (i = 0; i < db->queries_num; ++i)
345 = udb_query_allocate_preparation_area (db->queries[i]);
347 if (db->q_prep_areas[i] == NULL)
349 WARNING ("dbi plugin: udb_query_allocate_preparation_area failed");
358 /* If all went well, add this database to the global list of databases. */
361 cdbi_database_t **temp;
363 temp = (cdbi_database_t **) realloc (databases,
364 sizeof (*databases) * (databases_num + 1));
367 ERROR ("dbi plugin: realloc failed");
373 databases[databases_num] = db;
380 cdbi_database_free (db);
385 } /* }}} int cdbi_config_add_database */
387 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
391 for (i = 0; i < ci->children_num; i++)
393 oconfig_item_t *child = ci->children + i;
394 if (strcasecmp ("Query", child->key) == 0)
395 udb_query_create (&queries, &queries_num, child,
396 /* callback = */ NULL);
397 else if (strcasecmp ("Database", child->key) == 0)
398 cdbi_config_add_database (child);
401 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
403 } /* for (ci->children) */
406 } /* }}} int cdbi_config */
408 /* }}} End of configuration handling functions */
410 static int cdbi_init (void) /* {{{ */
412 static int did_init = 0;
418 if (queries_num == 0)
420 ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
421 "this plugin can't do anything useful, so we will returns an error.");
425 if (databases_num == 0)
427 ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
428 "this plugin can't do anything useful, so we will returns an error.");
432 status = dbi_initialize (NULL);
435 ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.",
439 else if (status == 0)
441 ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please "
442 "install at least one `DBD' or check your installation.");
445 DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.",
446 status, (status == 1) ? "" : "s");
449 } /* }}} int cdbi_init */
451 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
452 udb_query_t *q, udb_query_preparation_area_t *prep_area)
454 const char *statement;
458 char **column_values;
462 /* Macro that cleans up dynamically allocated memory and returns the
463 * specified status. */
464 #define BAIL_OUT(status) \
465 if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
466 if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
467 if (res != NULL) { dbi_result_free (res); res = NULL; } \
471 column_values = NULL;
474 statement = udb_query_get_statement (q);
475 assert (statement != NULL);
477 res = dbi_conn_query (db->connection, statement);
481 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
482 "dbi_conn_query failed: %s",
483 db->name, udb_query_get_name (q),
484 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
487 else /* Get the number of columns */
489 unsigned int db_status;
491 db_status = dbi_result_get_numfields (res);
492 if (db_status == DBI_FIELD_ERROR)
495 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
496 "dbi_result_get_numfields failed: %s",
497 db->name, udb_query_get_name (q),
498 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
502 column_num = (size_t) db_status;
503 DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
504 db->name, udb_query_get_name (q), column_num);
507 /* Allocate `column_names' and `column_values'. {{{ */
508 column_names = (char **) calloc (column_num, sizeof (char *));
509 if (column_names == NULL)
511 ERROR ("dbi plugin: malloc failed.");
515 column_names[0] = (char *) calloc (column_num,
516 DATA_MAX_NAME_LEN * sizeof (char));
517 if (column_names[0] == NULL)
519 ERROR ("dbi plugin: malloc failed.");
522 for (i = 1; i < column_num; i++)
523 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
525 column_values = (char **) calloc (column_num, sizeof (char *));
526 if (column_values == NULL)
528 ERROR ("dbi plugin: malloc failed.");
532 column_values[0] = (char *) calloc (column_num,
533 DATA_MAX_NAME_LEN * sizeof (char));
534 if (column_values[0] == NULL)
536 ERROR ("dbi plugin: malloc failed.");
539 for (i = 1; i < column_num; i++)
540 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
543 /* Copy the field names to `column_names' */
544 for (i = 0; i < column_num; i++) /* {{{ */
546 const char *column_name;
548 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
549 if (column_name == NULL)
551 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
552 "Cannot retrieve name of field %zu.",
553 db->name, udb_query_get_name (q), i + 1);
557 sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
558 } /* }}} for (i = 0; i < column_num; i++) */
560 udb_query_prepare_result (q, prep_area, (db->host ? db->host : hostname_g),
561 /* plugin = */ "dbi", db->name,
562 column_names, column_num, /* interval = */ 0);
564 /* 0 = error; 1 = success; */
565 status = dbi_result_first_row (res); /* {{{ */
569 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
570 "dbi_result_first_row failed: %s. Maybe the statement didn't "
572 db->name, udb_query_get_name (q),
573 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
574 udb_query_finish_result (q, prep_area);
578 /* Iterate over all rows and call `udb_query_handle_result' with each list of
583 /* Copy the value of the columns to `column_values' */
584 for (i = 0; i < column_num; i++) /* {{{ */
586 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
587 column_values[i], DATA_MAX_NAME_LEN);
591 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
592 "cdbi_result_get_field (%zu) failed.",
593 db->name, udb_query_get_name (q), i + 1);
597 } /* }}} for (i = 0; i < column_num; i++) */
599 /* If all values were copied successfully, call `udb_query_handle_result'
600 * to dispatch the row to the daemon. */
601 if (status == 0) /* {{{ */
603 status = udb_query_handle_result (q, prep_area, column_values);
606 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
607 "udb_query_handle_result failed.",
608 db->name, udb_query_get_name (q));
612 /* Get the next row from the database. */
613 status = dbi_result_next_row (res); /* {{{ */
616 if (dbi_conn_error (db->connection, NULL) != 0)
619 WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
620 "dbi_result_next_row failed: %s.",
621 db->name, udb_query_get_name (q),
622 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
626 } /* }}} while (42) */
628 /* Tell the db query interface that we're done with this query. */
629 udb_query_finish_result (q, prep_area);
631 /* Clean up and return `status = 0' (success) */
634 } /* }}} int cdbi_read_database_query */
636 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
643 if (db->connection != NULL)
645 status = dbi_conn_ping (db->connection);
646 if (status != 0) /* connection is alive */
649 dbi_conn_close (db->connection);
650 db->connection = NULL;
653 driver = dbi_driver_open (db->driver);
656 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.",
658 INFO ("dbi plugin: Maybe the driver isn't installed? "
659 "Known drivers are:");
660 for (driver = dbi_driver_list (NULL);
662 driver = dbi_driver_list (driver))
664 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
669 connection = dbi_conn_open (driver);
670 if (connection == NULL)
672 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
677 /* Set all the driver options. Because this is a very very very generic
678 * interface, the error handling is kind of long. If an invalid option is
679 * encountered, it will get a list of options understood by the driver and
680 * report that as `INFO'. This way, users hopefully don't have too much
681 * trouble finding out how to configure the plugin correctly.. */
682 for (i = 0; i < db->driver_options_num; i++)
684 if (db->driver_options[i].is_numeric)
686 status = dbi_conn_set_option_numeric (connection,
687 db->driver_options[i].key, db->driver_options[i].value.numeric);
691 ERROR ("dbi plugin: cdbi_connect_database (%s): "
692 "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.",
694 db->driver_options[i].key, db->driver_options[i].value.numeric,
695 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
700 status = dbi_conn_set_option (connection,
701 db->driver_options[i].key, db->driver_options[i].value.string);
705 ERROR ("dbi plugin: cdbi_connect_database (%s): "
706 "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.",
708 db->driver_options[i].key, db->driver_options[i].value.string,
709 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
717 INFO ("dbi plugin: This is a list of all options understood "
718 "by the `%s' driver:", db->driver);
719 for (opt = dbi_conn_get_option_list (connection, NULL);
721 opt = dbi_conn_get_option_list (connection, opt))
723 INFO ("dbi plugin: * %s", opt);
726 dbi_conn_close (connection);
729 } /* for (i = 0; i < db->driver_options_num; i++) */
731 status = dbi_conn_connect (connection);
735 ERROR ("dbi plugin: cdbi_connect_database (%s): "
736 "dbi_conn_connect failed: %s",
737 db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
738 dbi_conn_close (connection);
742 if (db->select_db != NULL)
744 status = dbi_conn_select_db (connection, db->select_db);
748 WARNING ("dbi plugin: cdbi_connect_database (%s): "
749 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
750 db->name, db->select_db,
751 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
752 dbi_conn_close (connection);
757 db->connection = connection;
759 } /* }}} int cdbi_connect_database */
761 static int cdbi_read_database (cdbi_database_t *db) /* {{{ */
767 unsigned int db_version;
769 status = cdbi_connect_database (db);
772 assert (db->connection != NULL);
774 db_version = dbi_conn_get_engine_version (db->connection);
775 /* TODO: Complain if `db_version == 0' */
778 for (i = 0; i < db->queries_num; i++)
780 /* Check if we know the database's version and if so, if this query applies
781 * to that version. */
782 if ((db_version != 0)
783 && (udb_query_check_version (db->queries[i], db_version) == 0))
786 status = cdbi_read_database_query (db,
787 db->queries[i], db->q_prep_areas[i]);
794 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
799 } /* }}} int cdbi_read_database */
801 static int cdbi_read (void) /* {{{ */
807 for (i = 0; i < databases_num; i++)
809 status = cdbi_read_database (databases[i]);
816 ERROR ("dbi plugin: No database could be read. Will return an error so "
817 "the plugin will be delayed.");
822 } /* }}} int cdbi_read */
824 static int cdbi_shutdown (void) /* {{{ */
828 for (i = 0; i < databases_num; i++)
830 if (databases[i]->connection != NULL)
832 dbi_conn_close (databases[i]->connection);
833 databases[i]->connection = NULL;
835 cdbi_database_free (databases[i]);
840 udb_query_free (queries, queries_num);
845 } /* }}} int cdbi_shutdown */
847 void module_register (void) /* {{{ */
849 plugin_register_complex_config ("dbi", cdbi_config);
850 plugin_register_init ("dbi", cdbi_init);
851 plugin_register_read ("dbi", cdbi_read);
852 plugin_register_shutdown ("dbi", cdbi_shutdown);
853 } /* }}} void module_register */
856 * vim: shiftwidth=2 softtabstop=2 et fdm=marker