数据库
首页 > 数据库> > MySQL 主从状态监控

MySQL 主从状态监控

作者:互联网

#!/bin/bash
USERNAME="monitor"
PASSWORD='123456'
SLAVE_HOST="127.0.0.1"
SLAVE_PORT="3307"
MYSQL="/usr/local/mysql/bin/mysql -u$USERNAME -p$PASSWORD"
SLAVE="$MYSQL -h $SLAVE_HOST -P $SLAVE_PORT"

datetime=`date +"%Y-%m-%d %H:%M:%S"`
#查看复制状态
MySQL_Status=`$SLAVE -e "SHOW SLAVE STATUS\G"  | grep -E "Running|Seconds_Behind_Master"|head -n3 `
#获取状态
Slave_IO_Running=`echo $MySQL_Status | grep Slave_IO_Running |awk '{print $2}'`
Slave_SQL_Running=`echo $MySQL_Status |grep Slave_SQL_Running |awk '{print $4}'`
Seconds_Behind_Master=`echo $MySQL_Status |grep Seconds_Behind_Master |awk '{print $6}'`
#IO和SQL线程监控
if [ "$Slave_IO_Running" != "Yes" -o "$Slave_SQL_Running" != "Yes" ]
then

    BODY="$datetime Slave_IO_Running:$Slave_IO_Running Slave_SQL_Running:$Slave_SQL_Running Seconds_Behind_Master:$Seconds_Behind_Master 外网MySQL slave replication error!"
    echo  $BODY
  else
#延时监控,不精准
   if [ "$Seconds_Behind_Master" -gt "180" ]
    then
      BODY="$datetime Slave_IO_Running:$Slave_IO_Running Slave_SQL_Running:$Slave_SQL_Running Seconds_Behind_Master:$Seconds_Behind_Master 外网MySQL slave replication error!"
      echo  $BODY

    fi

fi

 

标签:Slave,Seconds,SQL,Behind,Running,Master,监控,MySQL,主从
来源: https://www.cnblogs.com/VicLiu/p/14700256.html