Mail tool to send mysql slow check mail

Mail tool to send mysql slow check mail

Enable slow log query

[root@controller scripts]# grep "slow" /etc/my.cnf
slow_query_log=ON

Set slow query time to 0.1s

mysql> show variables  like '%long_query_time%';
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 0.100000 |
+-----------------+----------+
1 row in set (0.01 sec)

Edit script

[root@controller scripts]# cat mail.sh 
#!/bin/bash
slowfile="/data/mysql/data/controller-slow.log"
tmpslow="/data/mysql/data/tmp.log"
# create tmp-slow
if [ ! -e ${tmpslow}  ];then
        touch ${tmpslow}
else
        >${tmpslow}
fi

# slow-file copy to tmp-slow
if [ -s ${slowfile} ];then
        cat ${slowfile}  > ${tmpslow} 

fi
# send mail
        user="root"
        pass=" "
        host="127.0.0.1"
info() {
    
    
        time_first=`mysql -u$user -p$pass -h$host -e "select now();" |awk 'END{print}'`
        time_second=`mysql -u$user -p$pass -h$host -e "select ADDTIME(sysdate(),'0:10:0');" |awk 'END{print}'`
        StartTime=`grep -E "Time" /data/mysql/data/controller-slow.log | awk '{print $3}'`
        DBname=`mysql -u$user -p$pass -h$host  -e "select database();"|awk 'END{print}' `
        SQLtext=`awk 'END{print}' /data/mysql/data/controller-slow.log`
        repeat=`awk '/Rows_examined/{print $NF}' /data/mysql/data/controller-slow.log`
        QueryTimes=` awk '/Query_time/{print $3}' /data/mysql/data/controller-slow.log`
        HostAdress=`awk -F "[: ]+" '/User@Host/{print $3,$4,$5}' /data/mysql/data/controller-slow.log`
        LockTime=`awk '/Lock_time/{print $5}' /data/mysql/data/controller-slow.log`
        echo "time_range: " $time_first "-" $time_second
        echo -e "\n"
        echo "StartTime: " $StartTime
        echo "DBname: "$DBname
        echo "SQLtext: "$SQLtext
        echo "repeat: "$repeat
        echo "QueryTimes: "$QueryTimes
        echo "HostAdress: "$HostAdress
        echo "LockTime: "$LockTime
}
# 调用函数且发送邮件
if [ -s ${tmpslow} ];then
        main(){
    
    
        info >/data/mysql/data/info.log
        echo "mysql slow log already sending!" | mail -s "mysql slow log" [email protected] < /data/mysql/data/info.log
}
main
fi
if [ -s ${slowfile} ];then
        >${slowfile}
fi

Install mailx software

# yum install mailx -y 

Configure mailx

[root@controller scripts]# tail /etc/mail.rc 

set [email protected]

set smtp=smtp.qq.com                 

set [email protected]

set smtp-auth-password=wdvoujxnowowbgid

set smtp-auth=login

Mail display

Insert picture description here

Guess you like

Origin blog.csdn.net/jiaona_chen123/article/details/105981933