如何解决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:330610.10.10.100:3306")#主库列表#MainDB_list=("101.101.101.101:3306101.101.101.100:3306")#2.从元数据信息种获取实例列表Slave_list=$(/usr/bin/mysql-h227.0.0.1-P3306-uroot--default-character-set=utf8cmdb-Ne'selectconcat(slave_ip,":",port)fromcmdb_mysql_infowhereslave_instin();')MainDB_list=$(/usr/bin/mysql-h227.0.0.1-P3306-uroot--default-character-set=utf8cmdb-Ne'selectconcat(slave_ip,":",port)fromcmdb_mysql_infowheremsater_instin();')#RO实例复制状态巡检functionslv_check(){localip_port=($1)localwechat_url=($2)foriin${ip_port[@]}dosend_flag="init"ProcCnt=0ip=$(echo$i|awk-F":"'{print$1}')port=$(echo$i|awk-F":"'{print$2}')io_thread=$(mysql-uroot-h$ip-P$port-e"showslavestatus\G"|grepSlave_IO_Running:|awk'{print$2}')sql_thread=$(mysql-uroot-h$ip-P$port-e"showslavestatus\G"|grepSlave_SQL_Running:|awk'{print$2}')slv_delay=$(mysql-uroot-h$ip-P$port-e"showslavestatus\G"|grepSeconds_Behind_Master:|awk'{print$2}')ProcCnt=$(mysql-uroot-h$ip-P$port-Ne"selectcount(*)frominformation_schema.processlist;")TotalProc=$(mysql-uroot-h$ip-P$port-Ne"showvariableslike'max_connections';"|awk'{print$2}')ProcPct=$(printf"%d"$((ProcCnt*100/TotalProc)))echo$TotalProc$ProcCnt$ProcPctif[!-n"$io_thread"]&&[!-n"$sql_thread"];thenmsg="实例"$ip":"$port":主从配置为空,请及时查看!"elseif[x"$io_thread"==x'Yes']&&[x"$sql_thread"==x'Yes'];thenif[$slv_delay-gt10];thenmsg="实例"$ip":"$port":延迟"$slv_delay"秒"echo$curtime""$msg>>$logfileelsesend_flag="replication_ok"msg="实例"$ip":"$port":replication_ok"echo$curtime""$msg>>$logfilefielsemsg="实例"$ip":"$port",io_thread或者sql_thread断开,请及时查看"echo$curtime""$msg>>$logfilefifiif[$ProcPct-gt75]&&[$ProcPct-le90];thenmsg="实例"$ip":"$port"连接数百分比达到"$ProcPct"%,请注意!"echo$curtime""$msg>>$logfileelif[$ProcPct-gt90];thenmsg="实例"$ip":"$port"连接数百分比达到"$ProcPct"%,请立即查看!"echo$curtime""$msg>>$logfileelsemsg="实例"$ip":"$port"连接数百分比达到"$ProcPct"%,正常"echo$curtime""$msg>>$logfilefidone}functionMasterLoadMon(){localip_port=($1)foriin${ip_port[@]}doip=$(echo$i|awk-F":"'{print$1}')port=$(echo$i|awk-F":"'{print$2}')ProcCnt=$(mysql-uroot-h$ip-P$port-Ne"selectcount(*)frominformation_schema.processlist;")TotalProc=$(mysql-uroot-h$ip-P$port-Ne"showvariableslike'max_connections';"|awk'{print$2}')ProcPct=$(printf"%d"$((ProcCnt*100/TotalProc)))if[$ProcPct-gt75]&&[$ProcPct-le90];thenmsg="实例"$ip":"$port"连接数百分比达到"$ProcPct"%,请注意!"echo$curtime""$msg>>$logfileelif[$ProcPct-gt90];thenmsg="实例"$ip":"$port"连接数百分比达到"$ProcPct"%,请立即查看!"echo$curtime""$msg>>$logfileelsemsg="实例"$ip":"$port"连接数百分比达到"$ProcPct"%,正常"echo$curtime""$msg>>$logfilefidone}##微信公众号发送告警functionwechat_send(){localw_url=$1localw_msg=$2echo"startingsendmsgto"$w_urlcurl"$1"\-H'Content-Type:application/json'\-d'{"message":{"content":"'$w_msg'"}}'}#主函数functionmain(){slv_check"${slave_list[@]}""$prod_url"MasterLoadMon"${MainDB_list[@]}""$prod_url"}##StartRunning...main
关于如何解决Shell监控Mysql主从中断延迟以及连接数就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。