diff options
author | Sergei Golubchik <serg@mariadb.org> | 2014-08-25 19:08:55 +0200 |
---|---|---|
committer | Sergei Golubchik <serg@mariadb.org> | 2014-10-11 18:53:05 +0200 |
commit | 3182938d22b705d616b83d83dd72c31f24bc365f (patch) | |
tree | e19a2418f4ad49ddcc7db11ac300d9d45e44f455 /plugin/userstat | |
parent | db8af31831beb949dfcf7675b3c6392dc52dc38c (diff) | |
download | mariadb-git-3182938d22b705d616b83d83dd72c31f24bc365f.tar.gz |
move userstat tables to a plugin
Diffstat (limited to 'plugin/userstat')
-rw-r--r-- | plugin/userstat/CMakeLists.txt | 4 | ||||
-rw-r--r-- | plugin/userstat/client_stats.cc | 97 | ||||
-rw-r--r-- | plugin/userstat/index_stats.cc | 72 | ||||
-rw-r--r-- | plugin/userstat/table_stats.cc | 73 | ||||
-rw-r--r-- | plugin/userstat/user_stats.cc | 55 | ||||
-rw-r--r-- | plugin/userstat/userstat.cc | 81 |
6 files changed, 382 insertions, 0 deletions
diff --git a/plugin/userstat/CMakeLists.txt b/plugin/userstat/CMakeLists.txt new file mode 100644 index 00000000000..5daa4f59577 --- /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 00000000000..dd79f350c5c --- /dev/null +++ b/plugin/userstat/client_stats.cc @@ -0,0 +1,97 @@ +static ST_FIELD_INFO client_stats_fields[]= +{ + {"CLIENT", LIST_PROCESS_HOST_LEN, MYSQL_TYPE_STRING, 0, 0, "Client", 0}, + {"TOTAL_CONNECTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Total_connections", 0}, + {"CONCURRENT_CONNECTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Concurrent_connections", 0}, + {"CONNECTED_TIME", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Connected_time", 0}, + {"BUSY_TIME", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_DOUBLE, 0, 0, "Busy_time", 0}, + {"CPU_TIME", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_DOUBLE, 0, 0, "Cpu_time", 0}, + {"BYTES_RECEIVED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Bytes_received", 0}, + {"BYTES_SENT", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Bytes_sent", 0}, + {"BINLOG_BYTES_WRITTEN", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Binlog_bytes_written", 0}, + {"ROWS_READ", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_read", 0}, + {"ROWS_SENT", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_sent", 0}, + {"ROWS_DELETED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_deleted", 0}, + {"ROWS_INSERTED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_inserted", 0}, + {"ROWS_UPDATED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_updated", 0}, + {"SELECT_COMMANDS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Select_commands", 0}, + {"UPDATE_COMMANDS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Update_commands", 0}, + {"OTHER_COMMANDS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Other_commands", 0}, + {"COMMIT_TRANSACTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Commit_transactions", 0}, + {"ROLLBACK_TRANSACTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rollback_transactions", 0}, + {"DENIED_CONNECTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Denied_connections", 0}, + {"LOST_CONNECTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Lost_connections", 0}, + {"ACCESS_DENIED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Access_denied", 0}, + {"EMPTY_QUERIES", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Empty_queries", 0}, + {"MAX_STATEMENT_TIME_EXCEEDED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Max_statement_time_exceeded",SKIP_OPEN_TABLE}, + {0, 0, MYSQL_TYPE_STRING, 0, 0, 0, 0} +}; + +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->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, SUPER_ACL | 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= 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 00000000000..236130d327f --- /dev/null +++ b/plugin/userstat/index_stats.cc @@ -0,0 +1,72 @@ +static ST_FIELD_INFO index_stats_fields[]= +{ + {"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Table_schema",SKIP_OPEN_TABLE}, + {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Table_name",SKIP_OPEN_TABLE}, + {"INDEX_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Index_name",SKIP_OPEN_TABLE}, + {"ROWS_READ", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_read",SKIP_OPEN_TABLE}, + {0, 0, MYSQL_TYPE_STRING, 0, 0, 0,0} +}; + +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; + char *index_name; + size_t schema_name_length, table_name_length, index_name_length; + + bzero((char*) &tmp_table,sizeof(tmp_table)); + tmp_table.db= index_stats->index; + tmp_table.table_name= strend(index_stats->index)+1; + tmp_table.grant.privilege= 0; + if (check_access(thd, SELECT_ACL, tmp_table.db, + &tmp_table.grant.privilege, NULL, 0, 1) || + check_grant(thd, SELECT_ACL, &tmp_table, 1, UINT_MAX, 1)) + continue; + + index_name= strend(tmp_table.table_name)+1; + schema_name_length= (tmp_table.table_name - index_stats->index) -1; + table_name_length= (index_name - tmp_table.table_name)-1; + index_name_length= (index_stats->index_name_length - schema_name_length - + table_name_length - 3); + + table->field[0]->store(tmp_table.db, schema_name_length, + system_charset_info); + table->field[1]->store(tmp_table.table_name, table_name_length, + system_charset_info); + table->field[2]->store(index_name, 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= 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 00000000000..7b522a388d7 --- /dev/null +++ b/plugin/userstat/table_stats.cc @@ -0,0 +1,73 @@ +static ST_FIELD_INFO table_stats_fields[]= +{ + {"TABLE_SCHEMA", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Table_schema",SKIP_OPEN_TABLE}, + {"TABLE_NAME", NAME_LEN, MYSQL_TYPE_STRING, 0, 0, "Table_name",SKIP_OPEN_TABLE}, + {"ROWS_READ", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_read",SKIP_OPEN_TABLE}, + {"ROWS_CHANGED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_changed",SKIP_OPEN_TABLE}, + {"ROWS_CHANGED_X_INDEXES", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_changed_x_#indexes",SKIP_OPEN_TABLE}, + {0, 0, MYSQL_TYPE_STRING, 0, 0, 0, 0} +}; + +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= table_stats->table; + tmp_table.table_name= end_of_schema+1; + tmp_table.grant.privilege= 0; + if (check_access(thd, SELECT_ACL, tmp_table.db, + &tmp_table.grant.privilege, NULL, 0, 1) || + check_grant(thd, SELECT_ACL, &tmp_table, 1, UINT_MAX, + 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= 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 00000000000..71ba3f0397d --- /dev/null +++ b/plugin/userstat/user_stats.cc @@ -0,0 +1,55 @@ +static ST_FIELD_INFO user_stats_fields[]= +{ + {"USER", USERNAME_CHAR_LENGTH, MYSQL_TYPE_STRING, 0, 0, "User", 0}, + {"TOTAL_CONNECTIONS", MY_INT32_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONG, 0, 0, "Total_connections", 0}, + {"CONCURRENT_CONNECTIONS", MY_INT32_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONG, 0, 0, "Concurrent_connections", 0}, + {"CONNECTED_TIME", MY_INT32_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONG, 0, 0, "Connected_time", 0}, + {"BUSY_TIME", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_DOUBLE, 0, 0, "Busy_time", 0}, + {"CPU_TIME", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_DOUBLE, 0, 0, "Cpu_time", 0}, + {"BYTES_RECEIVED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Bytes_received", 0}, + {"BYTES_SENT", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Bytes_sent", 0}, + {"BINLOG_BYTES_WRITTEN", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Binlog_bytes_written", 0}, + {"ROWS_READ", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_read", 0}, + {"ROWS_SENT", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_sent", 0}, + {"ROWS_DELETED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_deleted", 0}, + {"ROWS_INSERTED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_inserted", 0}, + {"ROWS_UPDATED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rows_updated", 0}, + {"SELECT_COMMANDS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Select_commands", 0}, + {"UPDATE_COMMANDS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Update_commands", 0}, + {"OTHER_COMMANDS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Other_commands", 0}, + {"COMMIT_TRANSACTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Commit_transactions", 0}, + {"ROLLBACK_TRANSACTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Rollback_transactions", 0}, + {"DENIED_CONNECTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Denied_connections", 0}, + {"LOST_CONNECTIONS", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Lost_connections", 0}, + {"ACCESS_DENIED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Access_denied", 0}, + {"EMPTY_QUERIES", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Empty_queries", 0}, + {"MAX_STATEMENT_TIME_EXCEEDED", MY_INT64_NUM_DECIMAL_DIGITS, MYSQL_TYPE_LONGLONG, 0, 0, "Max_statement_time_exceeded",SKIP_OPEN_TABLE}, + {0, 0, MYSQL_TYPE_STRING, 0, 0, 0, 0} +}; + +static int user_stats_fill(THD* thd, TABLE_LIST* tables, COND* cond) +{ + if (check_global_access(thd, SUPER_ACL | 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= 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 00000000000..3e5afe3df0c --- /dev/null +++ b/plugin/userstat/userstat.cc @@ -0,0 +1,81 @@ +#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_GAMMA +}, +{ + 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_GAMMA +}, +{ + 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_GAMMA +}, +{ + 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_GAMMA +} +maria_declare_plugin_end; + |