通過filebeat的module蒐集nginx日誌
阿新 • • 發佈:2020-07-10
NGINX日誌格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '"$request_body" "$upstream_addr" "$uri" "$upstream_response_time" "$upstream_http_name" "$upstream_http_host" "$request_time"';
安裝filebeat
啟用nginx module
filebeat modules enable nginx
配置NGINX訪問日誌路徑
- 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: ["/var/log/nginx/*.access.log"]
/etc/filebeat/modules.d/nginx.yml
配置ES地址並修改index名稱
/etc/filebeat/filebeat.yml
#==================== Elasticsearch template setting ========================== setup.template.settings: index.number_of_shards: 1 #index.codec: best_compression #_source.enabled: false setup.ilm.enabled: false setup.template.name: "filebeat-nginx" setup.template.pattern:"filebeat-nginx-*" #-------------------------- Elasticsearch output ------------------------------ output.elasticsearch: # Array of hosts to connect to. hosts: ["192.168.160.34:9200"] index: "filebeat-nginx-%{[agent.version]}-%{+yyyy.MM.dd}" # Optional protocol and basic auth credentials. #protocol: "https" username: "elastic" password: "pass"
修改日誌的預設匹配規則
/usr/share/filebeat/module/nginx/access/ingest/default.json
新增upstream_addr、upstream_url、upstream_response_time、request_time等欄位
"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}\" \"%{URIHOST:nginx.access.upstream_addr}\" \"%{DATA:nginx.access.upstream_url}\" \"%{NUMBER:nginx.access.upstream_response_time:float}\" \"-\" \"-\" \"%{NUMBER:nginx.access.request_time:float}\"" ], "pattern_definitions": { "IP_LIST": "%{IP}(\"?,?\\s*%{IP})*" }, "ignore_missing": true }
},
修改fields欄位
/etc/filebeat/fields.yml
新增upstream_addr、upstream_url、upstream_response_time、request_time等欄位
- name: agent type: alias path: user_agent.original migration: true - name: upstream_response_time type: alias path: upstream_response_time migration: true - name: upstream_addr type: alias path: upstream_addr migration: true - name: upstream_url type: alias path: upstream_url migration: true - name: request_time type: alias path: request_time migration: true
欄位出來以後有黃色小三角,重新整理一下index 的欄位快取就好了