一、Filebeat安裝
1、Filebeat概述
Filebeat是一個日志文件托運工具,在你的服務器上安裝客戶端后,filebeat會監控日志目錄或者指定的日志文件,追蹤讀取這些文件(追蹤文件的變化,不停的讀),並且轉發這些信息到elasticsearch或者logstarsh中存放。
以下是filebeat的工作流程:當你開啟filebeat程序的時候,它會啟動一個或多個探測器(prospectors)去檢測你指定的日志目錄或文件,對於探測器找出的每一個日志文件,filebeat啟動收割進程(harvester),每一個收割進程讀取一個日志文件的新內容,並發送這些新的日志數據到處理程序(spooler),處理程序會集合這些事件,最后filebeat會發送集合的數據到你指定的地點。
(個人理解,filebeat是一個輕量級的logstash,當你需要收集信息的機器配置或資源並不是特別多時,使用filebeat來收集日志。日常使用中,filebeat十分穩定,筆者沒遇到過宕機。)
2、Filebeat安裝
直接到ELK官網下載安裝filebeat:https://www.elastic.co/cn/products(deb for Debian/Ubuntu, rpm for Redhat/Centos/Fedora, mac for OS X, and win for Windows).
Linux下直接下載RPM包安裝即可,Windows下可下載壓縮包解壓進行配置。
以下以windows客戶端為例:
- 在官網下載filebeat-6.1.0-windows-x86_64.zip 文件進行解壓
- 配置filebeat
filebeat的配置文件主要為filebeat.yml文件,對於大多數的基本filebeat配置:
1 filebeat.prospectors: 2 3 - type: log 4 5 enabled: true 6 paths: 7 8 - /var/log/*.log 9 #- C:\inetpub\logs\LogFiles\W3SVC3\*.log
在這個例子中,探測器會收集/var/log/*.log的所有匹配文件,這意味這filebeat會收集所有的/var/log下以.log結尾的文件,此處還支持Golang Glob支持的所有模式。
在預定義級別的子目錄中獲取所有文件,可以使用這個配置:/var/log/*/*.log,這會找到/var/log下所有子目錄中所有的以.log結尾的文件。但它並不會找到/var/log文件夾下的以.log結尾的文件。現在它還不能遞歸的在所有子目錄中獲取所有的日志文件。
如果你設置輸出到elasticsearch中,那么你需要在filebeat的配置文件中設置elasticsearch的IP地址與端口。
1 #-------------------------- Elasticsearch output ------------------------------ 2 output.elasticsearch: 3 hosts: ["localhost:9200"]
如果你設置輸出到logstash中,那么你需要在filebeat的配置文件中設置logstash的IP地址與端口。
1 #----------------------------- Logstash output -------------------------------- 2 output.logstash: 3 hosts: ["localhost:5044"]
配置好后,即可啟動filebeat,若要測試你的配置文件,切換到你安裝好的filebeat的可執行文件的目錄,然后在命令行運行以下選項:./filebeat -configtest -e ,確保您的配置文件在默認配置文件目錄下。
Windows下啟動filebeat,需要在cmd中執行install-service-filebeat.ps1文件,將其加入到Windows服務中:
現在,filebeat已經准備好讀取你的日志文件並發送到你定義的輸出中了。
- Filebeat區別不同的日志源
當一台服務器上有多個日志需要抓取時,如何區分抓取的日志源呢,可以通過在配置文件filebeat.yml中添加document_type來實現:
1 filebeat.prospectors: 2 3 # Each - is a prospector. Most options can be set at the prospector level, so 4 # you can use different prospectors for various configurations. 5 - type: log 6 enabled: true 7 paths: 8 - /home/thinkgamer/test/*.log 9 document_type: test_log 10 11 - type: log 12 enabled: true 13 paths: 14 - /var/log/*.log 15 document_type: auth_log
這里得document_type 就是對應filter中對應的log的type,在logstash的解析文件中可以這樣寫
1 input { 2 beats { 3 port => 5044 4 } 5 } 6 7 filter { 8 if [type] == 'auth_log'{ 9 } 10 else if [type] == 'test_log'{ 11 } 12 } 13 14 output { 15 elasticsearch { 16 hosts => ["http://localhost:9200"] 17 index =>"%{type}-%{+YYYY.MM.dd}" 18 } 19 }