奇峰小筑

留着,不懂过来翻翻

【SHELL】统计Nginx访问日志状态码

#########################################################################
# File Name: check_nginx_server.sh
# Author: Moshiyou
# mail: momo1a@qq.com
#Created Time:Mon 30 May 2016 10:21:52 AM CST
#########################################################################
#!/bin/bash

settrem=$(tput sgr0)

Check_Nginx_Server_Log()
{
    httpCode=(`cat /usr/local/nginx/logs/access.log |  grep -ioE "HTTP/1\.[1|2]\"[[:blank:]][0-9]{3}" | 
    awk -F "[ ]+" '{

        $2!=""?code[$2]++:""
        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,code[404],code[403],code[500]}'`)
    echo -e '\E[32m'"The status number [100+] num:" $settrem ${httpCode[0]}
    echo -e '\E[32m'"The status number [200+] num:" $settrem ${httpCode[1]}
    echo -e '\E[32m'"The status number [300+] num:" $settrem ${httpCode[2]}
    echo -e '\E[32m'"The status number [400+] num:" $settrem ${httpCode[3]}
    echo -e '\E[32m'"The status number [500+] num:" $settrem ${httpCode[4]}
    echo -e '\E[32m'"The status all  num:" $settrem ${httpCode[5]}
    echo -e '\E[32m'"The status 404  num:" $settrem ${httpCode[6]}
    echo -e '\E[32m'"The status 403  num:" $settrem ${httpCode[7]}
    echo -e '\E[32m'"The status 500  num:" $settrem ${httpCode[8]}
}

Check_Nginx_Server_Log

这里写图片描述

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_15974389/article/details/51536688
文章标签: nginx shell server awk
个人分类: 运维
上一篇【C语言】合并,排序两个链表
下一篇【C语言】位的运算(深入理解计算机系统第三版习题2.12)
想对作者说点什么? 我来说一句

nginx状态码

nginx状态码

jiangshan35 jiangshan35

2016-11-18 16:53:56

阅读数:3400

没有更多推荐了,返回首页

关闭
关闭