监控nginx错误日志并发邮件报警的shell

作者: siediyer 分类: linux 发布时间: 2021-10-24 01:46

1、nginx 默认的error_log 格式
2、centos7 系统
3、通过设置 /etc/mail.rc 来发报警邮件,设置示例

# Set smtp server of alert
set bsdcompat
set from=opsbot@example.com
set smtp=smtp.example.com
set smtp-auth-user=opsbot@example.com
set smtp-auth-password="password"
set smtp-auth=login

以上可根据实际情况替换邮箱和密码

4、代码

#!/bin/sh
## configuration
## nginx error_log
error_log=/var/log/nginx/error.log
temp_file=/tmp/nginx.temp
reciever="user@example.com  user2@example.com"
check_everything() {
if [ ! -f "$error_log" ] ; then
echo "error_log not exist." && exit
fi
if [ ! -f "$temp_file" ] ; then
touch $temp_file
fi
if [ ! -n "$reciever" ] ; then
echo "reviever not exist, please set." && exit
fi
}
## if $1 not exist,default minutes is from 00:00 to now
get_minutes() {
cur_time=`date +%H:%M:%S`
if [ $1 ] ; then
minute=$1
if [ "$minute" -le "9999" -a "$minute" -ge "1" ] ; then
ago_time=`date -d "$minute minutes ago" +%H:%M:%S`
fi
else
hour_minutes=$(expr `date +%H` \* 60)
minutes=$(expr $hour_minutes + `date +%M`)
ago_time=`date -d "$minutes minutes ago" +%H:%M:%S`
fi
}
get_error_upstream() {
awk '{if(($2>=start_time)&&($2<=end_time)) print $0}' start_time=$ago_time end_time=$cur_time $error_log | \
grep error | \
awk -F, '{print $5}'| \
awk -F':|/' '{if($1 ~ /upstream/)printf "%s:%s %s\n",$5,$6,$7}' |\
awk 'BEGIN{print "host:port service_name error_count"}{host_port[$1]=$1;service[$1]=$2;cnt[$1]+=1} \
END{for(i in host_port) print host_port[i],service[i],cnt[i]}' >$temp_file
}
send_notification() {
line=`wc -l $temp_file |awk '{print $1}'`
if [ $line -gt 1 ] ; then
cat $temp_file |mail -s "Some Service Maybe down,Please check now!" $reciever
fi
}
check_everything && \
get_minutes $@ && \
get_error_upstream &&\
send_notification &&\
cat $temp_file

 

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

Title - Artist
0:00