如何解决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主从中断延迟以及连接数就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
实例
百分
百分比
主从
延迟
监控
信息
内容
数据
方式
更多
帮助
配置
不错
公众
兴趣
函数
小伙
小伙伴
数据表
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
深圳市豪特网络技术有限公司
服务器端js数据库访问
公交车软件开发
信息安全网络安全的意义
关系数据库三级结构
安全数据库习题答案
网络安全工程师要掌握什么
网络安全宣传标语口号
squad 进不去服务器
荣耀手机服务无法连接服务器
福建工业软件开发零售价格
mesh网络安全产品
合并3个不同别表格数据库
shopex的数据库结构
服务器电源连接
正规网络技术分类标准
药学文献信息数据库
服务器公网IP一定需要域名吗
新浪接收邮件服务器
服务器主机的管理系统
青海联想服务器虚拟化定制
增值税安全接入服务器
网络安全和防范措施论文摘要
应用服务器层
squad 进不去服务器
化工常用基础数据库
网络安全阶段性工作总结
桃源软件开发大专学校
武汉网络安全大专应届生招聘
安全服务器断开连接不上