加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

如何解决Shell监控Mysql主从中断延缓以及连接数

发布时间:2022-01-13 14:23:39 所属栏目:MySql教程 来源:互联网
导读:这篇文章给大家介绍如何解决Shell监控Mysql主从中断延迟以及连接数,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。 #!/bin/bash #日志配置 curdate=$(date +%Y%m%d) curtime=$(date +%Y-%m-%d %H:%M:%S) logname=repl_check_$curda
     这篇文章给大家介绍如何解决Shell监控Mysql主从中断延迟以及连接数,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
 
#!/bin/bash
#日志配置
curdate=$(date +"%Y%m%d")
curtime=$(date +'%Y-%m-%d %H:%M:%S')
logname="repl_check_"$curdate".log"
logfile=/mysqldata/repl_check_log/$logname
## 两种方式获取需要检测从库主从关系,推荐第二种方式(将DB信息存入元数据表,方便管理)
# 1. 从库实例巡检列表,硬编码列表
#Slave_list=("10.10.10.10:3306 10.10.10.100:3306")
# 主库列表
#MainDB_list=("101.101.101.101:3306 101.101.101.100:3306")
# 2. 从元数据信息种获取实例列表
Slave_list=$(/usr/bin/mysql -h227.0.0.1 -P3306 -uroot  --default-character-set=utf8 cmdb -Ne'select concat(slave_ip,":",port) from cmdb_mysql_info where slave_inst in ();')
MainDB_list=$(/usr/bin/mysql -h227.0.0.1 -P3306 -uroot  --default-character-set=utf8 cmdb -Ne'select concat(slave_ip,":",port) from cmdb_mysql_info where msater_inst in ();')
# RO实例复制状态巡检
function slv_check() {
local ip_port=($1)
local wechat_url=($2)
for i in ${ip_port[@]}
do
send_flag="init"
ProcCnt=0
ip=$(echo $i|awk -F":" '{print $1}')
port=$(echo $i|awk -F":" '{print $2}')
io_thread=$(mysql -uroot -h$ip -P$port -e"show slave statusG" |grep Slave_IO_Running: |awk '{print $2}')
sql_thread=$(mysql -uroot -h$ip -P$port -e"show slave statusG" |grep Slave_SQL_Running: |awk '{print $2}')
slv_delay=$(mysql -uroot -h$ip -P$port -e"show slave statusG" |grep Seconds_Behind_Master: |awk '{print $2}')
ProcCnt=$(mysql -uroot -h$ip -P$port -Ne"select count(*) from information_schema.processlist;")
    TotalProc=$(mysql -uroot -h$ip -P$port -Ne"show variables like 'max_connections';" |awk '{print $2}')
    ProcPct=$(printf "%d" $((ProcCnt*100/TotalProc)))
    echo $TotalProc $ProcCnt $ProcPct
if [ ! -n "$io_thread" ] && [ ! -n "$sql_thread" ];then
msg="实例"$ip":"$port":主从配置为空,请及时查看!"
else
if [ x"$io_thread" == x'Yes' ] && [ x"$sql_thread" == x'Yes' ];then
if [ $slv_delay -gt 10 ];then
msg="实例"$ip":"$port":延迟"$slv_delay"秒"
echo $curtime" "$msg >>$logfile
else
send_flag="replication_ok"
msg="实例"$ip":"$port":replication_ok"
echo $curtime" "$msg >>$logfile
fi
else
msg="实例"$ip":"$port",io_thread或者sql_thread断开,请及时查看"
echo $curtime" "$msg >>$logfile
fi
fi
if [ $ProcPct -gt 75 ]  && [ $ProcPct -le 90 ];then
msg="实例"$ip":"$port"连接数百分比达到"$ProcPct"%,请注意!"
echo $curtime" "$msg >>$logfile
elif [ $ProcPct -gt 90 ];then
msg="实例"$ip":"$port"连接数百分比达到"$ProcPct"%,请立即查看!"
echo $curtime" "$msg >>$logfile
else
msg="实例"$ip":"$port"连接数百分比达到"$ProcPct"%,正常"
echo $curtime" "$msg >>$logfile
fi
done
}
function MasterLoadMon() {
local ip_port=($1)
for i in ${ip_port[@]}
do
ip=$(echo $i|awk -F":" '{print $1}')
port=$(echo $i|awk -F":" '{print $2}')
ProcCnt=$(mysql -uroot -h$ip -P$port -Ne"select count(*) from information_schema.processlist;")
TotalProc=$(mysql -uroot -h$ip -P$port -Ne"show variables like 'max_connections';" |awk '{print $2}')
ProcPct=$(printf "%d" $((ProcCnt*100/TotalProc)))
 if [ $ProcPct -gt 75 ]  && [ $ProcPct -le 90 ];then
msg="实例"$ip":"$port"连接数百分比达到"$ProcPct"%,请注意!"
echo $curtime" "$msg >>$logfile
elif [ $ProcPct -gt 90 ];then
msg="实例"$ip":"$port"连接数百分比达到"$ProcPct"%,请立即查看!"
echo $curtime" "$msg >>$logfile
else
msg="实例"$ip":"$port"连接数百分比达到"$ProcPct"%,正常"
echo $curtime" "$msg >>$logfile
fi
done
}
## 微信公众号发送告警
function wechat_send(){
local w_url=$1
local w_msg=$2
echo "starting send msg to "$w_url
curl "$1"
-H 'Content-Type: application/json'
-d '
{
"message": {
"content": "'$w_msg'"
}
}'
}
# 主函数
function main(){
slv_check "${slave_list[@]}" "$prod_url"
MasterLoadMon "${MainDB_list[@]}" "$prod_url"
}
## Start Running...
main
关于如何解决Shell监控Mysql主从中断延迟以及连接数就分享到这里了,希望以上内容可以对大家有一定的帮助。

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!