Linux运维之Shell编程------监控MySQL错误码及主从复制同步异常
Posted cured
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux运维之Shell编程------监控MySQL错误码及主从复制同步异常相关的知识,希望对你有一定的参考价值。
Linux运维之Shell编程
一、监控mysql错误码及主从复制同步异常
题目:监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员。提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:
阶段1:开发一个守护进程脚本每30秒实现检测一次。
阶段2:如果同步出现如下错误号(1158,1159,1008,1007,1062),则跳过错误。
阶段3:请使用数组技术实现上述脚本(获取主从判断及错误号部分)
阶段2:
#!/bin/bash
#
CONTACT=‘[email protected]‘
ERR_LOG=‘/var/log/mariadb/mariadb.log‘
email_to_admin() {
mailsubject="MySQL Error."
mailbody="`date ‘+%F %H:%M:%S‘`: MySQL Error,please check and repair."
echo $mailbody | mail -s "$mailsubject" $CONTACT
}
while true;do
for ERROR_CODE in `grep ‘Error_code: [[:digit:]]+‘ $ERR_LOG | grep -o ‘[[:digit:]]+‘` ;do
case $ERROR_CODE in
1007)
continue
;;
1008)
continue
;;
1158)
continue
;;
1159)
continue
;;
1162)
continue
;;
*) ;;
esac
email_to_admin
done
sleep 30
done
阶段3:
#!/bin/bash
#
DB_USER=‘root‘
DB_PASSWORD=‘jingpei‘
DB_PORT=‘3306‘
MASTER=‘192.168.0.76‘
SLAVE=‘192.168.0.77‘
CMD_CONN_MASTER=‘mysql -u$DB_USER -p$DB_PASSWORD -P$DB_PORT -h$MASTER‘
CMD_CONN_SLAVE=‘mysql -u$DB_USER -p$DB_PASSWORD‘ -P$DB_PORT -h$SLAVE‘
CONTACT=‘[email protected]‘
declare -a master_status
master_status[0]="CMD_CONN_MASTER -e ‘show master statusG‘ | egrep File | cut -d: -f2"
master_status[1]="CMD_CONN_MASTER -e ‘show master statusG‘ | egrep Position | cut -d: -f2"
declare -a slave_status
slave_status[0]="CMD_CONN_SLAVE -e ‘show slave statusG‘ | egrep Master_Log_File | cut -d: -f2"
slave_status[1]="CMD_CONN_SLAVE -e ‘show slave statusG‘ | egrep Read_Master_Log_Pos | cut -d: -f2"
slave_status[2]="CMD_CONN_SLAVE -e ‘show slave statusG‘ | egrep Last_Errno | cut -d: -f2"
email_to_admin() {
mailsubject="Syncing failed."
mailbody="`date ‘+%F %H:%M:%S‘`: Syncing of master-slave is failed,please check and repair."
echo $mailbody | mail -s "$mailsubject" $CONTACT
}
while true ; do
if [ ${master_status[0]} == ${slave_status[0]} -a ${master_status[1]} == ${slave_status[1]} -a ${slave_status[2]} == 0 ]; then
echo "Syncing of master-slave is OK."
else
echo "Syncing of master-slave is failed."
email_to_admin
fi
sleep 30
done
以上是关于Linux运维之Shell编程------监控MySQL错误码及主从复制同步异常的主要内容,如果未能解决你的问题,请参考以下文章
《跟老男孩学Linux运维之shell编程实战》-第五章 shell脚本的条件测试
《跟老男孩学Linux运维之shell编程实战》-第三章 shell变量知识进阶
《跟老男孩学Linux运维之shell编程实战》-第四章 变量的数值计算