- filebeat 版本 : 7.4.2
- elasticsearch 版本: 7.4.2
- Kibana 7.4.2
- nginx 日誌新增request_time和upstream_response_time兩個字段
1. nginx配置
http {
## main 爲log 日誌格式的標誌名
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time $upstream_response_time';
access_log logs/access.log main;
}
2. 自定義grok正則
- 這裏假設你打開了filebeats的 nginx module的話,就可以直接配置
$(filebeats的路徑)/module/nginx/access/default.json
這個文件。 - 如果你是在filetbeat.yml文件裏面配置的input的話,則可以使用kibana自帶的dev工具
http://xxxx/app/kibana#/dev_tools/console?_g=()
將pipeline的配置信息PUT到elastcisearch,如下圖所示,配置信息可以使用上面所說的default.json裏面的內容,記得input要指定pipeline該屬性。
- grok的正則配置可以參考:grok自帶的的pattern(這裏用logstash在github上的源文件)
- 下面除了正則的processor以外,也用了convert的processor,主要目的是爲了將request_time和response_time的數據類型轉化爲float,由於nginx的日誌輸出這兩個值的時候,如果沒有request_time的話,將會直接打印
-
這個字符,如果在grok裏面使用NUMBER匹配的話會匹配失敗,所以這裏使用NOTSPACE匹配,並且在convert進行數據類型轉化。
{
"processors": [
{
"grok": {
"field": "message",
"patterns": [
"\"?(?:%{IP_LIST:nginx.access.remote_ip_list}|%{DATA:source.address}) - %{DATA:user.name} \\[%{HTTPDATE:nginx.access.time}\\] \"%{DATA:nginx.access.info}\" %{NUMBER:http.response.status_code:long} %{NUMBER:http.response.body.bytes:long} \"%{DATA:http.request.referrer}\" \"%{DATA:user_agent.original}\" %{NOTSPACE:http.request_time} %{NOTSPACE:http.upstream.response_time}"
],
"pattern_definitions": {
"IP_LIST": "%{IP}(\"?,?\\s*%{IP})*"
},
"ignore_missing": true
}
},
{
"convert": {
"field": "http.request_time",
"type": "float",
"ignore_missing": true,
"on_failure": [
{
"set": {
"field": "http.request_time",
"value": -1.0
}
}
]
}
},
{
"convert": {
"field": "http.upstream.response_time",
"type": "float",
"ignore_missing": true,
"on_failure": [
{
"set": {
"field": "http.upstream.response_time",
"value": -1.0
}
}
]
}
}
}
4. 刪除elasticsearch的pipeline緩存
這裏可以使用kibana自帶的dev工具http://xxxx/app/kibana#/dev_tools/console?_g=()
DELETE _ingest/pipeline/xxxxxxx
5.重啓filebeats
如果在filebeats的主配置文件中有配置重新加載配置的話,就無須關掉filebeats進程,等待重新加載配置即可。
- filebeats.yml自動加載配置文件變化的配置(注意這裏自動加載僅僅是modules.d/文件夾裏面的配置文件)
filebeat.config.modules:
# Glob pattern for configuration loading
path: ${path.config}/modules.d/*.yml
# Set to true to enable config reloading
reload.enabled: true
# Period on which files under path should be checked for changes
#reload.period: 10s
- 爲了實時看出配置信息的變化,在啓動filebeats的時候可以指定參數輸出debug信息到控制檯
filebeats.exe -e -d "publish"
可能遇到的問題
- nginx的日誌改變之後,kibana界面顯示日誌格式不匹配。
進行第4步的刪除pipeline,或者使用PUT修改pipeline內容,如果是使用nginx module的話,pipeline的名字應該是filebeat-{filebeat.version}-nginx-access-default。 - 日誌解析正常,pipeline內容已經改變(可以在dev工具裏面使用
GET _ingest/pipeline/
查看)
進入kibana的managment-index pattern(http://xxxx/kibana#/management/kibana/index_patterns?_g=()
),選擇對應的index,刷新。