温馨提示×

温馨提示×

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

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

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

发布时间:2021-10-08 17:12:50 来源:亿速云 阅读:161 作者:柒染 栏目:MySQL数据库

这篇文章给大家介绍如何解决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 status\G" |grep Slave_IO_Running: |awk '{print $2}')	sql_thread=$(mysql -uroot -h$ip -P$port -e"show slave status\G" |grep Slave_SQL_Running: |awk '{print $2}')	slv_delay=$(mysql -uroot -h$ip -P$port -e"show slave status\G" |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主从中断延迟以及连接数就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

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

AI