Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1760349
  • 博文数量: 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-11-17 11:18:54

定时统计每天nginx error的脚本

目的

了解每天nginx errorlog里面出现的错误类型和次数,和zabbix的报警核对,确保报警无漏报或者误报

执行方式

crontab定时执行

0 10 * * * /root/total_nginx_error.sh

脚本介绍

脚本内容

cat total_nginx_error.sh
#!/bin/sh
error_log=/var/log/nginx/error.log-`date +%Y%m%d`
reciever="somename@likeyou.com"
key1="upstream prematurely closed connection while reading upstream"
get_error_upstream() {
  grep error $error_log |\
    awk -F, '{print $1}'  |\
    awk -F: '{print $4,$5}' |\
    awk '{for(i=2;i<=NF;i++){if(i==NF)printf "%s%1c\n" ,$i," " ;else printf "%s%1c" ,$i, " " }}' |\
    awk '{name[$0]=$0;cnt[$0]++}END{for(i in name)print cnt[i],name[i]}' |sort -nr -k 1
}

send_notification() {

  get_error_upstream  |mail -s "Nginx errors checklist `date +%Y%m%d`" $reciever

}
send_notification

发邮件需要安装的软件及配置

需要安装sendmail

安装略

需要有可以发邮件的邮箱,并在/etc/mail.rc 作如下类似配置

# Set smtp server of  alert
set bsdcompat
set from=opsbot@likeyou.com             #修改为自己的邮箱账户

set smtp="smtp.likeyou.com"            #修改为自己的邮箱账户
set smtp-auth-user=somename@likeyou.com  #修改为自己的邮箱账户
set smtp-auth-password="somewords"

set smtp-auth=login

然后每天可以收到错误统计,通过分析帮助改进运维监控

发送邮件内容示例

53 recv() failed (104 Connection reset by peer) while proxying connection 
31 directory index of "/goluk/teData/" is forbidden 
14 upstream prematurely closed connection while reading upstream 
3 open() "/usr/share/nginx/html/robots.txt" failed (2 No such file or directory) 
3 open() "/usr/share/nginx/html/cache/global/img/gs.gif" failed (2 No such file or directory) 
2 open() "/goluk/teData/favicon.ico" failed (2 No such file or directory) 
1 "/usr/share/nginx/html/PMA2005/index.html" is not found (2 No such file or directory) 
1 upstream prematurely closed connection while reading response header from upstream 
1 open() "/usr/share/nginx/html/webadmin/scripts/setup.php" failed (2 No such file or directory) 
1 open() "/usr/share/nginx/html/testproxy.php" failed (2 No such file or directory) 
1 open() "/usr/share/nginx/html/proxy.html" failed (2 No such file or directory) 
1 open() "/usr/share/nginx/html/p/m/a/scripts/setup.php" failed (2 No such file or directory) 
1 open() "/usr/share/nginx/html/phpmyadmin/scripts/setup.php" failed (2 No such file or directory) 
1 open() "/usr/share/nginx/html/phpMyAdmin-2.10.0/scripts/setup.php" failed (2 No such file or directory) 
1 open() "/usr/share/nginx/html/phpmanager/scripts/setup.php" failed (2 No such file or directory) 
1 open() "/usr/share/nginx/html/mysqladmin/scripts/setup.php" failed (2 No such file or directory) 
1 open() "/usr/share/nginx/html/mysql-admin/scripts/setup.php" failed (2 No such file or directory) 
1 open() "/usr/share/nginx/html/myAdmin/scripts/setup.php" failed (2 No such file or directory) 
1 open() "/usr/share/nginx/html/muieblackcat" failed (2 No such file or directory) 
1 open() "/usr/share/nginx/html/manager/html" failed (2 No such file or directory) 
1 open() "/usr/share/nginx/html/login.action" failed (2 No such file or directory) 
1 open() "/usr/share/nginx/html/dbadmin/scripts/setup.php" failed (2 No such file or directory) 
1 connect() failed (111 Connection refused) while connecting to upstream 
阅读(2156) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~