mysql常规巡检
目录
MySQL常规巡检
一、巡检脚本
巡检脚本包括三个文件inspection.conf、inspection_mysql.sh、mysqltuner.pl
bash>ll
-rw-r--r-- 1 root root 994 Nov 1 16:33 inspection.conf
-rwxr-xr-x 1 root root 39221 Nov 1 15:26 inspection_mysql_v4.sh
-rwxr-xr-x 1 root root 219803 Nov 1 11:32 mysqltuner.pl
二、下载巡检脚本
- 下载地址:
三、脚本执行说明
1、inspection.conf 使用说明
使用说明:配置mysql登录账号信息以及mysql的标准配置
#============================================================================================================
#MySQL巡检配置
#============================================================================================================
#mysql用户名
MYSQL_USER=root
#mysql用户密码
MYSQL_PASS='xxxxxxx'
#mysql客户端
MYSQL_COMM=mysql
#备份路径
BAK_PATH=/r2/bak_sql
#mysql全局日志
GLOBAL_LOG=`hostname`_global_`date +%Y%m%d`.txt
#mycnf内容输出
MYCNF=`hostname`_mycnf_`date +%Y%m%d`.txt
#mysqltuner输出日志
MYSQLTUNER_LOG=`hostname`_mysqltuner_`date +%Y%m%d`.txt
#检查mysql标准配置日志
#TANDARD_MYSQL_CONF=`hostname`_standard_`date +%Y%m%d`.txt
#检查mysql标准配置
standard_basedir=/usr/
standard_datadir=/r2/mysqldata/
standard_plugin=/usr/lib64/mysql/plugin/
standard_osmysqluser=mysql
standard_log_error=/r2/mysqldata/error.log
standard_pid=/r2/mysqldata/`hostname`.pid
standard_socket=/r2/mysqldata/mysql.sock
2、inspection_mysql.sh 使用说明
inspection_mysql.sh
脚本简述 :主脚本通过读取inspection.conf 配置文件来执行,调用perl来执行 mysqltuner.pl来提供优化建议
#!/bin/bash
# line: V1.4
# mail: gczheng@139.com
# data: 2018-11-01
# script_name: inspection_mysql.sh
# Function: MySQL巡检包含(mysqltuner.pl、inspection.conf)与inspection_mysql.sh放在同一目录下
#============================================================================================================
#定义颜色的变量
#============================================================================================================
. ./inspection.conf
echo_color(){
color=${1} && shift
case ${color} in
black)
echo -e "e[0;30m${@}e[0m"
;;
red)
echo -e "e[0;31m${@}e[0m"
;;
green)
echo -e "e[0;32m${@}e[0m"
;;
yellow)
echo -e "e[0;33m${@}e[0m"
;;
blue)
echo -e "e[0;34m${@}e[0m"
;;
purple)
echo -e "e[0;35m${@}e[0m"
;;
cyan)
echo -e "e[0;36m${@}e[0m"
;;
*)
echo -e "e[0;37m${@}e[0m"
;;
esac # --- end of case ---
}
#============================================================================================================
#设置MySQL配置信息
#============================================================================================================
#MYSQL_USER=root #mysql的用户名
#MYSQL_PASS=iforgot #mysql的登录用户密码
MYSQL_HOST="localhost" #mysql的主机IP
#MYSQL_COMM=mysql
DATE=`date -d today +"%Y-%m-%d %T"`
#BAK_PATH=/r2/bak_sql
TIME_INTERVAL=3 #等待时间
#GLOBAL_LOG=`hostname`_global_`date +%Y%m%d`.txt
#MYCNF=`hostname`_mycnf_`date +%Y%m%d`.txt
#MYSQLTUNER_LOG=`hostname`_mysqltuner_`date +%Y%m%d`.txt
#STANDARD_MYSQL_CONF=`hostname`_standard_`date +%Y%m%d`.txt
HOSTNAME=`hostname` #主机名
#============================================================================================================
# 判断mysql账号密码是否存在,不存在请输入密码
#============================================================================================================
echo_color green "#>>>>>>>>>>>>>>>>>>>>>>>>> ${HOSTNAME} ------ ${DATE} <<<<<<<<<<<<<<<<<<<<<<<#"
if [[ ! -n ${MYSQL_USER} && ! -n ${MYSQL_PASS} ]]; then
read -s -p "Enter your MySQL user:" MYSQL_USER
echo
echo_color green "User name is ${MYSQL_USER} "
echo
read -s -p "Enter your MySQL password:" MYSQL_PASS
echo
fi
${MYSQL_COMM} -h${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_PASS} -e "select version();"
if [ $? -ne 0 ]
then
echo_color red "mysql login failure, please check mysql process or the username and password match or mysql service no running!" > ${GLOBAL_LOG}
err_exit=`cat ${GLOBAL_LOG}`
echo_color red ${err_exit}
exit 0
else
echo_color green "MySQL is Running" > ${GLOBAL_LOG}
fi
#============================================================================================================
#设置主机配置信息
#============================================================================================================
hostname=`hostname` #主机名
ipaddress=`ip route | awk '/src/ && !/docker/{for(i=1;i<=NF;++i)if($i == "src"){print $(i+1)}}' ` #IP地址
cpuinfo=`cat /proc/cpuinfo|grep "name"|cut -d: -f2 |awk '{print "*"$1,$2,$3,$4}'|uniq -c` #cpu
phmem=`dmidecode | grep -A 16 "Memory Device$" |grep Size:|grep -v "No Module Installed"|awk '{print "*" $2,$3}'|uniq -c` #物理内存数量
server_versions=`cat /etc/redhat-release ` #系统版本
kernel_versions=`uname -a |awk '{print $3}'` #内核版本
product_name=`dmidecode | grep "Product Name" | awk 'NR==1'`
cpuload=`cat /proc/loadavg | awk '{print $1,$2,$3}'`
MYSQL_HOST="localhost" #mysql的主机IP
#MYSQL_COMM=mysql
DATE=`date -d today +"%Y-%m-%d %T"`
#BAK_PATH=/r2/bak_sql
TIME_INTERVAL=3 #等待时间
#============================================================================================================
#内存情况
#============================================================================================================
mem_total=$(free -m |grep Mem|awk '{print $2}')
mem_used=$(free -m |grep Mem|awk '{print $3}')
mem_rate=`expr $mem_used/$mem_total*100|bc -l`
#============================================================================================================
#内存使用量
#============================================================================================================
mem_sum=`free -m | xargs | awk '{print "Free/total memory: " $17 " / " $8 " MB"}'
| awk -F":" 'BEGIN{print " FREE / TOTAL " } {print $2 }'`
#============================================================================================================
#硬盘容量
#============================================================================================================
dk_usage=`df -H |awk -F ' ' '{ print $1,$2,$3,$4,$5,$6}'`
#============================================================================================================
# MySQL original state
#============================================================================================================
# cat >> $STANDARD_MYSQL_CONF <<EOF
# standard_basedir=/usr
# standard_datadir=/r2/mysqldata/
# standard_plugin=/usr/lib64/mysql/plugin/
# standard_osmysqluser=mysql
# standard_log_error=/r2/mysqldata/${hostname}.err
# standard_pid=/r2/mysqldata/${hostname}.pid
# standard_socket=/r2/mysqldata/mysql.sock
# EOF
osmysqluser=`ps -ef|grep -w mysqld |grep -v grep |awk '{print $1}'`
#============================================================================================================
# 1.1主机基本信息
#============================================================================================================
echo
echo_color yellow "#===========================================================================================================#"
echo_color yellow "# 1 The system basic infomation #"
echo_color yellow "#===========================================================================================================#"
echo
echo_color blue "#-----------------------------------------------------------------------------------------------------------#"
echo -e "1.0 server product name is:"
echo
echo ${product_name}
echo
echo_color blue "#-----------------------------------------------------------------------------------------------------------#"
echo -e "1.1 server cpuinfo is: "
echo
echo ${cpuinfo}
echo
echo_color blue "#-----------------------------------------------------------------------------------------------------------#"
echo -e "1.2 server physical memory number is: "
echo
echo ${phmem}
echo
echo_color blue "#-----------------------------------------------------------------------------------------------------------#"
echo -e "1.3 server hd disk info is: "
echo
echo -e "${dk_usage}"
echo
echo_color blue "#-----------------------------------------------------------------------------------------------------------#"
echo -e "1.4 server hostname is:"
echo
echo ${hostname}
echo
echo_color blue "#-----------------------------------------------------------------------------------------------------------#"
echo -e "1.5 server ipaddree is: "
echo
echo -e "${ipaddress}"
echo
echo_color blue "#-----------------------------------------------------------------------------------------------------------#"
echo -e "1.6 server version is: "
echo
echo ${server_versions}
echo
echo_color blue "#-----------------------------------------------------------------------------------------------------------#"
echo -e "1.7 server system kernel version is: "
echo
echo ${kernel_versions}
echo
echo_color blue "#-----------------------------------------------------------------------------------------------------------#"
echo -e "1.8 server CPU load average is: "
echo
echo ${cpuload}
echo
echo_color blue "#-----------------------------------------------------------------------------------------------------------#"
echo -e "1.9 server memory Summary is: "
echo
echo ${mem_sum}
echo
echo_color blue "#-----------------------------------------------------------------------------------------------------------#"
echo -e "1.10 server top task status is: "
echo
top -c -n 1 |grep Tasks
echo
echo_color blue "#-----------------------------------------------------------------------------------------------------------#"
echo -e "1.11 server top cpu status is: "
echo
top -c -n 1 |grep Cpu
echo
echo_color blue "#-----------------------------------------------------------------------------------------------------------#"
echo -e