监控主从复制的指标有:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0 (从服务器与主服务器延时多少秒)
# 指定用户
USER=root
# 指定密码
PASS=123456
# 指定主机地址
HOST=localhost
declare -i thread_err=0
declare -i repl_delay=0
# 将 yes yes 0 赋值 给一个arguments数组
arguments=(`mysql -u$USER -h$HOST -p$PASS -e "show slave statusG" | awk -F: '/_Running | _Behind_/{print $NF}'`)
# 从数组中取出对应的值,判断是否复制有错误
for((i=0;i<${#arguments[*]};i++));do
if [ "${arguments[${i}]}" != "Yes" ];then
let thread_err+=1
elif [ "${arguments[${i}]}" != "0" ];then
let repl_delay+=1
else
continue
fi
done
# 对于不同的错误进行不同的处理,例如
if [ $thread_err -gt 0 ];then
echo " IO_THREAD or SQL_THREAD is wrong"
elif [ $repl_delay -gt 0 ];then
echo " replication is delayed"
else
echo " replication is health !"
fi