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