X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fmysql.c;h=d2d0b4bfc57a9ad695b592835cadd4707935fac0;hb=9cbc0fe63abd2bd3658b42f19ee144f803b658fa;hp=48ad528b0f279b80f51df44cb64578d93ec0d7ea;hpb=b53110ef99e3d6a5e2f8e79b8f068cfe96a1accb;p=collectd.git diff --git a/src/mysql.c b/src/mysql.c index 48ad528b..d2d0b4bf 100644 --- a/src/mysql.c +++ b/src/mysql.c @@ -1,6 +1,6 @@ /** * collectd - src/mysql.c - * Copyright (C) 2006-2009 Florian octo Forster + * Copyright (C) 2006-2010 Florian octo Forster * Copyright (C) 2008 Mirko Buffoni * Copyright (C) 2009 Doug MacEachern * Copyright (C) 2009 Sebastian tokkee Harl @@ -20,7 +20,7 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: - * Florian octo Forster + * Florian octo Forster * Mirko Buffoni * Doug MacEachern * Sebastian tokkee Harl @@ -42,7 +42,6 @@ struct mysql_database_s /* {{{ */ { - /* instance == NULL => legacy mode */ char *instance; char *host; char *user; @@ -59,7 +58,7 @@ struct mysql_database_s /* {{{ */ _Bool slave_sql_running; MYSQL *con; - int state; + _Bool is_connected; }; typedef struct mysql_database_s mysql_database_t; /* }}} */ @@ -193,9 +192,9 @@ static int mysql_config_database (oconfig_item_t *ci) /* {{{ */ ud.data = (void *) db; ud.free_func = mysql_database_free; - if (db->database != NULL) + if (db->instance != NULL) ssnprintf (cb_name, sizeof (cb_name), "mysql-%s", - db->database); + db->instance); else sstrncpy (cb_name, "mysql", sizeof (cb_name)); @@ -238,30 +237,28 @@ static int mysql_config (oconfig_item_t *ci) /* {{{ */ static MYSQL *getconnection (mysql_database_t *db) { - if (db->state != 0) + if (db->is_connected) { - int err; - if ((err = mysql_ping (db->con)) != 0) - { - WARNING ("mysql_ping failed for %s: %s", - (db->instance != NULL) - ? db->instance - : "", - mysql_error (db->con)); - db->state = 0; - } - else - { - db->state = 1; + int status; + + status = mysql_ping (db->con); + if (status == 0) return (db->con); - } + + WARNING ("mysql plugin: Lost connection to instance \"%s\": %s", + db->instance, mysql_error (db->con)); } + db->is_connected = 0; - if ((db->con = mysql_init (db->con)) == NULL) + if (db->con == NULL) { - ERROR ("mysql_init failed: %s", mysql_error (db->con)); - db->state = 0; - return (NULL); + db->con = mysql_init (NULL); + if (db->con == NULL) + { + ERROR ("mysql plugin: mysql_init failed: %s", + mysql_error (db->con)); + return (NULL); + } } if (mysql_real_connect (db->con, db->host, db->user, db->pass, @@ -272,47 +269,29 @@ static MYSQL *getconnection (mysql_database_t *db) (db->database != NULL) ? db->database : "", (db->host != NULL) ? db->host : "localhost", mysql_error (db->con)); - db->state = 0; return (NULL); } - else - { - INFO ("mysql plugin: Successfully connected to database %s " - "at server %s (server version: %s, protocol version: %d)", - (db->database != NULL) ? db->database : "", - mysql_get_host_info (db->con), - mysql_get_server_info (db->con), - mysql_get_proto_info (db->con)); - db->state = 1; - return (db->con); - } + + INFO ("mysql plugin: Successfully connected to database %s " + "at server %s (server version: %s, protocol version: %d)", + (db->database != NULL) ? db->database : "", + mysql_get_host_info (db->con), + mysql_get_server_info (db->con), + mysql_get_proto_info (db->con)); + + db->is_connected = 1; + return (db->con); } /* static MYSQL *getconnection (mysql_database_t *db) */ static void set_host (mysql_database_t *db, char *buf, size_t buflen) { - /* XXX legacy mode - use hostname_g */ - if (db->instance == NULL) + if ((db->host == NULL) + || (strcmp ("", db->host) == 0) + || (strcmp ("localhost", db->host) == 0)) sstrncpy (buf, hostname_g, buflen); else - { - if ((db->host == NULL) - || (strcmp ("", db->host) == 0) - || (strcmp ("localhost", db->host) == 0)) - sstrncpy (buf, hostname_g, buflen); - else - sstrncpy (buf, db->host, buflen); - } -} - -static void set_plugin_instance (mysql_database_t *db, - char *buf, size_t buflen) -{ - /* XXX legacy mode - no plugin_instance */ - if (db->instance == NULL) - sstrncpy (buf, "", buflen); - else - sstrncpy (buf, db->instance, buflen); -} + sstrncpy (buf, db->host, buflen); +} /* void set_host */ static void submit (const char *type, const char *type_instance, value_t *values, size_t values_len, mysql_database_t *db) @@ -325,7 +304,10 @@ static void submit (const char *type, const char *type_instance, set_host (db, vl.host, sizeof (vl.host)); sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin)); - set_plugin_instance (db, vl.plugin_instance, sizeof (vl.plugin_instance)); + + /* Assured by "mysql_config_database" */ + assert (db->instance != NULL); + sstrncpy (vl.plugin_instance, db->instance, sizeof (vl.plugin_instance)); sstrncpy (vl.type, type, sizeof (vl.type)); if (type_instance != NULL) @@ -335,11 +317,11 @@ static void submit (const char *type, const char *type_instance, } /* submit */ static void counter_submit (const char *type, const char *type_instance, - counter_t value, mysql_database_t *db) + derive_t value, mysql_database_t *db) { value_t values[1]; - values[0].counter = value; + values[0].derive = value; submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db); } /* void counter_submit */ @@ -361,12 +343,12 @@ static void derive_submit (const char *type, const char *type_instance, submit (type, type_instance, values, STATIC_ARRAY_SIZE (values), db); } /* void derive_submit */ -static void traffic_submit (counter_t rx, counter_t tx, mysql_database_t *db) +static void traffic_submit (derive_t rx, derive_t tx, mysql_database_t *db) { value_t values[2]; - values[0].counter = rx; - values[1].counter = tx; + values[0].derive = rx; + values[1].derive = tx; submit ("mysql_octets", NULL, values, STATIC_ARRAY_SIZE (values), db); } /* void traffic_submit */ @@ -417,6 +399,7 @@ static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con) { ERROR ("mysql plugin: Failed to get master statistics: " "`%s' did not return any rows.", query); + mysql_free_result (res); return (-1); } @@ -425,6 +408,7 @@ static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con) { ERROR ("mysql plugin: Failed to get master statistics: " "`%s' returned less than two columns.", query); + mysql_free_result (res); return (-1); } @@ -468,6 +452,7 @@ static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con) { ERROR ("mysql plugin: Failed to get slave statistics: " "`%s' did not return any rows.", query); + mysql_free_result (res); return (-1); } @@ -476,6 +461,7 @@ static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con) { ERROR ("mysql plugin: Failed to get slave statistics: " "`%s' returned less than 33 columns.", query); + mysql_free_result (res); return (-1); } @@ -499,7 +485,7 @@ static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con) if (db->slave_notif) { - notification_t n = { 0, time (NULL), "", "", + notification_t n = { 0, cdtime (), "", "", "mysql", "", "time_offset", "", NULL }; char *io, *sql; @@ -508,8 +494,10 @@ static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con) sql = row[SLAVE_SQL_RUNNING_IDX]; set_host (db, n.host, sizeof (n.host)); - set_plugin_instance (db, - n.plugin_instance, sizeof (n.plugin_instance)); + + /* Assured by "mysql_config_database" */ + assert (db->instance != NULL); + sstrncpy (n.plugin_instance, db->instance, sizeof (n.plugin_instance)); if (((io == NULL) || (strcasecmp (io, "yes") != 0)) && (db->slave_io_running)) @@ -567,7 +555,6 @@ static int mysql_read (user_data_t *ud) MYSQL_RES *res; MYSQL_ROW row; char *query; - int field_num; derive_t qcache_hits = 0; derive_t qcache_inserts = 0; @@ -603,7 +590,6 @@ static int mysql_read (user_data_t *ud) if (res == NULL) return (-1); - field_num = mysql_num_fields (res); while ((row = mysql_fetch_row (res))) { char *key;