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