目标:对Mysql从库进行状态监控,如果从库复制异常就告警
思路:
1、首先查找Mysql从库的4个参数值(show slave status根据这个命令);分别是从库的IO线程、SQL线程、复制延迟时间(这个在以下脚本没用到,可以再写个脚本对其延迟进行告警)、错误码
2、函数Mysql_status判断,对IO线程、SQL线程、错误码三者逻辑与判断
3、如果为假则执行函数Mysql_Errno
4、对于mysql错误码为1158 1159 1008 1007 1062 我们可以进行停止slave,跳过当前复制条数,进行下一条复制
[[email protected] script]# cat mysql_check.sh
#!/bin/bash
#
#2017.5.8
#mysql状态监测告警
. /etc/init.d/functions
Mysql_IO_cmd=`mysql -h 10.222.1.5 -uroot -p123456 -e ‘show slave status\G‘|grep Slave_IO_Running |awk ‘{print $NF}‘`
Mysql_SQL_cmd=`mysql -h 10.222.1.5 -uroot -p123456 -e ‘show slave status\G‘|grep Slave_SQL_Running |awk ‘{print $NF}‘`
Mysql_Be_cmd=`mysql -h 10.222.1.5 -uroot -p123456 -e ‘show slave status\G‘|grep Seconds_Behind_Master |awk ‘{print $NF}‘`
Mysql_Errno_cmd=`mysql -h 10.222.1.5 -uroot -p123456 -e ‘show slave status\G‘|grep Last_SQL_Errno |awk ‘{print $NF}‘`
ErrorNo=(1158 1159 1008 1007 1062)
##判断mysql复制的错误码
Mysql_Errno() {
for sum in $ErrorNo
do
if [ "$Mysql_Errno_cmd" == "$sum" ];then
echo "$(date +%F_%R)错误状态码:$Mysql_Errno_cmd" >> /tmp/mysql_error.log
mysql -h 10.222.1.5 -uroot -p123456 -e ‘stop slave;set global sql_slave_skip_counter=1;start slave;‘
echo `sort -r /tmp/mysql_error.log` |mail -s "MySQL Slave is error" [email protected]
else
echo "$(date +%F_%R)错误状态码:$Mysql_Errno_cmd" >> /tmp/mysql_error.log
#echo `sort -r /tmp/mysql_error.log` |mail -s "MySQL Slave is error" [email protected]
uniq /tmp/mysql_error.log |mail -s "MySQL Slave is error" [email protected]
fi
done
}
##监测mysql状态函数
Mysql_status() {
if [ "$Mysql_IO_cmd" == "Yes" -a "$Mysql_SQL_cmd" == "Yes" -a "$Mysql_Errno_cmd" == "0" ];then
echo
action "mysql status" /bin/true
else
echo
action "mysql status" /bin/false
Mysql_Errno
fi
}
Mysql_status