centos下sh腳本 監聽mysql主從下的從庫狀態, 如果出現錯誤,發告警

實現功能:  centos下腳本監聽mysql主從下的從庫狀態, 如果出現錯誤,發告警

通過centos任務調度進行腳本執行:  crontab -e  => * * * * * /home/mysql/slave.sh

slave.sh

#!/bin/sh
#--------------------------------------------
# 監聽mysql主從下的從庫狀態, 如果出現錯誤,發告警
# author:by zero
#--------------------------------------------
cmd=/usr/bin/mysql
log=/var/log/mysql_slave.log
errlog=/var/log/mysql_err_slave.log
  datetime=`date '+%Y-%m-%d %H:%M:%S'`
  # echo $datetime
  array=$($cmd --defaults-extra-file=/home/mysql/dbconf.cnf -e "show slave status\G"|grep -iE "_Running|Last_error")
  #echo $array
  io_running=`echo $array|grep -i slave_io_running|awk '{print $2}'`
  #echo $io_running
  sql_running=`echo $array|awk -F 'Slave_SQL_Running: ' '{print $2}' | awk '{print $1}'`
  #echo $sql_running
  last_error=`echo "$array"|awk -F "Last_Error" '{print $2}'`
  #echo $last_error
  if [ "$io_running" == "Yes" -a "$sql_running" == "Yes" ]
  then
    echo "$datetime | OK | Slave is running!" >> $log
  else
    echo "$datetime | FAIL | Slave is not running!" >> $errlog
    echo "$datetime | FAIL | $last_error" >> $errlog
    #特定錯誤信息進行自動跳過,
	strB="Could not execute Update_rows event on table webapp.session; Can't find record in 'session', Error_code: 1032;"
	#echo $strB
	if [[ $last_error =~ $strB ]]
	then
	  $cmd --defaults-extra-file=/home/mysql/dbconf.cnf -e "stop slave;set global SQL_SLAVE_SKIP_COUNTER=1;start slave;"
	  echo "$datetime | FAIL | 自動跳過" >> $errlog
	else
	curl "https://www.baidu.com/notify/wxmsg?msg1=數據庫主從同步中止&msg2="$last_error"&msg3=請儘快處理" 2>/dev/null
	  echo 'unkill'
	fi
    break
  fi

dbconf.cnf

[client]
port = 3306
default-character-set = utf8mb4
host = localhost
user = root
password='xxxxxxxxxxxxxx'

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章