Beats 是安裝在服務器上的數據中轉代理。html
Beats 能夠將數據直接傳輸到 Elasticsearch 或傳輸到 Logstash 。mysql
Beats 有多種類型,能夠根據實際應用須要選擇合適的類型。linux
經常使用的類型有:nginx
參考sql
更多 Beats 類型能夠參考:community-beats服務器
說明網絡
因爲本人工做中只應用了 FileBeat,因此後面內容僅介紹 FileBeat 。elasticsearch
相比 Logstash,FileBeat 更加輕量化。ide
在任何環境下,應用程序都有停機的可能性。 Filebeat 讀取並轉發日誌行,若是中斷,則會記住全部事件恢復聯機狀態時所在位置。ui
Filebeat帶有內部模塊(auditd,Apache,Nginx,System和MySQL),可經過一個指定命令來簡化通用日誌格式的收集,解析和可視化。
FileBeat 不會讓你的管道超負荷。FileBeat 若是是向 Logstash 傳輸數據,當 Logstash 忙於處理數據,會通知 FileBeat 放慢讀取速度。一旦擁塞獲得解決,FileBeat 將恢復到原來的速度並繼續傳播。
Unix / Linux 系統建議使用下面方式安裝,由於比較通用。
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.1.1-linux-x86_64.tar.gz tar -zxf filebeat-6.1.1-linux-x86_64.tar.gz
參考
更多內容能夠參考:filebeat-installation
首先,須要知道的是:filebeat.yml
是 filebeat 的配置文件。配置文件的路徑會由於你安裝方式的不一樣而變化。
Beat 全部系列產品的配置文件都基於 YAML 格式,FileBeat 固然也不例外。
filebeat.yml 部分配置示例:
filebeat: prospectors: - type: log paths: - /var/log/*.log multiline: pattern: '^[' match: after
參考
更多 filebeat 配置內容能夠參考:配置 filebeat
更多 filebeat.yml 文件格式內容能夠參考:filebeat.yml 文件格式
(文件監視器)用於指定須要關注的文件。
示例
filebeat.prospectors: - type: log enabled: true paths: - /var/log/*.log
若是你但願使用 filebeat 直接向 elasticsearch 輸出數據,須要配置 output.elasticsearch 。
示例
output.elasticsearch: hosts: ["192.168.1.42:9200"]
若是你但願使用 filebeat 向 logstash輸出數據,而後由 logstash 再向elasticsearch 輸出數據,須要配置 output.logstash。
注意
相比於向 elasticsearch 輸出數據,我的更推薦向 logstash 輸出數據。
由於 logstash 和 filebeat 一塊兒工做時,若是 logstash 忙於處理數據,會通知 FileBeat 放慢讀取速度。一旦擁塞獲得解決,FileBeat 將恢復到原來的速度並繼續傳播。這樣,能夠減小管道超負荷的狀況。
示例
output.logstash: hosts: ["127.0.0.1:5044"]
此外,還須要在 logstash 的配置文件(如 logstash.conf)中指定 beats input 插件:
input { beats { port => 5044 # 此端口須要與 filebeat.yml 中的端口相同 } } # The filter part of this file is commented out to indicate that it is # optional. # filter { # # } output { elasticsearch { hosts => "localhost:9200" manage_template => false index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}" document_type => "%{[@metadata][type]}" } }
若是打算使用 Filebeat 提供的 Kibana 儀表板,須要配置 setup.kibana 。
示例
setup.kibana: host: "localhost:5601"
在 Elasticsearch 中,索引模板用於定義設置和映射,以肯定如何分析字段。
在 Filebeat 中,setup.template.settings 用於配置索引模板。
Filebeat 推薦的索引模板文件由 Filebeat 軟件包安裝。若是您接受 filebeat.yml 配置文件中的默認配置,Filebeat在成功鏈接到 Elasticsearch 後自動加載模板。
您能夠經過在 Filebeat 配置文件中配置模板加載選項來禁用自動模板加載,或加載本身的模板。您還能夠設置選項來更改索引和索引模板的名稱。
參考
更多內容能夠參考:filebeat-template
說明
如無必要,使用 Filebeat 配置文件中的默認索引模板便可。
Filebeat 附帶了示例 Kibana 儀表板。在使用儀表板以前,您須要建立索引模式 filebeat- *
,並將儀表板加載到Kibana 中。爲此,您能夠運行 setup
命令或在 filebeat.yml
配置文件中配置儀表板加載。
爲了在 Kibana 中加載 Filebeat 的儀表盤,須要在 filebeat.yml
配置中啓動開關:
setup.dashboards.enabled: true
參考
更多內容能夠參考:configuration-dashboards
filebeat 提供了一系列命令來完成各類功能。
執行命令方式:
./filebeat COMMAND
參考
更多內容能夠參考:command-line-options
說明
我的認爲命令行沒有必要一一掌握,由於絕大部分功能均可以經過配置來完成。且經過命令行指定功能這種方式要求每次輸入一樣參數,不利於固化啓動方式。
最重要的固然是啓動命令 run 了。
示例 指定配置文件啓動
./filebeat run -e -c filebeat.yml -d "publish" ./filebeat -e -c filebeat.yml -d "publish" # run 能夠省略
Filebeat 提供了一套預構建的模塊,讓您能夠快速實施和部署日誌監視解決方案,並附帶示例儀表板和數據可視化。這些模塊支持常見的日誌格式,例如Nginx,Apache2和MySQL 等。
output.elasticsearch: hosts: ["myEShost:9200"] username: "elastic" password: "elastic" setup.kibana: host: "mykibanahost:5601" username: "elastic" password: "elastic
username 和 password 是可選的,若是不須要認證則不填。
執行下面命令,filebeat 會加載推薦索引模板。
./filebeat setup -e
執行下面命令,指定但願加載的模塊。
./filebeat -e --modules system,nginx,mysql
參考
更多內容能夠參考: 配置 filebeat 模塊 | filebeat 支持模塊
Filebeat 有兩個主要組件:
harvester:負責讀取一個文件的內容。它會逐行讀取文件內容,並將內容發送到輸出目的地。
prospector:負責管理 harvester 並找到全部須要讀取的文件源。好比類型是日誌,prospector 就會遍歷制定路徑下的全部匹配要求的文件。
filebeat.prospectors: - type: log paths: - /var/log/*.log - /var/path2/*.log
Filebeat保持每一個文件的狀態,並常常刷新註冊表文件中的磁盤狀態。狀態用於記住 harvester 正在讀取的最後偏移量,並確保發送全部日誌行。
Filebeat 將每一個事件的傳遞狀態存儲在註冊表文件中。因此它能保證事件至少傳遞一次到配置的輸出,沒有數據丟失。