1 /**
2 * collectd - src/dbi.c
3 * Copyright (C) 2008-2015 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "utils_db_query.h"
33 #include <dbi/dbi.h>
35 /* libdbi 0.9.0 introduced a new thread-safe interface and marked the old
36 * functions "deprecated". These macros convert the new functions to their old
37 * counterparts for backwards compatibility. */
38 #if !defined(LIBDBI_VERSION) || (LIBDBI_VERSION < 900)
39 # define HAVE_LEGACY_LIBDBI 1
40 # define dbi_initialize_r(a,inst) dbi_initialize(a)
41 # define dbi_shutdown_r(inst) dbi_shutdown()
42 # define dbi_set_verbosity_r(a,inst) dbi_set_verbosity(a)
43 # define dbi_driver_list_r(a,inst) dbi_driver_list(a)
44 # define dbi_driver_open_r(a,inst) dbi_driver_open(a)
45 #endif
47 /*
48 * Data types
49 */
50 struct cdbi_driver_option_s /* {{{ */
51 {
52 char *key;
53 union
54 {
55 char *string;
56 int numeric;
57 } value;
58 _Bool is_numeric;
59 };
60 typedef struct cdbi_driver_option_s cdbi_driver_option_t; /* }}} */
62 struct cdbi_database_s /* {{{ */
63 {
64 char *name;
65 char *select_db;
67 cdtime_t interval;
69 char *driver;
70 char *host;
71 cdbi_driver_option_t *driver_options;
72 size_t driver_options_num;
74 udb_query_preparation_area_t **q_prep_areas;
75 udb_query_t **queries;
76 size_t queries_num;
78 dbi_conn connection;
79 };
80 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
82 /*
83 * Global variables
84 */
85 #if !defined(HAVE_LEGACY_LIBDBI) || !HAVE_LEGACY_LIBDBI
86 static dbi_inst dbi_instance = 0;
87 #endif
88 static udb_query_t **queries = NULL;
89 static size_t queries_num = 0;
90 static cdbi_database_t **databases = NULL;
91 static size_t databases_num = 0;
93 static int cdbi_read_database (user_data_t *ud);
95 /*
96 * Functions
97 */
98 static const char *cdbi_strerror (dbi_conn conn, /* {{{ */
99 char *buffer, size_t buffer_size)
100 {
101 const char *msg;
102 int status;
104 if (conn == NULL)
105 {
106 sstrncpy (buffer, "connection is NULL", buffer_size);
107 return (buffer);
108 }
110 msg = NULL;
111 status = dbi_conn_error (conn, &msg);
112 if ((status >= 0) && (msg != NULL))
113 ssnprintf (buffer, buffer_size, "%s (status %i)", msg, status);
114 else
115 ssnprintf (buffer, buffer_size, "dbi_conn_error failed with status %i",
116 status);
118 return (buffer);
119 } /* }}} const char *cdbi_conn_error */
121 static int cdbi_result_get_field (dbi_result res, /* {{{ */
122 unsigned int index, char *buffer, size_t buffer_size)
123 {
124 unsigned short src_type;
126 src_type = dbi_result_get_field_type_idx (res, index);
127 if (src_type == DBI_TYPE_ERROR)
128 {
129 ERROR ("dbi plugin: cdbi_result_get: "
130 "dbi_result_get_field_type_idx failed.");
131 return (-1);
132 }
134 if (src_type == DBI_TYPE_INTEGER)
135 {
136 long long value;
138 value = dbi_result_get_longlong_idx (res, index);
139 ssnprintf (buffer, buffer_size, "%lli", value);
140 }
141 else if (src_type == DBI_TYPE_DECIMAL)
142 {
143 double value;
145 value = dbi_result_get_double_idx (res, index);
146 ssnprintf (buffer, buffer_size, "%63.15g", value);
147 }
148 else if (src_type == DBI_TYPE_STRING)
149 {
150 const char *value;
152 value = dbi_result_get_string_idx (res, index);
153 if (value == NULL)
154 sstrncpy (buffer, "", buffer_size);
155 else if (strcmp ("ERROR", value) == 0)
156 return (-1);
157 else
158 sstrncpy (buffer, value, buffer_size);
159 }
160 /* DBI_TYPE_BINARY */
161 /* DBI_TYPE_DATETIME */
162 else
163 {
164 const char *field_name;
166 field_name = dbi_result_get_field_name (res, index);
167 if (field_name == NULL)
168 field_name = "<unknown>";
170 ERROR ("dbi plugin: Column `%s': Don't know how to handle "
171 "source type %hu.",
172 field_name, src_type);
173 return (-1);
174 }
176 return (0);
177 } /* }}} int cdbi_result_get_field */
179 static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
180 {
181 size_t i;
183 if (db == NULL)
184 return;
186 sfree (db->name);
187 sfree (db->driver);
189 for (i = 0; i < db->driver_options_num; i++)
190 {
191 sfree (db->driver_options[i].key);
192 if (!db->driver_options[i].is_numeric)
193 sfree (db->driver_options[i].value.string);
194 }
195 sfree (db->driver_options);
197 if (db->q_prep_areas)
198 for (i = 0; i < db->queries_num; ++i)
199 udb_query_delete_preparation_area (db->q_prep_areas[i]);
200 free (db->q_prep_areas);
202 sfree (db);
203 } /* }}} void cdbi_database_free */
205 /* Configuration handling functions {{{
206 *
207 * <Plugin dbi>
208 * <Query "plugin_instance0">
209 * Statement "SELECT name, value FROM table"
210 * <Result>
211 * Type "gauge"
212 * InstancesFrom "name"
213 * ValuesFrom "value"
214 * </Result>
215 * ...
216 * </Query>
217 *
218 * <Database "plugin_instance1">
219 * Driver "mysql"
220 * Interval 120
221 * DriverOption "hostname" "localhost"
222 * ...
223 * Query "plugin_instance0"
224 * </Database>
225 * </Plugin>
226 */
228 static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */
229 oconfig_item_t *ci)
230 {
231 cdbi_driver_option_t *option;
233 if ((ci->values_num != 2)
234 || (ci->values[0].type != OCONFIG_TYPE_STRING)
235 || ((ci->values[1].type != OCONFIG_TYPE_STRING)
236 && (ci->values[1].type != OCONFIG_TYPE_NUMBER)))
237 {
238 WARNING ("dbi plugin: The `DriverOption' config option "
239 "needs exactly two arguments.");
240 return (-1);
241 }
243 option = (cdbi_driver_option_t *) realloc (db->driver_options,
244 sizeof (*option) * (db->driver_options_num + 1));
245 if (option == NULL)
246 {
247 ERROR ("dbi plugin: realloc failed");
248 return (-1);
249 }
251 db->driver_options = option;
252 option = db->driver_options + db->driver_options_num;
253 memset (option, 0, sizeof (*option));
255 option->key = strdup (ci->values[0].value.string);
256 if (option->key == NULL)
257 {
258 ERROR ("dbi plugin: strdup failed.");
259 return (-1);
260 }
262 if (ci->values[1].type == OCONFIG_TYPE_STRING)
263 {
264 option->value.string = strdup (ci->values[1].value.string);
265 if (option->value.string == NULL)
266 {
267 ERROR ("dbi plugin: strdup failed.");
268 sfree (option->key);
269 return (-1);
270 }
271 }
272 else
273 {
274 assert (ci->values[1].type == OCONFIG_TYPE_NUMBER);
275 option->value.numeric = (int) (ci->values[1].value.number + .5);
276 option->is_numeric = 1;
277 }
279 db->driver_options_num++;
280 return (0);
281 } /* }}} int cdbi_config_add_database_driver_option */
283 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
284 {
285 cdbi_database_t *db;
286 int status;
287 int i;
289 if ((ci->values_num != 1)
290 || (ci->values[0].type != OCONFIG_TYPE_STRING))
291 {
292 WARNING ("dbi plugin: The `Database' block "
293 "needs exactly one string argument.");
294 return (-1);
295 }
297 db = (cdbi_database_t *) malloc (sizeof (*db));
298 if (db == NULL)
299 {
300 ERROR ("dbi plugin: malloc failed.");
301 return (-1);
302 }
303 memset (db, 0, sizeof (*db));
305 status = cf_util_get_string (ci, &db->name);
306 if (status != 0)
307 {
308 sfree (db);
309 return (status);
310 }
312 /* Fill the `cdbi_database_t' structure.. */
313 for (i = 0; i < ci->children_num; i++)
314 {
315 oconfig_item_t *child = ci->children + i;
317 if (strcasecmp ("Driver", child->key) == 0)
318 status = cf_util_get_string (child, &db->driver);
319 else if (strcasecmp ("DriverOption", child->key) == 0)
320 status = cdbi_config_add_database_driver_option (db, child);
321 else if (strcasecmp ("SelectDB", child->key) == 0)
322 status = cf_util_get_string (child, &db->select_db);
323 else if (strcasecmp ("Query", child->key) == 0)
324 status = udb_query_pick_from_list (child, queries, queries_num,
325 &db->queries, &db->queries_num);
326 else if (strcasecmp ("Host", child->key) == 0)
327 status = cf_util_get_string (child, &db->host);
328 else if (strcasecmp ("Interval", child->key) == 0)
329 status = cf_util_get_cdtime(child, &db->interval);
330 else
331 {
332 WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
333 status = -1;
334 }
336 if (status != 0)
337 break;
338 }
340 /* Check that all necessary options have been given. */
341 while (status == 0)
342 {
343 if (db->driver == NULL)
344 {
345 WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
346 status = -1;
347 }
348 if (db->driver_options_num == 0)
349 {
350 WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
351 "This will likely not work.", db->name);
352 }
354 break;
355 } /* while (status == 0) */
357 while ((status == 0) && (db->queries_num > 0))
358 {
359 size_t j;
360 db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
361 db->queries_num, sizeof (*db->q_prep_areas));
363 if (db->q_prep_areas == NULL)
364 {
365 WARNING ("dbi plugin: malloc failed");
366 status = -1;
367 break;
368 }
370 for (j = 0; j < db->queries_num; ++j)
371 {
372 db->q_prep_areas[j]
373 = udb_query_allocate_preparation_area (db->queries[j]);
375 if (db->q_prep_areas[j] == NULL)
376 {
377 WARNING ("dbi plugin: udb_query_allocate_preparation_area failed");
378 status = -1;
379 break;
380 }
381 }
383 break;
384 }
386 /* If all went well, add this database to the global list of databases. */
387 if (status == 0)
388 {
389 cdbi_database_t **temp;
391 temp = (cdbi_database_t **) realloc (databases,
392 sizeof (*databases) * (databases_num + 1));
393 if (temp == NULL)
394 {
395 ERROR ("dbi plugin: realloc failed");
396 status = -1;
397 }
398 else
399 {
400 user_data_t ud;
401 char *name = NULL;
403 databases = temp;
404 databases[databases_num] = db;
405 databases_num++;
407 memset (&ud, 0, sizeof (ud));
408 ud.data = (void *) db;
409 ud.free_func = NULL;
410 name = ssnprintf_alloc("dbi:%s", db->name);
412 plugin_register_complex_read (/* group = */ NULL,
413 /* name = */ name ? name : db->name,
414 /* callback = */ cdbi_read_database,
415 /* interval = */ (db->interval > 0) ? db->interval : 0,
416 /* user_data = */ &ud);
417 free (name);
418 }
419 }
421 if (status != 0)
422 {
423 cdbi_database_free (db);
424 return (-1);
425 }
427 return (0);
428 } /* }}} int cdbi_config_add_database */
430 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
431 {
432 int i;
434 for (i = 0; i < ci->children_num; i++)
435 {
436 oconfig_item_t *child = ci->children + i;
437 if (strcasecmp ("Query", child->key) == 0)
438 udb_query_create (&queries, &queries_num, child,
439 /* callback = */ NULL);
440 else if (strcasecmp ("Database", child->key) == 0)
441 cdbi_config_add_database (child);
442 else
443 {
444 WARNING ("dbi plugin: Ignoring unknown config option `%s'.", child->key);
445 }
446 } /* for (ci->children) */
448 return (0);
449 } /* }}} int cdbi_config */
451 /* }}} End of configuration handling functions */
453 static int cdbi_init (void) /* {{{ */
454 {
455 static int did_init = 0;
456 int status;
458 if (did_init != 0)
459 return (0);
461 if (queries_num == 0)
462 {
463 ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
464 "this plugin can't do anything useful, so we will returns an error.");
465 return (-1);
466 }
468 if (databases_num == 0)
469 {
470 ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
471 "this plugin can't do anything useful, so we will returns an error.");
472 return (-1);
473 }
475 status = dbi_initialize_r (/* driverdir = */ NULL, &dbi_instance);
476 if (status < 0)
477 {
478 ERROR ("dbi plugin: cdbi_init: dbi_initialize_r failed with status %i.",
479 status);
480 return (-1);
481 }
482 else if (status == 0)
483 {
484 ERROR ("dbi plugin: `dbi_initialize_r' could not load any drivers. Please "
485 "install at least one `DBD' or check your installation.");
486 return (-1);
487 }
488 DEBUG ("dbi plugin: cdbi_init: dbi_initialize_r reports %i driver%s.",
489 status, (status == 1) ? "" : "s");
491 return (0);
492 } /* }}} int cdbi_init */
494 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
495 udb_query_t *q, udb_query_preparation_area_t *prep_area)
496 {
497 const char *statement;
498 dbi_result res;
499 size_t column_num;
500 char **column_names;
501 char **column_values;
502 int status;
503 size_t i;
505 /* Macro that cleans up dynamically allocated memory and returns the
506 * specified status. */
507 #define BAIL_OUT(status) \
508 if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
509 if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
510 if (res != NULL) { dbi_result_free (res); res = NULL; } \
511 return (status)
513 column_names = NULL;
514 column_values = NULL;
515 res = NULL;
517 statement = udb_query_get_statement (q);
518 assert (statement != NULL);
520 res = dbi_conn_query (db->connection, statement);
521 if (res == NULL)
522 {
523 char errbuf[1024];
524 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
525 "dbi_conn_query failed: %s",
526 db->name, udb_query_get_name (q),
527 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
528 BAIL_OUT (-1);
529 }
530 else /* Get the number of columns */
531 {
532 unsigned int db_status;
534 db_status = dbi_result_get_numfields (res);
535 if (db_status == DBI_FIELD_ERROR)
536 {
537 char errbuf[1024];
538 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
539 "dbi_result_get_numfields failed: %s",
540 db->name, udb_query_get_name (q),
541 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
542 BAIL_OUT (-1);
543 }
545 column_num = (size_t) db_status;
546 DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
547 db->name, udb_query_get_name (q), column_num);
548 }
550 /* Allocate `column_names' and `column_values'. {{{ */
551 column_names = (char **) calloc (column_num, sizeof (char *));
552 if (column_names == NULL)
553 {
554 ERROR ("dbi plugin: malloc failed.");
555 BAIL_OUT (-1);
556 }
558 column_names[0] = (char *) calloc (column_num,
559 DATA_MAX_NAME_LEN * sizeof (char));
560 if (column_names[0] == NULL)
561 {
562 ERROR ("dbi plugin: malloc failed.");
563 BAIL_OUT (-1);
564 }
565 for (i = 1; i < column_num; i++)
566 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
568 column_values = (char **) calloc (column_num, sizeof (char *));
569 if (column_values == NULL)
570 {
571 ERROR ("dbi plugin: malloc failed.");
572 BAIL_OUT (-1);
573 }
575 column_values[0] = (char *) calloc (column_num,
576 DATA_MAX_NAME_LEN * sizeof (char));
577 if (column_values[0] == NULL)
578 {
579 ERROR ("dbi plugin: malloc failed.");
580 BAIL_OUT (-1);
581 }
582 for (i = 1; i < column_num; i++)
583 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
584 /* }}} */
586 /* Copy the field names to `column_names' */
587 for (i = 0; i < column_num; i++) /* {{{ */
588 {
589 const char *column_name;
591 column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
592 if (column_name == NULL)
593 {
594 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
595 "Cannot retrieve name of field %zu.",
596 db->name, udb_query_get_name (q), i + 1);
597 BAIL_OUT (-1);
598 }
600 sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
601 } /* }}} for (i = 0; i < column_num; i++) */
603 udb_query_prepare_result (q, prep_area, (db->host ? db->host : hostname_g),
604 /* plugin = */ "dbi", db->name,
605 column_names, column_num, /* interval = */ (db->interval > 0) ? db->interval : 0);
607 /* 0 = error; 1 = success; */
608 status = dbi_result_first_row (res); /* {{{ */
609 if (status != 1)
610 {
611 char errbuf[1024];
612 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
613 "dbi_result_first_row failed: %s. Maybe the statement didn't "
614 "return any rows?",
615 db->name, udb_query_get_name (q),
616 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
617 udb_query_finish_result (q, prep_area);
618 BAIL_OUT (-1);
619 } /* }}} */
621 /* Iterate over all rows and call `udb_query_handle_result' with each list of
622 * values. */
623 while (42) /* {{{ */
624 {
625 status = 0;
626 /* Copy the value of the columns to `column_values' */
627 for (i = 0; i < column_num; i++) /* {{{ */
628 {
629 status = cdbi_result_get_field (res, (unsigned int) (i + 1),
630 column_values[i], DATA_MAX_NAME_LEN);
632 if (status != 0)
633 {
634 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
635 "cdbi_result_get_field (%zu) failed.",
636 db->name, udb_query_get_name (q), i + 1);
637 status = -1;
638 break;
639 }
640 } /* }}} for (i = 0; i < column_num; i++) */
642 /* If all values were copied successfully, call `udb_query_handle_result'
643 * to dispatch the row to the daemon. */
644 if (status == 0) /* {{{ */
645 {
646 status = udb_query_handle_result (q, prep_area, column_values);
647 if (status != 0)
648 {
649 ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
650 "udb_query_handle_result failed.",
651 db->name, udb_query_get_name (q));
652 }
653 } /* }}} */
655 /* Get the next row from the database. */
656 status = dbi_result_next_row (res); /* {{{ */
657 if (status != 1)
658 {
659 if (dbi_conn_error (db->connection, NULL) != 0)
660 {
661 char errbuf[1024];
662 WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
663 "dbi_result_next_row failed: %s.",
664 db->name, udb_query_get_name (q),
665 cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
666 }
667 break;
668 } /* }}} */
669 } /* }}} while (42) */
671 /* Tell the db query interface that we're done with this query. */
672 udb_query_finish_result (q, prep_area);
674 /* Clean up and return `status = 0' (success) */
675 BAIL_OUT (0);
676 #undef BAIL_OUT
677 } /* }}} int cdbi_read_database_query */
679 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
680 {
681 dbi_driver driver;
682 dbi_conn connection;
683 size_t i;
684 int status;
686 if (db->connection != NULL)
687 {
688 status = dbi_conn_ping (db->connection);
689 if (status != 0) /* connection is alive */
690 return (0);
692 dbi_conn_close (db->connection);
693 db->connection = NULL;
694 }
696 driver = dbi_driver_open_r (db->driver, dbi_instance);
697 if (driver == NULL)
698 {
699 ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open_r (%s) failed.",
700 db->driver);
701 INFO ("dbi plugin: Maybe the driver isn't installed? "
702 "Known drivers are:");
703 for (driver = dbi_driver_list_r (NULL, dbi_instance);
704 driver != NULL;
705 driver = dbi_driver_list_r (driver, dbi_instance))
706 {
707 INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
708 }
709 return (-1);
710 }
712 connection = dbi_conn_open (driver);
713 if (connection == NULL)
714 {
715 ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
716 db->driver);
717 return (-1);
718 }
720 /* Set all the driver options. Because this is a very very very generic
721 * interface, the error handling is kind of long. If an invalid option is
722 * encountered, it will get a list of options understood by the driver and
723 * report that as `INFO'. This way, users hopefully don't have too much
724 * trouble finding out how to configure the plugin correctly.. */
725 for (i = 0; i < db->driver_options_num; i++)
726 {
727 if (db->driver_options[i].is_numeric)
728 {
729 status = dbi_conn_set_option_numeric (connection,
730 db->driver_options[i].key, db->driver_options[i].value.numeric);
731 if (status != 0)
732 {
733 char errbuf[1024];
734 ERROR ("dbi plugin: cdbi_connect_database (%s): "
735 "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.",
736 db->name,
737 db->driver_options[i].key, db->driver_options[i].value.numeric,
738 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
739 }
740 }
741 else
742 {
743 status = dbi_conn_set_option (connection,
744 db->driver_options[i].key, db->driver_options[i].value.string);
745 if (status != 0)
746 {
747 char errbuf[1024];
748 ERROR ("dbi plugin: cdbi_connect_database (%s): "
749 "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.",
750 db->name,
751 db->driver_options[i].key, db->driver_options[i].value.string,
752 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
753 }
754 }
756 if (status != 0)
757 {
758 char const *opt;
760 INFO ("dbi plugin: This is a list of all options understood "
761 "by the `%s' driver:", db->driver);
762 for (opt = dbi_conn_get_option_list (connection, NULL);
763 opt != NULL;
764 opt = dbi_conn_get_option_list (connection, opt))
765 {
766 INFO ("dbi plugin: * %s", opt);
767 }
769 dbi_conn_close (connection);
770 return (-1);
771 }
772 } /* for (i = 0; i < db->driver_options_num; i++) */
774 status = dbi_conn_connect (connection);
775 if (status != 0)
776 {
777 char errbuf[1024];
778 ERROR ("dbi plugin: cdbi_connect_database (%s): "
779 "dbi_conn_connect failed: %s",
780 db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
781 dbi_conn_close (connection);
782 return (-1);
783 }
785 if (db->select_db != NULL)
786 {
787 status = dbi_conn_select_db (connection, db->select_db);
788 if (status != 0)
789 {
790 char errbuf[1024];
791 WARNING ("dbi plugin: cdbi_connect_database (%s): "
792 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
793 db->name, db->select_db,
794 cdbi_strerror (connection, errbuf, sizeof (errbuf)));
795 dbi_conn_close (connection);
796 return (-1);
797 }
798 }
800 db->connection = connection;
801 return (0);
802 } /* }}} int cdbi_connect_database */
804 static int cdbi_read_database (user_data_t *ud) /* {{{ */
805 {
806 cdbi_database_t *db = (cdbi_database_t *) ud->data;
807 size_t i;
808 int success;
809 int status;
811 unsigned int db_version;
813 status = cdbi_connect_database (db);
814 if (status != 0)
815 return (status);
816 assert (db->connection != NULL);
818 db_version = dbi_conn_get_engine_version (db->connection);
819 /* TODO: Complain if `db_version == 0' */
821 success = 0;
822 for (i = 0; i < db->queries_num; i++)
823 {
824 /* Check if we know the database's version and if so, if this query applies
825 * to that version. */
826 if ((db_version != 0)
827 && (udb_query_check_version (db->queries[i], db_version) == 0))
828 continue;
830 status = cdbi_read_database_query (db,
831 db->queries[i], db->q_prep_areas[i]);
832 if (status == 0)
833 success++;
834 }
836 if (success == 0)
837 {
838 ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
839 return (-1);
840 }
842 return (0);
843 } /* }}} int cdbi_read_database */
845 static int cdbi_shutdown (void) /* {{{ */
846 {
847 size_t i;
849 for (i = 0; i < databases_num; i++)
850 {
851 if (databases[i]->connection != NULL)
852 {
853 dbi_conn_close (databases[i]->connection);
854 databases[i]->connection = NULL;
855 }
856 cdbi_database_free (databases[i]);
857 }
858 sfree (databases);
859 databases_num = 0;
861 udb_query_free (queries, queries_num);
862 queries = NULL;
863 queries_num = 0;
865 return (0);
866 } /* }}} int cdbi_shutdown */
868 void module_register (void) /* {{{ */
869 {
870 plugin_register_complex_config ("dbi", cdbi_config);
871 plugin_register_init ("dbi", cdbi_init);
872 plugin_register_shutdown ("dbi", cdbi_shutdown);
873 } /* }}} void module_register */
875 /*
876 * vim: shiftwidth=2 softtabstop=2 et fdm=marker
877 */