shell監控mysql 5.7資料庫
指令碼結果:
指令碼內容:
#!/bin/bash # Mysql Version 5.7.30 host='localhost' user=root password=System135 port=3306 #Mysql資料收集 cmd1="select version();" cmd2="show master status;" cmd3="show global status like 'Connections';" cmd4="show global status like 'Questions';" cmd5="show global status like 'Uptime';" cmd6="show global status like 'Com_Commit';" cmd7="show global status like 'Com_Rollback';" cmd8="show global status like 'key_reads';" cmd9="show global status like 'key_read_requests';" cmd10="show global status like 'key_writes';" cmd11="show global status like 'key_write_requests';" cmd12="show global status like 'innodb_buffer_pool_reads';" cmd13="show global status like 'innodb_buffer_pool_read_requests';" cmd14="show global status like 'Qcache_hits';" cmd15="show global status like 'Qcache_inserts';" cmd16="show global status like 'Threads_created';" cmd17="show global status like 'Connections';" cmd18="select count(*) from information_schema.innodb_trx;" cmd19="select count(*) from information_schema.innodb_locks;" cmd20="select count(*) from information_schema.innodb_lock_waits;" cmd21="select count(*) from information_schema.processlist;" #基本資訊 Basic() { version_number=`mysql -h$host -P$port -u$user -p$password -e "$cmd1" 2>&1 | grep -v version | grep -v "Using a password on the command"` master_status=`mysql -h$host -P$port -u$user -p$password -e "show master status;" 2>&1 | grep -v File | grep -v "Using a password on the command" | wc -l` slave_status=`mysql -h$host -P$port -u$user -p$password -e "show slave status\G;" 2>&1 | grep -v "Using a password on the command" | wc -l` echo "Mysql Version: $version_number" if [ $slave_status -gt 2 ];then echo "Mysql Role: Slave" elif [ $master_status == 1 ] && [ $slave_status -gt 2 ];then echo "Mysql Role: Master and Slave" else echo "Mysql Role: Master" fi } #Mysql埠檢查 Port() { process=`ps -ef | grep mysql | grep -v grep | grep sock | awk '{print $2}'` port=`ss -tulp | grep $process | wc -l` port_number=`ss -tulp | grep $process | awk '{print $5}' | awk -F":" '{print $4}'` if [ $process ] && [ $port == 1 ];then echo "---------------------------------Mysql埠和程式都正常[埠:$port_number]" else echo -e "\033[31m---------------------------------Mysql埠和程式都異常,退出指令碼\033[0m" exit fi } #Mysql程式檢查 Process() { process_number=`mysql -h$host -P$port -u$user -p$password -e "$cmd21" 2>&1 | grep -v 'count' | grep -v "Using a password on the command"` if [ $process_number -le 100 ];then echo "---------------------------------Mysql當前程式數小於等於100個正常[程式數:$process_number]" elif [ $process_number -gt 100 ] && [ $process_number -lt 200 ];then echo -e "\033[33m---------------------------------Mysql當前程式數大於100小於200個警告[程式數:$process_number]\033[0m" else echo -e "\033[31m---------------------------------Mysql當前程式數大於200個異常[程式數:$process_number]\033[0m" fi } #Mysql連線數 Connect() { connect_number=`mysql -h$host -P$port -u$user -p$password -e "$cmd3" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` if [ $connect_number -lt 100 ];then echo "---------------------------------Mysql當前連線數小於等於100個正常[連線數:$connect_number]" elif [ $connect_number -gt 100 ] && [ $connect_number -lt 200 ];then echo -e "\033[33m---------------------------------Mysql當前連線數大於100小於200個警告[連線數:$connect_number]\033[0m" else echo -e "\033[31m---------------------------------Mysql當前連線數大於200個異常[連線數:$connect_number]\033[0m" fi } #統計Mysql的QPS Qps() { Questions=`mysql -h$host -P$port -u$user -p$password -e "$cmd4" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` Uptime=`mysql -h$host -P$port -u$user -p$password -e "$cmd5" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` qps=$(echo "scale=0;$Questions / $Uptime"|bc) #qps=$(echo "scale=2;$Questions / $Uptime"|bc) if [ $qps -lt 1000 ];then echo "---------------------------------Mysql當前Qps小於等於1000個正常[Qps數:$qps]" elif [ $qps -gt 1000 ] && [ $qps -lt 2000 ];then echo -e "\033[33m---------------------------------Mysql當前Qps大於1000小於2000個警告[Qps數:$qps]\033[0m" else echo -e "\033[31m---------------------------------Mysql當前Qps大於2000個異常[Qps數:$qps]\033[0m" fi } #統計Mysql的Tps Tps() { Com_Commit=`mysql -h$host -P$port -u$user -p$password -e "$cmd6" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` Com_Rollback=`mysql -h$host -P$port -u$user -p$password -e "$cmd7" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` Uptime=`mysql -h$host -P$port -u$user -p$password -e "$cmd5" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` #tps=`expr $Com_Commit + $Com_Rollback / $Uptime` tps=$(echo "$Com_Commit + $Com_Rollback / $Uptime"|bc) if [ $tps -le 100 ];then echo "---------------------------------Mysql當前Tps小於等於100個正常[Com_Commit:$Com_Commit Com_Rollback:$Com_Rollback Tps:$tps]" fi } #統計key Buffer命中率 KeyBuffer() { key_reads=`mysql -h$host -P$port -u$user -p$password -e "$cmd8" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` key_read_requests=`mysql -h$host -P$port -u$user -p$password -e "$cmd9" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` key_writes=`mysql -h$host -P$port -u$user -p$password -e "$cmd10" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` key_write_requests=`mysql -h$host -P$port -u$user -p$password -e "$cmd11" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` if [ $key_read_requests != 0 ];then key_buffer_read_hits=`echo "scale=2;(1 - $key_reads / $key_read_requests)"|bc` key_buffer_read_hits=`awk 'BEGIN {printf "%.2f%%\n",('$key_buffer_read_hits' * 100)}'` echo "---------------------------------Mysql當前Key Buffer讀命中率正常[Key_Buffer_Read_Hits:$key_buffer_read_hits]" else echo "---------------------------------Mysql當前Key Buffer讀命中率為為0]" fi if [ $key_write_requests != 0 ];then key_buffer_write_hits=`echo "scale=2;(1 - $key_writes / $key_write_requests)"|bc` key_buffer_write_hits=`awk 'BEGIN {printf "%.2f%%\n",('$key_buffer_write_hits' * 100)}'` echo "---------------------------------Mysql當前Key Buffer寫命中率正常[Key_Buffer_Write_Hits:$key_buffer_write_hits]" else echo "---------------------------------Mysql當前Key Buffer寫命中率為0]" fi } #統計Innodb Buffer命中率 InnodbBuffer() { innodb_buffer_pool_reads=`mysql -h$host -P$port -u$user -p$password -e "$cmd12" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` innodb_buffer_pool_read_requests=`mysql -h$host -P$port -u$user -p$password -e "$cmd13" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` innodb_buffer_read_hits=`echo "scale=2;(1 - $innodb_buffer_pool_reads / $innodb_buffer_pool_read_requests)"|bc` innodb_buffer_read_hits=`awk 'BEGIN {printf "%.2f%%\n",('$innodb_buffer_read_hits' * 100)}'` echo "---------------------------------Mysql當前Innodb Buffer命中率正常[Innodb_Buffer_Read_Hits:$innodb_buffer_read_hits]" } #統計Query Cache命中率 QueryCache() { qcache_hits=`mysql -h$host -P$port -u$user -p$password -e "$cmd14" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` qcache_inserts=`mysql -h$host -P$port -u$user -p$password -e "$cmd15" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` #判斷qcache_hits+qcache_inserts是否為0,因為0不能做被除數 if [ `expr $qcache_hits + $qcache_inserts` != 0 ];then query_cache_hits=`echo "scale=2;$qcache_hits / ($qcache_hits + $qcache_inserts)"|bc` query_cache_hits=`awk 'BEGIN {printf "%.2f%%\n",('$query_cache_hits' * 100)}'` echo "---------------------------------Mysql當前Query Cache命中率正常[Query_Cache_Hits:$query_cache_hits]" else echo "---------------------------------Mysql當前Query Cache命中率為0" fi } #統計Thread Cache命中率 ThreadCache() { threads_created=`mysql -h$host -P$port -u$user -p$password -e "$cmd16" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` connections=`mysql -h$host -P$port -u$user -p$password -e "$cmd17" 2>&1 | egrep -v "Variable_name|Using a password on the command" | awk '{print $2}'` thread_cache_hits=`echo "scale=2;(1- $threads_created / $connections)"|bc` thread_cache_hits=`awk 'BEGIN {printf "%.2f%%\n",('$thread_cache_hits' * 100)}'` echo "---------------------------------Mysql當前Thread Cache命中率正常[Thread_Cache_Hits:$thread_cache_hits]" } #當前執行多少個事物 Trx() { trx_number=`mysql -h$host -P$port -u$user -p$password -e "$cmd18" 2>&1 | egrep -v "count|Using a password on the command"` echo "---------------------------------Mysql當前Transaction個數[Transaction:$trx_number]" } #統計多少個鎖 Lock() { lock_number=`mysql -h$host -P$port -u$user -p$password -e "$cmd19" 2>&1 | egrep -v "count|Using a password on the command"` lock_wait_number=`mysql -h$host -P$port -u$user -p$password -e "$cmd20" 2>&1 | egrep -v "count|Using a password on the command"` echo "---------------------------------Mysql當前Lock個數[Lock Number:$lock_number Lock_Wait_Number:$lock_wait_number]" } #判斷從庫狀態 Slave_status() { mster_status=`mysql -h$host -P$port -u$user -p$password -e "show master status;" 2>&1 | grep -v File | grep -v "Using a password on the command" | wc -l` slave_status=`mysql -h$host -P$port -u$user -p$password -e "show slave status\G;" 2>&1 | grep -v "Using a password on the command" | wc -l` if [ $master_status == 1 ];then exit elif [ $master_status == 0 ] && [ $slave_status -gt 2 ];then Slave_IO_Running=`mysql -h$host -P$port -u$user -p$password -e "show slave status \G" 2>&1 | grep -v "Using a password on the command" | grep Slave_IO_Running | awk '{print $2}'` Slave_SQL_Running=`mysql -h$host -P$port -u$user -p$password -e "show slave status \G" 2>&1 | grep -v "Using a password on the command" | grep Slave_SQL_Running | grep -v "Slave_SQL_Running_Sta te" | awk '{print $2}'` if [ $Slave_IO_Running == "Yes" ] && [ $Slave_SQL_Running == "Yes" ];then echo "---------------------------------Mysql從庫正常[Slave_IO_Running:$Slave_IO_Running Slave_SQL_Running:$Slave_SQL_Running]" else echo -e "\033[31m---------------------------------Mysql從庫異常[Slave_IO_Running:$Slave_IO_Running Slave_SQL_Running:$Slave_SQL_Running]\033[0m" fi Seconds_Behind_Master=`mysql -h$host -P$port -u$user -p$password -e "show slave status \G" 2>&1 | grep -v "Using a password on the command" | grep "Seconds_Behind_Master" | awk '{print $2}'` echo "---------------------------------Mysql主從資料延遲[Seconds_Behind_Master:$Seconds_Behind_Master]" fi } Basic Port Process Connect Qps Tps KeyBuffer InnodbBuffer QueryCache ThreadCache Trx Lock Slave_status
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25854343/viewspace-2935315/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- shell監控mysql 8.0資料庫MySql資料庫
- MySQL監控-Datadog資料庫監控調研MySql資料庫
- zabbix6.2新增mysql資料庫監控MySql資料庫
- mysql 5.7 sys資料庫初探MySql資料庫
- 遷移MySQL 5.7資料庫MySql資料庫
- MySQL資料庫與Nacos搭建監控服務MySql資料庫
- 02 zabbix-centos7 監控mysql5.7CentOSMySql
- 監控資料庫活動資料庫
- 資料庫監控---PIGOSS BSM資料庫Go
- 資料庫繁忙程度監控資料庫
- shell指令碼:監控MySQL服務是否正常指令碼MySql
- Shell指令碼監控MySQL主從狀態指令碼MySql
- Zabbix監控神通資料庫教程資料庫
- Mysql資料庫監聽binlogMySql資料庫
- Python呼叫阿里雲資料庫監控資料Python阿里資料庫
- phpStudy2018 升級資料庫 MySQL5.7PHP資料庫MySql
- 資料庫DML監控一例資料庫
- Zabbix+Python監控Oracle資料庫PythonOracle資料庫
- Shell 系統資訊監控指令碼指令碼
- MySQL資料庫備份的shell指令碼MySql資料庫指令碼
- MySQL資料庫之mysql5.7基礎 檢視一個資料庫中的所有表MySql資料庫
- 【資料庫】mysql5.6升級至5.7(物理方式)資料庫MySql
- 【shell】磁碟監控指令碼指令碼
- 資料庫效能大揭秘:玩轉MySQL監控指標狀態變數資料庫MySql指標變數
- mysql 5.7後使用sys資料庫下的表查詢資料庫效能狀況MySql資料庫
- 通過shell指令碼批量操作mysql資料庫指令碼MySql資料庫
- Liunx備份mysql資料庫的shell指令碼MySql資料庫指令碼
- 破解 MySQL5.7 資料庫的 root 登入密碼MySql資料庫密碼
- MySQL5.7/8.0效能分析shell指令碼MySql指令碼
- 資料庫監控工具--PIGOSSBSM運維監控管理系統資料庫Go運維
- Zabbix5.0 配置 ODBC 監控 Oracle 資料庫Oracle資料庫
- 【SQL】Oracle資料庫監控sql執行情況SQLOracle資料庫
- 【SQL】Oracle資料庫SQL監控報告示例SQLOracle資料庫
- Oracle 19C+13.4EMCC資料庫監控Oracle資料庫
- Centos7系統如何安裝MySQL5.7資料庫?CentOSMySql資料庫
- 故障分析 | MySQL 5.7 使用臨時表導致資料庫 CrashMySql資料庫
- 如何使用 taosKeeper 做好監控工作,時序資料庫 TDengine 3.0 監控工具詳解資料庫
- MySQL監控工具MySql