From d2ea011e87aa2e75150c854d7ee0914c76e61dd5 Mon Sep 17 00:00:00 2001 From: Carlos Salguero Date: Fri, 6 Apr 2018 15:10:09 -0300 Subject: [PATCH] PT-1525 Added support for roles in mysql-summary --- bin/pt-mysql-summary | 21 +++++++++++++++++++-- lib/bash/collect_mysql_info.sh | 9 ++++++++- lib/bash/report_mysql_info.sh | 14 +++++++++++++- t/pt-mysql-summary/pt-mysql-summary.t | 2 +- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/bin/pt-mysql-summary b/bin/pt-mysql-summary index 1439ea16..586a9a2b 100755 --- a/bin/pt-mysql-summary +++ b/bin/pt-mysql-summary @@ -932,9 +932,13 @@ collect_mysql_processlist () { collect_mysql_users () { $CMD_MYSQL $EXT_ARGV -ss -e 'SELECT COUNT(*), SUM(user=""), SUM(password=""), SUM(password NOT LIKE "*%") FROM mysql.user' 2>/dev/null if [ "$?" -ne 0 ]; then - $CMD_MYSQL $EXT_ARGV -ss -e 'SELECT COUNT(*), SUM(user=""), SUM(authentication_string=""), SUM(authentication_string NOT LIKE "*%") FROM mysql.user' 2>/dev/null + $CMD_MYSQL $EXT_ARGV -ss -e 'SELECT COUNT(*), SUM(user=""), SUM(authentication_string=""), SUM(authentication_string NOT LIKE "*%") FROM mysql.user WHERE account_locked <> "Y" AND password_expired <> "Y" AND authentication_string <> ""' 2>/dev/null fi +} +collect_mysql_roles () { + QUERY="SELECT DISTINCT User 'Role Name', if(from_user is NULL,0, 1) Active FROM mysql.user LEFT JOIN mysql.role_edges ON from_user=user WHERE account_locked='Y' AND password_expired='Y' AND authentication_string=''\G" + $CMD_MYSQL $EXT_ARGV -ss -e "$QUERY" 2>/dev/null } collect_mysql_show_slave_hosts () { @@ -1037,6 +1041,7 @@ collect_mysql_info () { collect_mysql_ndb_status > "$dir/ndb-status" collect_mysql_processlist > "$dir/mysql-processlist" collect_mysql_users > "$dir/mysql-users" + collect_mysql_roles > "$dir/mysql-roles" collect_mysqld_instances "$dir/mysql-variables" > "$dir/mysqld-instances" collect_mysqld_executables "$dir/mysqld-instances" > "$dir/mysqld-executables" @@ -1593,6 +1598,12 @@ format_ndb_status() { egrep '^[ \t]*Name:|[ \t]*Status:' $file|sed 's/^[ \t]*//g'|while read line; do echo $line; echo $line | grep '^Status:'>/dev/null && echo ; done } +format_mysql_roles() { + local file=$1 + [ -e "$file" ] || return + cat $file +} + format_overall_db_stats () { local file="$1" local tmpfile="$PT_TMPDIR/format_overall_db_stats.tmp" @@ -2318,7 +2329,8 @@ report_mysql_summary () { section "Plugins" name_val "InnoDB compression" "$(get_plugin_status "$dir/mysql-plugins" "INNODB_CMP")" - if [ "$(get_var have_query_cache "$dir/mysql-variables")" ]; then + local has_query_cache=$(get_var have_query_cache "$dir/mysql-variables") + if [ "$has_query_cache" = 'YES' ]; then section "Query cache" local query_cache_size=$(get_var query_cache_size "$dir/mysql-variables") local used=$(( ${query_cache_size} - $(get_var Qcache_free_memory "$dir/mysql-status") )) @@ -2458,6 +2470,11 @@ report_mysql_summary () { name_val "Users" "${users}" name_val "Old Passwords" "$(get_var old_passwords "$dir/mysql-variables")" + if [ -s "$dir/mysql-roles" ]; then + section "Roles" + format_mysql_roles "$dir/mysql-roles" + fi + section "Binary Logging" if [ -s "$dir/mysql-master-logs" ] \ diff --git a/lib/bash/collect_mysql_info.sh b/lib/bash/collect_mysql_info.sh index 39d5275c..dc6d6e2a 100644 --- a/lib/bash/collect_mysql_info.sh +++ b/lib/bash/collect_mysql_info.sh @@ -117,11 +117,17 @@ collect_mysql_processlist () { } collect_mysql_users () { +# The where clause has been added to skip listing MySQL 8+ roles as users. +# ROLES are locked accounts, without passwords and expired. $CMD_MYSQL $EXT_ARGV -ss -e 'SELECT COUNT(*), SUM(user=""), SUM(password=""), SUM(password NOT LIKE "*%") FROM mysql.user' 2>/dev/null if [ "$?" -ne 0 ]; then - $CMD_MYSQL $EXT_ARGV -ss -e 'SELECT COUNT(*), SUM(user=""), SUM(authentication_string=""), SUM(authentication_string NOT LIKE "*%") FROM mysql.user' 2>/dev/null + $CMD_MYSQL $EXT_ARGV -ss -e 'SELECT COUNT(*), SUM(user=""), SUM(authentication_string=""), SUM(authentication_string NOT LIKE "*%") FROM mysql.user WHERE account_locked <> "Y" AND password_expired <> "Y" AND authentication_string <> ""' 2>/dev/null fi +} +collect_mysql_roles () { + QUERY="SELECT DISTINCT User 'Role Name', if(from_user is NULL,0, 1) Active FROM mysql.user LEFT JOIN mysql.role_edges ON from_user=user WHERE account_locked='Y' AND password_expired='Y' AND authentication_string=''\G" + $CMD_MYSQL $EXT_ARGV -ss -e "$QUERY" 2>/dev/null } collect_mysql_show_slave_hosts () { @@ -234,6 +240,7 @@ collect_mysql_info () { collect_mysql_ndb_status > "$dir/ndb-status" collect_mysql_processlist > "$dir/mysql-processlist" collect_mysql_users > "$dir/mysql-users" + collect_mysql_roles > "$dir/mysql-roles" collect_mysqld_instances "$dir/mysql-variables" > "$dir/mysqld-instances" collect_mysqld_executables "$dir/mysqld-instances" > "$dir/mysqld-executables" diff --git a/lib/bash/report_mysql_info.sh b/lib/bash/report_mysql_info.sh index 8d2f3b84..23670108 100644 --- a/lib/bash/report_mysql_info.sh +++ b/lib/bash/report_mysql_info.sh @@ -565,6 +565,12 @@ format_ndb_status() { egrep '^[ \t]*Name:|[ \t]*Status:' $file|sed 's/^[ \t]*//g'|while read line; do echo $line; echo $line | grep '^Status:'>/dev/null && echo ; done } +format_mysql_roles() { + local file=$1 + [ -e "$file" ] || return + cat $file +} + # Summarizes per-database statistics for a bunch of different things: count of # tables, views, etc. $1 is the file name. $2 is the database name; if none, # then there should be multiple databases. @@ -1360,7 +1366,8 @@ report_mysql_summary () { # ######################################################################## # Query cache # ######################################################################## - if [ "$(get_var have_query_cache "$dir/mysql-variables")" ]; then + local has_query_cache=$(get_var have_query_cache "$dir/mysql-variables") + if [ "$has_query_cache" = 'YES' ]; then section "Query cache" local query_cache_size=$(get_var query_cache_size "$dir/mysql-variables") local used=$(( ${query_cache_size} - $(get_var Qcache_free_memory "$dir/mysql-status") )) @@ -1521,6 +1528,11 @@ report_mysql_summary () { name_val "Users" "${users}" name_val "Old Passwords" "$(get_var old_passwords "$dir/mysql-variables")" + if [ -s "$dir/mysql-roles" ]; then + section "Roles" + format_mysql_roles "$dir/mysql-roles" + fi + # ######################################################################## # Binary Logging # ######################################################################## diff --git a/t/pt-mysql-summary/pt-mysql-summary.t b/t/pt-mysql-summary/pt-mysql-summary.t index 9d7b7177..09b9b022 100644 --- a/t/pt-mysql-summary/pt-mysql-summary.t +++ b/t/pt-mysql-summary/pt-mysql-summary.t @@ -42,7 +42,7 @@ ok( my @files = glob("$dir/*"); my $n_files = scalar @files; ok( - $n_files >= 15 && $n_files <= 17, + $n_files >= 15 && $n_files <= 18, "And leaves all files in there" ) or diag($n_files, `ls -l $dir`);