Logstash处理Nginx日志
nginx log_format
|
|
日志用例
123.150.174.174 - - [15/Jul/2020:18:44:31 +0800] "POST /tencent&contenttype=json HTTP/1.1" 200 50 "-" "-" - 0.005 10.203.151.216:10042 0.005 "application/json" www.baidu.com
logstash配置
input {
kafka {
bootstrap_servers => "kafka_ip:9092"
topics => ["topic_name"]
consumer_threads => 8
type => "nginx_log"
}
}
filter {
if [type] == "nginx_log" {
json {
source => "message"
}
mutate {
remove_field => [ "input", "host", "input.type", "offset", "prospector" ]
}
grok {
match => [
"message", "%{IPORHOST:client} (%{USER:ident}|-) (%{USER:auth}|-) \[%{HTTPDATE:accept_date}\] \"(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:http_version})?|-)\" %{NUMBER:response} %{NUMBER:bytes:int} (%{QS:referrer}|-) %{QS:agent} (%{QS:x_forword}|-) %{NUMBER:request_time:float} (%{NOTSPACE:upstream_addr}|-) %{NUMBER:upstream_response_time:float} \"(%{NOTSPACE:content-type}|-)\" (%{NOTSPACE:host}|-)"
]
}
date {
match => ["accept_date","dd/MMM/yyy:HH:mm:ss"]
}
ruby {
code => "
event.set('locate_index',event.get('@timestamp').time.localtime.strftime('%Y.%m.%d'))
"
}
}
}
output {
if [type] == "nginx_log" {
elasticsearch {
hosts => ["es_ip"]
index => "nginx_log-%{locate_index}"
retry_initial_interval => 60
}
}
}