Logstash解析Nginx访问日志

发布时间:2020-06-30 05:20:28 作者:rong341233
来源:网络 阅读:1749

Nginx日志格式

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
                        '"$http_host" "$request_time" "$upstream_response_time" $http_device $http_appversion $upstream_addr '
                      '$http_openudid $http_code $http_networkType "$http_deviceModel" "$http_osVersion"';

解析格式为

%{IPORHOST:Client_IP} (%{WORD:ident}|-) (%{USERNAME:auth}|-) \[%{HTTPDATE:timestamp}\] "%{WORD:Http_Method} %{URIPATHPARAM:Http_Request} HTTP/%{NUMBER:Http_Version}" %{NUMBER:Http_Status_Code} (?:%{NUMBER:Http_Bytes}|-) (?:"(?:%{URI:Http_Referrer}|-)"|%{QS:Http_Referrer}) %{QS:User_Agent} "(%{QS:X_Forwarded_For}|-)" "(%{IPORHOST:Site}|-)" "(%{NUMBER:Request_Time}|-)" "(%{NUMBER:Upstream_Response_Time}|-)" (%{WORD:Device}|-) (%{USERNAME:App_Version}|-) (%{HOSTNAME:Upstream_Host}:%{POSINT:Upstram_Port}|-) (%{WORD:Openudid}|-) (%{WORD:Usercode}|-) (%{WORD:NetType}|-) "(%{GREEDYDATA:Device_Name}|-)" "(%{GREEDYDATA:System_Verion}|-)"

logstash配置文件如下:

input {
    redis {
        data_type => "list"
        key => "filebeat:nginx-access"
        host => "redis"
        port => 6379
        db => 0
        # password => "123456"
        # codec => "json"
    }
}
filter {
        grok {
                match => {
                        "message" => '%{IPORHOST:Client_IP} (%{WORD:ident}|-) (%{USERNAME:auth}|-) \[%{HTTPDATE:timestamp}\] "%{WORD:Http_Method} %{URIPATHPARAM:Http_Request} HTTP/%{NUMBER:Http_Version}" %{NUMBER:Http_Status_Code} (?:%{NUMBER:Http_Bytes}|-) (?:"(?:%{URI:Http_Referrer}|-)"|%{QS:Http_Referrer}) %{QS:User_Agent} "(%{QS:X_Forwarded_For}|-)" "(%{IPORHOST:Site}|-)" "(%{NUMBER:Request_Time}|-)" "(%{NUMBER:Upstream_Response_Time}|-)" (%{WORD:Device}|-) (%{USERNAME:App_Version}|-) (%{HOSTNAME:Upstream_Host}:%{POSINT:Upstram_Port}|-) (%{WORD:Openudid}|-) (%{WORD:Usercode}|-) (%{WORD:NetType}|-) "(%{GREEDYDATA:Device_Name}|-)" "(%{GREEDYDATA:System_Verion}|-)"'
                }
        }
        date {
                match => ["timestamp", "dd/MMM/yyyy:HH:mm:ss Z"]
        }
        geoip {
                database => "/usr/share/logstash/geodb/GeoLite2-City.mmdb"
                source => "Client_IP"
                target => "geoip"
                fields => ["country_name","region_name", "city_name", "ip", "longitude", "latitude", "location"]
                add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]
                add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}"  ]
        }
        useragent {
                source => "User_Agent"
                target => "ua"
        }
        useragent {
                source => "User_Agent"
                target => "ua"
        }
        mutate {
                convert => [ "[geoip][coordinates]", "float" ]
                convert => [ "Http_Status_Code", "integer" ]
                convert => [ "Http_Bytes", "integer" ]
                convert => [ "Request_Time", "float" ]
                convert => [ "Upstream_Response_Time", "float" ]
                # split => ["Http_Request", "?"]
                # add_field => { "Http_URI" => "%{Http_Request[0]}" }
                remove_field => [ "message", "beat", "@version", "auth", "prospector", "source", "offset"]
        }
}
output {
        #stdout {codec => rubydebug}
        elasticsearch {
                hosts => ["elasticsearch:9200"]
                index => "nginx-access-%{+YYYY.MM.dd}"
        }
}
推荐阅读:
  1. 如何使用logstash同步nginx日志到数据库
  2. nginx访问日志 logstash 配置文件实例2

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

logstash og st

上一篇:Infortrend GS/Gse统一存储1.38 OS正式发布提供下载更新

下一篇:Android中 字符串-数字 的转化

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》