1. 程式人生 > >ELK日誌檢索系統--FileBeat配置說明

ELK日誌檢索系統--FileBeat配置說明

0. FileBeat使用說明

FileBeat是一個日誌收集器,基於Logstash-Forwarder的原始碼。FileBeat一般以代理的身份執行在伺服器中,並監視使用者指定的目錄、檔案,同時把日誌檔案更新部分推送給Logstash解析或者直接推送給ES索引。

這裡寫圖片描述

FileBeat的工作方式:
當FileBeat在伺服器上啟動後,它同時啟動一個或者多個prospectors來監視使用者指定的日誌檔案目錄。
prospectors然後針對每個日誌檔案,都啟動一個harvester,每一個harvester監視一個檔案的新增內容,並把新增內容送到spooler中。然後spooler來彙總這些events

事件。然後把這些事件傳送給logstash或者es。

1. FileBeat的安裝

FileBeat的使用與ES、Logstash基本一樣,從官方網站下載*.tar.gz檔案,然後解壓。新增配置檔案,然後就可以運行了。

deb:

curl -L -O https://download.elastic.co/beats/filebeat/filebeat_1.3.1_amd64.deb
sudo dpkg -i filebeat_1.3.1_amd64.deb

rpm:

curl -L -O https://download.elastic.co/beats/filebeat/filebeat-1.3.1-x86_64.rpm
sudo rpm -vi filebeat-1.3.1-x86_64.rpm

如果用rpm或者deb的安裝包,配置檔案的位置在:/etc/filebeat/filebeat.yml,下面是一個預設的配置檔案:

filebeat:
  # List of prospectors to fetch data.
  prospectors:
    # Each - is a prospector. Below are the prospector specific configurations
    -
      # Paths that should be crawled and fetched. Glob based paths.
      # For
each file found under this path, a harvester is started. paths: - "/var/log/*.log" #- c:\programdata\elasticsearch\logs\* # Type of the files. Based on this the way the file is read is decided. # The different types cannot be mixed in one prospector # # Possible options are: # * log: Reads every line of the log file (default) # * stdin: Reads the standard in input_type: log ......

2. FileBeat檔案路徑配置、ES輸出

設定日誌檔案的路徑,每一個prospector監視一個路徑(目錄)。

/var/log/*/*.log這個路徑代表log的所有子目錄中的字尾為log的日誌檔案。但是不會監視log目錄中的日誌檔案。

設定FileBeat的輸出物件

如果output為ES,那麼配置檔案:

# Configure what outputs to use when sending the data collected by the beat.
# Multiple outputs may be used.
output:
  ### Elasticsearch as output
  elasticsearch:
    # Array of hosts to connect to.
     hosts: ["192.168.1.42:9200"]

3. 設定Logstash為output物件

可以設定FileBeat的輸出物件為Logstash,把FileBeat收集的資料推送到Logstash進行解析。

註釋掉第二步驟中的ES的輸出配置,新增下面的內容:

output:
  logstash:
    hosts: ["127.0.0.1:5044"]

其中hosts指定了Logstash的IP地址和Logstash在其自己的配置檔案中指定的監聽埠。

如果把Logstash設定為輸出物件,必須手動載入ES的索引模板。
通過./filebeat -configtest -e測試配置的FileBeat指令碼

4. 在FileBeat中為ES設定索引模板

有了FileBeat以後,收集器是FileBeat,也只有FileBeat自己清楚其所監視的日誌是什麼格式。所以它可以為ES的索引定製模板。

推薦模板在FileBeat安裝包裡有預設的配置。

如果FileBeat的output是ES,那麼可以在FileBeat的配置檔案中設定ES索引模板。
如果FileBeat的output是logstash,那麼必須手動載入ES索引模板。

FileBeat通過配置檔案自動為ES載入索引模板(輸出物件必須為ES):

output:
  elasticsearch:
    hosts: ["localhost:9200"]

    # A template is used to set the mapping in Elasticsearch
    # By default template loading is disabled and no template is loaded.
    # These settings can be adjusted to load your own template or overwrite existing ones
    template:

      # Template name. By default the template name is filebeat.
      #name: "filebeat"

      # Path to template file
      path: "filebeat.template.json"

      # Overwrite existing template
      #overwrite: false

在啟動FileBeat的時候,自動載入索引模板到ES中。如果索引模板已經存在於ES中,預設不會覆蓋。如果把overwrite設定為true就可以覆蓋存在的模板了。

手動載入ES索引模板的方法(輸出物件為Logstash):

curl -XPUT 'http://localhost:9200/_template/filebeat' -d@/etc/filebeat/filebeat.template.json

If you’ve already used Filebeat to index data into Elasticsearch, the index may contain old documents. After you load the index template, you can delete the old documents from filebeat-* to force Kibana to look at the newest documents. Use this command。curl -XDELETE 'http://localhost:9200/filebeat-*'

5. 啟動FileBeat

deb:

sudo /etc/init.d/filebeat start

rpm:

sudo /etc/init.d/filebeat start

直接tar.gz壓縮包:

sudo ./filebeat -e -c filebeat.yml -d "publish"

-d引數指定了除錯的selectors,例如-d publish顯示所有與publish相關的資訊。不同的selectors用逗號分隔,-d "*"表示除錯所有的資訊。

之後FileBeat就按照設定的輸入、輸出傳送資料了。

6. Loading the Kibana Index Pattern

7. Command Line Options