Filebeat 根據不同的日誌設置不同的索引
平時在物理機上使用 Filebeat 收集日誌時,會編寫多個 filebeat 配置文件然後啟動多個 filebeat 進程來收集不同路徑下的日誌並設置相對應的索引。那麼如果將所有的日誌路徑都寫到一個 filebeat 配置文件中,那麼就需要根據不同的日誌來設置索引了。
其實 logstash 也可以實現這個功能。但是此處只演示在 Filebeat 上實現。步驟和講解如下:
例如現在有如下三個日誌文件,需要輸出到不同的索引:
access.log ----> 索引:web-nginx-access-log
error.log ----> 索引:web-nginx-error-log
blacklist.log ----> 索引:web-nginx-blacklist-log
所需要的 filebeat 配置文件如下:
filebeat.idle_timeout: 2s
filebeat.name: filebeat-shiper
filebeat.spool_zie: 50000
filebeat.inputs: # 從這裡開始定義每個日誌的路徑、類型、收集方式等資訊
- type: log # 指定收集的類型為 log
paths:
- /usr/local/nginx/logs/access.log # 設置 access.log 的路徑
fields: # 設置一個 fields,用於標記這個日誌
type: access-log # 為 fields 設置一個關鍵字 type,值為 access-log
enabled: true
backoff: 1s
backoff_factor: 2
close_inactive: 1h
encoding: plain
harvester_buffer_size: 262144
max_backoff: 10s
max_bytes: 10485760
scan_frequency: 10s
tail_lines: true
- type: log
paths:
- /usr/local/nginx/logs/error.log # 設置 error.log 的路徑
fields: # 設置一個 fields,用於標記這個日誌
type: error-log # 為 fields 設置一個關鍵字 type,值為 error-log
enabled: true
backoff: 1s
backoff_factor: 2
close_inactive: 1h
encoding: plain
harvester_buffer_size: 262144
max_backoff: 10s
max_bytes: 10485760
scan_frequency: 10s
tail_lines: true
- type: log
paths:
- /usr/local/nginx/logs/blacklist.log # 設置 blacklist.log 的路徑
fields: # 設置一個 fields,用於標記這個日誌
type: blacklist-log # 為 fields 設置一個關鍵字 type,值為 blacklist-log
enabled: true
backoff: 1s
backoff_factor: 2
close_inactive: 1h
encoding: plain
harvester_buffer_size: 262144
max_backoff: 10s
max_bytes: 10485760
scan_frequency: 10s
tail_lines: true
output.elasticsearch:
workers: 4
bulk_max_size: 8192
hosts: # 設置 elastic 的地址
- 10.16.12.206:30187
- 10.16.12.207:30187
- 10.16.12.208:30187
- 10.16.13.214:30187
- 10.16.13.215:30187
index: web-nginx-%{[fields.type]}-%{+yyyy.MM.dd} # 設置索引名稱,後面引用的 fields.type 變數。此處的配置應該可以省略(不符合下面創建索引條件的日誌,會使用該索引,後續會測試是否是這樣)
indices: # 使用 indices 代表要創建多個索引
- index: web-nginx-access-log-%{+yyyy.MM.dd} # 設置 access.log 日誌的索引,注意索引前面的 web-nginx 要與setup.template.pattern 的配置相匹配
when.equals: # 設置創建索引的條件:當 fields.type 的值等於 access-log 時才生效
fields.type: access-log
- index: web-nginx-error-log-%{+yyyy.MM.dd}
when.equals:
fields.type: error-log
- index: web-nginx-blacklist-log-%{+yyyy.MM.dd}
when.equals:
fields.type: blacklist-log
processors:
- drop_fields:
fields:
- agent.ephemeral_id
- agent.hostname
- agent.id
- agent.type
- agent.version
- ecs.version
- input.type
- log.offset
- version
- decode_json_fields:
fields:
- message
max_depth: 1
overwrite_keys: true
setup.ilm.enabled: false # 如果要創建多個索引,需要將此項設置為 false
setup.template.name: web-nginx-log # 設置模板的名稱
setup.template.pattern: web-nginx-* # 設置模板的匹配方式,上面索引的前綴要和這裡保持一致
setup.template.overwrite: true
setup.template.enabled: true
編輯完成後,啟動 filebeat 進程。到 Kibana 中查看索引列表,可以發現已經有三個新創建的索引:
名稱 運行狀況 狀態 主分片 副本分片 文檔計數 存儲大小
web-nginx-access-log-2020.10.28 green open 1 1 110 73.9kb
web-nginx-error-log-2020.10.28 green open 1 1 354 155kb
web-nginx-blacklist-log-2020.10.28 green open 1 1 460 219.5kb
針對這三個索引創建索引模式後,就可以在 Kibana 中對日誌進行展示了。