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