Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1730913
  • 博文数量: 150
  • 博客积分: 660
  • 博客等级: 上士
  • 技术积分: 2480
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 11:39
文章分类

全部博文(150)

文章存档

2019年(4)

2018年(36)

2017年(53)

2016年(7)

2015年(3)

2014年(3)

2013年(27)

2012年(2)

2011年(1)

2006年(1)

2005年(13)

分类: 系统运维

2017-03-31 23:09:40

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

点击(此处)折叠或打开

  1. # Set smtp server of alert
  2. set bsdcompat
  3. set from=opsbot@example.com
  4. set smtp=smtp.example.com
  5. set smtp-auth-user=opsbot@example.com
  6. set smtp-auth-password="password"
  7. set smtp-auth=login

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

4、代码


点击(此处)折叠或打开

  1. #!/bin/sh
  2. ## configuration
  3. ## nginx error_log
  4. error_log=/var/log/nginx/error.log
  5. temp_file=/tmp/nginx.temp
  6. reciever="user@example.com  user2@example.com"
  7. check_everything() {
  8. if [ ! -f "$error_log" ] ; then
  9. echo "error_log not exist." && exit
  10. fi
  11. if [ ! -f "$temp_file" ] ; then
  12. touch $temp_file
  13. fi
  14. if [ ! -n "$reciever" ] ; then
  15. echo "reviever not exist, please set." && exit
  16. fi
  17. }
  18. ## if $1 not exist,default minutes is from 00:00 to now
  19. get_minutes() {
  20. cur_time=`date +%H:%M:%S`
  21. if [ $1 ] ; then
  22. minute=$1
  23. if [ "$minute" -le "9999" -a "$minute" -ge "1" ] ; then
  24. ago_time=`date -d "$minute minutes ago" +%H:%M:%S`
  25. fi
  26. else
  27. hour_minutes=$(expr `date +%H` \* 60)
  28. minutes=$(expr $hour_minutes + `date +%M`)
  29. ago_time=`date -d "$minutes minutes ago" +%H:%M:%S`
  30. fi
  31. }
  32. get_error_upstream() {
  33. awk '{if(($2>=start_time)&&($2<=end_time)) print $0}' start_time=$ago_time end_time=$cur_time $error_log | \
  34. grep error | \
  35. awk -F, '{print $5}'| \
  36. awk -F':|/' '{if($1 ~ /upstream/)printf "%s:%s %s\n",$5,$6,$7}' |\
  37. awk 'BEGIN{print "host:port service_name error_count"}{host_port[$1]=$1;service[$1]=$2;cnt[$1]+=1} \
  38. END{for(i in host_port) print host_port[i],service[i],cnt[i]}' >$temp_file
  39. }
  40. send_notification() {
  41. line=`wc -l $temp_file |awk '{print $1}'`
  42. if [ $line -gt 1 ] ; then
  43. cat $temp_file |mail -s "Some Service Maybe down,Please check now!" $reciever
  44. fi
  45. }
  46. check_everything && \
  47. get_minutes $@ && \
  48. get_error_upstream &&\
  49. send_notification &&\
  50. cat $temp_file

阅读(2597) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~