elk 入門 - 分析nginx日誌 + json格式

1.本次採用的一臺主機,將全部的軟件安裝一臺上進行測試工做。html

2.安裝部署:http://www.javashuo.com/article/p-knpcxoit-ce.htmlnode

3.簡單調試nginx

輸出rubydebug
input{
    file {
            path => "/usr/local/log_test/*/*/*.log"
        start_position => "beginning"
    }   
}

output {
elasticsearch {
hosts => ["10.0.0.92:9200"]
index => "myre-%{+YYY.MM.dd}"
}
stdout {codec => rubydebug}
}json

--------------------- 

原文:https://blog.csdn.net/yelllowcong/article/details/80847425 

 4.nginx日誌格式centos

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for" $request_time';

5.filter的定義 參考:http://www.javashuo.com/article/p-edppwlwm-dp.htmlruby

filter {
    grok {
        match => { "message" => "%{IPORHOST:remote_ip} - %{DATA:user_name} \[%{HTTPDATE:time}\] \"%{WORD:method} %{DATA:url} HTTP/%{NUMBER:http_version}\" %{NUMBER:response_code} %{NUMBER:body_sent:bytes} \"%{DATA:referrer}\" \"%{DATA:agent}\" \"%{DATA:x_forwarded_for}\" %{NUMBER:request_time}" }
#        remove_field => "message"
    }
}

 ########################################################app

1.目標定義好nginx的日誌格式爲json格式。分割成多份。demo:統計11:00 - 11:50的404status狀態的訪問elasticsearch

2.nginx的日誌格式測試

[root@rbtnode1 nginx]# cat /usr/local/nginx/conf/nginx.conf|egrep -v '#|^$'
。。。
http {
include mime.types;
default_type application/octet-stream;
log_format json '{ "@timestamp": "$time_iso8601", '
'"time": "$time_iso8601", '
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"body_bytes_sent": "$body_bytes_sent", '
'"request_time": "$request_time", '
'"status": "$status", '
'"host": "$host", '
'"request": "$request", '
'"request_method": "$request_method", '
'"uri": "$uri", '
'"http_referrer": "$http_referer", '
'"body_bytes_sent":"$body_bytes_sent", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"http_user_agent": "$http_user_agent" '
'}';
access_log /var/log/nginx/access.log json;
sendfile on;
keepalive_timeout 65;

。。。
}url

3.logstash的配置文件,鏈接es

[root@VM_0_92_centos bin]# cat  ../config/nginxdemojson.conf
input{
    file {
        path => "/var/log/nginx/access.log"
        codec => "json"
    }
}
filter{
}

output {
    elasticsearch {
        hosts => ["10.0.0.92:9200"]
        index => "demo-%{+YYY.MM.dd}"
    }
    stdout {codec => rubydebug}
}

 參考:http://www.javashuo.com/article/p-kjsjyvmj-hw.html

相關文章
相關標籤/搜索