#!/bin/sh
#date:2015-12-07
#filename:Check_MySQL-rep.sh
#作者:linuxzkq

#Email:1729294227@qq.com
#version:v1.0


Mysql_cmd="mysql -uroot -poldboy"
Error_num=(1158 1159 1008 1007 1062)

while true
do
    rep=($($Mysql_cmd -e "show slave status\G;"|egrep "_Running|Behind_Master|SQL_Errno"|awk '{print $NF}'))      
    if [  "${rep[0]}"  ==  "Yes"  -a  "${rep[1]}"  ==  "Yes"  -a  "${rep[2]}"  ==  "0"  ]
         then
            echo "MySQL  slave  rep  is OK"
    else
            for ((i=0;i<${#Error_num[*]};i++))
            do
                if [  "${rep[3]}"  ==  "${Error_num[$i]}"  ]
                   then
                       $Mysql_cmd -e "stop slave;set global sql_slave_skip_counter=1;start slave;"
                fi
            done
            char="Warnning,MySQL slave rep is not OK!"
            echo "$char"
            echo "$char"|mail -s "$char" 1729294227@qq.com
    fi              
      sleep 5
done