ELK-002-Beats-Filebeat的HelloWorld

1. 引言

Filebeat是一個輕量級的日誌採集器,當您要面對成百上千、甚至成千上萬的服務器、虛擬機和容器生成的日誌時,請告別 SSH 吧。Filebeat 將爲您提供一種輕量型方法,用於轉發和彙總日誌與文件,讓簡單的事情不再繁雜。可以用官網一幅圖直觀地對其進行描述:
這裏寫圖片描述

官網地址https://www.elastic.co/cn/downloads/beats/filebeat
文檔地址https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-getting-started.html

注意:之後所有的博文中的實驗都會在Ubuntu16.04進行。安裝方式爲使用官方最新tar包。

2. 下載並解壓

下載:

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.6.0-linux-x86_64.tar.gz

解壓:

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

3. 修改配置文件

在解壓的目錄下,我們可以看到filebeat完整的配置文件(filebeat.full.yml),裏面包含了filebeat所有的配置項,我們後面會逐一進行講解。簡化版本的配置文件是:filebeat.yml,你可以基於這個簡化版本的配置文件進行實驗。但是在實驗之前,最好對原始配置文件做個備份:

cp filebeat.yml filebeat.yml.bak

3.1 filebeat.yml配置文件解讀

通過命令:

cat filebeat.yml | grep -v "^#" | grep -v "#" |  grep -v "^$"

我們可以看到filebeat.yml中已經激活的配置項:

filebeat.prospectors:
- input_type: log
  paths:
    - /var/log/*.log
output.logstash:
  hosts: ["localhost:5044"]

在這個配置文件中,主要定義了兩個部分:第一個部分是數據採集源頭,也就是你要從哪個地方蒐集什麼樣的數據,比如本例中就是採集/var/log目錄下所有的.log後綴的日誌文件;第二個部分是輸出部分,也就是你採集到的數據要輸出到哪兒去,本例中是輸出到logstash中,logstash的host爲localhost,port爲5044。當然你可以設置爲控制檯、文件、kafka、ElasticSearch、logstash等。

3.2 用於測試的配置文件

爲了不在剛開始就和Elastic的其他組件進行交互,我們將輸出改爲控制檯輸出的方式,我們最終使用的測試配置文件(hello.yml)爲:

filebeat.prospectors:
- input_type: log
  paths:
    - /var/log/*.log
output.console:
  # Boolean flag to enable or disable the output module.
  enabled: true
  # Pretty print json event
  pretty: true

該測試的配置文件將/var/log下的所有.log日誌文件採集輸出到控制檯中。配置文件設置完成後,記得改變該配置文件的權限爲600,不然不能啓動

chmod 600 hello.yml

4. 運行filebeat

filebeat的啓動幫助可以通過

./filebeat -h

查看。本例中使用:

./filebeat -c hello.yml

啓動filebeat,啓動瞬間就會在控制檯出現以下形式的數據:

{
  "@timestamp": "2017-09-15T12:43:30.795Z",
  "beat": {
    "hostname": "yangyue",
    "name": "yangyue",
    "version": "5.6.0"
  },
  "input_type": "log",
  "message": "2017-09-14 22:24:24 status config-files linux-image-4.4.0-89-generic:amd64 4.4.0-89.112",
  "offset": 87198,
  "source": "/var/log/dpkg.log",
  "type": "log"
}

5. 小結

本文主要介紹了Filebeat的應用場景,並實現了Filebeat的HelloWord,如果有任何問題,歡迎評論留言。

發佈了44 篇原創文章 · 獲贊 28 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章