Merge pull request #2618 from ajssmith/amqp1_dev1_branch
[collectd.git] / src / mysql.c
index 8409d1b..40707be 100644 (file)
@@ -58,17 +58,17 @@ struct mysql_database_s /* {{{ */
   int port;
   int timeout;
 
-  _Bool master_stats;
-  _Bool slave_stats;
-  _Bool innodb_stats;
-  _Bool wsrep_stats;
+  bool master_stats;
+  bool slave_stats;
+  bool innodb_stats;
+  bool wsrep_stats;
 
-  _Bool slave_notif;
-  _Bool slave_io_running;
-  _Bool slave_sql_running;
+  bool slave_notif;
+  bool slave_io_running;
+  bool slave_sql_running;
 
   MYSQL *con;
-  _Bool is_connected;
+  bool is_connected;
 };
 typedef struct mysql_database_s mysql_database_t; /* }}} */
 
@@ -121,13 +121,13 @@ static int mysql_config_database(oconfig_item_t *ci) /* {{{ */
   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
     WARNING("mysql plugin: The `Database' block "
             "needs exactly one string argument.");
-    return (-1);
+    return -1;
   }
 
   db = calloc(1, sizeof(*db));
   if (db == NULL) {
     ERROR("mysql plugin: calloc failed.");
-    return (-1);
+    return -1;
   }
 
   /* initialize all the pointers */
@@ -147,13 +147,13 @@ static int mysql_config_database(oconfig_item_t *ci) /* {{{ */
   db->timeout = 0;
 
   /* trigger a notification, if it's not running */
-  db->slave_io_running = 1;
-  db->slave_sql_running = 1;
+  db->slave_io_running = true;
+  db->slave_sql_running = true;
 
   status = cf_util_get_string(ci, &db->instance);
   if (status != 0) {
     sfree(db);
-    return (status);
+    return status;
   }
   assert(db->instance != NULL);
 
@@ -218,7 +218,7 @@ static int mysql_config_database(oconfig_item_t *ci) /* {{{ */
           (db->database != NULL) ? db->database : "<default>");
 
     if (db->instance != NULL)
-      ssnprintf(cb_name, sizeof(cb_name), "mysql-%s", db->instance);
+      snprintf(cb_name, sizeof(cb_name), "mysql-%s", db->instance);
     else
       sstrncpy(cb_name, "mysql", sizeof(cb_name));
 
@@ -229,16 +229,16 @@ static int mysql_config_database(oconfig_item_t *ci) /* {{{ */
         });
   } else {
     mysql_database_free(db);
-    return (-1);
+    return -1;
   }
 
-  return (0);
+  return 0;
 } /* }}} int mysql_config_database */
 
 static int mysql_config(oconfig_item_t *ci) /* {{{ */
 {
   if (ci == NULL)
-    return (EINVAL);
+    return EINVAL;
 
   /* Fill the `mysql_database_t' structure.. */
   for (int i = 0; i < ci->children_num; i++) {
@@ -250,7 +250,7 @@ static int mysql_config(oconfig_item_t *ci) /* {{{ */
       WARNING("mysql plugin: Option \"%s\" not allowed here.", child->key);
   }
 
-  return (0);
+  return 0;
 } /* }}} int mysql_config */
 
 /* }}} End of configuration handling functions */
@@ -263,19 +263,23 @@ static MYSQL *getconnection(mysql_database_t *db) {
 
     status = mysql_ping(db->con);
     if (status == 0)
-      return (db->con);
+      return db->con;
 
     WARNING("mysql plugin: Lost connection to instance \"%s\": %s",
             db->instance, mysql_error(db->con));
   }
-  db->is_connected = 0;
+  db->is_connected = false;
 
+  /* Close the old connection before initializing a new one. */
+  if (db->con != NULL) {
+    mysql_close(db->con);
+    db->con = NULL;
+  }
+
+  db->con = mysql_init(NULL);
   if (db->con == NULL) {
-    db->con = mysql_init(NULL);
-    if (db->con == NULL) {
-      ERROR("mysql plugin: mysql_init failed: %s", mysql_error(db->con));
-      return (NULL);
-    }
+    ERROR("mysql plugin: mysql_init failed: %s", mysql_error(db->con));
+    return NULL;
   }
 
   /* Configure TCP connect timeout (default: 0) */
