shell監控mysql 8.0資料庫
指令碼結果:
指令碼內容:
#!/bin/bash # Mysql Version 8.0.20 host='localhost' user=root password=xxxxxxx port=3306 #統計程式個數 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 performance_schema.data_locks;" cmd20="select count(*) from performance_schema.data_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 15231 | grep 33060 | wc -l` port_number=`ss -tulp | grep $process | awk '{print $5}' | awk -F":" '{print $4}' | grep 33060` 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 '{p rint $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不能做被除數(${qcache_hits:-0}判斷變數是否有空值) qcache_num=`expr ${qcache_hits:-0} + ${qcache_inserts:-0}` if [ "$qcache_num" != "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_Runnin g | 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_Runn ing | grep -v "Slave_SQL_Running_State" | 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_B ehind_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-2935313/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- shell監控mysql 5.7資料庫MySql資料庫
- MySQL監控-Datadog資料庫監控調研MySql資料庫
- zabbix6.2新增mysql資料庫監控MySql資料庫
- MySQL資料庫與Nacos搭建監控服務MySql資料庫
- 監控資料庫活動資料庫
- 資料庫監控---PIGOSS BSM資料庫Go
- 資料庫繁忙程度監控資料庫
- shell指令碼:監控MySQL服務是否正常指令碼MySql
- Shell指令碼監控MySQL主從狀態指令碼MySql
- Zabbix監控神通資料庫教程資料庫
- Mysql資料庫監聽binlogMySql資料庫
- Python呼叫阿里雲資料庫監控資料Python阿里資料庫
- 資料庫DML監控一例資料庫
- Zabbix+Python監控Oracle資料庫PythonOracle資料庫
- Shell 系統資訊監控指令碼指令碼
- MySQL資料庫備份的shell指令碼MySql資料庫指令碼
- MySQL 8.0 之資料字典MySql
- C#連線MySQL8.0資料庫失敗C#MySql資料庫
- 【shell】磁碟監控指令碼指令碼
- 資料庫效能大揭秘:玩轉MySQL監控指標狀態變數資料庫MySql指標變數
- 通過shell指令碼批量操作mysql資料庫指令碼MySql資料庫
- Liunx備份mysql資料庫的shell指令碼MySql資料庫指令碼
- mysql8.0資料庫新增使用者和授權MySql資料庫
- 資料庫監控工具--PIGOSSBSM運維監控管理系統資料庫Go運維
- Zabbix5.0 配置 ODBC 監控 Oracle 資料庫Oracle資料庫
- 【SQL】Oracle資料庫監控sql執行情況SQLOracle資料庫
- 【SQL】Oracle資料庫SQL監控報告示例SQLOracle資料庫
- Oracle 19C+13.4EMCC資料庫監控Oracle資料庫
- MySQL5.7/8.0效能分析shell指令碼MySql指令碼
- 如何使用 taosKeeper 做好監控工作,時序資料庫 TDengine 3.0 監控工具詳解資料庫
- MySQL監控工具MySql
- 效能測試之資料庫監控分析工具PMM資料庫
- 幾款SQLSERVER資料庫監控軟體介紹SQLServer資料庫
- 自定義帶監控的資料庫連線池資料庫
- 前端資料監控到底在監控什麼?前端
- 資料庫 MySQL8.0+常用命令及操作命令詳解資料庫MySql
- MySQL 8.0 在關聯式資料庫方面有這些新特性MySql資料庫
- 系統監控&JVM監控指標資料查詢JVM指標