Beats是用於單用途數據託運人的平臺。它們以輕量級代理的形式安裝,並未來自成百上千臺機器的數據發送到Logstash或Elasticsearch。html
(畫外音:通俗地理解,就是採集數據,並上報到Logstash或Elasticsearch)linux
Beats對於收集數據很是有用。它們位於你的服務器上,將數據集中在Elasticsearch中,Beats也能夠發送到Logstash來進行轉換和解析。瀏覽器
爲了捕捉(捕獲)數據,Elastic提供了各類Beats:安全
Beats能夠直接(或者經過Logstash)將數據發送到Elasticsearch,在那裏你能夠進一步處理和加強數據,而後在Kibana中將其可視化。服務器
配置文件:filebeat.yml數據結構
爲了配置Filebeat:curl
1. 定義日誌文件路徑elasticsearch
對於最基本的Filebeat配置,你可使用單個路徑。例如:ui
filebeat.inputs: - type: log enabled: true paths: - /var/log/*.log
在這個例子中,獲取在/var/log/*.log路徑下的全部文件做爲輸入,這就意味着Filebeat將獲取/var/log目錄下全部以.log結尾的文件。url
爲了從預約義的子目錄級別下抓取全部文件,可使用如下模式:/var/log/*/*.log。這將抓取/var/log的子文件夾下全部的以.log結尾的文件。它不會從/var/log文件夾自己抓取。目前,不可能遞歸地抓取這個目錄下的全部子目錄下的全部.log文件。
(畫外音:
假設配置的輸入路徑是/var/log/*/*.log,假設目錄結構是這樣的:
那麼只會抓取到2.log和3.log,而不會抓到1.log和4.log。由於/var/log/aaa/ccc/1.log和/var/log/4.log不會被抓到。
)
2. 若是你發送輸出目錄到Elasticsearch(而且不用Logstash),那麼設置IP地址和端口以便可以找到Elasticsearch:
output.elasticsearch: hosts: ["192.168.1.42:9200"]
3. 若是你打算用Kibana儀表盤,能夠這樣配置Kibana端點:
setup.kibana: host: "localhost:5601"
4. 若是你的Elasticsearch和Kibana配置了安全策略,那麼在你啓動Filebeat以前須要在配置文件中指定訪問憑據。例如:
output.elasticsearch: hosts: ["myEShost:9200"] username: "filebeat_internal" password: "{pwd}" setup.kibana: host: "mykibanahost:5601" username: "my_kibana_user" password: "{pwd}"
若是你想使用Logstash對Filebeat收集的數據執行額外的處理,那麼你須要將Filebeat配置爲使用Logstash。
output.logstash: hosts: ["127.0.0.1:5044"]
在Elasticsearch中,索引模板用於定義設置和映射,以肯定如何分析字段。(畫外音:至關於定義索引文檔的數據結構,由於要把採集的數據轉成標準格式輸出)
Filebeat包已經安裝了推薦的索引模板。若是你接受filebeat.yml中的默認配置,那麼Filebeat在成功鏈接到Elasticsearch之後會自動加載模板。若是模板已經存在,不會覆蓋,除非你配置了必須這樣作。
經過在Filebeat配置文件中配置模板加載選項,你能夠禁用自動模板加載,或者自動加載你本身的目標。
配置模板加載
默認狀況下,若是Elasticsearch輸出是啓用的,那麼Filebeat會自動加載推薦的模板文件 ——— fields.yml。
setup.template.name: "your_template_name" setup.template.fields: "path/to/fields.yml"
覆蓋一個已存在的模板
setup.template.overwrite: true
禁用自動加載模板
setup.template.enabled: false
修改索引名稱
# 默認狀況下,Filebeat寫事件到名爲filebeat-6.3.2-yyyy.MM.dd的索引,其中yyyy.MM.dd是事件被索引的日期。爲了用一個不一樣的名字,你能夠在Elasticsearch輸出中設置index選項。例如:
output.elasticsearch.index: "customname-%{[beat.version]}-%{+yyyy.MM.dd}" setup.template.name: "customname" setup.template.pattern: "customname-*" setup.dashboards.index: "customname-*"
手動加載模板
./filebeat setup --template -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=["localhost:9200"]'
Filebeat附帶了Kibana儀表盤、可視化示例。在你用dashboards以前,你須要建立索引模式,filebeat-*,而且加載dashboards到Kibana中。爲此,你能夠運行setup命令或者在filebeat.yml配置文件中配置dashboard加載。
./filebeat setup --dashboards
./filebeat -e -c filebeat.yml -d "publish"
http://127.0.0.1:5601
#=========================== Filebeat inputs ============== filebeat.inputs: - type: log enabled: true paths: - /var/log/*.log #============================== Dashboards =============== setup.dashboards.enabled: false #============================== Kibana ================== setup.kibana: host: "192.168.101.5:5601" #-------------------------- Elasticsearch output --------- output.elasticsearch: hosts: ["localhost:9200"]
啓動Elasticsearch
/usr/local/programs/elasticsearch/elasticsearch-6.3.2/bin/elasticsearch
啓動Kibana
/usr/local/programs/kibana/kibana-6.3.2-linux-x86_64/bin/kibana
設置dashboard
./filebeat setup --dashboards
啓動Filebeat
./filebeat -e -c filebeat.yml -d "publish"
瀏覽器訪問 http://192.168.101.5:5601
查看索引
請求:
curl -X GET "localhost:9200/_cat/indices?v"
響應:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open bank 59jD3B4FR8iifWWjrdMzUg 5 1 1000 0 475.1kb 475.1kb green open .kibana DzGTSDo9SHSHcNH6rxYHHA 1 0 153 23 216.8kb 216.8kb yellow open filebeat-6.3.2-2018.08.08 otgYPvsgR3Ot-2GDcw_Upg 3 1 255 0 63.7kb 63.7kb yellow open customer DoM-O7QmRk-6f3Iuls7X6Q 5 1 1 0 4.5kb 4.5kb