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