MySQL 主从同步监控脚本例子一个
shell脚本,注意修改下面mysql数据库密码,还有如果需要发送报警邮件,需要服务器配置好外发邮件,也就是说用mail命令可以外发邮件,如果没有外发邮件需要请注释掉带有 [email protected]字样的语句。脚本要给于执行权限 chomd +x xxx.sh ,可以添加到crontab里自动间隔运行,日志位置在/data目录下。
#!/bin/bash
#check MySQL_Slave Status
#crontab time 00:10
MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $4}'`
MYSQLIP=`ifconfig eth0|grep "inet addr" | awk -F[:" "]+ '{print $4}'`
STATUS=$(mysql -u root -ppassword -e "show slave status\G" | grep -i "running")
IO_env=`echo $STATUS | grep IO | awk ' {print $2}'`
SQL_env=`echo $STATUS | grep SQL | awk '{print $2}'`
DATA=`date +"%y-%m-%d %H:%M:%S"`
if [ "$MYSQLPORT" == "3306" ]
then
echo "mysql is running"
else
mail -s "warn!server: $MYSQLIP mysql is down" [email protected]
fi
if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ]
then
echo "Slave is running!"
else
echo "####### $DATA #########">> /data/check_mysql_slave.log
echo "Slave is not running!" >> /data/check_mysql_slave.log
echo "Slave is not running!" | mail -s "warn! $MYSQLIP MySQL Slave is not running" [email protected]
fi