实时抓取主从的同步状态-20201020

#!/bin/bash                                                                                            

# monitor_mysql_slave status                                                                           

export master_ip=192.168.7.206  export slave_ip=192.168.7.207                                          

export cmd=/data/mysql/bin/mysql                                                                       

export mysqluser=root                                                                                  

export mysqlpwd=123456                                                                                 

export log=/data/monitor03.log                                                                         

while true                                                                                             

do                                                                                                     

datetime=`date '+%Y-%m-%d %H:%M:%S'`                                                                   

# echo $datetime                                                                                       

array=$($cmd -h $master_ip -u$mysqluser  -e "show slave status\G"|grep -iE "_running|last_error")      

# echo $array                                                                                          

io_running=`echo  $array|grep -i slave_io_running|awk '{print $2}'`                                    

# echo $io_running                                                                                     

sql_running=`echo   $array|grep -i slave_sql_running|awk '{print $2}'`                                 

# echo $sql_running                                                                                    

last_error=`echo $array|grep -i last_error|awk '{print $2}'`                                           

# echo $last_error                                                                                     

if [ "$io_running" == "Yes" -a "$sql_running" == "Yes" ]                                               

then                                                                                                   

echo "$datetime | 1.Master=>$master_ip,replaction is ok 2.Slave=>$slave_ip,replaction is ok" >> $log        

else                                                                                                   

echo "$datetime | FAIL | Slave is not running!" >> $log                                                

echo "$datetime | FAIL | $last_error" >> $log                                                          

$cmd -u$mysqluser  -e "stop slave;set global sql_slave_skip_counter=1;start slave;exit;"               

char="$datatime $ip MySQL slave is not running"                                                        

break                                                                                                  

fi                                                                                                     

sleep 20                                                                                               

                                                                                                        

array=$($cmd -h $slave_ip -u$mysqluser  -e "show slave status\G"|grep -iE "_running|last_error")       

# echo $array                                                                                          

io_running=`echo  $array|grep -i slave_io_running|awk '{print $2}'`                                    

# echo $io_running                                                                                     

sql_running=`echo   $array|grep -i slave_sql_running|awk '{print $2}'`                                 

# echo $sql_running                                                                                    

last_error=`echo $array|grep -i last_error|awk '{print $2}'`                                           

# echo $last_error                                                                                     

if [ "$io_running" == "Yes" -a "$sql_running" == "Yes" ]                                               

then                                                                                                   

echo "$datetime | 1.Master=>$slave_ip,replaction is ok 2.Slave=>$master_ip,replaction is ok" >> $log        

else                                                                                                   

echo "$datetime | FAIL | Slave is not running!" >> $log                                                

echo "$datetime | FAIL | $last_error" >> $log                                                          

$cmd -u$mysqluser  -e "stop slave;set global sql_slave_skip_counter=1;start slave;exit;"               

char="$datatime $ip MySQL slave is not running"                                                        

break                                                                                                  

fi                                                                                                     

sleep 20                                                                                               

done   

创建时间:2021-12-22 09:24
浏览量:0