0a3d86070b55e50273bed3408905484aff47b701
[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   cdtime_t interval;
68
69   char *driver;
70   char *host;
71   cdbi_driver_option_t *driver_options;
72   size_t driver_options_num;
73
74   udb_query_preparation_area_t **q_prep_areas;
75   udb_query_t **queries;
76   size_t        queries_num;
77
78   dbi_conn connection;
79 };
80 typedef struct cdbi_database_s cdbi_database_t; /* }}} */
81
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;
92
93 static int cdbi_read_database (user_data_t *ud);
94
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;
103
104   if (conn == NULL)
105   {
106     sstrncpy (buffer, "connection is NULL", buffer_size);
107     return (buffer);
108   }
109
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);
117
118   return (buffer);
119 } /* }}} const char *cdbi_conn_error */
120
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;
125
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   }
133
134   if (src_type == DBI_TYPE_INTEGER)
135   {
136     long long value;
137
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;
144
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;
151     
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;
165
166     field_name = dbi_result_get_field_name (res, index);
167     if (field_name == NULL)
168       field_name = "<unknown>";
169
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   }
175
176   return (0);
177 } /* }}} int cdbi_result_get_field */
178
179 static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
180 {
181   size_t i;
182
183   if (db == NULL)
184     return;
185
186   sfree (db->name);
187   sfree (db->driver);
188
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);
196
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);
201
202   sfree (db);
203 } /* }}} void cdbi_database_free */
204
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  */
227
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;
232
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   }
242
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   }
250
251   db->driver_options = option;
252   option = db->driver_options + db->driver_options_num;
253   memset (option, 0, sizeof (*option));
254
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   }
261
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   }
278
279   db->driver_options_num++;
280   return (0);
281 } /* }}} int cdbi_config_add_database_driver_option */
282
283 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
284 {
285   cdbi_database_t *db;
286   int status;
287   int i;
288
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   }
296
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));
304
305   status = cf_util_get_string (ci, &db->name);
306   if (status != 0)
307   {
308     sfree (db);
309     return (status);
310   }
311
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;
316
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     }
335
336     if (status != 0)
337       break;
338   }
339
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     }
353
354     break;
355   } /* while (status == 0) */
356
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));
362
363     if (db->q_prep_areas == NULL)
364     {
365       WARNING ("dbi plugin: malloc failed");
366       status = -1;
367       break;
368     }
369
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]);
374
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     }
382
383     break;
384   }
385
386   /* If all went well, add this database to the global list of databases. */
387   if (status == 0)
388   {
389     cdbi_database_t **temp;
390
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;
402       struct timespec interval = { 0, 0 };
403       CDTIME_T_TO_TIMESPEC (db->interval, &interval);
404
405       databases = temp;
406       databases[databases_num] = db;
407       databases_num++;
408
409       memset (&ud, 0, sizeof (ud));
410       ud.data = (void *) db;
411       ud.free_func = NULL;
412       name = ssnprintf_alloc("dbi:%s", db->name);
413
414       plugin_register_complex_read (/* group = */ NULL,
415           /* name = */ name ? name : db->name,
416           /* callback = */ cdbi_read_database,
417           /* interval = */ (db->interval > 0) ? &interval : NULL,
418           /* user_data = */ &ud);
419       free (name);
420     }
421   }
422
423   if (status != 0)
424   {
425     cdbi_database_free (db);
426     return (-1);
427   }
428
429   return (0);
430 } /* }}} int cdbi_config_add_database */
431
432 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
433 {
434   int i;
435
436   for (i = 0; i < ci->children_num; i++)
437   {
438     oconfig_item_t *child = ci->children + i;
439     if (strcasecmp ("Query", child->key) == 0)
440       udb_query_create (&queries, &queries_num, child,
441           /* callback = */ NULL);
442     else if (strcasecmp ("Database", child->key) == 0)
443       cdbi_config_add_database (child);
444     else
445     {
446       WARNING ("dbi plugin: Ignoring unknown config option `%s'.", child->key);
447     }
448   } /* for (ci->children) */
449
450   return (0);
451 } /* }}} int cdbi_config */
452
453 /* }}} End of configuration handling functions */
454
455 static int cdbi_init (void) /* {{{ */
456 {
457   static int did_init = 0;
458   int status;
459
460   if (did_init != 0)
461     return (0);
462
463   if (queries_num == 0)
464   {
465     ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
466         "this plugin can't do anything useful, so we will returns an error.");
467     return (-1);
468   }
469
470   if (databases_num == 0)
471   {
472     ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
473         "this plugin can't do anything useful, so we will returns an error.");
474     return (-1);
475   }
476
477   status = dbi_initialize_r (/* driverdir = */ NULL, &dbi_instance);
478   if (status < 0)
479   {
480     ERROR ("dbi plugin: cdbi_init: dbi_initialize_r failed with status %i.",
481         status);
482     return (-1);
483   }
484   else if (status == 0)
485   {
486     ERROR ("dbi plugin: `dbi_initialize_r' could not load any drivers. Please "
487         "install at least one `DBD' or check your installation.");
488     return (-1);
489   }
490   DEBUG ("dbi plugin: cdbi_init: dbi_initialize_r reports %i driver%s.",
491       status, (status == 1) ? "" : "s");
492
493   return (0);
494 } /* }}} int cdbi_init */
495
496 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
497     udb_query_t *q, udb_query_preparation_area_t *prep_area)
498 {
499   const char *statement;
500   dbi_result res;
501   size_t column_num;
502   char **column_names;
503   char **column_values;
504   int status;
505   size_t i;
506
507   /* Macro that cleans up dynamically allocated memory and returns the
508    * specified status. */
509 #define BAIL_OUT(status) \
510   if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
511   if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
512   if (res != NULL) { dbi_result_free (res); res = NULL; } \
513   return (status)
514
515   column_names = NULL;
516   column_values = NULL;
517   res = NULL;
518
519   statement = udb_query_get_statement (q);
520   assert (statement != NULL);
521
522   res = dbi_conn_query (db->connection, statement);
523   if (res == NULL)
524   {
525     char errbuf[1024];
526     ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
527         "dbi_conn_query failed: %s",
528         db->name, udb_query_get_name (q),
529         cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
530     BAIL_OUT (-1);
531   }
532   else /* Get the number of columns */
533   {
534     unsigned int db_status;
535
536     db_status = dbi_result_get_numfields (res);
537     if (db_status == DBI_FIELD_ERROR)
538     {
539       char errbuf[1024];
540       ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
541           "dbi_result_get_numfields failed: %s",
542           db->name, udb_query_get_name (q),
543           cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
544       BAIL_OUT (-1);
545     }
546
547     column_num = (size_t) db_status;
548     DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
549         db->name, udb_query_get_name (q), column_num);
550   }
551
552   /* Allocate `column_names' and `column_values'. {{{ */
553   column_names = (char **) calloc (column_num, sizeof (char *));
554   if (column_names == NULL)
555   {
556     ERROR ("dbi plugin: malloc failed.");
557     BAIL_OUT (-1);
558   }
559
560   column_names[0] = (char *) calloc (column_num,
561       DATA_MAX_NAME_LEN * sizeof (char));
562   if (column_names[0] == NULL)
563   {
564     ERROR ("dbi plugin: malloc failed.");
565     BAIL_OUT (-1);
566   }
567   for (i = 1; i < column_num; i++)
568     column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
569
570   column_values = (char **) calloc (column_num, sizeof (char *));
571   if (column_values == NULL)
572   {
573     ERROR ("dbi plugin: malloc failed.");
574     BAIL_OUT (-1);
575   }
576
577   column_values[0] = (char *) calloc (column_num,
578       DATA_MAX_NAME_LEN * sizeof (char));
579   if (column_values[0] == NULL)
580   {
581     ERROR ("dbi plugin: malloc failed.");
582     BAIL_OUT (-1);
583   }
584   for (i = 1; i < column_num; i++)
585     column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
586   /* }}} */
587
588   /* Copy the field names to `column_names' */
589   for (i = 0; i < column_num; i++) /* {{{ */
590   {
591     const char *column_name;
592
593     column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
594     if (column_name == NULL)
595     {
596       ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
597           "Cannot retrieve name of field %zu.",
598           db->name, udb_query_get_name (q), i + 1);
599       BAIL_OUT (-1);
600     }
601
602     sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
603   } /* }}} for (i = 0; i < column_num; i++) */
604
605   udb_query_prepare_result (q, prep_area, (db->host ? db->host : hostname_g),
606       /* plugin = */ "dbi", db->name,
607       column_names, column_num, /* interval = */ (db->interval > 0) ? db->interval : 0);
608
609   /* 0 = error; 1 = success; */
610   status = dbi_result_first_row (res); /* {{{ */
611   if (status != 1)
612   {
613     char errbuf[1024];
614     ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
615         "dbi_result_first_row failed: %s. Maybe the statement didn't "
616         "return any rows?",
617         db->name, udb_query_get_name (q),
618         cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
619     udb_query_finish_result (q, prep_area);
620     BAIL_OUT (-1);
621   } /* }}} */
622
623   /* Iterate over all rows and call `udb_query_handle_result' with each list of
624    * values. */
625   while (42) /* {{{ */
626   {
627     status = 0;
628     /* Copy the value of the columns to `column_values' */
629     for (i = 0; i < column_num; i++) /* {{{ */
630     {
631       status = cdbi_result_get_field (res, (unsigned int) (i + 1),
632           column_values[i], DATA_MAX_NAME_LEN);
633
634       if (status != 0)
635       {
636         ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
637             "cdbi_result_get_field (%zu) failed.",
638             db->name, udb_query_get_name (q), i + 1);
639         status = -1;
640         break;
641       }
642     } /* }}} for (i = 0; i < column_num; i++) */
643
644     /* If all values were copied successfully, call `udb_query_handle_result'
645      * to dispatch the row to the daemon. */
646     if (status == 0) /* {{{ */
647     {
648       status = udb_query_handle_result (q, prep_area, column_values);
649       if (status != 0)
650       {
651         ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
652             "udb_query_handle_result failed.",
653             db->name, udb_query_get_name (q));
654       }
655     } /* }}} */
656
657     /* Get the next row from the database. */
658     status = dbi_result_next_row (res); /* {{{ */
659     if (status != 1)
660     {
661       if (dbi_conn_error (db->connection, NULL) != 0)
662       {
663         char errbuf[1024];
664         WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
665             "dbi_result_next_row failed: %s.",
666             db->name, udb_query_get_name (q),
667             cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
668       }
669       break;
670     } /* }}} */
671   } /* }}} while (42) */
672
673   /* Tell the db query interface that we're done with this query. */
674   udb_query_finish_result (q, prep_area);
675
676   /* Clean up and return `status = 0' (success) */
677   BAIL_OUT (0);
678 #undef BAIL_OUT
679 } /* }}} int cdbi_read_database_query */
680
681 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
682 {
683   dbi_driver driver;
684   dbi_conn connection;
685   size_t i;
686   int status;
687
688   if (db->connection != NULL)
689   {
690     status = dbi_conn_ping (db->connection);
691     if (status != 0) /* connection is alive */
692       return (0);
693
694     dbi_conn_close (db->connection);
695     db->connection = NULL;
696   }
697
698   driver = dbi_driver_open_r (db->driver, dbi_instance);
699   if (driver == NULL)
700   {
701     ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open_r (%s) failed.",
702         db->driver);
703     INFO ("dbi plugin: Maybe the driver isn't installed? "
704         "Known drivers are:");
705     for (driver = dbi_driver_list_r (NULL, dbi_instance);
706         driver != NULL;
707         driver = dbi_driver_list_r (driver, dbi_instance))
708     {
709       INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
710     }
711     return (-1);
712   }
713
714   connection = dbi_conn_open (driver);
715   if (connection == NULL)
716   {
717     ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
718         db->driver);
719     return (-1);
720   }
721
722   /* Set all the driver options. Because this is a very very very generic
723    * interface, the error handling is kind of long. If an invalid option is
724    * encountered, it will get a list of options understood by the driver and
725    * report that as `INFO'. This way, users hopefully don't have too much
726    * trouble finding out how to configure the plugin correctly.. */
727   for (i = 0; i < db->driver_options_num; i++)
728   {
729     if (db->driver_options[i].is_numeric)
730     {
731       status = dbi_conn_set_option_numeric (connection,
732           db->driver_options[i].key, db->driver_options[i].value.numeric);
733       if (status != 0)
734       {
735         char errbuf[1024];
736         ERROR ("dbi plugin: cdbi_connect_database (%s): "
737             "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.",
738             db->name,
739             db->driver_options[i].key, db->driver_options[i].value.numeric,
740             cdbi_strerror (connection, errbuf, sizeof (errbuf)));
741       }
742     }
743     else
744     {
745       status = dbi_conn_set_option (connection,
746           db->driver_options[i].key, db->driver_options[i].value.string);
747       if (status != 0)
748       {
749         char errbuf[1024];
750         ERROR ("dbi plugin: cdbi_connect_database (%s): "
751             "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.",
752             db->name,
753             db->driver_options[i].key, db->driver_options[i].value.string,
754             cdbi_strerror (connection, errbuf, sizeof (errbuf)));
755       }
756     }
757
758     if (status != 0)
759     {
760       char const *opt;
761
762       INFO ("dbi plugin: This is a list of all options understood "
763           "by the `%s' driver:", db->driver);
764       for (opt = dbi_conn_get_option_list (connection, NULL);
765           opt != NULL;
766           opt = dbi_conn_get_option_list (connection, opt))
767       {
768         INFO ("dbi plugin: * %s", opt);
769       }
770
771       dbi_conn_close (connection);
772       return (-1);
773     }
774   } /* for (i = 0; i < db->driver_options_num; i++) */
775
776   status = dbi_conn_connect (connection);
777   if (status != 0)
778   {
779     char errbuf[1024];
780     ERROR ("dbi plugin: cdbi_connect_database (%s): "
781         "dbi_conn_connect failed: %s",
782         db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
783     dbi_conn_close (connection);
784     return (-1);
785   }
786
787   if (db->select_db != NULL)
788   {
789     status = dbi_conn_select_db (connection, db->select_db);
790     if (status != 0)
791     {
792       char errbuf[1024];
793       WARNING ("dbi plugin: cdbi_connect_database (%s): "
794           "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
795           db->name, db->select_db,
796           cdbi_strerror (connection, errbuf, sizeof (errbuf)));
797       dbi_conn_close (connection);
798       return (-1);
799     }
800   }
801
802   db->connection = connection;
803   return (0);
804 } /* }}} int cdbi_connect_database */
805
806 static int cdbi_read_database (user_data_t *ud) /* {{{ */
807 {
808   cdbi_database_t *db = (cdbi_database_t *) ud->data;
809   size_t i;
810   int success;
811   int status;
812
813   unsigned int db_version;
814
815   status = cdbi_connect_database (db);
816   if (status != 0)
817     return (status);
818   assert (db->connection != NULL);
819
820   db_version = dbi_conn_get_engine_version (db->connection);
821   /* TODO: Complain if `db_version == 0' */
822
823   success = 0;
824   for (i = 0; i < db->queries_num; i++)
825   {
826     /* Check if we know the database's version and if so, if this query applies
827      * to that version. */
828     if ((db_version != 0)
829         && (udb_query_check_version (db->queries[i], db_version) == 0))
830       continue;
831
832     status = cdbi_read_database_query (db,
833         db->queries[i], db->q_prep_areas[i]);
834     if (status == 0)
835       success++;
836   }
837
838   if (success == 0)
839   {
840     ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
841     return (-1);
842   }
843
844   return (0);
845 } /* }}} int cdbi_read_database */
846
847 static int cdbi_shutdown (void) /* {{{ */
848 {
849   size_t i;
850
851   for (i = 0; i < databases_num; i++)
852   {
853     if (databases[i]->connection != NULL)
854     {
855       dbi_conn_close (databases[i]->connection);
856       databases[i]->connection = NULL;
857     }
858     cdbi_database_free (databases[i]);
859   }
860   sfree (databases);
861   databases_num = 0;
862
863   udb_query_free (queries, queries_num);
864   queries = NULL;
865   queries_num = 0;
866
867   return (0);
868 } /* }}} int cdbi_shutdown */
869
870 void module_register (void) /* {{{ */
871 {
872   plugin_register_complex_config ("dbi", cdbi_config);
873   plugin_register_init ("dbi", cdbi_init);
874   plugin_register_shutdown ("dbi", cdbi_shutdown);
875 } /* }}} void module_register */
876
877 /*
878  * vim: shiftwidth=2 softtabstop=2 et fdm=marker
879  */