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"
38 struct cdbi_driver_option_s /* {{{ */
48 typedef struct cdbi_driver_option_s cdbi_driver_option_t; /* }}} */
50 struct cdbi_database_s /* {{{ */
57 cdbi_driver_option_t *driver_options;
58 size_t driver_options_num;
60 udb_query_preparation_area_t **q_prep_areas;
61 udb_query_t **queries;
66 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
71 static udb_query_t **queries = NULL;
72 static size_t queries_num = 0;
73 static cdbi_database_t **databases = NULL;
74 static size_t databases_num = 0;
76 static int cdbi_read_database (user_data_t *ud);
81 static const char *cdbi_strerror (dbi_conn conn, /* {{{ */
82 char *buffer, size_t buffer_size)
89 sstrncpy (buffer, "connection is NULL", buffer_size);
94 status = dbi_conn_error (conn, &msg);
95 if ((status >= 0) && (msg != NULL))
96 ssnprintf (buffer, buffer_size, "%s (status %i)", msg, status);
98 ssnprintf (buffer, buffer_size, "dbi_conn_error failed with status %i",
102 } /* }}} const char *cdbi_conn_error */
104 static int cdbi_result_get_field (dbi_result res, /* {{{ */
105 unsigned int index, char *buffer, size_t buffer_size)
107 unsigned short src_type;
109 src_type = dbi_result_get_field_type_idx (res, index);
110 if (src_type == DBI_TYPE_ERROR)
112 ERROR ("dbi plugin: cdbi_result_get: "
113 "dbi_result_get_field_type_idx failed.");
117 if (src_type == DBI_TYPE_INTEGER)
121 value = dbi_result_get_longlong_idx (res, index);
122 ssnprintf (buffer, buffer_size, "%lli", value);
124 else if (src_type == DBI_TYPE_DECIMAL)
128 value = dbi_result_get_double_idx (res, index);
129 ssnprintf (buffer, buffer_size, "%63.15g", value);
131 else if (src_type == DBI_TYPE_STRING)
135 value = dbi_result_get_string_idx (res, index);
137 sstrncpy (buffer, "", buffer_size);
138 else if (strcmp ("ERROR", value) == 0)
141 sstrncpy (buffer, value, buffer_size);
143 /* DBI_TYPE_BINARY */
144 /* DBI_TYPE_DATETIME */
147 const char *field_name;
149 field_name = dbi_result_get_field_name (res, index);
150 if (field_name == NULL)
151 field_name = "<unknown>";
153 ERROR ("dbi plugin: Column `%s': Don't know how to handle "
155 field_name, src_type);
160 } /* }}} int cdbi_result_get_field */
162 static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
172 for (i = 0; i < db->driver_options_num; i++)
174 sfree (db->driver_options[i].key);
175 if (!db->driver_options[i].is_numeric)
176 sfree (db->driver_options[i].value.string);
178 sfree (db->driver_options);
180 if (db->q_prep_areas)
181 for (i = 0; i < db->queries_num; ++i)
182 udb_query_delete_preparation_area (db->q_prep_areas[i]);
183 free (db->q_prep_areas);
186 } /* }}} void cdbi_database_free */
188 /* Configuration handling functions {{{
191 * <Query "plugin_instance0">
192 * Statement "SELECT name, value FROM table"
195 * InstancesFrom "name"
201 * <Database "plugin_instance1">
203 * DriverOption "hostname" "localhost"
205 * Query "plugin_instance0"
210 static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */
213 cdbi_driver_option_t *option;
215 if ((ci->values_num != 2)
216 || (ci->values[0].type != OCONFIG_TYPE_STRING)
217 || ((ci->values[1].type != OCONFIG_TYPE_STRING)
218 && (ci->values[1].type != OCONFIG_TYPE_NUMBER)))
220 WARNING ("dbi plugin: The `DriverOption' config option "
221 "needs exactly two arguments.");
225 option = (cdbi_driver_option_t *) realloc (db->driver_options,
226 sizeof (*option) * (db->driver_options_num + 1));
229 ERROR ("dbi plugin: realloc failed");
233 db->driver_options = option;
234 option = db->driver_options + db->driver_options_num;
235 memset (option, 0, sizeof (*option));
237 option->key = strdup (ci->values[0].value.string);
238 if (option->key == NULL)
240 ERROR ("dbi plugin: strdup failed.");
244 if (ci->values[1].type == OCONFIG_TYPE_STRING)
246 option->value.string = strdup (ci->values[1].value.string);
247 if (option->value.string == NULL)
249 ERROR ("dbi plugin: strdup failed.");
256 assert (ci->values[1].type == OCONFIG_TYPE_NUMBER);
257 option->value.numeric = (int) (ci->values[1].value.number + .5);
258 option->is_numeric = 1;
261 db->driver_options_num++;
263 } /* }}} int cdbi_config_add_database_driver_option */
265 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
271 if ((ci->values_num != 1)
272 || (ci->values[0].type != OCONFIG_TYPE_STRING))
274 WARNING ("dbi plugin: The `Database' block "
275 "needs exactly one string argument.");
279 db = (cdbi_database_t *) malloc (sizeof (*db));
282 ERROR ("dbi plugin: malloc failed.");
285 memset (db, 0, sizeof (*db));
287 status = cf_util_get_string (ci, &db->name);
294 /* Fill the `cdbi_database_t' structure.. */
295 for (i = 0; i < ci->children_num; i++)
297 oconfig_item_t *child = ci->children + i;
299 if (strcasecmp ("Driver", child->key) == 0)
300 status = cf_util_get_string (child, &db->driver);
301 else if (strcasecmp ("DriverOption", child->key) == 0)
302 status = cdbi_config_add_database_driver_option (db, child);
303 else if (strcasecmp ("SelectDB", child->key) == 0)
304 status = cf_util_get_string (child, &db->select_db);
305 else if (strcasecmp ("Query", child->key) == 0)
306 status = udb_query_pick_from_list (child, queries, queries_num,
307 &db->queries, &db->queries_num);
308 else if (strcasecmp ("Host", child->key) == 0)
309 status = cf_util_get_string (child, &db->host);
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");
383 databases[databases_num] = db;
386 memset (&ud, 0, sizeof (ud));
387 ud.data = (void *) db;
389 name = ssnprintf_alloc("dbi:%s", db->name);
391 plugin_register_complex_read (/* group = */ NULL,
392 /* name = */ name ? name : db->name,
393 /* callback = */ cdbi_read_database,
394 /* interval = */ NULL,
395 /* user_data = */ &ud);
402 cdbi_database_free (db);
407 } /* }}} int cdbi_config_add_database */
409 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
413 for (i = 0; i < ci->children_num; i++)
415 oconfig_item_t *child = ci->children + i;
416 if (strcasecmp ("Query", child->key) == 0)
417 udb_query_create (&queries, &queries_num, child,
418 /* callback = */ NULL);
419 else if (strcasecmp ("Database", child->key) == 0)
420 cdbi_config_add_database (child);
423 WARNING ("dbi plugin: Ignoring unknown config option `%s'.", child->key);
425 } /* for (ci->children) */
428 } /* }}} int cdbi_config */
430 /* }}} End of configuration handling functions */
432 static int cdbi_init (void) /* {{{ */
434 static int did_init = 0;
440 if (queries_num == 0)
442 ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
443 "this plugin can't do anything useful, so we will returns an error.");
447 if (databases_num == 0)
449 ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
450 "this plugin can't do anything useful, so we will returns an error.");
454 status = dbi_initialize (NULL);
457 ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.",
461 else if (status == 0)
463 ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please "
464 "install at least one `DBD' or check your installation.");
467 DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.",
468 status, (status == 1) ? "" : "s");
471 } /* }}} int cdbi_init */
473 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
474 udb_query_t *q, udb_query_preparation_area_t *prep_area)
476 const char *statement;
480 char **column_values;
484 /* Macro that cleans up dynamically allocated memory and returns the
485 * specified status. */
486 #define BAIL_OUT(status) \
487 if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
488 if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
489 if (res != NULL) { dbi_result_free (res); res = NULL; } \
493 column_values = NULL;
496 statement = udb_query_get_statement (q);
497 assert (statement != NULL);
499 res = dbi_conn_query (db->connection, statement);
503 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
504 "dbi_conn_query failed: %s",
505 db->name, udb_query_get_name (q),
506 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
509 else /* Get the number of columns */
511 unsigned int db_status;
513 db_status = dbi_result_get_numfields (res);
514 if (db_status == DBI_FIELD_ERROR)
517 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
518 "dbi_result_get_numfields failed: %s",
519 db->name, udb_query_get_name (q),
520 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
524 column_num = (size_t) db_status;
525 DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
526 db->name, udb_query_get_name (q), column_num);
529 /* Allocate `column_names' and `column_values'. {{{ */
530 column_names = (char **) calloc (column_num, sizeof (char *));
531 if (column_names == NULL)
533 ERROR ("dbi plugin: malloc failed.");
537 column_names[0] = (char *) calloc (column_num,
538 DATA_MAX_NAME_LEN * sizeof (char));
539 if (column_names[0] == NULL)
541 ERROR ("dbi plugin: malloc failed.");
544 for (i = 1; i < column_num; i++)
545 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
547 column_values = (char **) calloc (column_num, sizeof (char *));
548 if (column_values == NULL)
550 ERROR ("dbi plugin: malloc failed.");
554 column_values[0] = (char *) calloc (column_num,
555 DATA_MAX_NAME_LEN * sizeof (char));
556 if (column_values[0] == NULL)
558 ERROR ("dbi plugin: malloc failed.");
561 for (i = 1; i < column_num; i++)
562 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
565 /* Copy the field names to `column_names' */
566 for (i = 0; i < column_num; i++) /* {{{ */
568 const char *column_name;
570 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
571 if (column_name == NULL)
573 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
574 "Cannot retrieve name of field %zu.",
575 db->name, udb_query_get_name (q), i + 1);
579 sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
580 } /* }}} for (i = 0; i < column_num; i++) */
582 udb_query_prepare_result (q, prep_area, (db->host ? db->host : hostname_g),
583 /* plugin = */ "dbi", db->name,
584 column_names, column_num, /* interval = */ 0);
586 /* 0 = error; 1 = success; */
587 status = dbi_result_first_row (res); /* {{{ */
591 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
592 "dbi_result_first_row failed: %s. Maybe the statement didn't "
594 db->name, udb_query_get_name (q),
595 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
596 udb_query_finish_result (q, prep_area);
600 /* Iterate over all rows and call `udb_query_handle_result' with each list of
605 /* Copy the value of the columns to `column_values' */
606 for (i = 0; i < column_num; i++) /* {{{ */
608 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
609 column_values[i], DATA_MAX_NAME_LEN);
613 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
614 "cdbi_result_get_field (%zu) failed.",
615 db->name, udb_query_get_name (q), i + 1);
619 } /* }}} for (i = 0; i < column_num; i++) */
621 /* If all values were copied successfully, call `udb_query_handle_result'
622 * to dispatch the row to the daemon. */
623 if (status == 0) /* {{{ */
625 status = udb_query_handle_result (q, prep_area, column_values);
628 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
629 "udb_query_handle_result failed.",
630 db->name, udb_query_get_name (q));
634 /* Get the next row from the database. */
635 status = dbi_result_next_row (res); /* {{{ */
638 if (dbi_conn_error (db->connection, NULL) != 0)
641 WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
642 "dbi_result_next_row failed: %s.",
643 db->name, udb_query_get_name (q),
644 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
648 } /* }}} while (42) */
650 /* Tell the db query interface that we're done with this query. */
651 udb_query_finish_result (q, prep_area);
653 /* Clean up and return `status = 0' (success) */
656 } /* }}} int cdbi_read_database_query */
658 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
665 if (db->connection != NULL)
667 status = dbi_conn_ping (db->connection);
668 if (status != 0) /* connection is alive */
671 dbi_conn_close (db->connection);
672 db->connection = NULL;
675 driver = dbi_driver_open (db->driver);
678 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.",
680 INFO ("dbi plugin: Maybe the driver isn't installed? "
681 "Known drivers are:");
682 for (driver = dbi_driver_list (NULL);
684 driver = dbi_driver_list (driver))
686 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
691 connection = dbi_conn_open (driver);
692 if (connection == NULL)
694 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
699 /* Set all the driver options. Because this is a very very very generic
700 * interface, the error handling is kind of long. If an invalid option is
701 * encountered, it will get a list of options understood by the driver and
702 * report that as `INFO'. This way, users hopefully don't have too much
703 * trouble finding out how to configure the plugin correctly.. */
704 for (i = 0; i < db->driver_options_num; i++)
706 if (db->driver_options[i].is_numeric)
708 status = dbi_conn_set_option_numeric (connection,
709 db->driver_options[i].key, db->driver_options[i].value.numeric);
713 ERROR ("dbi plugin: cdbi_connect_database (%s): "
714 "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.",
716 db->driver_options[i].key, db->driver_options[i].value.numeric,
717 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
722 status = dbi_conn_set_option (connection,
723 db->driver_options[i].key, db->driver_options[i].value.string);
727 ERROR ("dbi plugin: cdbi_connect_database (%s): "
728 "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.",
730 db->driver_options[i].key, db->driver_options[i].value.string,
731 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
739 INFO ("dbi plugin: This is a list of all options understood "
740 "by the `%s' driver:", db->driver);
741 for (opt = dbi_conn_get_option_list (connection, NULL);
743 opt = dbi_conn_get_option_list (connection, opt))
745 INFO ("dbi plugin: * %s", opt);
748 dbi_conn_close (connection);
751 } /* for (i = 0; i < db->driver_options_num; i++) */
753 status = dbi_conn_connect (connection);
757 ERROR ("dbi plugin: cdbi_connect_database (%s): "
758 "dbi_conn_connect failed: %s",
759 db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
760 dbi_conn_close (connection);
764 if (db->select_db != NULL)
766 status = dbi_conn_select_db (connection, db->select_db);
770 WARNING ("dbi plugin: cdbi_connect_database (%s): "
771 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
772 db->name, db->select_db,
773 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
774 dbi_conn_close (connection);
779 db->connection = connection;
781 } /* }}} int cdbi_connect_database */
783 static int cdbi_read_database (user_data_t *ud) /* {{{ */
785 cdbi_database_t *db = (cdbi_database_t *) ud->data;
790 unsigned int db_version;
792 status = cdbi_connect_database (db);
795 assert (db->connection != NULL);
797 db_version = dbi_conn_get_engine_version (db->connection);
798 /* TODO: Complain if `db_version == 0' */
801 for (i = 0; i < db->queries_num; i++)
803 /* Check if we know the database's version and if so, if this query applies
804 * to that version. */
805 if ((db_version != 0)
806 && (udb_query_check_version (db->queries[i], db_version) == 0))
809 status = cdbi_read_database_query (db,
810 db->queries[i], db->q_prep_areas[i]);
817 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
822 } /* }}} int cdbi_read_database */
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_shutdown ("dbi", cdbi_shutdown);
852 } /* }}} void module_register */
855 * vim: shiftwidth=2 softtabstop=2 et fdm=marker