j1.获取nginx 日志访问状态码;

(1).脚本实例;

#/bin/bash

resettem=$(tput sgr0)
Logfile_path='/var/log/nginx/access.log'
#grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" nginx_app.api.zhaoyifen.ssl.log
check_status_codes(){
http_status_codes=(`cat $Logfile_path |grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" |awk -F"[ ]+" '{
                            if($2>=100&&$2<200)
                                      {i++} 
                            else if($2>=200&&$2<300)
                                      {j++} 
                            else if($2>=300&&$2<400)
                                      {k++} 
                            else if($2>=400&&$2<500)
                                      {n++} 
                            else if($2>=500)
                                    {p++}
                            }END{
                               print i?i:0,j?j:0,k?k:0,n?n:0,p?p:0,i+j+k+n+p
                              }'
                          `)
  echo -e '\E[34m'"The number of http status [100+]:" ${resettem} ${http_status_codes[0]}
  echo -e '\E[34m'"The number of http status [200+]:" ${resettem} ${http_status_codes[1]}
  echo -e '\E[34m'"The number of http status [300+]:" ${resettem} ${http_status_codes[2]}
  echo -e '\E[34m'"The number of http status [400+]:" ${resettem} ${http_status_codes[3]}
  echo -e '\E[34m'"The number of http status [500+]:" ${resettem} ${http_status_codes[4]}
  echo -e '\E[34m'"ALL request numbers:"  ${resettem} ${http_status_codes[5]}
          }
check_status_codes


(2).脚本执行系统消耗时长;


shell 脚本分析nginx 访问日志状态码_第1张图片




(3).简单优化实例;

#/bin/bash
resettem=$(tput sgr0)
Logfile_path='/var/log/nginx/access.log'
#grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" nginx_app.api.zhaoyifen.ssl.log
check_status_codes(){
http_status_codes=(`grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" $Logfile_path |awk -F"[ ]+" '{
                            if($2>=100&&$2<200)
                                      {i++} 
                            else if($2>=200&&$2<300)
                                      {j++} 
                            else if($2>=300&&$2<400)
                                      {k++} 
                            else if($2>=400&&$2<500)
                                      {n++} 
                            else if($2>=500)
                                    {p++}
                            }END{
                               print i?i:0,j?j:0,k?k:0,n?n:0,p?p:0,i+j+k+n+p
                              }'
                          `)
  echo -e '\E[33m'"The number of http status [100+]:" ${resettem} ${http_status_codes[0]}
  echo -e '\E[33m'"The number of http status [200+]:" ${resettem} ${http_status_codes[1]}
  echo -e '\E[33m'"The number of http status [300+]:" ${resettem} ${http_status_codes[2]}
  echo -e '\E[33m'"The number of http status [400+]:" ${resettem} ${http_status_codes[3]}
  echo -e '\E[33m'"The number of http status [500+]:" ${resettem} ${http_status_codes[4]}
  echo -e '\E[33m'"ALL request numbers:"  ${resettem} ${http_status_codes[5]}
          }
check_status_codes


(4).脚本执行系统消耗时长;

shell 脚本分析nginx 访问日志状态码_第2张图片




2.shell 获取 nginx 日志 403 和 404 状态码;

(1).脚本实例;

Check_Http_Code(){
 Http_Code=(`grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" $Logfile_path|awk -v total=0 -F '[ ]+' '
 {                                                                  
       if ($2!="")
        {code[$2]++;total++}                        
      else  
       {exit}                     
     }END
      {     
         print code[404]?code[404]:0,code[403]?code[403]:0,total}'            
      `)  
                               
     echo -e '\E[33m'"The number of http status [404]:" ${resettem} ${Http_Code[0]}     
     echo -e '\E[33m'"The number of http status [403+]:" ${resettem} ${Http_Code[1]}     
     echo -e '\E[33m'"The ALL request numbers:" ${resettem} ${Http_Code[2]}
          
     }
Check_Http_Code


(2).脚本执行系统消耗时长;

shell 脚本分析nginx 访问日志状态码_第3张图片