#!/bin/bash
source /etc/profile

# 主从同步
# master:binlog
# slave:relaylog
# 写->master->binlog<-relaylog->slave

DB_IP="172.16.1.122"
HOST="localhost"
PORT="3306"
USER="root"
PASS="Liuchang@2020"

MYSQL_COMMAND="mysql -h${HOST} -P${PORT} -u${USER} -p${PASS}"
IO_SQL_STATUS="$($MYSQL_COMMAND -e 'show slave status\G;' 2>/dev/null |awk '/Slave_.*_Running:/{print $1$2}')"

for i in $IO_SQL_STATUS; do
THREAD_STATUS_NAME="${i%:*}"
# 取冒号(:)左边的数据
THREAD_STATUS="${i#*:}"
# 取冒号(:)右边的数据
if [ "$THREAD_STATUS" != "Yes" ]; then
echo "Error: MySQL Slave $DB_IP $THREAD_STATUS_NAME Status is $THREAD_STATUS" |mail -s "MySQL Slave $DB_IP $THREAD_STATUS_NAME THREAD Abnormal" 2504164765@qq.com
fi
done

# 加入到crontab
# chmod +x /tools/11MonitorMysqlMasterSlaveStatus.sh
# */1 * * * * /tools/11MonitorMysqlMasterSlaveStatus.sh &>/dev/null