一个兼容性强的错误日志预警机制

需求:错误日志报警,如果5分钟内出现相同错误已报警,则不继续报警。

实现:为不影响之前系统其他功能,在nginx层面用luajit提取日志,并每分钟生成一个日志。用脚本处理后删除这一分钟的日志。(注:与原先的access.log互不影响)

1. shell脚本:(目前按每分钟进行错误统计报警)

#错误次数,超出报警

limit_time=4
#错误发送时间间隔秒数
time_long=300
#日志时间
lastminute=`date -d "1 minutes ago" "+%Y-%m-%d %H:%M"`
meorder_time="$lastminute:00"
echo $lastminute
log_minute=`echo ${lastminute}|sed "s/ /:/g"`
#echo $idcard_minute
#短信发送路径
SMS_URL=......#此处可修改为自己的发邮件或者发短信接口
#日志文件路径
LOGPATH=/var/log/nginx/accesslogs
#日志文件
LOGNAME_PRE="auth-"${log_minute}"-acc.log"
LOGALL=${LOGPATH}"/"${LOGNAME_PRE}
#LOGNAME_PRE=idcard.log
LOGNAME=/home/cheyunlong/test1.log
#配置文件
CONFIGNAME=/home/cheyunlong/config.properties
#错误码数组
error_codes=("A0001" "A1000" "A1001" "A1002" "A2003" "A1004" "A1005" "A1006" "A1007" "A2000" "A2001" "A2002" "A2003" "A2004" "A2005" "A2006")
#ip数组,暂不用
ips=("10.10.11.222" "10.10.11.224" "10.10.11.15")
#应用名称数组
apps=("/test1/v1" "/test2/v1")
#超时时间数组
timepe=(5000 10000)
split_char=#
find $LOGPATH -type f -name "$LOGNAME_PRE" -exec grep "$lastminute" {} \; > $LOGNAME;
#find $LOGPATH -type f -name "$LOGNAME_PRE" -exec grep "10" {} \; > $LOGNAME;
response_result=""
#根据错误码统计
for (( i=0 ; i<${#error_codes[@]} ; i++ ))
do
# for (( j=0 ; j<${#ips[@]} ; j++))
# do
for(( k=0 ; k<${#apps[@]} ; k++))
do
now_error=${error_codes[$i]}
#now_ip=${ips[$j]}
#echo $now_ip
now_app=${apps[$k]}
#echo $now_app
error0=`cat $LOGNAME|awk -v err=$now_error -v app=$now_app -F "|" 'BEGIN { error=0;}{gsub(/^ *| *$/,"",$7); gsub(/^ *| *$/,$6); if( $7==err && $6==app error++;}END{ print (error); }'`
#echo ${error0}
#response_result="${response_result}${lastminute}|${apps[$k]}|${ips[$j]}|${error_codes[$i]}|${error0}\n" #时间|应用|ip|错误码|错误次数
response_result="${response_result}${lastminute}|${apps[$k]}|${error_codes[$i]}|${error0}${split_char}" #时间|应用|ip|错误码|错误次数
done
# done
done
#echo -e ${response_result}
#超时统计
for(( k=0 ; k<${#apps[@]} ; k++))
do
now_app=${apps[$k]}
for (( t=0 ; t<${#timepe[@]} ; t++ ))
do
#echo $now_app
if [ $(($t+1)) == ${#timepe[@]} ]; then
time_5=`cat $LOGNAME|awk -v app=$now_app -v now_time=${timepe[$t]} -F "|" 'BEGIN { error=0; }{gsub(/^ *| *$/,$3); gsub(/^ *| *$/,$6); if( $3 >= now_time && $6==app) error++;}END{ print (error); }'`
else
nextt=${timepe[$t+1]}
time_5=`cat $LOGNAME|awk -v app=$now_app -v now_time=${timepe[$t]} -v next_time=$nextt -F "|" 'BEGIN { gsub(/^ *| *$/,$6); error=0; }{if( $3 >= now_time && $3 < next_time && $6==app) error++;}END{ print (error); }'`
fi
#echo $time_5
#response_result="${response_result}${lastminute}|${apps[$k]}|${apps[$k]}|${ips[$j]}|overtime${timepe[$t]}|${time_5}\n"
response_result="${response_result}${lastminute}|${apps[$k]}|overtime${timepe[$t]}|${time_5}${split_char}"
done
#返回值非200的情况
not_200=`cat $LOGNAME|awk -v app=$now_app -F "|" 'BEGIN { error=0; }{gsub(/^ *| *$/,$4); gsub(/^ *| *$/,$6); if( $4 != 200 && $6==app) error++;}END{ print (error); }'`
response_result="${response_result}${lastminute}|${apps[$k]}|not200|${not_200}${split_char}"
done
#echo -e ${response_result}
OLD_IFS="$IFS"
IFS="#"
error_count=($response_result)
#请求参数组装分别为:次数,错误码,时间,应用名称
err_times=""
err_codes=""
err_dates=""
err_apps=""
err_num=0
for(( e=0 ; e<${#error_count[@]} ; e++ ))
do
OLD_IFS="$IFS"
IFS="|"
error_now=(${error_count[$e]})
if [[ ${error_now[3]} -ge ${limit_time} ]]; then
time_period=0
#获取配置文件的时间配置
config1=`cat ${CONFIGNAME}|grep "${error_now[1]}|${error_now[2]}"`
#echo "config1:"${config1}
new_config="${error_now[1]}|${error_now[2]}|${error_now[0]}"
if [[ $config1 == '' ]]; then
time_period=1
` echo "${new_config}" >> ${CONFIGNAME}`
else
IFS="|"
config_arr=(${config1})
last_time=`date +%s -d ${config_arr[2]}:00`
now_time=`date +%s -d ${lastminute}:00`
period1=$(($now_time-$last_time))
if [[ $period1 -gt ${time_long} ]]; then
time_period=1
config2="${config_arr[0]}|${config_arr[1]}|${config_arr[2]}"
`sed -i "s#$config1##;/^ *$/d" ${CONFIGNAME}`
`echo "${new_config}" >> ${CONFIGNAME}`

fi
fi
#需要报警
if [[ ${time_period} > 0 ]]; then
if [[ ${err_num} == 0 ]]; then
err_dates=$err_dates${error_now[0]}
err_apps=$err_apps${error_now[1]}
err_codes=$err_codes${error_now[2]}
err_times=$err_times${error_now[3]}
else
err_dates=$err_dates","${error_now[0]}
err_apps=$err_apps","${error_now[1]}
err_codes=$err_codes","${error_now[2]}
err_times=$err_times","${error_now[3]}
fi
err_num=$(($err_num + 1));
fi
fi
done
#echo $err_dates $err_apps $err_codes $err_times
if [[ $err_dates != '' ]]; then
#在此处发送短信
echo "curl:err_dates=${err_dates} -F err_apps=${err_apps} -F err_codes=${err_codes} -F err_times=${err_times}"
curl -F err_dates=${err_dates} -F err_apps=${err_apps} -F err_codes=${err_codes} -F err_times=${err_times} ${SMS_URL}
fi
rm -rf ${LOGALL}

2.设置定时任务

(1). 键入 crontab -e 编辑crontab服务文件 文件内容添加: 日志监控: */1 * * * * nohup bash /home/cheyunlong/deal_log.sh >> /home/cheyunlong/deal.log 2>&1 (2). 保存之后,crontab -l 查看是否创建成功

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...