@@ -289,7 +293,7 @@ static MYSQL *getconnection(mysql_database_t *db) {
           "at server %s: %s",
           (db->database != NULL) ? db->database : "<none>",
           (db->host != NULL) ? db->host : "localhost", mysql_error(db->con));
-    return (NULL);
+    return NULL;
   }
 
   cipher = mysql_get_ssl_cipher(db->con);
@@ -301,8 +305,8 @@ static MYSQL *getconnection(mysql_database_t *db) {
        mysql_get_host_info(db->con), (cipher != NULL) ? cipher : "<none>",
        mysql_get_server_info(db->con), mysql_get_proto_info(db->con));
 
-  db->is_connected = 1;
-  return (db->con);
+  db->is_connected = true;
+  return db->con;
 } /* static MYSQL *getconnection (mysql_database_t *db) */
 
 static void set_host(mysql_database_t *db, char *buf, size_t buflen) {
@@ -364,17 +368,17 @@ static MYSQL_RES *exec_query(MYSQL *con, const char *query) {
   if (mysql_real_query(con, query, query_len)) {
     ERROR("mysql plugin: Failed to execute query: %s", mysql_error(con));
     INFO("mysql plugin: SQL query was: %s", query);
-    return (NULL);
+    return NULL;
   }
 
   res = mysql_store_result(con);
   if (res == NULL) {
     ERROR("mysql plugin: Failed to store query result: %s", mysql_error(con));
     INFO("mysql plugin: SQL query was: %s", query);
-    return (NULL);
+    return NULL;
   }
 
-  return (res);
+  return res;
 } /* exec_query */
 
 static int mysql_read_master_stats(mysql_database_t *db, MYSQL *con) {
@@ -389,7 +393,7 @@ static int mysql_read_master_stats(mysql_database_t *db, MYSQL *con) {
 
   res = exec_query(con, query);
   if (res == NULL)
-    return (-1);
+    return -1;
 
   row = mysql_fetch_row(res);
   if (row == NULL) {
@@ -397,7 +401,7 @@ static int mysql_read_master_stats(mysql_database_t *db, MYSQL *con) {
           "`%s' did not return any rows.",
           query);
     mysql_free_result(res);
-    return (-1);
+    return -1;
   }
 
   field_num = mysql_num_fields(res);
@@ -406,7 +410,7 @@ static int mysql_read_master_stats(mysql_database_t *db, MYSQL *con) {
           "`%s' returned less than two columns.",
           query);
     mysql_free_result(res);
-    return (-1);
+    return -1;
   }
 
   position = atoll(row[1]);
@@ -420,7 +424,7 @@ static int mysql_read_master_stats(mysql_database_t *db, MYSQL *con) {
 
   mysql_free_result(res);
 
-  return (0);
+  return 0;
 } /* mysql_read_master_stats */
 
 static int mysql_read_slave_stats(mysql_database_t *db, MYSQL *con) {
@@ -442,7 +446,7 @@ static int mysql_read_slave_stats(mysql_database_t *db, MYSQL *con) {
 
   res = exec_query(con, query);
   if (res == NULL)
-    return (-1);
+    return -1;
 
   row = mysql_fetch_row(res);
   if (row == NULL) {
@@ -450,7 +454,7 @@ static int mysql_read_slave_stats(mysql_database_t *db, MYSQL *con) {
           "`%s' did not return any rows.",
           query);
     mysql_free_result(res);
-    return (-1);
+    return -1;
   }
 
   field_num = mysql_num_fields(res);
@@ -459,7 +463,7 @@ static int mysql_read_slave_stats(mysql_database_t *db, MYSQL *con) {
           "`%s' returned less than 33 columns.",
           query);
     mysql_free_result(res);
-    return (-1);
+    return -1;
   }
 
   if (db->slave_stats) {
@@ -496,31 +500,31 @@ static int mysql_read_slave_stats(mysql_database_t *db, MYSQL *con) {
     if (((io == NULL) || (strcasecmp(io, "yes") != 0)) &&
         (db->slave_io_running)) {
       n.severity = NOTIF_WARNING;
-      ssnprintf(n.message, sizeof(n.message),
-                "slave I/O thread not started or not connected to master");
+      snprintf(n.message, sizeof(n.message),
+               "slave I/O thread not started or not connected to master");
       plugin_dispatch_notification(&n);
-      db->slave_io_running = 0;
+      db->slave_io_running = false;
     } else if (((io != NULL) && (strcasecmp(io, "yes") == 0)) &&
                (!db->slave_io_running)) {
       n.severity = NOTIF_OKAY;
-      ssnprintf(n.message, sizeof(n.message),
-                "slave I/O thread started and connected to master");
+      snprintf(n.message, sizeof(n.message),
+               "slave I/O thread started and connected to master");
       plugin_dispatch_notification(&n);
-      db->slave_io_running = 1;
+      db->slave_io_running = true;
     }
 
     if (((sql == NULL) || (strcasecmp(sql, "yes") != 0)) &&
         (db->slave_sql_running)) {
       n.severity = NOTIF_WARNING;
-      ssnprintf(n.message, sizeof(n.message), "slave SQL thread not started");
+      snprintf(n.message, sizeof(n.message), "slave SQL thread not started");
       plugin_dispatch_notification(&n);
-      db->slave_sql_running = 0;
+      db->slave_sql_running = false;
     } else if (((sql != NULL) && (strcasecmp(sql, "yes") == 0)) &&
                (!db->slave_sql_running)) {
       n.severity = NOTIF_OKAY;
-      ssnprintf(n.message, sizeof(n.message), "slave SQL thread started");
+      snprintf(n.message, sizeof(n.message), "slave SQL thread started");
       plugin_dispatch_notification(&n);
-      db->slave_sql_running = 1;
+      db->slave_sql_running = true;
     }
   }
 
@@ -532,7 +536,7 @@ static int mysql_read_slave_stats(mysql_database_t *db, MYSQL *con) {
 
   mysql_free_result(res);
 
-  return (0);
+  return 0;
 } /* mysql_read_slave_stats */
 
 static int mysql_read_innodb_stats(mysql_database_t *db, MYSQL *con) {
@@ -591,7 +595,7 @@ static int mysql_read_innodb_stats(mysql_database_t *db, MYSQL *con) {
 
   res = exec_query(con, query);
   if (res == NULL)
-    return (-1);
+    return -1;
 
   while ((row = mysql_fetch_row(res))) {
     int i;
@@ -621,7 +625,7 @@ static int mysql_read_innodb_stats(mysql_database_t *db, MYSQL *con) {
   }
 
   mysql_free_result(res);
-  return (0);
+  return 0;
 }
 
 static int mysql_read_wsrep_stats(mysql_database_t *db, MYSQL *con) {
@@ -670,7 +674,7 @@ static int mysql_read_wsrep_stats(mysql_database_t *db, MYSQL *con) {
 
   res = exec_query(con, query);
   if (res == NULL)
-    return (-1);
+    return -1;
 
   row = mysql_fetch_row(res);
   if (row == NULL) {
@@ -678,7 +682,7 @@ static int mysql_read_wsrep_stats(mysql_database_t *db, MYSQL *con) {
           "`%s' did not return any rows.",
           query);
     mysql_free_result(res);
-    return (-1);
+    return -1;
   }
 
   while ((row = mysql_fetch_row(res))) {
@@ -706,7 +710,7 @@ static int mysql_read_wsrep_stats(mysql_database_t *db, MYSQL *con) {
   }
 
   mysql_free_result(res);
-  return (0);
+  return 0;
 } /* mysql_read_wsrep_stats */
 
 static int mysql_read(user_data_t *ud) {
@@ -733,14 +737,14 @@ static int mysql_read(user_data_t *ud) {
 
   if ((ud == NULL) || (ud->data == NULL)) {
     ERROR("mysql plugin: mysql_database_read: Invalid user data.");
-    return (-1);
+    return -1;
   }
 
   db = (mysql_database_t *)ud->data;
 
   /* An error message will have been printed in this case */
   if ((con = getconnection(db)) == NULL)
-    return (-1);
+    return -1;
 
   mysql_version = mysql_get_server_version(con);
 
@@ -750,7 +754,7 @@ static int mysql_read(user_data_t *ud) {
 
   res = exec_query(con, query);
   if (res == NULL)
-    return (-1);
+    return -1;
 
   while ((row = mysql_fetch_row(res))) {
     char *key;
@@ -938,7 +942,7 @@ static int mysql_read(user_data_t *ud) {
   if (db->wsrep_stats)
     mysql_read_wsrep_stats(db, con);
 
-  return (0);
+  return 0;
 } /* int mysql_read */
 
 void module_register(void) {