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 /* {{{ */
51 cdbi_driver_option_t *driver_options;
52 size_t driver_options_num;
54 udb_query_preparation_area_t **q_prep_areas;
55 udb_query_t **queries;
60 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
65 static udb_query_t **queries = NULL;
66 static size_t queries_num = 0;
67 static cdbi_database_t **databases = NULL;
68 static size_t databases_num = 0;
73 static const char *cdbi_strerror (dbi_conn conn, /* {{{ */
74 char *buffer, size_t buffer_size)
81 sstrncpy (buffer, "connection is NULL", buffer_size);
86 status = dbi_conn_error (conn, &msg);
87 if ((status >= 0) && (msg != NULL))
88 ssnprintf (buffer, buffer_size, "%s (status %i)", msg, status);
90 ssnprintf (buffer, buffer_size, "dbi_conn_error failed with status %i",
94 } /* }}} const char *cdbi_conn_error */
96 static int cdbi_result_get_field (dbi_result res, /* {{{ */
97 unsigned int index, char *buffer, size_t buffer_size)
99 unsigned short src_type;
101 src_type = dbi_result_get_field_type_idx (res, index);
102 if (src_type == DBI_TYPE_ERROR)
104 ERROR ("dbi plugin: cdbi_result_get: "
105 "dbi_result_get_field_type_idx failed.");
109 if (src_type == DBI_TYPE_INTEGER)
113 value = dbi_result_get_longlong_idx (res, index);
114 ssnprintf (buffer, buffer_size, "%lli", value);
116 else if (src_type == DBI_TYPE_DECIMAL)
120 value = dbi_result_get_double_idx (res, index);
121 ssnprintf (buffer, buffer_size, "%63.15g", value);
123 else if (src_type == DBI_TYPE_STRING)
127 value = dbi_result_get_string_idx (res, index);
129 sstrncpy (buffer, "", buffer_size);
130 else if (strcmp ("ERROR", value) == 0)
133 sstrncpy (buffer, value, buffer_size);
135 /* DBI_TYPE_BINARY */
136 /* DBI_TYPE_DATETIME */
139 const char *field_name;
141 field_name = dbi_result_get_field_name (res, index);
142 if (field_name == NULL)
143 field_name = "<unknown>";
145 ERROR ("dbi plugin: Column `%s': Don't know how to handle "
147 field_name, src_type);
152 } /* }}} int cdbi_result_get_field */
154 static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
164 for (i = 0; i < db->driver_options_num; i++)
166 sfree (db->driver_options[i].key);
167 if (!db->driver_options[i].is_numeric)
168 sfree (db->driver_options[i].value.string);
170 sfree (db->driver_options);
172 if (db->q_prep_areas)
173 for (i = 0; i < db->queries_num; ++i)
174 udb_query_delete_preparation_area (db->q_prep_areas[i]);
175 free (db->q_prep_areas);
178 } /* }}} void cdbi_database_free */
180 /* Configuration handling functions {{{
183 * <Query "plugin_instance0">
184 * Statement "SELECT name, value FROM table"
187 * InstancesFrom "name"
193 * <Database "plugin_instance1">
195 * DriverOption "hostname" "localhost"
197 * Query "plugin_instance0"
202 static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */
205 cdbi_driver_option_t *option;
207 if ((ci->values_num != 2)
208 || (ci->values[0].type != OCONFIG_TYPE_STRING)
209 || ((ci->values[1].type != OCONFIG_TYPE_STRING)
210 && (ci->values[1].type != OCONFIG_TYPE_NUMBER)))
212 WARNING ("dbi plugin: The `DriverOption' config option "
213 "needs exactly two arguments.");
217 option = (cdbi_driver_option_t *) realloc (db->driver_options,
218 sizeof (*option) * (db->driver_options_num + 1));
221 ERROR ("dbi plugin: realloc failed");
225 db->driver_options = option;
226 option = db->driver_options + db->driver_options_num;
227 memset (option, 0, sizeof (*option));
229 option->key = strdup (ci->values[0].value.string);
230 if (option->key == NULL)
232 ERROR ("dbi plugin: strdup failed.");
236 if (ci->values[1].type == OCONFIG_TYPE_STRING)
238 option->value.string = strdup (ci->values[1].value.string);
239 if (option->value.string == NULL)
241 ERROR ("dbi plugin: strdup failed.");
248 assert (ci->values[1].type == OCONFIG_TYPE_NUMBER);
249 option->value.numeric = (int) (ci->values[1].value.number + .5);
250 option->is_numeric = 1;
253 db->driver_options_num++;
255 } /* }}} int cdbi_config_add_database_driver_option */
257 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
263 if ((ci->values_num != 1)
264 || (ci->values[0].type != OCONFIG_TYPE_STRING))
266 WARNING ("dbi plugin: The `Database' block "
267 "needs exactly one string argument.");
271 db = (cdbi_database_t *) malloc (sizeof (*db));
274 ERROR ("dbi plugin: malloc failed.");
277 memset (db, 0, sizeof (*db));
279 status = cf_util_get_string (ci, &db->name);
286 /* Fill the `cdbi_database_t' structure.. */
287 for (i = 0; i < ci->children_num; i++)
289 oconfig_item_t *child = ci->children + i;
291 if (strcasecmp ("Driver", child->key) == 0)
292 status = cf_util_get_string (child, &db->driver);
293 else if (strcasecmp ("DriverOption", child->key) == 0)
294 status = cdbi_config_add_database_driver_option (db, child);
295 else if (strcasecmp ("SelectDB", child->key) == 0)
296 status = cf_util_get_string (child, &db->select_db);
297 else if (strcasecmp ("Query", child->key) == 0)
298 status = udb_query_pick_from_list (child, queries, queries_num,
299 &db->queries, &db->queries_num);
302 WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
310 /* Check that all necessary options have been given. */
313 if (db->driver == NULL)
315 WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
318 if (db->driver_options_num == 0)
320 WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
321 "This will likely not work.", db->name);
325 } /* while (status == 0) */
327 while ((status == 0) && (db->queries_num > 0))
329 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
330 db->queries_num, sizeof (*db->q_prep_areas));
332 if (db->q_prep_areas == NULL)
334 WARNING ("dbi plugin: malloc failed");
339 for (i = 0; i < db->queries_num; ++i)
342 = udb_query_allocate_preparation_area (db->queries[i]);
344 if (db->q_prep_areas[i] == NULL)
346 WARNING ("dbi plugin: udb_query_allocate_preparation_area failed");
355 /* If all went well, add this database to the global list of databases. */
358 cdbi_database_t **temp;
360 temp = (cdbi_database_t **) realloc (databases,
361 sizeof (*databases) * (databases_num + 1));
364 ERROR ("dbi plugin: realloc failed");
370 databases[databases_num] = db;
377 cdbi_database_free (db);
382 } /* }}} int cdbi_config_add_database */
384 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
388 for (i = 0; i < ci->children_num; i++)
390 oconfig_item_t *child = ci->children + i;
391 if (strcasecmp ("Query", child->key) == 0)
392 udb_query_create (&queries, &queries_num, child,
393 /* callback = */ NULL);
394 else if (strcasecmp ("Database", child->key) == 0)
395 cdbi_config_add_database (child);
398 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
400 } /* for (ci->children) */
403 } /* }}} int cdbi_config */
405 /* }}} End of configuration handling functions */
407 static int cdbi_init (void) /* {{{ */
409 static int did_init = 0;
415 if (queries_num == 0)
417 ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
418 "this plugin can't do anything useful, so we will returns an error.");
422 if (databases_num == 0)
424 ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
425 "this plugin can't do anything useful, so we will returns an error.");
429 status = dbi_initialize (NULL);
432 ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.",
436 else if (status == 0)
438 ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please "
439 "install at least one `DBD' or check your installation.");
442 DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.",
443 status, (status == 1) ? "" : "s");
446 } /* }}} int cdbi_init */
448 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
449 udb_query_t *q, udb_query_preparation_area_t *prep_area)
451 const char *statement;
455 char **column_values;
459 /* Macro that cleans up dynamically allocated memory and returns the
460 * specified status. */
461 #define BAIL_OUT(status) \
462 if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
463 if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
464 if (res != NULL) { dbi_result_free (res); res = NULL; } \
468 column_values = NULL;
471 statement = udb_query_get_statement (q);
472 assert (statement != NULL);
474 res = dbi_conn_query (db->connection, statement);
478 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
479 "dbi_conn_query failed: %s",
480 db->name, udb_query_get_name (q),
481 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
484 else /* Get the number of columns */
486 unsigned int db_status;
488 db_status = dbi_result_get_numfields (res);
489 if (db_status == DBI_FIELD_ERROR)
492 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
493 "dbi_result_get_numfields failed: %s",
494 db->name, udb_query_get_name (q),
495 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
499 column_num = (size_t) db_status;
500 DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
501 db->name, udb_query_get_name (q), column_num);
504 /* Allocate `column_names' and `column_values'. {{{ */
505 column_names = (char **) calloc (column_num, sizeof (char *));
506 if (column_names == NULL)
508 ERROR ("dbi plugin: malloc failed.");
512 column_names[0] = (char *) calloc (column_num,
513 DATA_MAX_NAME_LEN * sizeof (char));
514 if (column_names[0] == NULL)
516 ERROR ("dbi plugin: malloc failed.");
519 for (i = 1; i < column_num; i++)
520 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
522 column_values = (char **) calloc (column_num, sizeof (char *));
523 if (column_values == NULL)
525 ERROR ("dbi plugin: malloc failed.");
529 column_values[0] = (char *) calloc (column_num,
530 DATA_MAX_NAME_LEN * sizeof (char));
531 if (column_values[0] == NULL)
533 ERROR ("dbi plugin: malloc failed.");
536 for (i = 1; i < column_num; i++)
537 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
540 /* Copy the field names to `column_names' */
541 for (i = 0; i < column_num; i++) /* {{{ */
543 const char *column_name;
545 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
546 if (column_name == NULL)
548 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
549 "Cannot retrieve name of field %zu.",
550 db->name, udb_query_get_name (q), i + 1);
554 sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
555 } /* }}} for (i = 0; i < column_num; i++) */
557 udb_query_prepare_result (q, prep_area, hostname_g,
558 /* plugin = */ "dbi", db->name,
559 column_names, column_num, /* interval = */ 0);
561 /* 0 = error; 1 = success; */
562 status = dbi_result_first_row (res); /* {{{ */
566 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
567 "dbi_result_first_row failed: %s. Maybe the statement didn't "
569 db->name, udb_query_get_name (q),
570 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
571 udb_query_finish_result (q, prep_area);
575 /* Iterate over all rows and call `udb_query_handle_result' with each list of
580 /* Copy the value of the columns to `column_values' */
581 for (i = 0; i < column_num; i++) /* {{{ */
583 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
584 column_values[i], DATA_MAX_NAME_LEN);
588 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
589 "cdbi_result_get_field (%zu) failed.",
590 db->name, udb_query_get_name (q), i + 1);
594 } /* }}} for (i = 0; i < column_num; i++) */
596 /* If all values were copied successfully, call `udb_query_handle_result'
597 * to dispatch the row to the daemon. */
598 if (status == 0) /* {{{ */
600 status = udb_query_handle_result (q, prep_area, column_values);
603 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
604 "udb_query_handle_result failed.",
605 db->name, udb_query_get_name (q));
609 /* Get the next row from the database. */
610 status = dbi_result_next_row (res); /* {{{ */
613 if (dbi_conn_error (db->connection, NULL) != 0)
616 WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
617 "dbi_result_next_row failed: %s.",
618 db->name, udb_query_get_name (q),
619 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
623 } /* }}} while (42) */
625 /* Tell the db query interface that we're done with this query. */
626 udb_query_finish_result (q, prep_area);
628 /* Clean up and return `status = 0' (success) */
631 } /* }}} int cdbi_read_database_query */
633 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
640 if (db->connection != NULL)
642 status = dbi_conn_ping (db->connection);
643 if (status != 0) /* connection is alive */
646 dbi_conn_close (db->connection);
647 db->connection = NULL;
650 driver = dbi_driver_open (db->driver);
653 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.",
655 INFO ("dbi plugin: Maybe the driver isn't installed? "
656 "Known drivers are:");
657 for (driver = dbi_driver_list (NULL);
659 driver = dbi_driver_list (driver))
661 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
666 connection = dbi_conn_open (driver);
667 if (connection == NULL)
669 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
674 /* Set all the driver options. Because this is a very very very generic
675 * interface, the error handling is kind of long. If an invalid option is
676 * encountered, it will get a list of options understood by the driver and
677 * report that as `INFO'. This way, users hopefully don't have too much
678 * trouble finding out how to configure the plugin correctly.. */
679 for (i = 0; i < db->driver_options_num; i++)
681 if (db->driver_options[i].is_numeric)
683 status = dbi_conn_set_option_numeric (connection,
684 db->driver_options[i].key, db->driver_options[i].value.numeric);
688 ERROR ("dbi plugin: cdbi_connect_database (%s): "
689 "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.",
691 db->driver_options[i].key, db->driver_options[i].value.numeric,
692 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
697 status = dbi_conn_set_option (connection,
698 db->driver_options[i].key, db->driver_options[i].value.string);
702 ERROR ("dbi plugin: cdbi_connect_database (%s): "
703 "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.",
705 db->driver_options[i].key, db->driver_options[i].value.string,
706 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
714 INFO ("dbi plugin: This is a list of all options understood "
715 "by the `%s' driver:", db->driver);
716 for (opt = dbi_conn_get_option_list (connection, NULL);
718 opt = dbi_conn_get_option_list (connection, opt))
720 INFO ("dbi plugin: * %s", opt);
723 dbi_conn_close (connection);
726 } /* for (i = 0; i < db->driver_options_num; i++) */
728 status = dbi_conn_connect (connection);
732 ERROR ("dbi plugin: cdbi_connect_database (%s): "
733 "dbi_conn_connect failed: %s",
734 db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
735 dbi_conn_close (connection);
739 if (db->select_db != NULL)
741 status = dbi_conn_select_db (connection, db->select_db);
745 WARNING ("dbi plugin: cdbi_connect_database (%s): "
746 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
747 db->name, db->select_db,
748 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
749 dbi_conn_close (connection);
754 db->connection = connection;
756 } /* }}} int cdbi_connect_database */
758 static int cdbi_read_database (cdbi_database_t *db) /* {{{ */
764 unsigned int db_version;
766 status = cdbi_connect_database (db);
769 assert (db->connection != NULL);
771 db_version = dbi_conn_get_engine_version (db->connection);
772 /* TODO: Complain if `db_version == 0' */
775 for (i = 0; i < db->queries_num; i++)
777 /* Check if we know the database's version and if so, if this query applies
778 * to that version. */
779 if ((db_version != 0)
780 && (udb_query_check_version (db->queries[i], db_version) == 0))
783 status = cdbi_read_database_query (db,
784 db->queries[i], db->q_prep_areas[i]);
791 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
796 } /* }}} int cdbi_read_database */
798 static int cdbi_read (void) /* {{{ */
804 for (i = 0; i < databases_num; i++)
806 status = cdbi_read_database (databases[i]);
813 ERROR ("dbi plugin: No database could be read. Will return an error so "
814 "the plugin will be delayed.");
819 } /* }}} int cdbi_read */
821 static int cdbi_shutdown (void) /* {{{ */
825 for (i = 0; i < databases_num; i++)
827 if (databases[i]->connection != NULL)
829 dbi_conn_close (databases[i]->connection);
830 databases[i]->connection = NULL;
832 cdbi_database_free (databases[i]);
837 udb_query_free (queries, queries_num);
842 } /* }}} int cdbi_shutdown */
844 void module_register (void) /* {{{ */
846 plugin_register_complex_config ("dbi", cdbi_config);
847 plugin_register_init ("dbi", cdbi_init);
848 plugin_register_read ("dbi", cdbi_read);
849 plugin_register_shutdown ("dbi", cdbi_shutdown);
850 } /* }}} void module_register */
853 * vim: shiftwidth=2 softtabstop=2 et fdm=marker