您的位置:首页 > 数据库 > SQL

监控mysql主从一致性shell脚本

2016-04-13 13:55 696 查看
#监测主从一致性 #该脚本只检查io进程和sql进程是否都为yes,sql进程是否出现延迟,当延迟时间大于1分钟,会发邮件报警。并没有检查slave上io读取的主二进制日志文件与位置是否和主的完全一致(因为如果io进程为yes的话,很少会不一致),且position位置变化很快,#远程连接主库和从库进行比较的功夫,说不定position已经变化了。port='3306'user='root'password1='……'HostGroup1=('10.192.200.100' '10.192.200.101')source='……'target='……'MailUser='……'MailPassword='……'date=`date +%y%m%d-%H:%M:`echo $datefor host1 in ${HostGroup1[@]}dotitle1=$host1' slave problem alert'v1=$(/usr/local/mysql/bin/mysql --host=$host1 --port=$port --user=$user --password=$password1 -e "show slave status\G"| awk '/Slave_IO_Running/' | awk -F ":" '{print $2}')v2=$(/usr/local/mysql/bin/mysql --host=$host1 --port=$port --user=$user --password=$password1 -e "show slave status\G"| awk '/Slave_SQL_Running/' | awk -F ":" '{print $2}')v3=$(/usr/local/mysql/bin/mysql --host=$host1 --port=$port --user=$user --password=$password1 -e "show slave status\G"| awk '/Seconds_Behind_Master/' | awk -F ":" '{print $2}')if [ "$v1" = "" ]thenecho 'the username or password is wrong,or the mysql server is down,so we can not get value'content4='the username or password is wrong,or the mysql server is down,so we can not get value'/usr/local/bin/sendEmail -f $source -t $target -s smtp.chinaunicom.cn -u $title1 -xu $MailUser -xp $MailPassword -m $content4elseif [ $v1 = 'Yes' ]thenif [ $v2 = 'Yes' ]then#判断sql进程是否出现延迟if [ $v3 != 0 ]thenif [ $v3 -ge 60 ]thencontent3=$host1' the status of io process and sql process is yes,but slave delayed '$v3' seconds,more than 1 minutes'echo $content3/usr/local/bin/sendEmail -f $source -t $target -s smtp.chinaunicom.cn -u $title1 -xu $MailUser -xp $MailPassword -m $content3elseecho $host1' the status of io process and sql process is yes,but slave delayed '$v3' seconds,less than 1 minutes'fielseecho 'There is no problem'fielsecontent2=$host1' Slave_IO_Running status:'$v1',Slave_SQL_Running status:'$v2',please deal with it as soon as possible!'echo $content2/usr/local/bin/sendEmail -f $source -t $target -s smtp.chinaunicom.cn -u $title1 -xu $MailUser -xp $MailPassword -m $content2fielsecontent1=$host1' Slave_IO_Running status:'$v1',Slave_SQL_Running status:'$v2',please deal with it as soon as possible!'echo $content1/usr/local/bin/sendEmail -f $source -t $target -s smtp.chinaunicom.cn -u $title1 -xu $MailUser -xp $MailPassword -m $content1fifidonefor host2 in ${HostGroup2[@]}dotitle2=$host2' slave problem alert'v11=$(/usr/local/mysql/bin/mysql --host=$host2 --port=$port --user=$user --password=$password2 -e "show slave status\G"| awk '/Slave_IO_Running/' | awk -F ":" '{print $2}')v22=$(/usr/local/mysql/bin/mysql --host=$host2 --port=$port --user=$user --password=$password2 -e "show slave status\G"| awk '/Slave_SQL_Running/' | awk -F ":" '{print $2}')v33=$(/usr/local/mysql/bin/mysql --host=$host2 --port=$port --user=$user --password=$password2 -e "show slave status\G"| awk '/Seconds_Behind_Master/' | awk -F ":" '{print $2}')echo $v11if [ "$v11" = "" ]thenecho 'the username or password is wrong,or the mysql server is down,so we can not get value'content44='the username or password is wrong,or the mysql server is down,so we can not get value'/usr/local/bin/sendEmail -f $source -t $target -s smtp.chinaunicom.cn -u $title2 -xu $MailUser -xp $MailPassword -m $content44elseif [ $v11 = 'Yes' ]thenif [ $v22 = 'Yes' ]then#判断sql进程是否出现延迟if [ $v33 != 0 ]thenif [ $v33 -ge 60 ]thencontent33=$host2' the status of io process and sql process is yes,but slave delayed '$v33' seconds,more than 1 minutes'echo $content33/usr/local/bin/sendEmail -f $source -t $target -s smtp.chinaunicom.cn -u $title2 -xu $MailUser -xp $MailPassword -m $content33elseecho $host2' the status of io process and sql process is yes,but slave delayed '$v33' seconds,less than 1 minutes'fielseecho 'There is no problem'fielsecontent22=$host2' Slave_IO_Running status:'$v11',Slave_SQL_Running status:'$v22',please deal with it as soon as possible!'echo $content22/usr/local/bin/sendEmail -f $source -t $target -s smtp.chinaunicom.cn -u $title2 -xu $MailUser -xp $MailPassword -m $content22fielsecontent11=$host2' Slave_IO_Running status:'$v11',Slave_SQL_Running status:'$v22',please deal with it as soon as possible!'echo $content11/usr/local/bin/sendEmail -f $source -t $target -s smtp.chinaunicom.cn -u $title2 -xu $MailUser -xp $MailPassword -m $content11fifidone

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: