#!/bin/bash
slave='slave1'
#formatDate1=`date "+%Y-%m-%d %H:%M:%S"`
user='root'
password='password'
host='127.0.0.1'
port='9310'
doCount=10
catalog='/home/script/leadLog.txt'
while [ $doCount -ge 1 ]
do
sleep 0.5s
let doCount--
formatDate=`date "+%Y-%m-%d %H:%M:%S"`
#SLAVE_IO_S&SLAVE_SQL_S is YES
thread_status=`mysql -P"$port" -h"$host" -u"$user" -p"$password" -e 'show slave status\G'|grep -i yes|wc -l`
status=(`mysql -P"$port" -h"$host" -u"$user" -p"$password" -e 'show slave status\G'| egrep -i "Master_Log_File|Relay_Master_Log_File|Read_master_log_Pos|Exec_Master_log_pos|Seconds_Behind_Master" |awk -F':' '{print $2}'`)
echo "${formatDate}-${slave}-INFO : SLAVE_IO_S&SLAVE_SQL_S:${thread_status},status:Master_Log_File:${status[0]},Relay_Master_Log_File:${status[1]},Read_master_log_Pos:${status[2]},Exec_Master_log_pos:${status[3]},Seconds_Behind_Master:${status[4]}" >> $catalog
if [[ "$thread_status" != 2 ]]; then
echo "${formatDate}-${slave}-ERROR : SLAVE_IO_S & SLAVE_SQL_S 参数异常请检查!" >> $catalog
fi
if [[ "${status[4]}" -gt "1" ]];then
echo "${formatDate}-${slave}-ERROR :Seconds_Behind_Master=${status[4]},延迟过高,请注意!" >> $catalog
fi
if [[ ${status[0]} == ${status[2]} ]] && [[ ${status[1]} == ${status[3]} ]]; then
echo "${formatDate}-${slave}-INFO : 监听主从参数正常!"
else
echo "${formatDate}-${slave}-ERROR : 从库同步主控LOG日志点位落后,请注意!" >> $catalog
fi
done