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