千家信息网

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

发表于:2024-10-18 作者:千家信息网编辑
千家信息网最后更新 2024年10月18日,这篇文章给大家介绍如何解决Shell监控Mysql主从中断延迟以及连接数,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。#!/bin/bash#日志配置curdate=$(date
千家信息网最后更新 2024年10月18日如何解决Shell监控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        fidone}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        fidone}## 微信公众号发送告警function wechat_send(){local w_url=$1local w_msg=$2echo "starting send msg to "$w_urlcurl "$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主从中断延迟以及连接数就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

0