Mysql主从复制状态监控及微信告警shell脚本实现当mysql主从状态异常时,触发微信告警1. 设置主从监控脚本vi /opt/scripts/mysql/check_repl_status.sh#!/
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
--结束END--
本文标题: MySQL主从复制状态监控及微信告警
本文链接: https://www.lsjlt.com/news/44962.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-26
2024-04-26
2024-04-26
2024-04-26
2024-04-26
2024-04-26
2024-04-24
2024-04-24
2024-04-24
2024-04-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0