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資料庫
- oracle資料庫執行狀態監控SHELLOracle資料庫
- 監控Oracle資料庫的常用shell指令碼Oracle資料庫指令碼
- 配置CACTI監控MySQL資料庫狀態(5)增加MySQL監控模板MySql資料庫
- MySQL資料庫監控項說明MySql資料庫
- [轉]監控Oracle資料庫的常用shell指令碼Oracle資料庫指令碼
- 監控Oracle資料庫的常用shell指令碼(轉)Oracle資料庫指令碼
- zabbix使用percona監控mysql資料庫MySql資料庫
- zabbix6.2新增mysql資料庫監控MySql資料庫
- 資料庫監控資料庫
- 【轉載】監控Oracle資料庫的常用shell指令碼Oracle資料庫指令碼
- MySQL資料庫與Nacos搭建監控服務MySql資料庫
- 使用zabbix自帶模板監控MySQL資料庫MySql資料庫
- 資料庫效能監控資料庫
- 監控資料庫活動資料庫
- shell 操作mysql資料庫MySql資料庫
- 資料庫繁忙程度監控資料庫
- 資料庫監控軟體資料庫
- SQL Server資料庫監控SQLServer資料庫
- 資料庫監控指令碼資料庫指令碼
- 監控Oracle資料庫方法Oracle資料庫
- 監控資料庫指令碼資料庫指令碼
- zabbix監控oracle資料庫Oracle資料庫
- zabbix和mysql performance monitor模板實現mysql資料庫的監控MySqlORM資料庫
- 配置CACTI監控MySQL資料庫狀態(4)配置cactiMySql資料庫
- 通過shell指令碼同時監控多個資料庫負載指令碼資料庫負載
- 資料庫監控---PIGOSS BSM資料庫Go
- [zt]資料庫監控指令碼資料庫指令碼
- 資料庫監控指令碼(一)資料庫指令碼
- 資料庫監控指令碼(二)資料庫指令碼
- 資料庫監控指令碼(三)資料庫指令碼
- 監控資料庫效能的SQL資料庫SQL
- Zabbix監控神通資料庫教程資料庫
- 配置CACTI監控MySQL資料庫狀態(1)準備工作MySql資料庫
- 配置CACTI監控MySQL資料庫狀態(3)配置apache模組MySql資料庫Apache
- shell指令碼:監控MySQL服務是否正常指令碼MySql
- Shell指令碼監控MySQL主從狀態指令碼MySql