#!/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 statusG"|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 | 'Master is:' | $master_ip | replaction is ok | 'Slave is:' | $slave_ip | slave is running!" >> $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"
echo "$last_error"|mail -s "$char" 1******5@163.com
break
fi
sleep 20
array=$($cmd -h $slave_ip -u$mysqluser -e "show slave statusG"|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 | 'Master is:' | $slave_ip | replaction is ok | 'Slave is:' | $master_ip | slave is running!" >> $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"
echo "$last_error"|mail -s "$char" 1******5@163.com
break
fi
sleep 20
done