X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdbi.c;h=a7963ea223371f5d70a4ef63f2d0a3a6263db22a;hb=290741f2e6de9e9b467463c1f0c6f031c4036428;hp=29cf5bd52c7783beca315b9e9bd2a0dd649b02d5;hpb=82e27678f8972d2625b466e7e7ba9daaa6395c98;p=collectd.git diff --git a/src/dbi.c b/src/dbi.c index 29cf5bd5..273cce8e 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -2,18 +2,23 @@ * collectd - src/dbi.c * 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 - * Free Software Foundation; only version 2 of the License is applicable. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * * Authors: * Florian octo Forster @@ -59,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; @@ -83,6 +90,8 @@ static size_t queries_num = 0; static cdbi_database_t **databases = NULL; static size_t databases_num = 0; +static int cdbi_read_database (user_data_t *ud); + /* * Functions */ @@ -139,7 +148,7 @@ static int cdbi_result_get_field (dbi_result res, /* {{{ */ else if (src_type == DBI_TYPE_STRING) { const char *value; - + value = dbi_result_get_string_idx (res, index); if (value == NULL) sstrncpy (buffer, "", buffer_size); @@ -205,9 +214,10 @@ static void cdbi_database_free (cdbi_database_t *db) /* {{{ */ * * ... * - * + * * * Driver "mysql" + * Interval 120 * DriverOption "hostname" "localhost" * ... * Query "plugin_instance0" @@ -230,7 +240,7 @@ static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ * return (-1); } - option = (cdbi_driver_option_t *) realloc (db->driver_options, + option = realloc (db->driver_options, sizeof (*option) * (db->driver_options_num + 1)); if (option == NULL) { @@ -284,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) @@ -315,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); @@ -344,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; @@ -375,7 +386,7 @@ static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */ { cdbi_database_t **temp; - temp = (cdbi_database_t **) realloc (databases, + temp = realloc (databases, sizeof (*databases) * (databases_num + 1)); if (temp == NULL) { @@ -384,9 +395,24 @@ static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */ } else { + user_data_t ud; + char *name = NULL; + databases = temp; databases[databases_num] = db; databases_num++; + + memset (&ud, 0, sizeof (ud)); + ud.data = (void *) db; + ud.free_func = NULL; + name = ssnprintf_alloc("dbi:%s", db->name); + + plugin_register_complex_read (/* group = */ NULL, + /* name = */ name ? name : db->name, + /* callback = */ cdbi_read_database, + /* interval = */ (db->interval > 0) ? db->interval : 0, + /* user_data = */ &ud); + free (name); } } @@ -484,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); @@ -520,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++) @@ -574,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); /* {{{ */ @@ -773,8 +796,9 @@ static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */ return (0); } /* }}} int cdbi_connect_database */ -static int cdbi_read_database (cdbi_database_t *db) /* {{{ */ +static int cdbi_read_database (user_data_t *ud) /* {{{ */ { + cdbi_database_t *db = (cdbi_database_t *) ud->data; size_t i; int success; int status; @@ -813,32 +837,6 @@ static int cdbi_read_database (cdbi_database_t *db) /* {{{ */ return (0); } /* }}} int cdbi_read_database */ -static int cdbi_read (void) /* {{{ */ -{ - size_t i; - 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]); - if (status == 0) - success++; - } - - if (success == 0) - { - ERROR ("dbi plugin: No database could be read. Will return an error so " - "the plugin will be delayed."); - return (-1); - } - - return (0); -} /* }}} int cdbi_read */ - static int cdbi_shutdown (void) /* {{{ */ { size_t i; @@ -866,7 +864,6 @@ void module_register (void) /* {{{ */ { plugin_register_complex_config ("dbi", cdbi_config); plugin_register_init ("dbi", cdbi_init); - plugin_register_read ("dbi", cdbi_read); plugin_register_shutdown ("dbi", cdbi_shutdown); } /* }}} void module_register */