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