慢查询数据超时告警脚本

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

#!/bin/bash
file=/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
#判断导出的数据是否有大于5s
cat "$info" | grep "# Q" | sed 's/\..*$//'| awk '$3>5' | grep Q
if [ $? = 0 ];then
#发送邮件
mail -s "慢查询超时警告 $date" [email protected] < $info
fi
fi

猜你喜欢

转载自blog.csdn.net/qq_34457768/article/details/80738044