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 **/
26
27 #include "collectd.h"
28
29 #include "plugin.h"
30 #include "utils/common/common.h"
31 #include "utils/db_query/db_query.h"
32
33 #include <dbi/dbi.h>
34
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
46
47 /*
48 * Data types
49 */
50 struct cdbi_driver_option_s /* {{{ */
51 {
52 char *key;
53 union {
54 char *string;
55 int numeric;
56 } value;
57 bool is_numeric;
58 };
59 typedef struct cdbi_driver_option_s cdbi_driver_option_t; /* }}} */
60
61 struct cdbi_database_s /* {{{ */
62 {
63 char *name;
64 char *select_db;
65 char *plugin_name;
66
67 char *driver;
68 char *host;
69 cdbi_driver_option_t *driver_options;
70 size_t driver_options_num;
71
72 udb_query_preparation_area_t **q_prep_areas;
73 udb_query_t **queries;
74 size_t queries_num;
75
76 dbi_conn connection;
77 };
78 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
79
80 /*
81 * Global variables
82 */
83 #if !defined(HAVE_LEGACY_LIBDBI) || !HAVE_LEGACY_LIBDBI
84 static dbi_inst dbi_instance;
85 #endif
86 static udb_query_t **queries;
87 static size_t queries_num;
88 static cdbi_database_t **databases;
89 static size_t databases_num;
90
91 static int cdbi_read_database(user_data_t *ud);
92
93 /*
94 * Functions
95 */
cdbi_strerror(dbi_conn conn,char * buffer,size_t buffer_size)96 static const char *cdbi_strerror(dbi_conn conn, /* {{{ */
97 char *buffer, size_t buffer_size) {
98 const char *msg;
99 int status;
100
101 if (conn == NULL) {
102 sstrncpy(buffer, "connection is NULL", buffer_size);
103 return buffer;
104 }
105
106 msg = NULL;
107 status = dbi_conn_error(conn, &msg);
108 if ((status >= 0) && (msg != NULL))
109 snprintf(buffer, buffer_size, "%s (status %i)", msg, status);
110 else
111 snprintf(buffer, buffer_size, "dbi_conn_error failed with status %i",
112 status);
113
114 return buffer;
115 } /* }}} const char *cdbi_conn_error */
116
cdbi_result_get_field(dbi_result res,unsigned int index,char * buffer,size_t buffer_size)117 static int cdbi_result_get_field(dbi_result res, /* {{{ */
118 unsigned int index, char *buffer,
119 size_t buffer_size) {
120 unsigned short src_type;
121
122 src_type = dbi_result_get_field_type_idx(res, index);
123 if (src_type == DBI_TYPE_ERROR) {
124 ERROR("dbi plugin: cdbi_result_get: "
125 "dbi_result_get_field_type_idx failed.");
126 return -1;
127 }
128
129 if (src_type == DBI_TYPE_INTEGER) {
130 long long value;
131
132 value = dbi_result_get_longlong_idx(res, index);
133 snprintf(buffer, buffer_size, "%lli", value);
134 } else if (src_type == DBI_TYPE_DECIMAL) {
135 double value;
136
137 value = dbi_result_get_double_idx(res, index);
138 snprintf(buffer, buffer_size, "%63.15g", value);
139 } else if (src_type == DBI_TYPE_STRING) {
140 const char *value;
141
142 value = dbi_result_get_string_idx(res, index);
143 if (value == NULL)
144 sstrncpy(buffer, "", buffer_size);
145 else if (strcmp("ERROR", value) == 0)
146 return -1;
147 else
148 sstrncpy(buffer, value, buffer_size);
149 }
150 /* DBI_TYPE_BINARY */
151 /* DBI_TYPE_DATETIME */
152 else {
153 const char *field_name;
154
155 field_name = dbi_result_get_field_name(res, index);
156 if (field_name == NULL)
157 field_name = "<unknown>";
158
159 ERROR("dbi plugin: Column `%s': Don't know how to handle "
160 "source type %hu.",
161 field_name, src_type);
162 return -1;
163 }
164
165 return 0;
166 } /* }}} int cdbi_result_get_field */
167
cdbi_database_free(cdbi_database_t * db)168 static void cdbi_database_free(cdbi_database_t *db) /* {{{ */
169 {
170 if (db == NULL)
171 return;
172
173 sfree(db->name);
174 sfree(db->select_db);
175 sfree(db->plugin_name);
176 sfree(db->driver);
177 sfree(db->host);
178
179 for (size_t i = 0; i < db->driver_options_num; i++) {
180 sfree(db->driver_options[i].key);
181 if (!db->driver_options[i].is_numeric)
182 sfree(db->driver_options[i].value.string);
183 }
184 sfree(db->driver_options);
185
186 if (db->q_prep_areas)
187 for (size_t i = 0; i < db->queries_num; ++i)
188 udb_query_delete_preparation_area(db->q_prep_areas[i]);
189 sfree(db->q_prep_areas);
190 /* N.B.: db->queries references objects "owned" by the global queries
191 * variable. Free the array here, but not the content. */
192 sfree(db->queries);
193
194 sfree(db);
195 } /* }}} void cdbi_database_free */
196
197 /* Configuration handling functions {{{
198 *
199 * <Plugin dbi>
200 * <Query "plugin_instance0">
201 * Statement "SELECT name, value FROM table"
202 * <Result>
203 * Type "gauge"
204 * InstancesFrom "name"
205 * ValuesFrom "value"
206 * </Result>
207 * ...
208 * </Query>
209 *
210 * <Database "plugin_instance1">
211 * Driver "mysql"
212 * Interval 120
213 * DriverOption "hostname" "localhost"
214 * ...
215 * Query "plugin_instance0"
216 * </Database>
217 * </Plugin>
218 */
219
cdbi_config_add_database_driver_option(cdbi_database_t * db,oconfig_item_t * ci)220 static int cdbi_config_add_database_driver_option(cdbi_database_t *db, /* {{{ */
221 oconfig_item_t *ci) {
222 cdbi_driver_option_t *option;
223
224 if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
225 ((ci->values[1].type != OCONFIG_TYPE_STRING) &&
226 (ci->values[1].type != OCONFIG_TYPE_NUMBER))) {
227 WARNING("dbi plugin: The `DriverOption' config option "
228 "needs exactly two arguments.");
229 return -1;
230 }
231
232 option = realloc(db->driver_options,
233 sizeof(*option) * (db->driver_options_num + 1));
234 if (option == NULL) {
235 ERROR("dbi plugin: realloc failed");
236 return -1;
237 }
238
239 db->driver_options = option;
240 option = db->driver_options + db->driver_options_num;
241 memset(option, 0, sizeof(*option));
242
243 option->key = strdup(ci->values[0].value.string);
244 if (option->key == NULL) {
245 ERROR("dbi plugin: strdup failed.");
246 return -1;
247 }
248
249 if (ci->values[1].type == OCONFIG_TYPE_STRING) {
250 option->value.string = strdup(ci->values[1].value.string);
251 if (option->value.string == NULL) {
252 ERROR("dbi plugin: strdup failed.");
253 sfree(option->key);
254 return -1;
255 }
256 } else {
257 assert(ci->values[1].type == OCONFIG_TYPE_NUMBER);
258 option->value.numeric = (int)(ci->values[1].value.number + .5);
259 option->is_numeric = true;
260 }
261
262 db->driver_options_num++;
263 return 0;
264 } /* }}} int cdbi_config_add_database_driver_option */
265
cdbi_config_add_database(oconfig_item_t * ci)266 static int cdbi_config_add_database(oconfig_item_t *ci) /* {{{ */
267 {
268 cdtime_t interval = 0;
269 cdbi_database_t *db;
270 int status;
271
272 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
273 WARNING("dbi plugin: The `Database' block "
274 "needs exactly one string argument.");
275 return -1;
276 }
277
278 db = calloc(1, sizeof(*db));
279 if (db == NULL) {
280 ERROR("dbi plugin: calloc failed.");
281 return -1;
282 }
283
284 status = cf_util_get_string(ci, &db->name);
285 if (status != 0) {
286 sfree(db);
287 return status;
288 }
289
290 /* Fill the `cdbi_database_t' structure.. */
291 for (int i = 0; i < ci->children_num; i++) {
292 oconfig_item_t *child = ci->children + i;
293
294 if (strcasecmp("Driver", child->key) == 0)
295 status = cf_util_get_string(child, &db->driver);
296 else if (strcasecmp("DriverOption", child->key) == 0)
297 status = cdbi_config_add_database_driver_option(db, child);
298 else if (strcasecmp("SelectDB", child->key) == 0)
299 status = cf_util_get_string(child, &db->select_db);
300 else if (strcasecmp("Query", child->key) == 0)
301 status = udb_query_pick_from_list(child, queries, queries_num,
302 &db->queries, &db->queries_num);
303 else if (strcasecmp("Host", child->key) == 0)
304 status = cf_util_get_string(child, &db->host);
305 else if (strcasecmp("Interval", child->key) == 0)
306 status = cf_util_get_cdtime(child, &interval);
307 else if (strcasecmp("Plugin", child->key) == 0)
308 status = cf_util_get_string(child, &db->plugin_name);
309 else {
310 WARNING("dbi plugin: Option `%s' not allowed here.", child->key);
311 status = -1;
312 }
313
314 if (status != 0)
315 break;
316 }
317
318 /* Check that all necessary options have been given. */
319 while (status == 0) {
320 if (db->driver == NULL) {
321 WARNING("dbi plugin: `Driver' not given for database `%s'", db->name);
322 status = -1;
323 }
324 if (db->driver_options_num == 0) {
325 WARNING("dbi plugin: No `DriverOption' given for database `%s'. "
326 "This will likely not work.",
327 db->name);
328 }
329
330 break;
331 } /* while (status == 0) */
332
333 while ((status == 0) && (db->queries_num > 0)) {
334 db->q_prep_areas = calloc(db->queries_num, sizeof(*db->q_prep_areas));
335 if (db->q_prep_areas == NULL) {
336 WARNING("dbi plugin: calloc failed");
337 status = -1;
338 break;
339 }
340
341 for (size_t i = 0; i < db->queries_num; ++i) {
342 db->q_prep_areas[i] = udb_query_allocate_preparation_area(db->queries[i]);
343
344 if (db->q_prep_areas[i] == NULL) {
345 WARNING("dbi plugin: udb_query_allocate_preparation_area failed");
346 status = -1;
347 break;
348 }
349 }
350
351 break;
352 }
353
354 /* If all went well, add this database to the global list of databases. */
355 if (status == 0) {
356 cdbi_database_t **temp;
357
358 temp = realloc(databases, sizeof(*databases) * (databases_num + 1));
359 if (temp == NULL) {
360 ERROR("dbi plugin: realloc failed");
361 status = -1;
362 } else {
363 databases = temp;
364 databases[databases_num] = db;
365 databases_num++;
366
367 char *name = ssnprintf_alloc("dbi:%s", db->name);
368 plugin_register_complex_read(
369 /* group = */ NULL,
370 /* name = */ name ? name : db->name,
371 /* callback = */ cdbi_read_database,
372 /* interval = */ interval,
373 &(user_data_t){
374 .data = db,
375 });
376 sfree(name);
377 }
378 }
379
380 if (status != 0) {
381 cdbi_database_free(db);
382 return -1;
383 }
384
385 return 0;
386 } /* }}} int cdbi_config_add_database */
387
cdbi_config(oconfig_item_t * ci)388 static int cdbi_config(oconfig_item_t *ci) /* {{{ */
389 {
390 for (int i = 0; i < ci->children_num; i++) {
391 oconfig_item_t *child = ci->children + i;
392 if (strcasecmp("Query", child->key) == 0)
393 udb_query_create(&queries, &queries_num, child,
394 /* callback = */ NULL);
395 else if (strcasecmp("Database", child->key) == 0)
396 cdbi_config_add_database(child);
397 else {
398 WARNING("dbi plugin: Ignoring unknown config option `%s'.", child->key);
399 }
400 } /* for (ci->children) */
401
402 return 0;
403 } /* }}} int cdbi_config */
404
405 /* }}} End of configuration handling functions */
406
cdbi_init(void)407 static int cdbi_init(void) /* {{{ */
408 {
409 static int did_init;
410 int status;
411
412 if (did_init != 0)
413 return 0;
414
415 if (queries_num == 0) {
416 ERROR("dbi plugin: No <Query> blocks have been found. Without them, "
417 "this plugin can't do anything useful, so we will return an error.");
418 return -1;
419 }
420
421 if (databases_num == 0) {
422 ERROR("dbi plugin: No <Database> blocks have been found. Without them, "
423 "this plugin can't do anything useful, so we will return an error.");
424 return -1;
425 }
426
427 status = dbi_initialize_r(/* driverdir = */ NULL, &dbi_instance);
428 if (status < 0) {
429 ERROR("dbi plugin: cdbi_init: dbi_initialize_r failed with status %i.",
430 status);
431 return -1;
432 } else if (status == 0) {
433 ERROR("dbi plugin: `dbi_initialize_r' could not load any drivers. Please "
434 "install at least one `DBD' or check your installation.");
435 return -1;
436 }
437 DEBUG("dbi plugin: cdbi_init: dbi_initialize_r reports %i driver%s.", status,
438 (status == 1) ? "" : "s");
439
440 return 0;
441 } /* }}} int cdbi_init */
442
cdbi_read_database_query(cdbi_database_t * db,udb_query_t * q,udb_query_preparation_area_t * prep_area)443 static int cdbi_read_database_query(cdbi_database_t *db, /* {{{ */
444 udb_query_t *q,
445 udb_query_preparation_area_t *prep_area) {
446 const char *statement;
447 dbi_result res;
448 size_t column_num;
449 char **column_names;
450 char **column_values;
451 int status;
452
453 /* Macro that cleans up dynamically allocated memory and returns the
454 * specified status. */
455 #define BAIL_OUT(status) \
456 if (column_names != NULL) { \
457 sfree(column_names[0]); \
458 sfree(column_names); \
459 } \
460 if (column_values != NULL) { \
461 sfree(column_values[0]); \
462 sfree(column_values); \
463 } \
464 if (res != NULL) { \
465 dbi_result_free(res); \
466 res = NULL; \
467 } \
468 return (status)
469
470 column_names = NULL;
471 column_values = NULL;
472
473 statement = udb_query_get_statement(q);
474 assert(statement != NULL);
475
476 res = dbi_conn_query(db->connection, statement);
477 if (res == NULL) {
478 char errbuf[1024];
479 ERROR("dbi plugin: cdbi_read_database_query (%s, %s): "
480 "dbi_conn_query failed: %s",
481 db->name, udb_query_get_name(q),
482 cdbi_strerror(db->connection, errbuf, sizeof(errbuf)));
483 BAIL_OUT(-1);
484 } else /* Get the number of columns */
485 {
486 unsigned int db_status;
487
488 db_status = dbi_result_get_numfields(res);
489 if (db_status == DBI_FIELD_ERROR) {
490 char errbuf[1024];
491 ERROR("dbi plugin: cdbi_read_database_query (%s, %s): "
492 "dbi_result_get_numfields failed: %s",
493 db->name, udb_query_get_name(q),
494 cdbi_strerror(db->connection, errbuf, sizeof(errbuf)));
495 BAIL_OUT(-1);
496 }
497
498 column_num = (size_t)db_status;
499 DEBUG("cdbi_read_database_query (%s, %s): There are %" PRIsz " columns.",
500 db->name, udb_query_get_name(q), column_num);
501 }
502
503 /* Allocate `column_names' and `column_values'. {{{ */
504 column_names = calloc(column_num, sizeof(*column_names));
505 if (column_names == NULL) {
506 ERROR("dbi plugin: calloc failed.");
507 BAIL_OUT(-1);
508 }
509
510 column_names[0] = calloc(column_num, DATA_MAX_NAME_LEN);
511 if (column_names[0] == NULL) {
512 ERROR("dbi plugin: calloc failed.");
513 BAIL_OUT(-1);
514 }
515 for (size_t i = 1; i < column_num; i++)
516 column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
517
518 column_values = calloc(column_num, sizeof(*column_values));
519 if (column_values == NULL) {
520 ERROR("dbi plugin: calloc failed.");
521 BAIL_OUT(-1);
522 }
523
524 column_values[0] = calloc(column_num, DATA_MAX_NAME_LEN);
525 if (column_values[0] == NULL) {
526 ERROR("dbi plugin: calloc failed.");
527 BAIL_OUT(-1);
528 }
529 for (size_t i = 1; i < column_num; i++)
530 column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
531 /* }}} */
532
533 /* Copy the field names to `column_names' */
534 for (size_t i = 0; i < column_num; i++) /* {{{ */
535 {
536 const char *column_name;
537
538 column_name = dbi_result_get_field_name(res, (unsigned int)(i + 1));
539 if (column_name == NULL) {
540 ERROR("dbi plugin: cdbi_read_database_query (%s, %s): "
541 "Cannot retrieve name of field %" PRIsz ".",
542 db->name, udb_query_get_name(q), i + 1);
543 BAIL_OUT(-1);
544 }
545
546 sstrncpy(column_names[i], column_name, DATA_MAX_NAME_LEN);
547 } /* }}} for (i = 0; i < column_num; i++) */
548
549 status = udb_query_prepare_result(
550 q, prep_area, (db->host ? db->host : hostname_g),
551 /* plugin = */ (db->plugin_name != NULL) ? db->plugin_name : "dbi",
552 db->name, column_names, column_num);
553
554 if (status != 0) {
555 ERROR("dbi plugin: udb_query_prepare_result failed with status %i.",
556 status);
557 BAIL_OUT(-1);
558 }
559
560 /* 0 = error; 1 = success; */
561 status = dbi_result_first_row(res); /* {{{ */
562 if (status != 1) {
563 char errbuf[1024];
564 ERROR("dbi plugin: cdbi_read_database_query (%s, %s): "
565 "dbi_result_first_row failed: %s. Maybe the statement didn't "
566 "return any rows?",
567 db->name, udb_query_get_name(q),
568 cdbi_strerror(db->connection, errbuf, sizeof(errbuf)));
569 udb_query_finish_result(q, prep_area);
570 BAIL_OUT(-1);
571 } /* }}} */
572
573 /* Iterate over all rows and call `udb_query_handle_result' with each list of
574 * values. */
575 while (42) /* {{{ */
576 {
577 status = 0;
578 /* Copy the value of the columns to `column_values' */
579 for (size_t i = 0; i < column_num; i++) /* {{{ */
580 {
581 status = cdbi_result_get_field(res, (unsigned int)(i + 1),
582 column_values[i], DATA_MAX_NAME_LEN);
583
584 if (status != 0) {
585 ERROR("dbi plugin: cdbi_read_database_query (%s, %s): "
586 "cdbi_result_get_field (%" PRIsz ") failed.",
587 db->name, udb_query_get_name(q), i + 1);
588 status = -1;
589 break;
590 }
591 } /* }}} for (i = 0; i < column_num; i++) */
592
593 /* If all values were copied successfully, call `udb_query_handle_result'
594 * to dispatch the row to the daemon. */
595 if (status == 0) /* {{{ */
596 {
597 status = udb_query_handle_result(q, prep_area, column_values);
598 if (status != 0) {
599 ERROR("dbi plugin: cdbi_read_database_query (%s, %s): "
600 "udb_query_handle_result failed.",
601 db->name, udb_query_get_name(q));
602 }
603 } /* }}} */
604
605 /* Get the next row from the database. */
606 if (!dbi_result_has_next_row(res))
607 break;
608
609 status = dbi_result_next_row(res); /* {{{ */
610 if (status != 1) {
611 char errbuf[1024];
612 WARNING("dbi plugin: cdbi_read_database_query (%s, %s): "
613 "dbi_result_next_row failed: %s.",
614 db->name, udb_query_get_name(q),
615 cdbi_strerror(db->connection, errbuf, sizeof(errbuf)));
616 break;
617 } /* }}} */
618 } /* }}} while (42) */
619
620 /* Tell the db query interface that we're done with this query. */
621 udb_query_finish_result(q, prep_area);
622
623 /* Clean up and return `status = 0' (success) */
624 BAIL_OUT(0);
625 #undef BAIL_OUT
626 } /* }}} int cdbi_read_database_query */
627
cdbi_connect_database(cdbi_database_t * db)628 static int cdbi_connect_database(cdbi_database_t *db) /* {{{ */
629 {
630 dbi_driver driver;
631 dbi_conn connection;
632 int status;
633
634 if (db->connection != NULL) {
635 status = dbi_conn_ping(db->connection);
636 if (status != 0) /* connection is alive */
637 return 0;
638
639 dbi_conn_close(db->connection);
640 db->connection = NULL;
641 }
642
643 driver = dbi_driver_open_r(db->driver, dbi_instance);
644 if (driver == NULL) {
645 ERROR("dbi plugin: cdbi_connect_database: dbi_driver_open_r (%s) failed.",
646 db->driver);
647 INFO("dbi plugin: Maybe the driver isn't installed? "
648 "Known drivers are:");
649 for (driver = dbi_driver_list_r(NULL, dbi_instance); driver != NULL;
650 driver = dbi_driver_list_r(driver, dbi_instance)) {
651 INFO("dbi plugin: * %s", dbi_driver_get_name(driver));
652 }
653 return -1;
654 }
655
656 connection = dbi_conn_open(driver);
657 if (connection == NULL) {
658 ERROR("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
659 db->driver);
660 return -1;
661 }
662
663 /* Set all the driver options. Because this is a very very very generic
664 * interface, the error handling is kind of long. If an invalid option is
665 * encountered, it will get a list of options understood by the driver and
666 * report that as `INFO'. This way, users hopefully don't have too much
667 * trouble finding out how to configure the plugin correctly.. */
668 for (size_t i = 0; i < db->driver_options_num; i++) {
669 if (db->driver_options[i].is_numeric) {
670 status =
671 dbi_conn_set_option_numeric(connection, db->driver_options[i].key,
672 db->driver_options[i].value.numeric);
673 if (status != 0) {
674 char errbuf[1024];
675 ERROR("dbi plugin: cdbi_connect_database (%s): "
676 "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.",
677 db->name, db->driver_options[i].key,
678 db->driver_options[i].value.numeric,
679 cdbi_strerror(connection, errbuf, sizeof(errbuf)));
680 }
681 } else {
682 status = dbi_conn_set_option(connection, db->driver_options[i].key,
683 db->driver_options[i].value.string);
684 if (status != 0) {
685 char errbuf[1024];
686 ERROR("dbi plugin: cdbi_connect_database (%s): "
687 "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.",
688 db->name, db->driver_options[i].key,
689 db->driver_options[i].value.string,
690 cdbi_strerror(connection, errbuf, sizeof(errbuf)));
691 }
692 }
693
694 if (status != 0) {
695 INFO("dbi plugin: This is a list of all options understood "
696 "by the `%s' driver:",
697 db->driver);
698 for (const char *opt = dbi_conn_get_option_list(connection, NULL);
699 opt != NULL; opt = dbi_conn_get_option_list(connection, opt)) {
700 INFO("dbi plugin: * %s", opt);
701 }
702
703 dbi_conn_close(connection);
704 return -1;
705 }
706 } /* for (i = 0; i < db->driver_options_num; i++) */
707
708 status = dbi_conn_connect(connection);
709 if (status != 0) {
710 char errbuf[1024];
711 ERROR("dbi plugin: cdbi_connect_database (%s): "
712 "dbi_conn_connect failed: %s",
713 db->name, cdbi_strerror(connection, errbuf, sizeof(errbuf)));
714 dbi_conn_close(connection);
715 return -1;
716 }
717
718 if (db->select_db != NULL) {
719 status = dbi_conn_select_db(connection, db->select_db);
720 if (status != 0) {
721 char errbuf[1024];
722 WARNING(
723 "dbi plugin: cdbi_connect_database (%s): "
724 "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
725 db->name, db->select_db,
726 cdbi_strerror(connection, errbuf, sizeof(errbuf)));
727 dbi_conn_close(connection);
728 return -1;
729 }
730 }
731
732 db->connection = connection;
733 return 0;
734 } /* }}} int cdbi_connect_database */
735
cdbi_read_database(user_data_t * ud)736 static int cdbi_read_database(user_data_t *ud) /* {{{ */
737 {
738 cdbi_database_t *db = (cdbi_database_t *)ud->data;
739 int success;
740 int status;
741
742 unsigned int db_version;
743
744 status = cdbi_connect_database(db);
745 if (status != 0)
746 return status;
747 assert(db->connection != NULL);
748
749 db_version = dbi_conn_get_engine_version(db->connection);
750 /* TODO: Complain if `db_version == 0' */
751
752 success = 0;
753 for (size_t i = 0; i < db->queries_num; i++) {
754 /* Check if we know the database's version and if so, if this query applies
755 * to that version. */
756 if ((db_version != 0) &&
757 (udb_query_check_version(db->queries[i], db_version) == 0))
758 continue;
759
760 status = cdbi_read_database_query(db, db->queries[i], db->q_prep_areas[i]);
761 if (status == 0)
762 success++;
763 }
764
765 if (success == 0) {
766 ERROR("dbi plugin: All queries failed for database `%s'.", db->name);
767 return -1;
768 }
769
770 return 0;
771 } /* }}} int cdbi_read_database */
772
cdbi_shutdown(void)773 static int cdbi_shutdown(void) /* {{{ */
774 {
775 for (size_t i = 0; i < databases_num; i++) {
776 if (databases[i]->connection != NULL) {
777 dbi_conn_close(databases[i]->connection);
778 databases[i]->connection = NULL;
779 }
780 cdbi_database_free(databases[i]);
781 }
782 sfree(databases);
783 databases_num = 0;
784
785 udb_query_free(queries, queries_num);
786 queries = NULL;
787 queries_num = 0;
788
789 return 0;
790 } /* }}} int cdbi_shutdown */
791
module_register(void)792 void module_register(void) /* {{{ */
793 {
794 plugin_register_complex_config("dbi", cdbi_config);
795 plugin_register_init("dbi", cdbi_init);
796 plugin_register_shutdown("dbi", cdbi_shutdown);
797 } /* }}} void module_register */
798