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