统计Nginx错误Code数量

    1. #!/bin/sh
    2. #Program function:Nginx's log analysis
    3. resettem=$(tput sgr0)
    4. Logfile_path='/opt/local/nginx/logs/access.log'
    5. Check_http_status()
    6. {
    7. Http_statu_codes=(`cat $Logfile_path|grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}"|awk -F "[ ]+" '{
    8. if($2>=100&&$2<200){i++}
    9. else if($2>=200&&$2<300){j++}
    10. else if($2>=300&&$2<400){k++}
    11. else if($2>=400&&$2<500){l++}
    12. else if($2>=500){m++}
    13. }END{
    14. print i?i:0,j?j:0,k?k:0,l?l:0,m?m:0,i+j+k+l+m
    15. }'`)
    16. echo -e '\E[33m' "The number of http status[100+]:" ${resettem} ${Http_statu_codes[0]}
    17. echo -e '\E[33m' "The number of http status[200+]:" ${resettem} ${Http_statu_codes[1]}
    18. echo -e '\E[33m' "The number of http status[300+]:" ${resettem} ${Http_statu_codes[2]}
    19. echo -e '\E[33m' "The number of http status[400+]:" ${resettem} ${Http_statu_codes[3]}
    20. echo -e '\E[33m' "The number of http status[500+]:" ${resettem} ${Http_statu_codes[4]}
    21. echo -e '\E[33m' "All request number:" ${resettem} ${Http_statu_codes[5]}
    22. }
    23. Check_http_code()
    24. {
    25. Http_Code=(`cat $Logfile_path|grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}"|awk -v total=0 -F '[ ]+' '{
    26. if($2!=""){code[$2]++;total++}
    27. else{exit}
    28. }END{
    29. print code[404]?code[404]:0,code[403]?code[403]:0,code[500]?code[500]:0,total
    30. }'`)
    31. echo -e '\E[33m' "The number of http status[404]:" ${resettem} ${Http_Code[0]}
    32. echo -e '\E[33m' "The number of http status[403]:" ${resettem} ${Http_Code[1]}
    33. echo -e '\E[33m' "The number of http status[500]:" ${resettem} ${Http_Code[2]}
    34. echo -e '\E[33m' "All request number:" ${resettem} ${Http_Code[3]}
    35. }
    36. Check_http_status
    37. Check_http_code