一个mysql主从监控的脚本,供大家学习参考。
#!/bin/bash
#mysql check by lks
Date=`date +'%y-%m-%d %H:%M:%S'`
Port=3306
Master_IP=192.168.4.11
Slave_IP='192.168.4.15 192.168.4.18'
Mysql_bin=/usr/local/mysql/bin/
Mysql_User='a12333a_li'
Mysql_Passwd='123456789'
Master_Command='show databases'
Slave_Command='show slave statusG'
Check_Mysql_log=/var/mysql.log
Sendmail_log=/tmp/mysql.log
ERROR=0
:>$Sendmail_log
#Master
echo "$Master_Command" |"$Mysql_bin"/mysql -h "$Master_IP" -u"$Mysql_User" -p"$Mysql_Passwd" -P"$Port"
if [ $? != 0 ];then
ERROR=1
echo ""$Date" mysql "$Master_IP" error" >>"$Check_Mysql_log" >>"$Sendmail_log"
fi
#slave
for i in $Slave_IP
do
IO_status=`echo "$Slave_Command" | "$Mysql_bin"/mysql -h "$i" -u"$Mysql_User" -p"$Mysql_Passwd" -P"$Port" |grep Slave_IO_Running | awk '{print $2}'`
SQL_status=`echo "$Slave_Command" | "$Mysql_bin"/mysql -h "$i" -u"$Mysql_User" -p"$Mysql_Passwd" -P"$Port" |grep Slave_SQL_Running | awk '{print $2}'`
if [ "$IO_status" != "Yes" -o "$SQL_status" != "Yes" ];then
ERROR=1
echo ""$Date" mysql "$i" error" >>"$Check_Mysql_log" >>"$Sendmail_log"
fi
done
#check ERROR
if [ "$ERROR" == 1 ];then
/usr/local/mutt/bin/mutt -s "mysql error" 28145***@qq.com <"$Sendmail_log" //邮件发送,根据自己的环境来发送
fi
关于邮件发送的例子可以参考这篇文章 shell脚本发送邮件的例子 。
另外,这里有篇mysql主从健康状态监控的文章,大家可以参考看看。
您可能感兴趣的文章:
修复MySQL主从同步的shell脚本
自动配置mysql主从的shell脚本
监控mysql主从健康状态的shell脚本