[toc]
## 介紹
Filebeat是beat家族中的一員,Beats是用于單用途數據托運人的平臺。它們以輕量級代理的形式安裝,并將來自成百上千臺機器的數據發送到Logstash或Elasticsearch。
(畫外音:通俗地理解,就是**采集數據**,并**上報**到Logstash或Elasticsearch)
Beats對于收集數據非常有用。它們位于你的服務器上,將數據集中在Elasticsearch中,Beats也可以發送到Logstash來進行轉換和解析。
為了捕捉(捕獲)數據,Elastic提供了各種Beats:

**其中Filebeat就是用于Log files的數據采集。**
Filebeat是一個日志文件托運工具,在你的服務器上安裝客戶端后,filebeat會監控日志目錄或者指定的日志文件,追蹤讀取這些文件(追蹤文件的變化,不停的讀),并且轉發這些信息到elasticsearch或者logstarsh中存放。
以下是filebeat的工作流程:當你開啟filebeat程序的時候,它會啟動一個或多個探測器(prospectors)去檢測你指定的日志目錄或文件,對于探測器找出的每一個日志文件,filebeat啟動收割進程(harvester),每一個收割進程讀取一個日志文件的新內容,并發送這些新的日志數據到處理程序(spooler),處理程序會集合這些事件,最后filebeat會發送集合的數據到你指定的地點。

## 安裝
下載安裝:https://www.elastic.co/cn/downloads/beats/filebeat
Filebeat can also be installed from our package repositories using apt or yum
這里我們試一下用yum安裝
1. Download and install the public signing key:
~~~
sudo rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
~~~
2. Create a file with a`.repo`extension (for example,`elastic.repo`) in your`/etc/yum.repos.d/`directory and add the following lines:
~~~
[elastic-6.x]
name=Elastic repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
~~~
Your repository is ready to use. For example, you can install Filebeat by running:
~~~
sudo yum install filebeat
~~~
3. To configure the Beat to start automatically during boot, run:
~~~
sudo chkconfig --add filebeat
~~~
## [配置](https://www.cnblogs.com/smile361/p/7688545.html)
> `vim /etc/filebeat/filebeat.yml`
### 定義prospectors塊
~~~
filebeat.inputs:
- type: log
//開啟監視,不開不采集
enable: true
paths:
- /var/log/elkTest/error/*.log
# 日志多行合并采集
# 匹配開始標識
multiline.pattern: '^\['
multiline.negate: true
multiline.match: after
# 為每個項目標識,或者分組,可區分不同格式的日志
# 可以在logstash里邊通過這個tags 過濾并格式化自己想要的內容
tags: ["seas-logs"]
# 這個文件記錄日志讀取的位置,如果容器重啟,可以從記錄的位置開始取日志
registry_file: /usr/share/filebeat/data/registry
# 如果是多個的話就定義多個prospector塊
- type: log
enabled: true
paths:
- /var/log/*.log
# 退出查看
grep "^\s*[^# \t].*$" /etc/filebeat/filebeat.yml
~~~
在這個例子中,獲取在/var/log/\*.log路徑下的所有文件作為輸入,這就意味著Filebeat將獲取/var/log目錄下所有以.log結尾的文件。
為了從預定義的子目錄級別下抓取所有文件,可以使用以下模式:/var/log/\*/\*.log。這將抓取/var/log的子文件夾下所有的以.log結尾的文件。它不會從/var/log文件夾本身抓取。目前,不可能遞歸地抓取這個目錄下的所有子目錄下的所有.log文件。
<br />
### 輸出到Elasticsearch
**如果你發送輸出目錄到Elasticsearch(并且不用Logstash),那么設置IP地址和端口以便能夠找到Elasticsearch**
~~~
output.elasticsearch:
hosts: ["172.28.3.199:9200"]
~~~
**如果你打算用Kibana儀表盤,可以這樣配置Kibana端點**
~~~
setup.kibana:
host: "localhost:5601"
~~~
**如果你的Elasticsearch和Kibana配置了安全策略,那么在你啟動Filebeat之前需要在配置文件中指定訪問憑據。**
~~~
output.elasticsearch:
hosts: ["myEShost:9200"]
username: "filebeat_internal"
password: "{pwd}"
setup.kibana:
host: "mykibanahost:5601"
username: "my_kibana_user"
password: "{pwd}"
~~~
### 在Elasticsearch中加載索引模板
在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"]'
~~~
### 設置Kibana dashboards
Filebeat附帶了Kibana儀表盤、可視化示例。在你用dashboards之前,你需要創建索引模式,filebeat-\*,并且加載dashboards到Kibana中。為此,你可以運行setup命令或者在filebeat.yml配置文件中配置dashboard加載。
~~~
./filebeat setup --dashboards
~~~
### 輸出到Logstash
如果你想使用Logstash對Filebeat收集的數據執行額外的處理,那么你需要將Filebeat配置為使用Logstash。
~~~
output.logstash:
hosts: ["127.0.0.1:5044"]
~~~
### 參考
~~~
#=========================== 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"]
~~~
## 啟動
~~~
# 設置dashboard
filebeat setup --dashboards
# 啟動filebeat
filebeat -e -c /etc/filebeat/filebeat.yml -d "publish"
# 進es查看索引
GET /_cat/indices?format=json
{
"health" : "yellow",
"status" : "open",
"index" : "filebeat-6.7.0-2019.03.29",
"uuid" : "UEv8Ryi1RViK76tTP1Wwmw",
"pri" : "3",
"rep" : "1",
"docs.count" : "514",
"docs.deleted" : "0",
"store.size" : "158.7kb",
"pri.store.size" : "158.7kb"
}
~~~
進es查看文檔會發現日志被記錄在一個 `message `的字段上。其實一般情況下,只有這個字段是需要的,所以要經過`logstash`來中轉一下。
## 參考資料
* [輕量型日志采集器](https://www.elastic.co/cn/products/beats/filebeat)