当前位置: 代码迷 >> 综合 >> 慢查询数据超时告警脚本
  详细解决方案

慢查询数据超时告警脚本

热度:52   发布时间:2023-09-27 11:04:04.0

  基本逻辑为,每日跑一次该脚本,若前一天有超过5秒的慢查询记录,则发送邮件告警。

 

#!/bin/bashfile=/usr/local/mysql/data/slow.log
info=/slowquery/info
date=`date +%Y_%m_%d`
date_day_1=`date -d '1 days ago' '+%y%m%d'`#判断一天前是否有慢查询数据
cat $file |grep "Time: $date_day_1"
if [ $? = 0 ];then#获取前一天慢查询数据开始行数sl=`grep -n "$date_day_1" $file | cut -d: -f1 | head -1`#获取前一天慢查询数据结束行数el=`wc -l $file | cut -d' ' -f1`#前一天慢查询数据导出sed -n "$sl,$el p" $file > $info#判断导出的数据是否有大于5scat "$info" | grep "# Q" | sed 's/\..*$//'| awk '$3>5' | grep Qif [ $? = 0 ];then#发送邮件mail -s "慢查询超时警告 $date" XX@qq.com < $infofi
fi

 

 

 

 

  相关解决方案