自动监控从MySQL同步的脚本 (2011-09-15 16:41) 标签: mysql shell 分类: Shell 脚本设计思路:
1、此脚本应该能适应各种各样不同的内外网环境,即IP不同的环境;
2、让脚本也顺便监控下MySQL是否正常运行;
3、Slave机器的IO和SQL状态都必须为YES,缺一不可,这里用到了多重条件判断-a。
#!/bin/bash #check MySQL_Slave Status #crontab time 00:10
Mail-list=
MysqlUser= MysqlPass=
MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'` MYSQLIP=`ifconfig eth0|grep "inet addr" | awk -F[:" "]+ '{print $4}'` STATUS=$(/usr/local/mysql/bin/mysql -u $MysqlUser -$MysqlPass -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}'`
if [ "$MYSQLPORT" == "3306" ] then echo "mysql is running" else /bin/mail -s "warning!server: $MYSQLIP mysql is down" $Mail-list fi
if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ] then echo "Slave is running!" else echo "####### $date #########" >> /tmp/check_mysql_slave.log echo "Slave is not running!" >> /tmp/check_mysql_slave.log echo "Slave is not running!" #mail -s "warn! $MySQLIP_replicate_error" $Mail-list << /tmp/check_mysql_slave.log echo "`cat /tmp/check_mysql_slave.log`" |mail -s "Warning...slave is not running!!" $Mail-list fi
使用方式:
用crontab设置定期运行,建议每十分钟运行一次 */10 * * * * /root/mysql-slave-status.sh
记得在每台MySQL从机上分配一个jiankong的用户,权限大些也没关系,只限定在本地运行,如下所示:
|