[[email protected] shell]# cat check_mysql_slave.sh
#!/bin/sh
#检测mysql的从机同步情况,若有异常则发出邮件告警
mysql_binfile=/usr/local/mysql/bin/mysql
mysql_user=root
mysql_pass=123456
datetime=`date +"%Y-%m-%d %H:%M:%S"`
mysql_slave_logfile=/data/script/shell/check_mysql_slave.log
status=$($mysql_binfile -u$mysql_user -p$mysql_pass -e "show slave status\G"|grep -i "running")
Slave_IO_Running=`echo $status | grep Slave_IO_Running | awk ‘ {print $2}‘`
Slave_SQL_Running=`echo $status | grep Slave_SQL_Running | awk ‘{print $2}‘`
if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]
then echo "Slave is Running!"
else
echo " $datetime $slave_ip Slave is not running!" >> $mysql_slave_logfile
$mysql_binfile -u$mysql_user -p$mysql_pass -e "SLAVE STOP;"
$mysql_binfile -u$mysql_user -p$mysql_pass -e "SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;"
$mysql_binfile -u$mysql_user -p$mysql_pass -e "SLAVE START;"
$mysql_binfile -u$mysql_user -p$mysql_pass -e "EXIT"
echo "mysql_slave同步出现错误,请及时登入192.168.1.250数据库从机服务器进行检查" | mutt -s "mysql_slave检测" [email protected]
fi
其中发邮件功能需要另行配置
配置方法:MUTI+MSMTP实现
参考资料
原文:http://alwaysyunwei.blog.51cto.com/3224143/1357232