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