Loading

11 监控MySQL主从状态是否异常

#!/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

 

posted @ 2021-04-24 20:17  云起时。  阅读(87)  评论(0编辑  收藏  举报