欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

shell脚本监控mysql主从状态

程序员文章站 2022-10-13 22:16:23
此脚本首先判断mysql服务器是否运行,如果正常,继续判断主从,否则发邮件告警,只发一次。判断主从状态是判断io和sql线程是否都为yes,如果不是则发邮件通知,只发一次。...

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

复制代码 代码如下:

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

mysql_status=`netstat -nl | 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 status\g" | 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