#!/bin/sh
ip=eth0 #网卡名称
mysql_binfile=/usr/bin/mysql
mysql_user=root #MySQL数据库账号
mysql_pass=123456 #密码
datetime=`date +"%Y-%m-%d/%H:%M:%S"` #获取当前时间
mysql_slave_logfile=/var/log/mysql/check_mysql_slave.log #日志文件路径,必须提前创建好
slave_ip=`ifconfig $ip|grep "inet " | awk -F[:" "]+ '{print $4}'`
status=$($mysql_binfile -u$mysql_user -p$mysql_pass -e "show slave status\G" | grep -i "running")
#验证方式:Slave_IO_Running=`echo -n "$status" | grep Slave_IO_Running | awk ' {print $1}'` (输出Slave_IO_Running为正常)
Slave_IO_Running=`echo -n "$status" | grep Slave_IO_Running | awk ' {print $2}'`
#验证方式:Slave_IO_Running=`echo -n "$status" | grep Slave_SQL_Running | awk ' {print $1}'` (输出Slave_SQL_Running为正常)
Slave_SQL_Running=`echo -n "$status" | grep Slave_SQL_Running | awk '{print $2}'` #网上有类似的代码,这里的echo应该加-n参数,并且变量应加引号,否则不转义换行符,实际匹配出来的结果是Slave_IO_Running参数的值
if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]
then
echo "Slave is Running!"
echo "$datetime $slave_ip Slave is Running!" >> $mysql_slave_logfile
else
echo "$datetime $slave_ip Slave is not running!"
echo "$datetime $slave_ip Slave is not running!" >> $mysql_slave_logfile
fi