目錄php
MySQL常規巡檢html
1、巡檢腳本
巡檢腳本包括三個文件inspection.conf、inspection_mysql.sh、mysqltuner.plnode
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
2、下載巡檢腳本
- 下載地址:
Github:mysql_inspection.zipmysql
3、腳本執行說明
一、inspection.conf 使用說明
使用說明:配置mysql登陸帳號信息以及mysql的標準配置git
#============================================================================================================ #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
二、inspection_mysql.sh 使用說明
inspection_mysql.shgithub
腳本簡述 :主腳本經過讀取inspection.conf 配置文件來執行,調用perl來執行 mysqltuner.pl來提供優化建議sql
#!/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 '\t' '{ 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 "1.12 server top mem status is: " echo top -c -n 1|grep Mem echo echo_color blue "#-----------------------------------------------------------------------------------------------------------#" echo -e