3 * Copyright (C) 2008-2013 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
30 #include "configfile.h"
31 #include "utils_db_query.h"
41 struct cdbi_driver_option_s /* {{{ */
51 typedef struct cdbi_driver_option_s cdbi_driver_option_t; /* }}} */
53 struct cdbi_database_s /* {{{ */
60 cdbi_driver_option_t *driver_options;
61 size_t driver_options_num;
63 udb_query_preparation_area_t **q_prep_areas;
64 udb_query_t **queries;
69 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
74 static udb_query_t **queries = NULL;
75 static size_t queries_num = 0;
76 static cdbi_database_t **databases = NULL;
77 static size_t databases_num = 0;
79 static int cdbi_read_database (user_data_t *ud);
84 static const char *cdbi_strerror (dbi_conn conn, /* {{{ */
85 char *buffer, size_t buffer_size)
92 sstrncpy (buffer, "connection is NULL", buffer_size);
97 status = dbi_conn_error (conn, &msg);
98 if ((status >= 0) && (msg != NULL))
99 ssnprintf (buffer, buffer_size, "%s (status %i)", msg, status);
101 ssnprintf (buffer, buffer_size, "dbi_conn_error failed with status %i",
105 } /* }}} const char *cdbi_conn_error */
107 static int cdbi_result_get_field (dbi_result res, /* {{{ */
108 unsigned int index, char *buffer, size_t buffer_size)
110 unsigned short src_type;
112 src_type = dbi_result_get_field_type_idx (res, index);
113 if (src_type == DBI_TYPE_ERROR)
115 ERROR ("dbi plugin: cdbi_result_get: "
116 "dbi_result_get_field_type_idx failed.");
120 if (src_type == DBI_TYPE_INTEGER)
124 value = dbi_result_get_longlong_idx (res, index);
125 ssnprintf (buffer, buffer_size, "%lli", value);
127 else if (src_type == DBI_TYPE_DECIMAL)
131 value = dbi_result_get_double_idx (res, index);
132 ssnprintf (buffer, buffer_size, "%63.15g", value);
134 else if (src_type == DBI_TYPE_STRING)
138 value = dbi_result_get_string_idx (res, index);
140 sstrncpy (buffer, "", buffer_size);
141 else if (strcmp ("ERROR", value) == 0)
144 sstrncpy (buffer, value, buffer_size);
146 /* DBI_TYPE_BINARY */
147 /* DBI_TYPE_DATETIME */
150 const char *field_name;
152 field_name = dbi_result_get_field_name (res, index);
153 if (field_name == NULL)
154 field_name = "<unknown>";
156 ERROR ("dbi plugin: Column `%s': Don't know how to handle "
158 field_name, src_type);
163 } /* }}} int cdbi_result_get_field */
165 static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
175 for (i = 0; i < db->driver_options_num; i++)
177 sfree (db->driver_options[i].key);
178 if (!db->driver_options[i].is_numeric)
179 sfree (db->driver_options[i].value.string);
181 sfree (db->driver_options);
183 if (db->q_prep_areas)
184 for (i = 0; i < db->queries_num; ++i)
185 udb_query_delete_preparation_area (db->q_prep_areas[i]);
186 free (db->q_prep_areas);
189 } /* }}} void cdbi_database_free */
191 /* Configuration handling functions {{{
194 * <Query "plugin_instance0">
195 * Statement "SELECT name, value FROM table"
198 * InstancesFrom "name"
204 * <Database "plugin_instance1">
206 * DriverOption "hostname" "localhost"
208 * Query "plugin_instance0"
213 static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */
216 cdbi_driver_option_t *option;
218 if ((ci->values_num != 2)
219 || (ci->values[0].type != OCONFIG_TYPE_STRING)
220 || ((ci->values[1].type != OCONFIG_TYPE_STRING)
221 && (ci->values[1].type != OCONFIG_TYPE_NUMBER)))
223 WARNING ("dbi plugin: The `DriverOption' config option "
224 "needs exactly two arguments.");
228 option = (cdbi_driver_option_t *) realloc (db->driver_options,
229 sizeof (*option) * (db->driver_options_num + 1));
232 ERROR ("dbi plugin: realloc failed");
236 db->driver_options = option;
237 option = db->driver_options + db->driver_options_num;
238 memset (option, 0, sizeof (*option));
240 option->key = strdup (ci->values[0].value.string);
241 if (option->key == NULL)
243 ERROR ("dbi plugin: strdup failed.");
247 if (ci->values[1].type == OCONFIG_TYPE_STRING)
249 option->value.string = strdup (ci->values[1].value.string);
250 if (option->value.string == NULL)
252 ERROR ("dbi plugin: strdup failed.");
259 assert (ci->values[1].type == OCONFIG_TYPE_NUMBER);
260 option->value.numeric = (int) (ci->values[1].value.number + .5);
261 option->is_numeric = 1;
264 db->driver_options_num++;
266 } /* }}} int cdbi_config_add_database_driver_option */
268 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
274 if ((ci->values_num != 1)
275 || (ci->values[0].type != OCONFIG_TYPE_STRING))
277 WARNING ("dbi plugin: The `Database' block "
278 "needs exactly one string argument.");
282 db = (cdbi_database_t *) malloc (sizeof (*db));
285 ERROR ("dbi plugin: malloc failed.");
288 memset (db, 0, sizeof (*db));
290 status = cf_util_get_string (ci, &db->name);
297 /* Fill the `cdbi_database_t' structure.. */
298 for (i = 0; i < ci->children_num; i++)
300 oconfig_item_t *child = ci->children + i;
302 if (strcasecmp ("Driver", child->key) == 0)
303 status = cf_util_get_string (child, &db->driver);
304 else if (strcasecmp ("DriverOption", child->key) == 0)
305 status = cdbi_config_add_database_driver_option (db, child);
306 else if (strcasecmp ("SelectDB", child->key) == 0)
307 status = cf_util_get_string (child, &db->select_db);
308 else if (strcasecmp ("Query", child->key) == 0)
309 status = udb_query_pick_from_list (child, queries, queries_num,
310 &db->queries, &db->queries_num);
311 else if (strcasecmp ("Host", child->key) == 0)
312 status = cf_util_get_string (child, &db->host);
315 WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
323 /* Check that all necessary options have been given. */
326 if (db->driver == NULL)
328 WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
331 if (db->driver_options_num == 0)
333 WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
334 "This will likely not work.", db->name);
338 } /* while (status == 0) */
340 while ((status == 0) && (db->queries_num > 0))
342 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
343 db->queries_num, sizeof (*db->q_prep_areas));
345 if (db->q_prep_areas == NULL)
347 WARNING ("dbi plugin: malloc failed");
352 for (i = 0; i < db->queries_num; ++i)
355 = udb_query_allocate_preparation_area (db->queries[i]);
357 if (db->q_prep_areas[i] == NULL)
359 WARNING ("dbi plugin: udb_query_allocate_preparation_area failed");
368 /* If all went well, add this database to the global list of databases. */
371 cdbi_database_t **temp;
373 temp = (cdbi_database_t **) realloc (databases,
374 sizeof (*databases) * (databases_num + 1));
377 ERROR ("dbi plugin: realloc failed");
386 databases[databases_num] = db;
389 memset (&ud, 0, sizeof (ud));
390 ud.data = (void *) db;
392 name = ssnprintf_alloc("dbi:%s", db->name);
394 plugin_register_complex_read (/* group = */ NULL,
395 /* name = */ name ? name : db->name,
396 /* callback = */ cdbi_read_database,
397 /* interval = */ NULL,
398 /* user_data = */ &ud);
405 cdbi_database_free (db);
410 } /* }}} int cdbi_config_add_database */
412 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
416 for (i = 0; i < ci->children_num; i++)
418 oconfig_item_t *child = ci->children + i;
419 if (strcasecmp ("Query", child->key) == 0)
420 udb_query_create (&queries, &queries_num, child,
421 /* callback = */ NULL);
422 else if (strcasecmp ("Database", child->key) == 0)
423 cdbi_config_add_database (child);
426 WARNING ("dbi plugin: Ignoring unknown config option `%s'.", child->key);
428 } /* for (ci->children) */
431 } /* }}} int cdbi_config */
433 /* }}} End of configuration handling functions */
435 static int cdbi_init (void) /* {{{ */
437 static int did_init = 0;
443 if (queries_num == 0)
445 ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
446 "this plugin can't do anything useful, so we will returns an error.");
450 if (databases_num == 0)
452 ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
453 "this plugin can't do anything useful, so we will returns an error.");
458 status = dbi_initialize_r (NULL, &inst);
460 status = dbi_initialize (NULL);
464 ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.",
468 else if (status == 0)
470 ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please "
471 "install at least one `DBD' or check your installation.");
474 DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.",
475 status, (status == 1) ? "" : "s");
478 } /* }}} int cdbi_init */
480 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
481 udb_query_t *q, udb_query_preparation_area_t *prep_area)
483 const char *statement;
487 char **column_values;
491 /* Macro that cleans up dynamically allocated memory and returns the
492 * specified status. */
493 #define BAIL_OUT(status) \
494 if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
495 if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
496 if (res != NULL) { dbi_result_free (res); res = NULL; } \
500 column_values = NULL;
503 statement = udb_query_get_statement (q);
504 assert (statement != NULL);
506 res = dbi_conn_query (db->connection, statement);
510 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
511 "dbi_conn_query failed: %s",
512 db->name, udb_query_get_name (q),
513 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
516 else /* Get the number of columns */
518 unsigned int db_status;
520 db_status = dbi_result_get_numfields (res);
521 if (db_status == DBI_FIELD_ERROR)
524 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
525 "dbi_result_get_numfields failed: %s",
526 db->name, udb_query_get_name (q),
527 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
531 column_num = (size_t) db_status;
532 DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
533 db->name, udb_query_get_name (q), column_num);
536 /* Allocate `column_names' and `column_values'. {{{ */
537 column_names = (char **) calloc (column_num, sizeof (char *));
538 if (column_names == NULL)
540 ERROR ("dbi plugin: malloc failed.");
544 column_names[0] = (char *) calloc (column_num,
545 DATA_MAX_NAME_LEN * sizeof (char));
546 if (column_names[0] == NULL)
548 ERROR ("dbi plugin: malloc failed.");
551 for (i = 1; i < column_num; i++)
552 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
554 column_values = (char **) calloc (column_num, sizeof (char *));
555 if (column_values == NULL)
557 ERROR ("dbi plugin: malloc failed.");
561 column_values[0] = (char *) calloc (column_num,
562 DATA_MAX_NAME_LEN * sizeof (char));
563 if (column_values[0] == NULL)
565 ERROR ("dbi plugin: malloc failed.");
568 for (i = 1; i < column_num; i++)
569 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
572 /* Copy the field names to `column_names' */
573 for (i = 0; i < column_num; i++) /* {{{ */
575 const char *column_name;
577 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
578 if (column_name == NULL)
580 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
581 "Cannot retrieve name of field %zu.",
582 db->name, udb_query_get_name (q), i + 1);
586 sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
587 } /* }}} for (i = 0; i < column_num; i++) */
589 udb_query_prepare_result (q, prep_area, (db->host ? db->host : hostname_g),
590 /* plugin = */ "dbi", db->name,
591 column_names, column_num, /* interval = */ 0);
593 /* 0 = error; 1 = success; */
594 status = dbi_result_first_row (res); /* {{{ */
598 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
599 "dbi_result_first_row failed: %s. Maybe the statement didn't "
601 db->name, udb_query_get_name (q),
602 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
603 udb_query_finish_result (q, prep_area);
607 /* Iterate over all rows and call `udb_query_handle_result' with each list of
612 /* Copy the value of the columns to `column_values' */
613 for (i = 0; i < column_num; i++) /* {{{ */
615 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
616 column_values[i], DATA_MAX_NAME_LEN);
620 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
621 "cdbi_result_get_field (%zu) failed.",
622 db->name, udb_query_get_name (q), i + 1);
626 } /* }}} for (i = 0; i < column_num; i++) */
628 /* If all values were copied successfully, call `udb_query_handle_result'
629 * to dispatch the row to the daemon. */
630 if (status == 0) /* {{{ */
632 status = udb_query_handle_result (q, prep_area, column_values);
635 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
636 "udb_query_handle_result failed.",
637 db->name, udb_query_get_name (q));
641 /* Get the next row from the database. */
642 status = dbi_result_next_row (res); /* {{{ */
645 if (dbi_conn_error (db->connection, NULL) != 0)
648 WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
649 "dbi_result_next_row failed: %s.",
650 db->name, udb_query_get_name (q),
651 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
655 } /* }}} while (42) */
657 /* Tell the db query interface that we're done with this query. */
658 udb_query_finish_result (q, prep_area);
660 /* Clean up and return `status = 0' (success) */
663 } /* }}} int cdbi_read_database_query */
665 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
672 if (db->connection != NULL)
674 status = dbi_conn_ping (db->connection);
675 if (status != 0) /* connection is alive */
678 dbi_conn_close (db->connection);
679 db->connection = NULL;
683 driver = dbi_driver_open_r (db->driver, inst);
685 driver = dbi_driver_open (db->driver);
689 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.",
691 INFO ("dbi plugin: Maybe the driver isn't installed? "
692 "Known drivers are:");
694 for (driver = dbi_driver_list_r (NULL, inst);
696 driver = dbi_driver_list_r (driver, inst))
698 for (driver = dbi_driver_list (NULL);
700 driver = dbi_driver_list (driver))
703 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
708 connection = dbi_conn_open (driver);
709 if (connection == NULL)
711 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
716 /* Set all the driver options. Because this is a very very very generic
717 * interface, the error handling is kind of long. If an invalid option is
718 * encountered, it will get a list of options understood by the driver and
719 * report that as `INFO'. This way, users hopefully don't have too much
720 * trouble finding out how to configure the plugin correctly.. */
721 for (i = 0; i < db->driver_options_num; i++)
723 if (db->driver_options[i].is_numeric)
725 status = dbi_conn_set_option_numeric (connection,
726 db->driver_options[i].key, db->driver_options[i].value.numeric);
730 ERROR ("dbi plugin: cdbi_connect_database (%s): "
731 "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.",
733 db->driver_options[i].key, db->driver_options[i].value.numeric,
734 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
739 status = dbi_conn_set_option (connection,
740 db->driver_options[i].key, db->driver_options[i].value.string);
744 ERROR ("dbi plugin: cdbi_connect_database (%s): "
745 "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.",
747 db->driver_options[i].key, db->driver_options[i].value.string,
748 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
756 INFO ("dbi plugin: This is a list of all options understood "
757 "by the `%s' driver:", db->driver);
758 for (opt = dbi_conn_get_option_list (connection, NULL);
760 opt = dbi_conn_get_option_list (connection, opt))
762 INFO ("dbi plugin: * %s", opt);
765 dbi_conn_close (connection);
768 } /* for (i = 0; i < db->driver_options_num; i++) */
770 status = dbi_conn_connect (connection);
774 ERROR ("dbi plugin: cdbi_connect_database (%s): "
775 "dbi_conn_connect failed: %s",
776 db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
777 dbi_conn_close (connection);
781 if (db->select_db != NULL)
783 status = dbi_conn_select_db (connection, db->select_db);
787 WARNING ("dbi plugin: cdbi_connect_database (%s): "
788 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
789 db->name, db->select_db,
790 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
791 dbi_conn_close (connection);
796 db->connection = connection;
798 } /* }}} int cdbi_connect_database */
800 static int cdbi_read_database (user_data_t *ud) /* {{{ */
802 cdbi_database_t *db = (cdbi_database_t *) ud->data;
807 unsigned int db_version;
809 status = cdbi_connect_database (db);
812 assert (db->connection != NULL);
814 db_version = dbi_conn_get_engine_version (db->connection);
815 /* TODO: Complain if `db_version == 0' */
818 for (i = 0; i < db->queries_num; i++)
820 /* Check if we know the database's version and if so, if this query applies
821 * to that version. */
822 if ((db_version != 0)
823 && (udb_query_check_version (db->queries[i], db_version) == 0))
826 status = cdbi_read_database_query (db,
827 db->queries[i], db->q_prep_areas[i]);
834 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
839 } /* }}} int cdbi_read_database */
841 static int cdbi_shutdown (void) /* {{{ */
845 for (i = 0; i < databases_num; i++)
847 if (databases[i]->connection != NULL)
849 dbi_conn_close (databases[i]->connection);
850 databases[i]->connection = NULL;
852 cdbi_database_free (databases[i]);
857 udb_query_free (queries, queries_num);
862 } /* }}} int cdbi_shutdown */
864 void module_register (void) /* {{{ */
866 plugin_register_complex_config ("dbi", cdbi_config);
867 plugin_register_init ("dbi", cdbi_init);
868 plugin_register_shutdown ("dbi", cdbi_shutdown);
869 } /* }}} void module_register */
872 * vim: shiftwidth=2 softtabstop=2 et fdm=marker