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