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