1.nginx日志調成json樣式
log_format json '{"@timestamp":"$time_iso8601",'
'"server_addr":"$server_addr",'
'"server_name":"$server_name",'
'"server_port":"$server_port",'
'"server_protocol":"$server_protocol",'
'"client_ip":"$remote_addr",'
'"client_user":"$remote_user",'
'"status":"$status",'
'"request_method": "$request_method",'
'"request_length":"$request_length",'
'"request_time":"$request_time",'
'"request_url":"$request_uri",'
'"request_line":"$request",'
'"send_client_size":"$bytes_sent",'
'"send_client_body_size":"$body_bytes_sent",'
'"proxy_protocol_addr":"$proxy_protocol_addr",'
'"proxy_add_x_forward":"$proxy_add_x_forwarded_for",'
'"proxy_port":"$proxy_port",'
'"proxy_host":"$proxy_host",'
'"upstream_host":"$upstream_addr",'
'"upstream_status":"$upstream_status",'
'"upstream_cache_status":"$upstream_cache_status",'
'"upstream_connect_time":"$upstream_connect_time",'
'"upstream_response_time":"$upstream_response_time",'
'"upstream_header_time":"$upstream_header_time",'
'"upstream_cookie_name":"$upstream_cookie_name",'
'"upstream_response_length":"$upstream_response_length",'
'"upstream_bytes_received":"$upstream_bytes_received",'
'"upstream_bytes_sent":"$upstream_bytes_sent",'
'"http_host":"$host",'
'"http_cookie":"$http_cooke",'
'"http_user_agent":"$http_user_agent",'
'"http_origin":"$http_origin",'
'"http_upgrade":"$http_upgrade",'
'"http_referer":"$http_referer",'
'"http_x_forward":"$http_x_forwarded_for",'
'"http_x_forwarded_proto":"$http_x_forwarded_proto",'
'"https":"$https",'
'"http_scheme":"$scheme",'
'"invalid_referer":"$invalid_referer",'
'"gzip_ratio":"$gzip_ratio",'
'"realpath_root":"$realpath_root",'
'"document_root":"$document_root",'
'"is_args":"$is_args",'
'"args":"$args",'
'"connection_requests":"$connection_requests",'
'"connection_number":"$connection",'
'"ssl_protocol":"$ssl_protocol",'
'"ssl_cipher":"$ssl_cipher"}';
凡是需要使用nginx日志的均采用如下寫法:
# 事前創建保存nginx日志的文件夾
# mkdir -p /var/log/nginx/
access_log /var/log/nginx/test.access.log json;
2.使用filebeat收集nginx的日志
注意:這里不采用自帶的模塊形式,也就是不開啟nginx的module收集功能
注意:方式1和方式2的其他配置都相同,這里只列出不同的地方,后面會具體分析這倆不同的配置有啥不同的效果
設置方式1
- type: log
enabled: true
paths:
- /var/log/nginx/test.access.log
fields:
log_source: nginx
log_type: www
fields_under_root: true
tags: ["nginx"]
json:
keys_under_root: true
overwrite_keys: true
message_key: "message"
add_error_key: true
使用方式1的設置后,nginx的日志就會輸出到es中,且日志中的每個鍵值對就是頂級字段,如下圖所示
但是有一個問題,原有message字段的值是空的,導致在kibana日志導航中無法顯示ngixn的日志(具體是access日志,error日志因為還是原來的設置仍會顯示).
這種設置在grafana中會正常顯示數據出來的。
設置方式2
- type: log
enabled: true
paths:
- /var/log/nginx/test.access.log
fields:
log_source: nginx
log_type: www
fields_under_root: true
tags: ["nginx"]
processors:
- add_host_metadata:
when.not.contains.tags: forwarded
- decode_json_fields:
fields: ['message']
target: ""
overwrite_keys: false
process_array: false
max_depth: 1
使用方式2的設置后,nginx的日志就會輸出到es中,且日志中的每個鍵值對就是頂級字段,並且原有message字段的值仍存在,在kibana日志導航中還可以繼續顯示ngixn的日志(具體是access日志,error日志因為還是原來的設置仍會顯示).
這種設置在grafana中會正常顯示數據出來的。
綜合方式1和方式2,方式2的方式是符合需求的,既能繼續查詢日志,還能把日志信息圖形化顯示出來(不使用自帶的nginx module收集方式)
3.es中設置索引模式
注意:若索引模式不是filebeat開頭的,則在grafana中不會顯示出數據來
filebeat-*
4.grafana中添加dashboard
打開 Grafana 頁面,登錄后,在 Data Sources 添加類型為 Elasticsearch 的數據源
在 HTTP 段,把 es 機器的IP地址和端口填寫到 URL 輸入框中,比如: http://localhost:9200
,訪問有密碼的話需要加上密碼
在 Elasticsearch details 段
把上面 filebeat 配置的nginx索引名稱填寫到 Index name 輸入框中,比如: filebeat-*
Version 選擇 7.0+
保存測試
導入Dashboard,ID: 14913
報錯:Panel plugin not found: grafana-piechart-panel
解決辦法:
grafana-cli plugins install grafana-piechart-panel
systemctl restart grafana-server
6.上述方式1和方式2的字段含義
方式1
json 這些選項使得Filebeat將日志作為JSON消息來解析。例如:
json.keys_under_root: true
json.add_error_key: true
json.message_key: log
為了啟用JSON解析模式,你必須至少指定下列設置項中的一個:
keys_under_root: 默認情況下,解碼后的JSON被放置在一個以"json"為key的輸出文檔中。如果你啟用這個設置,那么這個key在文檔中被復制為頂級。默認是false。
overwrite_keys: 如果keys_under_root被啟用,那么在key沖突的情況下,解碼后的JSON對象將覆蓋Filebeat正常的字段
add_error_key: 如果啟用,則當JSON反編排出現錯誤的時候Filebeat添加 "error.message" 和 "error.type: json"兩個key,或者當沒有使用message_key的時候。
message_key: 一個可選的配置,用於在應用行過濾和多行設置的時候指定一個JSON key。指定的這個key必須在JSON對象中是頂級的,而且其關聯的值必須是一個字符串,否則沒有過濾或者多行聚集發送。
ignore_decoding_error: 一個可選的配置,用於指定是否JSON解碼錯誤應該被記錄到日志中。如果設為true,錯誤將被記錄。默認是false。
方式2
用filebeat想對收集到的日志進行這樣的解析:如果為json的話,就將json對象中的每個子字段解析成頂級結構下的一個字段,但是發現,解析后,保存日志完整內容的message字段(也即完整的json串)消失了,最終找到如下解決方法:
用processors中的decode_json_fields處理器進行處理,它類似logstash中的filter,具體格式如下:
processors:
- add_host_metadata:
when.not.contains.tags: forwarded
- decode_json_fields:
fields: ['message'] #要進行解析的字段
target: "" #json內容解析到指定的字段,如果為空(""),則解析到頂級結構下
overwrite_keys: false #如果解析出的json結構中某個字段在原始的event(在filebeat中傳輸的一條數據為一個event)中也存在,是否覆蓋event中該字段的值,默認值:false
process_array: false #數組是否解碼,默認值:false
max_depth: 1 #解碼深度,默認值:1