Filebeat


Beats系列產品:

  

 Filebeat:

  

  架構:
    用於監控、收集服務器日志文件

  部署與運行:

    下載地址:https://www.elastic.co/downloads/beats

    mkdir /usr/local/beats

    tar -zxvf filebeat-6.5.4-linux-x86_64.tar.gz

    cd filebeat-6.5.4-linux-x86_64

    創建如下配置文件 fan.yml

      filebeat.inputs:
      - type: stdin #輸入的方式
        enabled: true #啟用輸入
      output.console: # 輸出到控制台
        pretty: true
        enable: true

    啟動filebeat
      ./filebeat -e -c fan.yml  # -e 輸出到標准輸出,默認輸出到syslog和logs下,-c 指定配置文件,-d 輸出debug信息(-d "publish")

    輸入hello運行結果如下:hello

{
  "@timestamp": "2020-03-27T03:32:57.130Z",
  "@metadata": {    #元數據信息
    "beat": "filebeat",
    "type": "doc",
    "version": "6.5.4"
  },
  "input": {    #控制台標准輸入
    "type": "stdin"
  },
  "beat": {    #beat版本以及主機信息
    "name": "fan",
    "hostname": "fan",
    "version": "6.5.4"
  },
  "host": {
    "name": "fan"
  },
  "source": "",
  "offset": 0,
  "message": "hello",    #輸入的內容
  "prospector": {    #標准輸入勘探器
    "type": "stdin"
  }
}

  讀取文件:

    復制一份配置並修改

      cp fan.yml fan-log.yml

      filebeat.inputs:
      - type: log
        enabled: true
        paths:
          - /fan/beats/logs/*.log
      setup.template.settings:
        index.number_of_shards: 3
      output.console:
        pretty: true
        enable: true

    在 /fan/beats/logs 目錄下准備一下日志文件進行測試

      啟動 filebeat
        ./filebeat -e -c fan-log.yml

      可以看出,開始會讀取已有的日志文件,后面檢測到日志文件有更新,立刻就會讀取到更新的內容,並且輸出到控制台。

  自定義字段:

    修改配置文件
      filebeat.inputs:
      - type: log
        enabled: true
        paths:
          - /fan/beats/logs/*.log

        tags: ["web"]  #添加自定義tag,便於后續的處理

        fields:  #添加自定義字段

          from: test-web

        fields_under_root: true #true為添加到根節點,false為添加到子節點中
      output.console:
        pretty: true
        enable: true

    啟動測試:

      

   輸出到Elasticsearch:

    修改配置文件
      filebeat.inputs:
      - type: log
        enabled: true
        paths:
          - /fan/beats/logs/*.log

        tags: ["web"]  #添加自定義tag,便於后續的處理

        fields:  #添加自定義字段

          from: test-web

        fields_under_root: true #true為添加到根節點,false為添加到子節點中
      setup.template.settings:
        index.number_of_shards: 3 #指定es索引的分區數

      output.elasticsearch: #指定ES的配置
        hosts: ["192.168.43.182:9200","192.168.43.182:9201","192.168.43.182:9202"]

  Filebeat工作原理:

    Filebeat由兩個主要組件組成:prospector 和 harvester。
    harvester:
      負責讀取單個文件的內容。
      如果文件在讀取時被刪除或重命名,Filebeat將繼續讀取文件。
    prospector
      prospector 負責管理harvester並找到所有要讀取的文件來源。
      如果輸入類型為日志,則查找器將查找路徑匹配的所有文件,並為每個文件啟動一個harvester。
      Filebeat目前支持兩種prospector類型:log和stdin。
    Filebeat如何保持文件的狀態
      Filebeat 保存每個文件的狀態並經常將狀態刷新到磁盤上的注冊文件中。
      該狀態用於記住harvester正在讀取的最后偏移量,並確保發送所有日志行。
      如果輸出(例如Elasticsearch或Logstash)無法訪問,Filebeat會跟蹤最后發送的行,並在輸出再次可用時繼續讀取文件。

      在Filebeat運行時,每個prospector內存中也會保存的文件狀態信息,當重新啟動Filebeat時,將使用注冊文件的數據來重建文件狀態,Filebeat將每個harvester在從保存的最后偏移量繼續讀取。
      文件狀態記錄在data/registry文件中。

  讀取Nginx日志文件:

    創建配置文件 fan-nginx.yml

      filebeat.inputs:

      - type: log

        enabled: true

        paths:

          - /usr/local/nginx/logs/*.log

        tags: ["nginx"]

      setup.template.settings:

        index.number_of_shards: 3

      output.elasticsearch:

          hosts: ["192.168.43.182:9200","192.168.43.182:9201","192.168.43.182:9202"]

      啟動后,可以在Elasticsearch中看到索引以及查看數據:

         

        可以看到,在message中已經獲取到了nginx的日志,但是,內容並沒有經過處理,只是讀取到原數據。

  Module:

    前面要想實現日志數據的讀取以及處理都是自己手動配置的,其實,在Filebeat中,有大量的Module,可以簡化我們的配置,直接就可以使用,如下:
    查看module:./filebeat modules list

      

     可以看到,內置了很多的module,但是都沒有啟用,如果需要啟用需要進行enable操作:

      ./filebeat modules enable nginx #啟動
      ./filebeat modules disable nginx #禁用

    nginx module 配置:

      vim modules.d/nginx.yml

        

     配置filebeat:

      vim fan-nginx.yml

        filebeat.inputs:
        setup.template.settings:
          index.number_of_shards: 3
        output.elasticsearch:
          hosts: ["192.168.43.182:9200","192.168.43.182:9201","192.168.43.182:9202"]
        filebeat.config.modules:
          path: ${path.config}/modules.d/*.yml
          reload.enabled: false

      測試:

        

         解決:可以直接使用sudo命令安裝,但速度很慢,這里使用壓縮包

          1.把ingest-user-agent.tar、ingest-geoip.tar解壓到Elasticsearch的plugins目錄下

           2.把ingest-geoip-conf.tar解壓到Elasticsearch的config目錄下

          注意:所有集群中的節點都需要安裝。

        重啟后可以看到,數據已經寫入到了Elasticsearch中,並且拿到的數據更加明確了

        

 其他的Module的用法參考官方文檔:https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-modules.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM