Filebeat Nginx Module 自定義欄位
1. 前言
filebeat 6.6.2 nginx module自定義欄位
filebeat提供了多種Module預製模組,簡化了各種日誌的格式化
在nginx中預設的欄位並不滿足實際需求,例如我們需要記錄額外的Nginx欄位
例如 請求時間、後端響應時間、主機頭等資訊
那麼在filebeat的nginx module中需要同步定義
2. rpm模式安裝filebeat
curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.6.2-x86_64.rpm sudo rpm -vi filebeat-6.6.2-x86_64.rpm
3. Nginx 自定義Log格式化
nginx原始log格式化定義:
#log format log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" '
nginx自定義格式化log:
#log format log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '"$http_x_real_ip" "$server_addr" "$host" ' '$request_time $upstream_response_time "$upstream_addr" ' '"$time_iso8601"';
4. 啟用filebeat的nginx module
執行
filebeat modules enable nginx
filebeat nginx module 增加欄位
檔案 /usr/share/filebeat/module/nginx/access/ingest/default.json 中
原始:
"grok": { "field": "message", "patterns":[ "\"?%{IP_LIST:nginx.access.remote_ip_list} - %{DATA:nginx.access.user_name} \\[%{HTTPDATE:nginx.access.time}\\] \"%{GREEDYDATA:nginx.access.info}\" %{NUMBER:nginx.access.response_code} %{NUMBER:nginx.access.body_sent.bytes} \"%{DATA:nginx.access.referrer}\" \"%{DATA:nginx.access.agent}\"" ], "pattern_definitions": { "IP_LIST": "%{IP}(\"?,?\\s*%{IP})*" }
自定義修改:
"grok": { "field": "message", "patterns":[ "\"?%{IP_LIST:nginx.access.remote_ip_list} - %{DATA:nginx.access.user_name} \\[%{HTTPDATE:nginx.access.time}\\] \"%{GREEDYDATA:nginx.access.info}\" %{NUMBER:nginx.access.response_code} %{NUMBER:nginx.access.body_sent.bytes} \"%{DATA:nginx.access.referrer}\" \"%{DATA:nginx.access.agent}\" \"%{DATA:nginx.access.xff}\" \"%{DATA:nginx.access.x_real_ip}\" \"%{DATA:nginx.access.server_addr}\" \"%{DATA:nginx.access.host}\" %{DATA:nginx.access.request_time} %{DATA:nginx.access.upstream_response_time} \"%{DATA:nginx.access.upstream_addr}\" \"%{DATA:nginx.access.time_iso8601}\"" ], "pattern_definitions": { "IP_LIST": "%{IP}(\"?,?\\s*%{IP})*" }
如果定製自己的欄位
可以使用http://grokdebug.herokuapp.com/ 線上工具驗證grok規則
可以使用Json工具處理轉義問題
檔案 /etc/filebeat/fields.yml 中
找到nginx欄位配置
在
- name: agent type: text description: > Contains the un-parsed user agent string. Only present if the user agent Elasticsearch plugin is not available or not used.
在後面增加
- name: xff type: group description: > http_x_forwarded_for. - name: x_real_ip type: group description: > http_x_real_ip. - name: server_addr type: group description: > server_addr 伺服器地址. - name: host type: group description: > host http_host http主機頭. - name: request_time type: group description: > request_time 請求時間. - name: upstream_response_time type: group description: > upstream_response_time 後端響應時間. - name: upstream_addr type: group description: > upstream_addr 後端地址. - name: time_iso8601 type: group description: > time_iso8601 iso8601格式時間.
5. Filebeta中 nginx 日誌路徑定義
在檔案 /etc/filebeat/modules.d/nginx.yml 中修改日誌路徑
- module: nginx # Access logs access: enabled: true # Set custom paths for the log files. If left empty, # Filebeat will choose the paths depending on your OS. var.paths: ["/data/wwwlogs/*.log*"] # Error logs error: enabled: true # Set custom paths for the log files. If left empty, # Filebeat will choose the paths depending on your OS. var.paths: ["/usr/local/nginx/logs/error.log*"]
6. 擴充套件
6.1 解決增加欄位後Kibana中不顯示
- 在Kibana的 Dev Tools中執行
DELETE _ingest/pipeline/filebeat-6.6.2-nginx-access-default
- 欄位出來以後有黃色小三角,重新整理一下index 的欄位快取就好了
-
重啟下Filebeat
方法來源