温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-03 21:44:15 来源:网络 阅读:462 作者:易语随风去 栏目:MySQL数据库

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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI