监控mysql主从状态的shell脚本

发布时间:2021-01-25编辑:脚本学堂
分享一例监控mysql主从状态的shell脚本,如何用shell脚本实时监测mysql主从同步状态,这个例子大家可以参考下,个人感觉很不错。

不少朋友不知道如何用shell/ target=_blank class=infotextkey>shell脚本监控mysql主从状态,遇异常则邮件警告,这里脚本小编为大家介绍一个例子,一起来看看吧。

此脚本首先判断mysql服务器是否运行,如果正常,继续判断主从,否则发邮件告警,只发一次。
判断主从状态是判断IO和SQL线程是否都为yes,如果不是则发邮件通知,只发一次。

完整代码:
 

复制代码 代码示例:

#!/bin/bash
#define mysql variable
mysql_user="root"
mysql_pass="123456"
email_addr="slave@jb200.com"

mysql_status=`netstat -nl | linuxjishu/13830.html target=_blank class=infotextkey>awk 'NR>2{if ($4 ~ /.*:3306/) {print "Yes";exit 0}}'`
if [ "$mysql_status" == "Yes" ];then
        slave_status=`mysql -u${mysql_user} -p${mysql_pass} -e"show slave statusG" | grep "Running" | awk '{if ($2 != "Yes") {print "No";exit 1}}'`
        if [ "$slave_status" == "No" ];then
                echo "slave is not working!"
                [ ! -f "/tmp/slave" ] && echo "Slave is not working!" | mail -s "Warn!MySQL Slave is not working" ${email_addr}
                touch /tmp/slave
        else
                echo "slave is working."
                [ -f "/tmp/slave" ] && rm -f /tmp/slave
        fi
        [ -f "/tmp/mysql_down" ] && rm -f /tmp/mysql_down
else
        [ ! -f "/tmp/mysql_down" ] && echo "Mysql Server is down!" | mail -s "Warn!MySQL server is down!" ${email_addr}
        touch /tmp/mysql_down
fi