原理、腳本都很簡單,就是截取同步狀態的Slave_IO_Running和Slave_SQL_Running兩個值是否都爲Yes。若是其中不是,就發短信報警。mysql
- [root@mysqlb script]# cat Check_Mysql_Slave_Replication.sh
- #!/bin/sh
- . /etc/profile
- db_user=reed
- db_paasswd=reed
- LogPath=/root/script/Check_Mysql_Slave_Replication_Log
- [ ! -d $LogPath ] && mkdir -p $LogPath
- cd $LogPath
- Slave_IO_Running=$(/usr/local/mysql/bin/mysql -u$db_user -p$db_paasswd -e"show slave status \G"|grep 'Slave_IO_Running'|cut -d':' -f 2|cut -d' ' -f 2)
- Slave_SQL_Running=$(//usr/local/mysql/bin/mysql -u$db_user -p$db_paasswd -e"show slave status \G"|grep 'Slave_SQL_Running'|cut -d':' -f 2|cut -d' ' -f 2)
- FUNC()
- {
- echo "[INFO]$(date +'%F %T') begin to monitor mysql replication..."
- if [ "$Slave_IO_Running" == "Yes" ] && [ "$Slave_SQL_Running" == "Yes" ];then
- echo "[INFO]$(date +'%F %T') mysql replication is ok!"
- else
- echo "[ERROR]$(date +'%F %T') fuck!mysql24 replication is failed..."
- #調用本身寫的發短信腳本
- /root/script/info_takeok.sh "fuck!mysql24 replication is failed"
- fi
- }
- FUNC >>Check_Mysql_Slave_Replication$(date +'%F').log
最後寫到crontab裏面,一分鐘檢測一次sql