summaryrefslogtreecommitdiffstats
path: root/plugin/userstat
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/userstat')
-rw-r--r--plugin/userstat/CMakeLists.txt4
-rw-r--r--plugin/userstat/client_stats.cc103
-rw-r--r--plugin/userstat/index_stats.cc75
-rw-r--r--plugin/userstat/table_stats.cc78
-rw-r--r--plugin/userstat/user_stats.cc60
-rw-r--r--plugin/userstat/userstat.cc82
6 files changed, 402 insertions, 0 deletions
diff --git a/plugin/userstat/CMakeLists.txt b/plugin/userstat/CMakeLists.txt
new file mode 100644
index 00000000..5daa4f59
--- /dev/null
+++ b/plugin/userstat/CMakeLists.txt
@@ -0,0 +1,4 @@
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/sql)
+
+MYSQL_ADD_PLUGIN(USERSTAT userstat.cc MANDATORY)
+
diff --git a/plugin/userstat/client_stats.cc b/plugin/userstat/client_stats.cc
new file mode 100644
index 00000000..72c71785
--- /dev/null
+++ b/plugin/userstat/client_stats.cc
@@ -0,0 +1,103 @@
+namespace Show {
+
+static ST_FIELD_INFO client_stats_fields[]=
+{
+ Column("CLIENT",Varchar(LIST_PROCESS_HOST_LEN), NOT_NULL, "Client"),
+ Column("TOTAL_CONNECTIONS", SLonglong(), NOT_NULL, "Total_connections"),
+ Column("CONCURRENT_CONNECTIONS", SLonglong(), NOT_NULL, "Concurrent_connections"),
+ Column("CONNECTED_TIME", SLonglong(), NOT_NULL, "Connected_time"),
+ Column("BUSY_TIME", Double(MY_INT64_NUM_DECIMAL_DIGITS), NOT_NULL, "Busy_time"),
+ Column("CPU_TIME", Double(MY_INT64_NUM_DECIMAL_DIGITS), NOT_NULL, "Cpu_time"),
+ Column("BYTES_RECEIVED", SLonglong(), NOT_NULL, "Bytes_received"),
+ Column("BYTES_SENT", SLonglong(), NOT_NULL, "Bytes_sent"),
+ Column("BINLOG_BYTES_WRITTEN", SLonglong(), NOT_NULL, "Binlog_bytes_written"),
+ Column("ROWS_READ", SLonglong(), NOT_NULL, "Rows_read"),
+ Column("ROWS_SENT", SLonglong(), NOT_NULL, "Rows_sent"),
+ Column("ROWS_DELETED", SLonglong(), NOT_NULL, "Rows_deleted"),
+ Column("ROWS_INSERTED", SLonglong(), NOT_NULL, "Rows_inserted"),
+ Column("ROWS_UPDATED", SLonglong(), NOT_NULL, "Rows_updated"),
+ Column("SELECT_COMMANDS", SLonglong(), NOT_NULL, "Select_commands"),
+ Column("UPDATE_COMMANDS", SLonglong(), NOT_NULL, "Update_commands"),
+ Column("OTHER_COMMANDS", SLonglong(), NOT_NULL, "Other_commands"),
+ Column("COMMIT_TRANSACTIONS", SLonglong(), NOT_NULL, "Commit_transactions"),
+ Column("ROLLBACK_TRANSACTIONS", SLonglong(), NOT_NULL, "Rollback_transactions"),
+ Column("DENIED_CONNECTIONS", SLonglong(), NOT_NULL, "Denied_connections"),
+ Column("LOST_CONNECTIONS", SLonglong(), NOT_NULL, "Lost_connections"),
+ Column("ACCESS_DENIED", SLonglong(), NOT_NULL, "Access_denied"),
+ Column("EMPTY_QUERIES", SLonglong(), NOT_NULL, "Empty_queries"),
+ Column("TOTAL_SSL_CONNECTIONS", ULonglong(), NOT_NULL, "Total_ssl_connections"),
+ Column("MAX_STATEMENT_TIME_EXCEEDED", SLonglong(), NOT_NULL, "Max_statement_time_exceeded"),
+ CEnd()
+};
+
+} // namespace Show
+
+static int send_user_stats(THD* thd, HASH *all_user_stats, TABLE *table)
+{
+ mysql_mutex_lock(&LOCK_global_user_client_stats);
+ for (uint i= 0; i < all_user_stats->records; i++)
+ {
+ uint j= 0;
+ USER_STATS *user_stats= (USER_STATS*) my_hash_element(all_user_stats, i);
+
+ table->field[j++]->store(user_stats->user, user_stats->user_name_length,
+ system_charset_info);
+ table->field[j++]->store((longlong)user_stats->total_connections,TRUE);
+ table->field[j++]->store((longlong)user_stats->concurrent_connections, TRUE);
+ table->field[j++]->store((longlong)user_stats->connected_time, TRUE);
+ table->field[j++]->store((double)user_stats->busy_time);
+ table->field[j++]->store((double)user_stats->cpu_time);
+ table->field[j++]->store((longlong)user_stats->bytes_received, TRUE);
+ table->field[j++]->store((longlong)user_stats->bytes_sent, TRUE);
+ table->field[j++]->store((longlong)user_stats->binlog_bytes_written, TRUE);
+ table->field[j++]->store((longlong)user_stats->rows_read, TRUE);
+ table->field[j++]->store((longlong)user_stats->rows_sent, TRUE);
+ table->field[j++]->store((longlong)user_stats->rows_deleted, TRUE);
+ table->field[j++]->store((longlong)user_stats->rows_inserted, TRUE);
+ table->field[j++]->store((longlong)user_stats->rows_updated, TRUE);
+ table->field[j++]->store((longlong)user_stats->select_commands, TRUE);
+ table->field[j++]->store((longlong)user_stats->update_commands, TRUE);
+ table->field[j++]->store((longlong)user_stats->other_commands, TRUE);
+ table->field[j++]->store((longlong)user_stats->commit_trans, TRUE);
+ table->field[j++]->store((longlong)user_stats->rollback_trans, TRUE);
+ table->field[j++]->store((longlong)user_stats->denied_connections, TRUE);
+ table->field[j++]->store((longlong)user_stats->lost_connections, TRUE);
+ table->field[j++]->store((longlong)user_stats->access_denied_errors, TRUE);
+ table->field[j++]->store((longlong)user_stats->empty_queries, TRUE);
+ table->field[j++]->store((longlong)user_stats->total_ssl_connections, TRUE);
+ table->field[j++]->store((longlong)user_stats->max_statement_time_exceeded, TRUE);
+ if (schema_table_store_record(thd, table))
+ {
+ mysql_mutex_unlock(&LOCK_global_user_client_stats);
+ return 1;
+ }
+ }
+ mysql_mutex_unlock(&LOCK_global_user_client_stats);
+ return 0;
+}
+
+static int client_stats_fill(THD* thd, TABLE_LIST* tables, COND* cond)
+{
+ if (check_global_access(thd, PROCESS_ACL, true))
+ return 0;
+
+ return send_user_stats(thd, &global_client_stats, tables->table);
+}
+
+static int client_stats_reset()
+{
+ mysql_mutex_lock(&LOCK_global_user_client_stats);
+ free_global_client_stats();
+ init_global_client_stats();
+ mysql_mutex_unlock(&LOCK_global_user_client_stats);
+ return 0;
+}
+
+static int client_stats_init(void *p)
+{
+ ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE *)p;
+ schema->fields_info= Show::client_stats_fields;
+ schema->fill_table= client_stats_fill;
+ schema->reset_table= client_stats_reset;
+ return 0;
+}
diff --git a/plugin/userstat/index_stats.cc b/plugin/userstat/index_stats.cc
new file mode 100644
index 00000000..97305e89
--- /dev/null
+++ b/plugin/userstat/index_stats.cc
@@ -0,0 +1,75 @@
+namespace Show {
+
+static ST_FIELD_INFO index_stats_fields[]=
+{
+ Column("TABLE_SCHEMA", Varchar(NAME_LEN), NOT_NULL, "Table_schema"),
+ Column("TABLE_NAME", Varchar(NAME_LEN), NOT_NULL, "Table_name"),
+ Column("INDEX_NAME", Varchar(NAME_LEN), NOT_NULL, "Index_name"),
+ Column("ROWS_READ", SLonglong(), NOT_NULL, "Rows_read"),
+ CEnd()
+};
+
+} // namespace Show
+
+static int index_stats_fill(THD *thd, TABLE_LIST *tables, COND *cond)
+{
+ TABLE *table= tables->table;
+
+ mysql_mutex_lock(&LOCK_global_index_stats);
+ for (uint i= 0; i < global_index_stats.records; i++)
+ {
+ INDEX_STATS *index_stats =
+ (INDEX_STATS*) my_hash_element(&global_index_stats, i);
+ TABLE_LIST tmp_table;
+ const char *index_name;
+ size_t index_name_length;
+
+ bzero((char*) &tmp_table,sizeof(tmp_table));
+ tmp_table.db.str= index_stats->index;
+ tmp_table.db.length= strlen(index_stats->index);
+ tmp_table.table_name.str= index_stats->index + tmp_table.db.length + 1;
+ tmp_table.table_name.length= strlen(tmp_table.table_name.str);
+ tmp_table.grant.privilege= NO_ACL;
+ if (check_access(thd, SELECT_ACL, tmp_table.db.str,
+ &tmp_table.grant.privilege, NULL, 0, 1) ||
+ check_grant(thd, SELECT_ACL, &tmp_table, 1, 1, 1))
+ continue;
+
+ index_name= tmp_table.table_name.str + tmp_table.table_name.length + 1;
+ index_name_length= (index_stats->index_name_length - tmp_table.db.length -
+ tmp_table.table_name.length - 3);
+
+ table->field[0]->store(tmp_table.db.str, tmp_table.db.length, system_charset_info);
+ table->field[1]->store(tmp_table.table_name.str, tmp_table.table_name.length,
+ system_charset_info);
+ table->field[2]->store(index_name, (uint) index_name_length, system_charset_info);
+ table->field[3]->store((longlong)index_stats->rows_read, TRUE);
+
+ if (schema_table_store_record(thd, table))
+ {
+ mysql_mutex_unlock(&LOCK_global_index_stats);
+ return 1;
+ }
+ }
+ mysql_mutex_unlock(&LOCK_global_index_stats);
+ return 0;
+}
+
+static int index_stats_reset()
+{
+ mysql_mutex_lock(&LOCK_global_index_stats);
+ free_global_index_stats();
+ init_global_index_stats();
+ mysql_mutex_unlock(&LOCK_global_index_stats);
+ return 0;
+}
+
+static int index_stats_init(void *p)
+{
+ ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE *)p;
+ schema->fields_info= Show::index_stats_fields;
+ schema->fill_table= index_stats_fill;
+ schema->reset_table= index_stats_reset;
+ return 0;
+}
+
diff --git a/plugin/userstat/table_stats.cc b/plugin/userstat/table_stats.cc
new file mode 100644
index 00000000..0a656793
--- /dev/null
+++ b/plugin/userstat/table_stats.cc
@@ -0,0 +1,78 @@
+namespace Show {
+
+static ST_FIELD_INFO table_stats_fields[]=
+{
+ Column("TABLE_SCHEMA", Varchar(NAME_LEN), NOT_NULL, "Table_schema"),
+ Column("TABLE_NAME", Varchar(NAME_LEN), NOT_NULL, "Table_name"),
+ Column("ROWS_READ", SLonglong(), NOT_NULL, "Rows_read"),
+ Column("ROWS_CHANGED", SLonglong(), NOT_NULL, "Rows_changed"),
+ Column("ROWS_CHANGED_X_INDEXES",SLonglong(), NOT_NULL, "Rows_changed_x_#indexes"),
+ CEnd()
+};
+
+} // namespace Show
+
+static int table_stats_fill(THD *thd, TABLE_LIST *tables, COND *cond)
+{
+ TABLE *table= tables->table;
+
+ mysql_mutex_lock(&LOCK_global_table_stats);
+ for (uint i= 0; i < global_table_stats.records; i++)
+ {
+ char *end_of_schema;
+ TABLE_STATS *table_stats=
+ (TABLE_STATS*)my_hash_element(&global_table_stats, i);
+ TABLE_LIST tmp_table;
+ size_t schema_length, table_name_length;
+
+ end_of_schema= strend(table_stats->table);
+ schema_length= (size_t) (end_of_schema - table_stats->table);
+ table_name_length= strlen(table_stats->table + schema_length + 1);
+
+ bzero((char*) &tmp_table,sizeof(tmp_table));
+ tmp_table.db.str= table_stats->table;
+ tmp_table.db.length= schema_length;
+ tmp_table.table_name.str= end_of_schema+1;
+ tmp_table.table_name.length= table_name_length;
+ tmp_table.grant.privilege= NO_ACL;
+ if (check_access(thd, SELECT_ACL, tmp_table.db.str,
+ &tmp_table.grant.privilege, NULL, 0, 1) ||
+ check_grant(thd, SELECT_ACL, &tmp_table, 1, 1, 1))
+ continue;
+
+ table->field[0]->store(table_stats->table, schema_length,
+ system_charset_info);
+ table->field[1]->store(table_stats->table + schema_length+1,
+ table_name_length, system_charset_info);
+ table->field[2]->store((longlong)table_stats->rows_read, TRUE);
+ table->field[3]->store((longlong)table_stats->rows_changed, TRUE);
+ table->field[4]->store((longlong)table_stats->rows_changed_x_indexes,
+ TRUE);
+ if (schema_table_store_record(thd, table))
+ {
+ mysql_mutex_unlock(&LOCK_global_table_stats);
+ return 1;
+ }
+ }
+ mysql_mutex_unlock(&LOCK_global_table_stats);
+ return 0;
+}
+
+static int table_stats_reset()
+{
+ mysql_mutex_lock(&LOCK_global_table_stats);
+ free_global_table_stats();
+ init_global_table_stats();
+ mysql_mutex_unlock(&LOCK_global_table_stats);
+ return 0;
+}
+
+static int table_stats_init(void *p)
+{
+ ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE *)p;
+ schema->fields_info= Show::table_stats_fields;
+ schema->fill_table= table_stats_fill;
+ schema->reset_table= table_stats_reset;
+ return 0;
+}
+
diff --git a/plugin/userstat/user_stats.cc b/plugin/userstat/user_stats.cc
new file mode 100644
index 00000000..de3d4e12
--- /dev/null
+++ b/plugin/userstat/user_stats.cc
@@ -0,0 +1,60 @@
+namespace Show {
+
+static ST_FIELD_INFO user_stats_fields[]=
+{
+ Column("USER",Varchar(USERNAME_CHAR_LENGTH),NOT_NULL, "User"),
+ Column("TOTAL_CONNECTIONS", SLong(), NOT_NULL, "Total_connections"),
+ Column("CONCURRENT_CONNECTIONS",SLong(), NOT_NULL, "Concurrent_connections"),
+ Column("CONNECTED_TIME", SLong(), NOT_NULL, "Connected_time"),
+ Column("BUSY_TIME", Double(MY_INT64_NUM_DECIMAL_DIGITS), NOT_NULL, "Busy_time"),
+ Column("CPU_TIME", Double(MY_INT64_NUM_DECIMAL_DIGITS), NOT_NULL, "Cpu_time"),
+ Column("BYTES_RECEIVED", SLonglong(), NOT_NULL, "Bytes_received"),
+ Column("BYTES_SENT", SLonglong(), NOT_NULL, "Bytes_sent"),
+ Column("BINLOG_BYTES_WRITTEN", SLonglong(), NOT_NULL, "Binlog_bytes_written"),
+ Column("ROWS_READ", SLonglong(), NOT_NULL, "Rows_read"),
+ Column("ROWS_SENT", SLonglong(), NOT_NULL, "Rows_sent"),
+ Column("ROWS_DELETED", SLonglong(), NOT_NULL, "Rows_deleted"),
+ Column("ROWS_INSERTED", SLonglong(), NOT_NULL, "Rows_inserted"),
+ Column("ROWS_UPDATED", SLonglong(), NOT_NULL, "Rows_updated"),
+ Column("SELECT_COMMANDS", SLonglong(), NOT_NULL, "Select_commands"),
+ Column("UPDATE_COMMANDS", SLonglong(), NOT_NULL, "Update_commands"),
+ Column("OTHER_COMMANDS", SLonglong(), NOT_NULL, "Other_commands"),
+ Column("COMMIT_TRANSACTIONS", SLonglong(), NOT_NULL, "Commit_transactions"),
+ Column("ROLLBACK_TRANSACTIONS",SLonglong(), NOT_NULL, "Rollback_transactions"),
+ Column("DENIED_CONNECTIONS", SLonglong(), NOT_NULL, "Denied_connections"),
+ Column("LOST_CONNECTIONS", SLonglong(), NOT_NULL, "Lost_connections"),
+ Column("ACCESS_DENIED", SLonglong(), NOT_NULL, "Access_denied"),
+ Column("EMPTY_QUERIES", SLonglong(), NOT_NULL, "Empty_queries"),
+ Column("TOTAL_SSL_CONNECTIONS",ULonglong(), NOT_NULL, "Total_ssl_connections"),
+ Column("MAX_STATEMENT_TIME_EXCEEDED",SLonglong(),NOT_NULL, "Max_statement_time_exceeded"),
+ CEnd()
+};
+
+} // namespace Show
+
+static int user_stats_fill(THD* thd, TABLE_LIST* tables, COND* cond)
+{
+ if (check_global_access(thd, PROCESS_ACL, true))
+ return 0;
+
+ return send_user_stats(thd, &global_user_stats, tables->table);
+}
+
+static int user_stats_reset()
+{
+ mysql_mutex_lock(&LOCK_global_user_client_stats);
+ free_global_user_stats();
+ init_global_user_stats();
+ mysql_mutex_unlock(&LOCK_global_user_client_stats);
+ return 0;
+}
+
+static int user_stats_init(void *p)
+{
+ ST_SCHEMA_TABLE *schema= (ST_SCHEMA_TABLE *)p;
+ schema->fields_info= Show::user_stats_fields;
+ schema->fill_table= user_stats_fill;
+ schema->reset_table= user_stats_reset;
+ return 0;
+}
+
diff --git a/plugin/userstat/userstat.cc b/plugin/userstat/userstat.cc
new file mode 100644
index 00000000..2084453f
--- /dev/null
+++ b/plugin/userstat/userstat.cc
@@ -0,0 +1,82 @@
+#include <my_global.h>
+#include <mysql/plugin.h>
+#include <mysql_version.h>
+#include "table.h"
+#include "sql_connect.h"
+#include "field.h"
+#include "sql_const.h"
+#include "sql_acl.h"
+
+bool schema_table_store_record(THD *thd, TABLE *table);
+
+#include "client_stats.cc"
+#include "index_stats.cc"
+#include "table_stats.cc"
+#include "user_stats.cc"
+
+static struct st_mysql_information_schema userstat_info=
+{ MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION };
+
+maria_declare_plugin(userstat)
+{
+ MYSQL_INFORMATION_SCHEMA_PLUGIN,
+ &userstat_info,
+ "CLIENT_STATISTICS",
+ "Percona and Sergei Golubchik",
+ "Client Statistics",
+ PLUGIN_LICENSE_GPL,
+ client_stats_init,
+ 0,
+ 0x0200,
+ NULL,
+ NULL,
+ "2.0",
+ MariaDB_PLUGIN_MATURITY_STABLE
+},
+{
+ MYSQL_INFORMATION_SCHEMA_PLUGIN,
+ &userstat_info,
+ "INDEX_STATISTICS",
+ "Percona and Sergei Golubchik",
+ "Index Statistics",
+ PLUGIN_LICENSE_GPL,
+ index_stats_init,
+ 0,
+ 0x0200,
+ NULL,
+ NULL,
+ "2.0",
+ MariaDB_PLUGIN_MATURITY_STABLE
+},
+{
+ MYSQL_INFORMATION_SCHEMA_PLUGIN,
+ &userstat_info,
+ "TABLE_STATISTICS",
+ "Percona and Sergei Golubchik",
+ "Table Statistics",
+ PLUGIN_LICENSE_GPL,
+ table_stats_init,
+ 0,
+ 0x0200,
+ NULL,
+ NULL,
+ "2.0",
+ MariaDB_PLUGIN_MATURITY_STABLE
+},
+{
+ MYSQL_INFORMATION_SCHEMA_PLUGIN,
+ &userstat_info,
+ "USER_STATISTICS",
+ "Percona and Sergei Golubchik",
+ "User Statistics",
+ PLUGIN_LICENSE_GPL,
+ user_stats_init,
+ 0,
+ 0x0200,
+ NULL,
+ NULL,
+ "2.0",
+ MariaDB_PLUGIN_MATURITY_STABLE
+}
+maria_declare_plugin_end;
+