• ADADADADAD

    MySQL主从复制状态监控及微信告警[ mysql数据库 ]

    mysql数据库 时间:2024-12-24 19:12:34

    作者:文/会员上传

    简介:

    MySQL主从复制状态监控及微信告警
    shell脚本实现当MySQL主从状态异常时,触发微信告警
    1. 设置主从监控脚本
    vi /opt/scripts/mysql/check_repl_status.sh
    #!/bin/bashhost=''

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    MySQL主从复制状态监控及微信告警


    shell脚本实现当MySQL主从状态异常时,触发微信告警


    1. 设置主从监控脚本


    vi /opt/scripts/mysql/check_repl_status.sh


    #!/bin/bash

    host='' # Slave 机器

    user='' # mysql 帐号(能执行show slave status即可,不需要太大的权限)

    passwd='' # mysql 密码

    _msgBodys()

    {

    local int AppID=0

    local UserID=$1

    local Msg="$2"

    cat << EOF

    {

    "touser": "${UserID}",

    "msgtype": "text",

    "agentid": "${AppID}",

    "text": {

    "content": "${Msg}"

    },

    "safe": "0"

    }

    EOF

    }

    _sendMsgs()

    {

    CropID='' #微信公众号ID

    Secret='' #微信公众号Secret

    GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${CropID}&corpsecret=${Secret}"

    Gtoken=$(/usr/bin/curl -s -G $GURL | grep -Po "(?<=access_token\W{3})[\w-]+")

    PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${Gtoken}"

    /usr/bin/curl -s -k --data-ascii "$(_msgBodys "$1" "$2")" $PURL

    }

    num=`/opt/mysql/bin/mysql -u$user -p$passwd -h$host -e 'show slave status\G' |grep -E "Slave_IO_Running:|Slave_SQL_Running:"|awk '{print $2}'|grep -c Yes`

    #检查主从是否存在

    if [ ! $num ]

    then

    msgs="Conn lost with mysql"

    logtime=`date "+%Y-%m-%d %H:%M:%S"`

    send_msg="XXX MySql Slave连接状态\n${msgs}\n检查时间:${logtime}"


    #发送给所有人

    _sendMsgs "@all" "$send_msg"


    #状态不全为yes

    elif [ $num -lt 2 ]

    then

    msgs=`/opt/mysql/bin/mysql -u$user -p$passwd -h$host -e 'show slave status\G' |grep -E "Master_Host:|Slave_IO_Running:|Slave_SQL_Running:"|sed s/[[:space:]]//g`

    logtime=`date "+%Y-%m-%d %H:%M:%S"`

    send_msg="XXX MySql主从复制状态\n${msgs}\n检查时间:${logtime}"

    #发送给所有人

    _sendMsgs "@all" "$send_msg"

    fi


    2. 设置crontab定时任务

    */5 * * * * /bin/bash /opt/scripts/mysql/check_repl_status.sh &>/dev/null 2>&1


    MySQL主从复制状态监控及微信告警.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载