前提安裝mailx
yum -y install mailx
2.初始值設爲0
echo 0 >/tmp/mysqlerrorNB.log
3.每隔5分鐘檢測mysql日誌
crontab -e
*/5 * * * * sh /scripts/mysqlerr.sh >/dev/null 2>&1
腳本如下:
#!/bin/shell
log=/home/DBdata/DB2.err
#原來error記錄數
numA=`cat /tmp/mysqlerrorNB.log`
#當前error數
numB=`cat $log|grep "ERROR"|wc -l`
#增加的error條目
numC=$(($numB - $numA))
#從日誌中過濾出error日誌
errTotal=/tmp/mysqlerrTotal.log
#對新增的error日誌提取,並上報告警
errIncrease=/tmp/errIncrease.log
if [[ $numC > 0 ]]
then
`cat $log|grep "ERROR" >$errTotal`
tail -n $numC $errTotal >$errIncrease
mail -s "mysql10.0.0.111 error alert!" to [email protected] <$errIncrease #上報告警功能
echo $numB >/tmp/mysqlerrorNB.log
exit 1
else
echo "it's ok" >/dev/null 2>&1
exit 1
fi