首頁 > 軟體

Filebeat 根據不同的紀錄檔設定不同的索引

2020-11-02 09:30:07

平時在物理機上使用 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 中對紀錄檔進行展示了。


IT145.com E-mail:sddin#qq.com