+static MYSQL_RES *exec_query (MYSQL *con, const char *query)
+{
+ MYSQL_RES *res;
+
+ int query_len = strlen (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);
+ }
+
+ 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 (res);
+} /* exec_query */
+
+static int mysql_read_master_stats (mysql_database_t *db, MYSQL *con)
+{
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+
+ char *query;
+ int field_num;
+ unsigned long long position;
+
+ query = "SHOW MASTER STATUS";
+
+ res = exec_query (con, query);
+ if (res == NULL)
+ return (-1);
+
+ row = mysql_fetch_row (res);
+ if (row == NULL)
+ {
+ ERROR ("mysql plugin: Failed to get master statistics: "
+ "`%s' did not return any rows.", query);
+ return (-1);
+ }
+
+ field_num = mysql_num_fields (res);
+ if (field_num < 2)
+ {
+ ERROR ("mysql plugin: Failed to get master statistics: "
+ "`%s' returned less than two columns.", query);
+ return (-1);
+ }
+
+ position = atoll (row[1]);
+ counter_submit ("mysql_log_position", "master-bin", position, db);
+
+ row = mysql_fetch_row (res);
+ if (row != NULL)
+ WARNING ("mysql plugin: `%s' returned more than one row - "
+ "ignoring further results.", query);
+
+ mysql_free_result (res);
+
+ return (0);
+} /* mysql_read_master_stats */
+
+static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con)
+{
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+
+ char *query;
+ int field_num;
+
+ /* WTF? libmysqlclient does not seem to provide any means to
+ * translate a column name to a column index ... :-/ */
+ const int READ_MASTER_LOG_POS_IDX = 6;
+ const int EXEC_MASTER_LOG_POS_IDX = 21;
+ const int SECONDS_BEHIND_MASTER_IDX = 32;
+
+ unsigned long long counter;
+ double gauge;
+
+ query = "SHOW SLAVE STATUS";
+
+ res = exec_query (con, query);
+ if (res == NULL)
+ return (-1);
+
+ row = mysql_fetch_row (res);
+ if (row == NULL)
+ {
+ ERROR ("mysql plugin: Failed to get slave statistics: "
+ "`%s' did not return any rows.", query);
+ return (-1);
+ }
+
+ field_num = mysql_num_fields (res);
+ if (field_num < 33)
+ {
+ ERROR ("mysql plugin: Failed to get slave statistics: "
+ "`%s' returned less than 33 columns.", query);
+ return (-1);
+ }
+
+ counter = atoll (row[READ_MASTER_LOG_POS_IDX]);
+ counter_submit ("mysql_log_position", "slave-read", counter, db);
+
+ counter = atoll (row[EXEC_MASTER_LOG_POS_IDX]);
+ counter_submit ("mysql_log_position", "slave-exec", counter, db);
+
+ if (row[SECONDS_BEHIND_MASTER_IDX] != NULL)
+ {
+ gauge = atof (row[SECONDS_BEHIND_MASTER_IDX]);
+ gauge_submit ("time_offset", NULL, gauge, db);
+ }
+
+ row = mysql_fetch_row (res);
+ if (row != NULL)
+ WARNING ("mysql plugin: `%s' returned more than one row - "
+ "ignoring further results.", query);
+
+ mysql_free_result (res);
+
+ return (0);
+} /* mysql_read_slave_stats */
+