X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdbi.c;h=779c723f91741de5f5db273993a4929a1bcb2a78;hb=72ec1d0ed6cde4aa78f17d031e7a48e37cbf7a57;hp=ce4cd02f551a5cc1f47aca54d1a2c5425764dd11;hpb=37905c5a2370c8e6edae5011978773eeaa589d09;p=collectd.git diff --git a/src/dbi.c b/src/dbi.c index ce4cd02f..779c723f 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -1,6 +1,6 @@ /** * collectd - src/dbi.c - * Copyright (C) 2008,2009 Florian octo Forster + * Copyright (C) 2008-2013 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 @@ -16,7 +16,7 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: - * Florian octo Forster + * Florian octo Forster **/ #include "collectd.h" @@ -33,7 +33,12 @@ struct cdbi_driver_option_s /* {{{ */ { char *key; - char *value; + union + { + char *string; + int numeric; + } value; + _Bool is_numeric; }; typedef struct cdbi_driver_option_s cdbi_driver_option_t; /* }}} */ @@ -43,9 +48,11 @@ struct cdbi_database_s /* {{{ */ char *select_db; char *driver; + char *host; cdbi_driver_option_t *driver_options; size_t driver_options_num; + udb_query_preparation_area_t **q_prep_areas; udb_query_t **queries; size_t queries_num; @@ -61,6 +68,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 */ @@ -158,10 +167,16 @@ static void cdbi_database_free (cdbi_database_t *db) /* {{{ */ for (i = 0; i < db->driver_options_num; i++) { sfree (db->driver_options[i].key); - sfree (db->driver_options[i].value); + if (!db->driver_options[i].is_numeric) + sfree (db->driver_options[i].value.string); } sfree (db->driver_options); + if (db->q_prep_areas) + for (i = 0; i < db->queries_num; ++i) + udb_query_delete_preparation_area (db->q_prep_areas[i]); + free (db->q_prep_areas); + sfree (db); } /* }}} void cdbi_database_free */ @@ -187,33 +202,6 @@ static void cdbi_database_free (cdbi_database_t *db) /* {{{ */ * */ -static int cdbi_config_set_string (char **ret_string, /* {{{ */ - oconfig_item_t *ci) -{ - char *string; - - if ((ci->values_num != 1) - || (ci->values[0].type != OCONFIG_TYPE_STRING)) - { - WARNING ("dbi plugin: The `%s' config option " - "needs exactly one string argument.", ci->key); - return (-1); - } - - string = strdup (ci->values[0].value.string); - if (string == NULL) - { - ERROR ("dbi plugin: strdup failed."); - return (-1); - } - - if (*ret_string != NULL) - free (*ret_string); - *ret_string = string; - - return (0); -} /* }}} int cdbi_config_set_string */ - static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ */ oconfig_item_t *ci) { @@ -221,10 +209,11 @@ static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ * if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) - || (ci->values[1].type != OCONFIG_TYPE_STRING)) + || ((ci->values[1].type != OCONFIG_TYPE_STRING) + && (ci->values[1].type != OCONFIG_TYPE_NUMBER))) { WARNING ("dbi plugin: The `DriverOption' config option " - "needs exactly two string arguments."); + "needs exactly two arguments."); return (-1); } @@ -238,6 +227,7 @@ static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ * db->driver_options = option; option = db->driver_options + db->driver_options_num; + memset (option, 0, sizeof (*option)); option->key = strdup (ci->values[0].value.string); if (option->key == NULL) @@ -246,12 +236,21 @@ static int cdbi_config_add_database_driver_option (cdbi_database_t *db, /* {{{ * return (-1); } - option->value = strdup (ci->values[1].value.string); - if (option->value == NULL) + if (ci->values[1].type == OCONFIG_TYPE_STRING) { - ERROR ("dbi plugin: strdup failed."); - sfree (option->key); - return (-1); + option->value.string = strdup (ci->values[1].value.string); + if (option->value.string == NULL) + { + ERROR ("dbi plugin: strdup failed."); + sfree (option->key); + return (-1); + } + } + else + { + assert (ci->values[1].type == OCONFIG_TYPE_NUMBER); + option->value.numeric = (int) (ci->values[1].value.number + .5); + option->is_numeric = 1; } db->driver_options_num++; @@ -280,7 +279,7 @@ static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */ } memset (db, 0, sizeof (*db)); - status = cdbi_config_set_string (&db->name, ci); + status = cf_util_get_string (ci, &db->name); if (status != 0) { sfree (db); @@ -293,14 +292,16 @@ static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */ oconfig_item_t *child = ci->children + i; if (strcasecmp ("Driver", child->key) == 0) - status = cdbi_config_set_string (&db->driver, child); + status = cf_util_get_string (child, &db->driver); else if (strcasecmp ("DriverOption", child->key) == 0) status = cdbi_config_add_database_driver_option (db, child); else if (strcasecmp ("SelectDB", child->key) == 0) - status = cdbi_config_set_string (&db->select_db, child); + status = cf_util_get_string (child, &db->select_db); 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); @@ -328,6 +329,34 @@ static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */ break; } /* while (status == 0) */ + 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)); + + if (db->q_prep_areas == NULL) + { + WARNING ("dbi plugin: malloc failed"); + status = -1; + break; + } + + for (i = 0; i < db->queries_num; ++i) + { + db->q_prep_areas[i] + = udb_query_allocate_preparation_area (db->queries[i]); + + if (db->q_prep_areas[i] == NULL) + { + WARNING ("dbi plugin: udb_query_allocate_preparation_area failed"); + status = -1; + break; + } + } + + break; + } + /* If all went well, add this database to the global list of databases. */ if (status == 0) { @@ -342,9 +371,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 = */ NULL, + /* user_data = */ &ud); + free (name); } } @@ -366,12 +410,12 @@ static int cdbi_config (oconfig_item_t *ci) /* {{{ */ oconfig_item_t *child = ci->children + i; if (strcasecmp ("Query", child->key) == 0) udb_query_create (&queries, &queries_num, child, - /* callback = */ NULL, /* legacy mode = */ 0); + /* callback = */ NULL); else if (strcasecmp ("Database", child->key) == 0) 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) */ @@ -422,7 +466,7 @@ static int cdbi_init (void) /* {{{ */ } /* }}} int cdbi_init */ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */ - udb_query_t *q) + udb_query_t *q, udb_query_preparation_area_t *prep_area) { const char *statement; dbi_result res; @@ -530,8 +574,9 @@ 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, hostname_g, /* plugin = */ "dbi", db->name, - column_names, column_num); + udb_query_prepare_result (q, prep_area, (db->host ? db->host : hostname_g), + /* plugin = */ "dbi", db->name, + column_names, column_num, /* interval = */ 0); /* 0 = error; 1 = success; */ status = dbi_result_first_row (res); /* {{{ */ @@ -543,7 +588,7 @@ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */ "return any rows?", db->name, udb_query_get_name (q), cdbi_strerror (db->connection, errbuf, sizeof (errbuf))); - udb_query_finish_result (q); + udb_query_finish_result (q, prep_area); BAIL_OUT (-1); } /* }}} */ @@ -572,7 +617,7 @@ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */ * to dispatch the row to the daemon. */ if (status == 0) /* {{{ */ { - status = udb_query_handle_result (q, column_values); + status = udb_query_handle_result (q, prep_area, column_values); if (status != 0) { ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): " @@ -598,7 +643,7 @@ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */ } /* }}} while (42) */ /* Tell the db query interface that we're done with this query. */ - udb_query_finish_result (q); + udb_query_finish_result (q, prep_area); /* Clean up and return `status = 0' (success) */ BAIL_OUT (0); @@ -653,24 +698,38 @@ static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */ * trouble finding out how to configure the plugin correctly.. */ for (i = 0; i < db->driver_options_num; i++) { - DEBUG ("dbi plugin: cdbi_connect_database (%s): " - "key = %s; value = %s;", - db->name, - db->driver_options[i].key, - db->driver_options[i].value); + if (db->driver_options[i].is_numeric) + { + status = dbi_conn_set_option_numeric (connection, + db->driver_options[i].key, db->driver_options[i].value.numeric); + if (status != 0) + { + char errbuf[1024]; + ERROR ("dbi plugin: cdbi_connect_database (%s): " + "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.", + db->name, + db->driver_options[i].key, db->driver_options[i].value.numeric, + cdbi_strerror (connection, errbuf, sizeof (errbuf))); + } + } + else + { + status = dbi_conn_set_option (connection, + db->driver_options[i].key, db->driver_options[i].value.string); + if (status != 0) + { + char errbuf[1024]; + ERROR ("dbi plugin: cdbi_connect_database (%s): " + "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.", + db->name, + db->driver_options[i].key, db->driver_options[i].value.string, + cdbi_strerror (connection, errbuf, sizeof (errbuf))); + } + } - status = dbi_conn_set_option (connection, - db->driver_options[i].key, db->driver_options[i].value); if (status != 0) { - char errbuf[1024]; - const char *opt; - - ERROR ("dbi plugin: cdbi_connect_database (%s): " - "dbi_conn_set_option (%s, %s) failed: %s.", - db->name, - db->driver_options[i].key, db->driver_options[i].value, - cdbi_strerror (connection, errbuf, sizeof (errbuf))); + char const *opt; INFO ("dbi plugin: This is a list of all options understood " "by the `%s' driver:", db->driver); @@ -716,8 +775,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; @@ -741,7 +801,8 @@ static int cdbi_read_database (cdbi_database_t *db) /* {{{ */ && (udb_query_check_version (db->queries[i], db_version) == 0)) continue; - status = cdbi_read_database_query (db, db->queries[i]); + status = cdbi_read_database_query (db, + db->queries[i], db->q_prep_areas[i]); if (status == 0) success++; } @@ -755,29 +816,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; - - 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; @@ -805,7 +843,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 */