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'

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值