X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdbi.c;h=6e98168788526a4b9a515d14ecf84969c0c34590;hb=e88be91ffc1d4ea252f19d5c126390285bd0917e;hp=223e159f3f9ab88293a8a7edd4d68df1bc661459;hpb=8aad55ab7d737a97d5927458b2b00885e27cae4d;p=collectd.git diff --git a/src/dbi.c b/src/dbi.c index 223e159f..6e981687 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 * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -32,9 +32,18 @@ #include -#ifdef HAVE_LIBDBI_R - dbi_inst inst = NULL; +/* 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 */ @@ -55,6 +64,8 @@ struct cdbi_database_s /* {{{ */ char *name; char *select_db; + cdtime_t interval; + char *driver; char *host; cdbi_driver_option_t *driver_options; @@ -71,6 +82,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; @@ -203,6 +217,7 @@ static void cdbi_database_free (cdbi_database_t *db) /* {{{ */ * * * Driver "mysql" + * Interval 120 * DriverOption "hostname" "localhost" * ... * Query "plugin_instance0" @@ -279,13 +294,12 @@ static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */ return (-1); } - db = (cdbi_database_t *) malloc (sizeof (*db)); + db = calloc (1, sizeof (*db)); if (db == NULL) { - ERROR ("dbi plugin: malloc failed."); + ERROR ("dbi plugin: calloc failed."); return (-1); } - memset (db, 0, sizeof (*db)); status = cf_util_get_string (ci, &db->name); if (status != 0) @@ -310,6 +324,8 @@ static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */ &db->queries, &db->queries_num); else if (strcasecmp ("Host", child->key) == 0) status = cf_util_get_string (child, &db->host); + else if (strcasecmp ("Interval", child->key) == 0) + status = cf_util_get_cdtime(child, &db->interval); else { WARNING ("dbi plugin: Option `%s' not allowed here.", child->key); @@ -339,22 +355,22 @@ static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */ while ((status == 0) && (db->queries_num > 0)) { - db->q_prep_areas = (udb_query_preparation_area_t **) calloc ( - db->queries_num, sizeof (*db->q_prep_areas)); + size_t j; + db->q_prep_areas = calloc (db->queries_num, sizeof (*db->q_prep_areas)); if (db->q_prep_areas == NULL) { - WARNING ("dbi plugin: malloc failed"); + WARNING ("dbi plugin: calloc failed"); status = -1; break; } - for (i = 0; i < db->queries_num; ++i) + for (j = 0; j < db->queries_num; ++j) { - db->q_prep_areas[i] - = udb_query_allocate_preparation_area (db->queries[i]); + db->q_prep_areas[j] + = udb_query_allocate_preparation_area (db->queries[j]); - if (db->q_prep_areas[i] == NULL) + if (db->q_prep_areas[j] == NULL) { WARNING ("dbi plugin: udb_query_allocate_preparation_area failed"); status = -1; @@ -394,7 +410,7 @@ static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */ plugin_register_complex_read (/* group = */ NULL, /* name = */ name ? name : db->name, /* callback = */ cdbi_read_database, - /* interval = */ NULL, + /* interval = */ (db->interval > 0) ? db->interval : 0, /* user_data = */ &ud); free (name); } @@ -454,24 +470,20 @@ static int cdbi_init (void) /* {{{ */ return (-1); } -#ifdef HAVE_LIBDBI_R - status = dbi_initialize_r (NULL, &inst); -#else - status = dbi_initialize (NULL); -#endif + 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); @@ -498,7 +510,6 @@ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */ column_names = NULL; column_values = NULL; - res = NULL; statement = udb_query_get_statement (q); assert (statement != NULL); @@ -534,35 +545,33 @@ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */ } /* Allocate `column_names' and `column_values'. {{{ */ - column_names = (char **) calloc (column_num, sizeof (char *)); + column_names = calloc (column_num, sizeof (*column_names)); if (column_names == NULL) { - ERROR ("dbi plugin: malloc failed."); + ERROR ("dbi plugin: calloc failed."); BAIL_OUT (-1); } - column_names[0] = (char *) calloc (column_num, - DATA_MAX_NAME_LEN * sizeof (char)); + column_names[0] = calloc (column_num, DATA_MAX_NAME_LEN); if (column_names[0] == NULL) { - ERROR ("dbi plugin: malloc failed."); + ERROR ("dbi plugin: calloc failed."); BAIL_OUT (-1); } for (i = 1; i < column_num; i++) column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN; - column_values = (char **) calloc (column_num, sizeof (char *)); + column_values = calloc (column_num, sizeof (*column_values)); if (column_values == NULL) { - ERROR ("dbi plugin: malloc failed."); + ERROR ("dbi plugin: calloc failed."); BAIL_OUT (-1); } - column_values[0] = (char *) calloc (column_num, - DATA_MAX_NAME_LEN * sizeof (char)); + column_values[0] = calloc (column_num, DATA_MAX_NAME_LEN); if (column_values[0] == NULL) { - ERROR ("dbi plugin: malloc failed."); + ERROR ("dbi plugin: calloc failed."); BAIL_OUT (-1); } for (i = 1; i < column_num; i++) @@ -588,7 +597,7 @@ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */ udb_query_prepare_result (q, prep_area, (db->host ? db->host : hostname_g), /* plugin = */ "dbi", db->name, - column_names, column_num, /* interval = */ 0); + column_names, column_num, /* interval = */ (db->interval > 0) ? db->interval : 0); /* 0 = error; 1 = success; */ status = dbi_result_first_row (res); /* {{{ */ @@ -679,26 +688,16 @@ static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */ db->connection = NULL; } -#ifdef HAVE_LIBDBI_R - driver = dbi_driver_open_r (db->driver, inst); -#else - driver = dbi_driver_open (db->driver); -#endif + 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:"); -#ifdef HAVE_LIBDBI_R - for (driver = dbi_driver_list_r (NULL, inst); + for (driver = dbi_driver_list_r (NULL, dbi_instance); driver != NULL; - driver = dbi_driver_list_r (driver, inst)) -#else - for (driver = dbi_driver_list (NULL); - driver != NULL; - driver = dbi_driver_list (driver)) -#endif + driver = dbi_driver_list_r (driver, dbi_instance)) { INFO ("dbi plugin: * %s", dbi_driver_get_name (driver)); }