X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fdbi.c;h=29cf5bd52c7783beca315b9e9bd2a0dd649b02d5;hb=c334d63f821c5390af86b90c30e5334b2e97c6ad;hp=0c1982d71719fc2187beeb6d4240da76ad87253d;hpb=47d3c32c3d4c7af10a926f97fab9e81a0c8cbb48;p=collectd.git diff --git a/src/dbi.c b/src/dbi.c index 0c1982d7..29cf5bd5 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -1,6 +1,6 @@ /** * collectd - src/dbi.c - * Copyright (C) 2008-2013 Florian octo Forster + * Copyright (C) 2008-2015 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -27,6 +27,18 @@ #include +/* libdbi 0.9.0 introduced a new thread-safe interface and marked the old + * functions "deprecated". These macros convert the new functions to their old + * counterparts for backwards compatibility. */ +#if !defined(LIBDBI_VERSION) || (LIBDBI_VERSION < 900) +# define HAVE_LEGACY_LIBDBI 1 +# define dbi_initialize_r(a,inst) dbi_initialize(a) +# define dbi_shutdown_r(inst) dbi_shutdown() +# define dbi_set_verbosity_r(a,inst) dbi_set_verbosity(a) +# define dbi_driver_list_r(a,inst) dbi_driver_list(a) +# define dbi_driver_open_r(a,inst) dbi_driver_open(a) +#endif + /* * Data types */ @@ -48,6 +60,7 @@ struct cdbi_database_s /* {{{ */ char *select_db; char *driver; + char *host; cdbi_driver_option_t *driver_options; size_t driver_options_num; @@ -62,6 +75,9 @@ typedef struct cdbi_database_s cdbi_database_t; /* }}} */ /* * Global variables */ +#if !defined(HAVE_LEGACY_LIBDBI) || !HAVE_LEGACY_LIBDBI +static dbi_inst dbi_instance = 0; +#endif static udb_query_t **queries = NULL; static size_t queries_num = 0; static cdbi_database_t **databases = NULL; @@ -297,6 +313,8 @@ static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */ else if (strcasecmp ("Query", child->key) == 0) status = udb_query_pick_from_list (child, queries, queries_num, &db->queries, &db->queries_num); + else if (strcasecmp ("Host", child->key) == 0) + status = cf_util_get_string (child, &db->host); else { WARNING ("dbi plugin: Option `%s' not allowed here.", child->key); @@ -395,7 +413,7 @@ static int cdbi_config (oconfig_item_t *ci) /* {{{ */ cdbi_config_add_database (child); else { - WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key); + WARNING ("dbi plugin: Ignoring unknown config option `%s'.", child->key); } } /* for (ci->children) */ @@ -426,20 +444,20 @@ static int cdbi_init (void) /* {{{ */ return (-1); } - status = dbi_initialize (NULL); + status = dbi_initialize_r (/* driverdir = */ NULL, &dbi_instance); if (status < 0) { - ERROR ("dbi plugin: cdbi_init: dbi_initialize failed with status %i.", + ERROR ("dbi plugin: cdbi_init: dbi_initialize_r failed with status %i.", status); return (-1); } else if (status == 0) { - ERROR ("dbi plugin: `dbi_initialize' could not load any drivers. Please " + ERROR ("dbi plugin: `dbi_initialize_r' could not load any drivers. Please " "install at least one `DBD' or check your installation."); return (-1); } - DEBUG ("dbi plugin: cdbi_init: dbi_initialize reports %i driver%s.", + DEBUG ("dbi plugin: cdbi_init: dbi_initialize_r reports %i driver%s.", status, (status == 1) ? "" : "s"); return (0); @@ -554,7 +572,7 @@ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */ sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN); } /* }}} for (i = 0; i < column_num; i++) */ - udb_query_prepare_result (q, prep_area, hostname_g, + udb_query_prepare_result (q, prep_area, (db->host ? db->host : hostname_g), /* plugin = */ "dbi", db->name, column_names, column_num, /* interval = */ 0); @@ -647,16 +665,16 @@ static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */ db->connection = NULL; } - driver = dbi_driver_open (db->driver); + driver = dbi_driver_open_r (db->driver, dbi_instance); if (driver == NULL) { - ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open (%s) failed.", + ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open_r (%s) failed.", db->driver); INFO ("dbi plugin: Maybe the driver isn't installed? " "Known drivers are:"); - for (driver = dbi_driver_list (NULL); + for (driver = dbi_driver_list_r (NULL, dbi_instance); driver != NULL; - driver = dbi_driver_list (driver)) + driver = dbi_driver_list_r (driver, dbi_instance)) { INFO ("dbi plugin: * %s", dbi_driver_get_name (driver)); } @@ -801,6 +819,9 @@ static int cdbi_read (void) /* {{{ */ int success = 0; int status; + /* TODO(octo): Starting with libdbi 0.9.0, there is an "instance" argument to + * the *_r-functions. We should probably have multiple read callbacks instead + * of this loop. */ for (i = 0; i < databases_num; i++) { status = cdbi_read_database (databases[i]);