|
|
为了便于量化分析nginx access日志,使用logstash 进行筛选匹配
1.确定nginx 日志格式
log_format access '$remote_addr - $remote_user [$time_local] '
'$http_host $request_method $uri '
'$status $body_bytes_sent '
'$upstream_status $upstream_addr $request_time '
'$upstream_response_time $http_user_agent';
2.使用logstash grok 对 日志进行匹配
filter {
if [type] == 'mobile-access' {
#message 匹配的pattern便于阅读拆分成了多行书写,在配置文件里其实是一行的
grok {
match =>[
"message",'%{IPV4:remote_addr} - (%{USERNAME:user}|-)
\[%{HTTPDATE:nginx_timestamp}\]%{HOSTNAME:http_host}
%{WORD:request_method} %{URIPATH:uri}%{BASE10NUM:http_status}
%{BASE10NUM:body_bytes_sent:int}(%{BASE10NUM:upstream_status}|-)
(?:%{HOSTPORT:upstream_addr}|-)(%{BASE16FLOAT:request_time}|-)
(%{BASE16FLOAT:upstream_response_time}|-)(?<http_user_agent>[^"]*)'
]
}
date {
locale => "en"
match =>["nginx_timestamp" , "dd/MMM/YYYY:HH:mm:ss Z"]
}
#实际中grok并没有将匹配的字段转换成期望的int、float等类型,因此这里使用了convert再次做转换
mutate {
convert =>[
"request_time","float",
"body_bytes_sent","integer",
"upstream_response_time","float",
"http_status","integer"
]
}
}
|
|
|