Merge branch 'collectd-5.1' into collectd-5.2
[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_add_database_driver_option (cdbi_database_t *db, /* {{{ */
197     oconfig_item_t *ci)
198 {
199   cdbi_driver_option_t *option;
200
201   if ((ci->values_num != 2)
202       || (ci->values[0].type != OCONFIG_TYPE_STRING)
203       || (ci->values[1].type != OCONFIG_TYPE_STRING))
204   {
205     WARNING ("dbi plugin: The `DriverOption' config option "
206         "needs exactly two string arguments.");
207     return (-1);
208   }
209
210   option = (cdbi_driver_option_t *) realloc (db->driver_options,
211       sizeof (*option) * (db->driver_options_num + 1));
212   if (option == NULL)
213   {
214     ERROR ("dbi plugin: realloc failed");
215     return (-1);
216   }
217
218   db->driver_options = option;
219   option = db->driver_options + db->driver_options_num;
220
221   option->key = strdup (ci->values[0].value.string);
222   if (option->key == NULL)
223   {
224     ERROR ("dbi plugin: strdup failed.");
225     return (-1);
226   }
227
228   option->value = strdup (ci->values[1].value.string);
229   if (option->value == NULL)
230   {
231     ERROR ("dbi plugin: strdup failed.");
232     sfree (option->key);
233     return (-1);
234   }
235
236   db->driver_options_num++;
237   return (0);
238 } /* }}} int cdbi_config_add_database_driver_option */
239
240 static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
241 {
242   cdbi_database_t *db;
243   int status;
244   int i;
245
246   if ((ci->values_num != 1)
247       || (ci->values[0].type != OCONFIG_TYPE_STRING))
248   {
249     WARNING ("dbi plugin: The `Database' block "
250         "needs exactly one string argument.");
251     return (-1);
252   }
253
254   db = (cdbi_database_t *) malloc (sizeof (*db));
255   if (db == NULL)
256   {
257     ERROR ("dbi plugin: malloc failed.");
258     return (-1);
259   }
260   memset (db, 0, sizeof (*db));
261
262   status = cf_util_get_string (ci, &db->name);
263   if (status != 0)
264   {
265     sfree (db);
266     return (status);
267   }
268
269   /* Fill the `cdbi_database_t' structure.. */
270   for (i = 0; i < ci->children_num; i++)
271   {
272     oconfig_item_t *child = ci->children + i;
273
274     if (strcasecmp ("Driver", child->key) == 0)
275       status = cf_util_get_string (child, &db->driver);
276     else if (strcasecmp ("DriverOption", child->key) == 0)
277       status = cdbi_config_add_database_driver_option (db, child);
278     else if (strcasecmp ("SelectDB", child->key) == 0)
279       status = cf_util_get_string (child, &db->select_db);
280     else if (strcasecmp ("Query", child->key) == 0)
281       status = udb_query_pick_from_list (child, queries, queries_num,
282           &db->queries, &db->queries_num);
283     else
284     {
285       WARNING ("dbi plugin: Option `%s' not allowed here.", child->key);
286       status = -1;
287     }
288
289     if (status != 0)
290       break;
291   }
292
293   /* Check that all necessary options have been given. */
294   while (status == 0)
295   {
296     if (db->driver == NULL)
297     {
298       WARNING ("dbi plugin: `Driver' not given for database `%s'", db->name);
299       status = -1;
300     }
301     if (db->driver_options_num == 0)
302     {
303       WARNING ("dbi plugin: No `DriverOption' given for database `%s'. "
304           "This will likely not work.", db->name);
305     }
306
307     break;
308   } /* while (status == 0) */
309
310   while ((status == 0) && (db->queries_num > 0))
311   {
312     db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
313         db->queries_num, sizeof (*db->q_prep_areas));
314
315     if (db->q_prep_areas == NULL)
316     {
317       WARNING ("dbi plugin: malloc failed");
318       status = -1;
319       break;
320     }
321
322     for (i = 0; i < db->queries_num; ++i)
323     {
324       db->q_prep_areas[i]
325         = udb_query_allocate_preparation_area (db->queries[i]);
326
327       if (db->q_prep_areas[i] == NULL)
328       {
329         WARNING ("dbi plugin: udb_query_allocate_preparation_area failed");
330         status = -1;
331         break;
332       }
333     }
334
335     break;
336   }
337
338   /* If all went well, add this database to the global list of databases. */
339   if (status == 0)
340   {
341     cdbi_database_t **temp;
342
343     temp = (cdbi_database_t **) realloc (databases,
344         sizeof (*databases) * (databases_num + 1));
345     if (temp == NULL)
346     {
347       ERROR ("dbi plugin: realloc failed");
348       status = -1;
349     }
350     else
351     {
352       databases = temp;
353       databases[databases_num] = db;
354       databases_num++;
355     }
356   }
357
358   if (status != 0)
359   {
360     cdbi_database_free (db);
361     return (-1);
362   }
363
364   return (0);
365 } /* }}} int cdbi_config_add_database */
366
367 static int cdbi_config (oconfig_item_t *ci) /* {{{ */
368 {
369   int i;
370
371   for (i = 0; i < ci->children_num; i++)
372   {
373     oconfig_item_t *child = ci->children + i;
374     if (strcasecmp ("Query", child->key) == 0)
375       udb_query_create (&queries, &queries_num, child,
376           /* callback = */ NULL);
377     else if (strcasecmp ("Database", child->key) == 0)
378       cdbi_config_add_database (child);
379     else
380     {
381       WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
382     }
383   } /* for (ci->children) */
384
385   return (0);
386 } /* }}} int cdbi_config */
387
388 /* }}} End of configuration handling functions */
389
390 static int cdbi_init (void) /* {{{ */
391 {
392   static int did_init = 0;
393   int status;
394
395   if (did_init != 0)
396     return (0);
397
398   if (queries_num == 0)
399   {
400     ERROR ("dbi plugin: No <Query> blocks have been found. Without them, "
401         "this plugin can't do anything useful, so we will returns an error.");
402     return (-1);
403   }
404
405   if (databases_num == 0)
406   {
407     ERROR ("dbi plugin: No <Database> blocks have been found. Without them, "
408         "this plugin can't do anything useful, so we will returns an error.");
409     return (-1);
410   }
411
412   status = dbi_initialize (NULL);
413   if (status < 0)
414   {
415     ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.",
416         status);
417     return (-1);
418   }
419   else if (status == 0)
420   {
421     ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please "
422         "install at least one `DBD' or check your installation.");
423     return (-1);
424   }
425   DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.",
426       status, (status == 1) ? "" : "s");
427
428   return (0);
429 } /* }}} int cdbi_init */
430
431 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
432     udb_query_t *q, udb_query_preparation_area_t *prep_area)
433 {
434   const char *statement;
435   dbi_result res;
436   size_t column_num;
437   char **column_names;
438   char **column_values;
439   int status;
440   size_t i;
441
442   /* Macro that cleans up dynamically allocated memory and returns the
443    * specified status. */
444 #define BAIL_OUT(status) \
445   if (column_names != NULL) { sfree (column_names[0]); sfree (column_names); } \
446   if (column_values != NULL) { sfree (column_values[0]); sfree (column_values); } \
447   if (res != NULL) { dbi_result_free (res); res = NULL; } \
448   return (status)
449
450   column_names = NULL;
451   column_values = NULL;
452   res = NULL;
453
454   statement = udb_query_get_statement (q);
455   assert (statement != NULL);
456
457   res = dbi_conn_query (db->connection, statement);
458   if (res == NULL)
459   {
460     char errbuf[1024];
461     ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
462         "dbi_conn_query failed: %s",
463         db->name, udb_query_get_name (q),
464         cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
465     BAIL_OUT (-1);
466   }
467   else /* Get the number of columns */
468   {
469     unsigned int db_status;
470
471     db_status = dbi_result_get_numfields (res);
472     if (db_status == DBI_FIELD_ERROR)
473     {
474       char errbuf[1024];
475       ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
476           "dbi_result_get_numfields failed: %s",
477           db->name, udb_query_get_name (q),
478           cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
479       BAIL_OUT (-1);
480     }
481
482     column_num = (size_t) db_status;
483     DEBUG ("cdbi_read_database_query (%s, %s): There are %zu columns.",
484         db->name, udb_query_get_name (q), column_num);
485   }
486
487   /* Allocate `column_names' and `column_values'. {{{ */
488   column_names = (char **) calloc (column_num, sizeof (char *));
489   if (column_names == NULL)
490   {
491     ERROR ("dbi plugin: malloc failed.");
492     BAIL_OUT (-1);
493   }
494
495   column_names[0] = (char *) calloc (column_num,
496       DATA_MAX_NAME_LEN * sizeof (char));
497   if (column_names[0] == NULL)
498   {
499     ERROR ("dbi plugin: malloc failed.");
500     BAIL_OUT (-1);
501   }
502   for (i = 1; i < column_num; i++)
503     column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
504
505   column_values = (char **) calloc (column_num, sizeof (char *));
506   if (column_values == NULL)
507   {
508     ERROR ("dbi plugin: malloc failed.");
509     BAIL_OUT (-1);
510   }
511
512   column_values[0] = (char *) calloc (column_num,
513       DATA_MAX_NAME_LEN * sizeof (char));
514   if (column_values[0] == NULL)
515   {
516     ERROR ("dbi plugin: malloc failed.");
517     BAIL_OUT (-1);
518   }
519   for (i = 1; i < column_num; i++)
520     column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
521   /* }}} */
522
523   /* Copy the field names to `column_names' */
524   for (i = 0; i < column_num; i++) /* {{{ */
525   {
526     const char *column_name;
527
528     column_name = dbi_result_get_field_name (res, (unsigned int) (i + 1));
529     if (column_name == NULL)
530     {
531       ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
532           "Cannot retrieve name of field %zu.",
533           db->name, udb_query_get_name (q), i + 1);
534       BAIL_OUT (-1);
535     }
536
537     sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
538   } /* }}} for (i = 0; i < column_num; i++) */
539
540   udb_query_prepare_result (q, prep_area, hostname_g,
541       /* plugin = */ "dbi", db->name,
542       column_names, column_num, /* interval = */ 0);
543
544   /* 0 = error; 1 = success; */
545   status = dbi_result_first_row (res); /* {{{ */
546   if (status != 1)
547   {
548     char errbuf[1024];
549     ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
550         "dbi_result_first_row failed: %s. Maybe the statement didn't "
551         "return any rows?",
552         db->name, udb_query_get_name (q),
553         cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
554     udb_query_finish_result (q, prep_area);
555     BAIL_OUT (-1);
556   } /* }}} */
557
558   /* Iterate over all rows and call `udb_query_handle_result' with each list of
559    * values. */
560   while (42) /* {{{ */
561   {
562     status = 0;
563     /* Copy the value of the columns to `column_values' */
564     for (i = 0; i < column_num; i++) /* {{{ */
565     {
566       status = cdbi_result_get_field (res, (unsigned int) (i + 1),
567           column_values[i], DATA_MAX_NAME_LEN);
568
569       if (status != 0)
570       {
571         ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
572             "cdbi_result_get_field (%zu) failed.",
573             db->name, udb_query_get_name (q), i + 1);
574         status = -1;
575         break;
576       }
577     } /* }}} for (i = 0; i < column_num; i++) */
578
579     /* If all values were copied successfully, call `udb_query_handle_result'
580      * to dispatch the row to the daemon. */
581     if (status == 0) /* {{{ */
582     {
583       status = udb_query_handle_result (q, prep_area, column_values);
584       if (status != 0)
585       {
586         ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
587             "udb_query_handle_result failed.",
588             db->name, udb_query_get_name (q));
589       }
590     } /* }}} */
591
592     /* Get the next row from the database. */
593     status = dbi_result_next_row (res); /* {{{ */
594     if (status != 1)
595     {
596       if (dbi_conn_error (db->connection, NULL) != 0)
597       {
598         char errbuf[1024];
599         WARNING ("dbi plugin: cdbi_read_database_query (%s, %s): "
600             "dbi_result_next_row failed: %s.",
601             db->name, udb_query_get_name (q),
602             cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
603       }
604       break;
605     } /* }}} */
606   } /* }}} while (42) */
607
608   /* Tell the db query interface that we're done with this query. */
609   udb_query_finish_result (q, prep_area);
610
611   /* Clean up and return `status = 0' (success) */
612   BAIL_OUT (0);
613 #undef BAIL_OUT
614 } /* }}} int cdbi_read_database_query */
615
616 static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
617 {
618   dbi_driver driver;
619   dbi_conn connection;
620   size_t i;
621   int status;
622
623   if (db->connection != NULL)
624   {
625     status = dbi_conn_ping (db->connection);
626     if (status != 0) /* connection is alive */
627       return (0);
628
629     dbi_conn_close (db->connection);
630     db->connection = NULL;
631   }
632
633   driver = dbi_driver_open (db->driver);
634   if (driver == NULL)
635   {
636     ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.",
637         db->driver);
638     INFO ("dbi plugin: Maybe the driver isn't installed? "
639         "Known drivers are:");
640     for (driver = dbi_driver_list (NULL);
641         driver != NULL;
642         driver = dbi_driver_list (driver))
643     {
644       INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
645     }
646     return (-1);
647   }
648
649   connection = dbi_conn_open (driver);
650   if (connection == NULL)
651   {
652     ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
653         db->driver);
654     return (-1);
655   }
656
657   /* Set all the driver options. Because this is a very very very generic
658    * interface, the error handling is kind of long. If an invalid option is
659    * encountered, it will get a list of options understood by the driver and
660    * report that as `INFO'. This way, users hopefully don't have too much
661    * trouble finding out how to configure the plugin correctly.. */
662   for (i = 0; i < db->driver_options_num; i++)
663   {
664     DEBUG ("dbi plugin: cdbi_connect_database (%s): "
665         "key = %s; value = %s;",
666         db->name,
667         db->driver_options[i].key,
668         db->driver_options[i].value);
669
670     status = dbi_conn_set_option (connection,
671         db->driver_options[i].key, db->driver_options[i].value);
672     if (status != 0)
673     {
674       char errbuf[1024];
675       const char *opt;
676
677       ERROR ("dbi plugin: cdbi_connect_database (%s): "
678           "dbi_conn_set_option (%s, %s) failed: %s.",
679           db->name,
680           db->driver_options[i].key, db->driver_options[i].value,
681           cdbi_strerror (connection, errbuf, sizeof (errbuf)));
682
683       INFO ("dbi plugin: This is a list of all options understood "
684           "by the `%s' driver:", db->driver);
685       for (opt = dbi_conn_get_option_list (connection, NULL);
686           opt != NULL;
687           opt = dbi_conn_get_option_list (connection, opt))
688       {
689         INFO ("dbi plugin: * %s", opt);
690       }
691
692       dbi_conn_close (connection);
693       return (-1);
694     }
695   } /* for (i = 0; i < db->driver_options_num; i++) */
696
697   status = dbi_conn_connect (connection);
698   if (status != 0)
699   {
700     char errbuf[1024];
701     ERROR ("dbi plugin: cdbi_connect_database (%s): "
702         "dbi_conn_connect failed: %s",
703         db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
704     dbi_conn_close (connection);
705     return (-1);
706   }
707
708   if (db->select_db != NULL)
709   {
710     status = dbi_conn_select_db (connection, db->select_db);
711     if (status != 0)
712     {
713       char errbuf[1024];
714       WARNING ("dbi plugin: cdbi_connect_database (%s): "
715           "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
716           db->name, db->select_db,
717           cdbi_strerror (connection, errbuf, sizeof (errbuf)));
718       dbi_conn_close (connection);
719       return (-1);
720     }
721   }
722
723   db->connection = connection;
724   return (0);
725 } /* }}} int cdbi_connect_database */
726
727 static int cdbi_read_database (cdbi_database_t *db) /* {{{ */
728 {
729   size_t i;
730   int success;
731   int status;
732
733   unsigned int db_version;
734
735   status = cdbi_connect_database (db);
736   if (status != 0)
737     return (status);
738   assert (db->connection != NULL);
739
740   db_version = dbi_conn_get_engine_version (db->connection);
741   /* TODO: Complain if `db_version == 0' */
742
743   success = 0;
744   for (i = 0; i < db->queries_num; i++)
745   {
746     /* Check if we know the database's version and if so, if this query applies
747      * to that version. */
748     if ((db_version != 0)
749         && (udb_query_check_version (db->queries[i], db_version) == 0))
750       continue;
751
752     status = cdbi_read_database_query (db,
753         db->queries[i], db->q_prep_areas[i]);
754     if (status == 0)
755       success++;
756   }
757
758   if (success == 0)
759   {
760     ERROR ("dbi plugin: All queries failed for database `%s'.", db->name);
761     return (-1);
762   }
763
764   return (0);
765 } /* }}} int cdbi_read_database */
766
767 static int cdbi_read (void) /* {{{ */
768 {
769   size_t i;
770   int success = 0;
771   int status;
772
773   for (i = 0; i < databases_num; i++)
774   {
775     status = cdbi_read_database (databases[i]);
776     if (status == 0)
777       success++;
778   }
779
780   if (success == 0)
781   {
782     ERROR ("dbi plugin: No database could be read. Will return an error so "
783         "the plugin will be delayed.");
784     return (-1);
785   }
786
787   return (0);
788 } /* }}} int cdbi_read */
789
790 static int cdbi_shutdown (void) /* {{{ */
791 {
792   size_t i;
793
794   for (i = 0; i < databases_num; i++)
795   {
796     if (databases[i]->connection != NULL)
797     {
798       dbi_conn_close (databases[i]->connection);
799       databases[i]->connection = NULL;
800     }
801     cdbi_database_free (databases[i]);
802   }
803   sfree (databases);
804   databases_num = 0;
805
806   udb_query_free (queries, queries_num);
807   queries = NULL;
808   queries_num = 0;
809
810   return (0);
811 } /* }}} int cdbi_shutdown */
812
813 void module_register (void) /* {{{ */
814 {
815   plugin_register_complex_config ("dbi", cdbi_config);
816   plugin_register_init ("dbi", cdbi_init);
817   plugin_register_read ("dbi", cdbi_read);
818   plugin_register_shutdown ("dbi", cdbi_shutdown);
819 } /* }}} void module_register */
820
821 /*
822  * vim: shiftwidth=2 softtabstop=2 et fdm=marker
823  */