日誌收集器Filebeat詳解

後開啟撒打發了發表於2017-11-22

一、簡介

1、Beats是elastic公司的一款輕量級資料採集產品,它包含了幾個子產品:

1)packetbeat(用於監控網路流量)

2)filebeat(用於監聽日誌資料,可以替代logstash-input-file)

3)topbeat(用於蒐集程式的資訊、負載、記憶體、磁碟等資料)

4)winlogbeat(用於蒐集windows事件日誌)

注:社群還提供了dockerbeat等工具(非elastic公司)

2、Filebeat是一個開源的檔案收集器,採用go語言開發,重構logstash採集器原始碼,安裝在伺服器上作為代理來監視日誌目錄或特定的日誌檔案,並把它們傳送到logstash或elasticsearch等。

3、Filebeat是代替logstash-forwarder的資料採集方案,原因是logstash執行在jvm上,對伺服器的資源消耗比較大

https://www.elastic.co/downloads/beats

二、實現原理


當開啟filebeat程式的時候,它會啟動一個或多個探測器(prospectors)去檢測指定的日誌目錄或檔案,對於探測器找出的每一個日誌檔案,filebeat啟動收割程式(harvester),每一個收割程式讀取一個日誌檔案的新內容,併傳送這些新的日誌資料到處理程式(spooler),處理程式會集合這些事件,最後filebeat會傳送集合的資料到你指定的地點。

三、輸出方式

1)Elasticsearch(推薦)

2)Logstash(推薦,注:logstash用於過濾)

3)File

4)Console

5)Kafka(網上普遍吐槽)

6)Redis

 

官方文件:

https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-configuration-details.html#configuration-output

四、使用

1、下載、安裝

1)原始碼

wget https://download.elastic.co/beats/filebeat/filebeat-1.3.0-x86_64.tar.gz

tar -zxvf filebeat-1.3.0-x86_64.tar.gz

2)deb

curl -L -Ohttps://download.elastic.co/beats/filebeat/filebeat_1.3.0_amd64.deb

sudo dpkg -i filebeat_1.3.0_amd64.deb

3)rpm

curl -L -Ohttps://download.elastic.co/beats/filebeat/filebeat-1.3.0-x86_64.rpm

sudo rpm -vi filebeat-1.3.0-x86_64.rpm

4)MAC

curl -L -Ohttps://download.elastic.co/beats/filebeat/filebeat-1.3.0-darwin.tgz

tar -xzvf filebeat-1.3.0-darwin.tgz2、安裝

         5)win

a、下載windows zip檔案 點選下載.

b、解壓檔案到 C:\ProgramFiles.

c、重新命名為 Filebeat.

d、開啟PowerShell提示符作為管理員(右鍵單擊PowerShell的圖示,並選擇以管理員身份執行)。如果您執行的是Windows XP,則可能需要下載並安裝PowerShell

e、執行以下命令來安裝Filebeat作為Windows服務

cd ‘C:\ProgramFiles\Filebeat’

C:\ProgramFiles\Filebeat> .\install-service-filebeat.ps1

2、修改配置檔案

Filebeat的配置檔案是/etc/filebeat/filebeat.yml,遵循YAML語法。具體可以配置如下幾個專案:

  • Filebeat
  • Output
  • Shipper
  • Logging(可選)
  • Run Options(可選)

Filebeat主要定義prospector的列表,定義監控哪裡的日誌檔案,關於如何定義的詳細資訊可以參考filebeat.yml中的註釋,下面主要介紹一些需要注意的地方。

  • paths:指定要監控的日誌,目前按照Go語言的glob函式處理。沒有對配置目錄做遞迴處理,比如配置的如果是:

/var/log/*/*.log

則只會去/var/log目錄的所有子目錄中尋找以”.log”結尾的檔案,而不會尋找/var/log目錄下以”.log”結尾的檔案。

·        encoding:指定被監控的檔案的編碼型別,使用plainutf-8都是可以處理中文日誌的。

·        input_type:指定檔案的輸入型別log(預設)或者stdin

·        exclude_lines:在輸入中排除符合正規表示式列表的那些行。

·        include_lines:包含輸入中符合正規表示式列表的那些行(預設包含所有行),include_lines執行完畢之後會執行exclude_lines

·        exclude_files:忽略掉符合正規表示式列表的檔案(預設為每一個符合paths定義的檔案都建立一個harvester)。

·        fields:向輸出的每一條日誌新增額外的資訊,比如“level:debug”,方便後續對日誌進行分組統計。預設情況下,會在輸出資訊的fields子目錄下以指定的新增fields建立子目錄,例如fields.level

fields: 
level: debug

  • fields_under_root:如果該選項設定為true,則新增fields成為頂級目錄,而不是將其放在fields目錄下。自定義的field會覆蓋filebeat預設的field。例如新增如下配置:

fields: 
level: debug 
fields_under_root: true

·        ignore_older:可以指定Filebeat忽略指定時間段以外修改的日誌內容,比如2h(兩個小時)或者5m(5分鐘)

·        close_older:如果一個檔案在某個時間段內沒有發生過更新,則關閉監控的檔案handle。預設1h,change只會在下一次scan才會被發現

·        force_close_filesFilebeat會在沒有到達close_older之前一直保持檔案的handle,如果在這個時間窗內刪除檔案會有問題,所以可以把force_close_files設定為true,只要filebeat檢測到檔名字發生變化,就會關掉這個handle

·        scan_frequencyFilebeat以多快的頻率去prospector指定的目錄下面檢測檔案更新(比如是否有新增檔案),如果設定為0s,則Filebeat會盡可能快地感知更新(佔用的CPU會變高)。預設是10s

·        document_type:設定Elasticsearch輸出時的documenttype欄位,也可以用來給日誌進行分類。

·        harvester_buffer_size:每個harvester監控檔案時,使用的buffer的大小。

·        max_bytes:日誌檔案中增加一行算一個日誌事件,max_bytes限制在一次日誌事件中最多上傳的位元組數,多出的位元組會被丟棄。

·        multiline:適用於日誌中每一條日誌佔據多行的情況,比如各種語言的報錯資訊呼叫棧。這個配置的下面包含如下配置:

<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">pattern:多行日誌開始的那一行匹配的pattern negate:是否需要對pattern條件轉置使用,不翻轉設為<span class="hljs-constant" style="box-sizing: border-box;">true</span>,反轉設定為<span class="hljs-constant" style="box-sizing: border-box;">false</span> match:匹配pattern後,與前面(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">before</span>)還是後面(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">after</span>)的內容合併為一條日誌 max_lines:合併的最多行數(包含匹配pattern的那一行) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">timeout</span>:到了<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">timeout</span>之後,即使沒有匹配一個新的pattern(發生一個新的事件),也把已經匹配的日誌事件傳送出去</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

·        tail_files:如果設定為trueFilebeat從檔案尾開始監控檔案新增內容,把新增的每一行檔案作為一個事件依次傳送,而不是從檔案開始處重新傳送所有內容。

·        backoffFilebeat檢測到某個檔案到了EOF之後,每次等待多久再去檢測檔案是否有更新,預設為1s

·        max_backoffFilebeat檢測到某個檔案到了EOF之後,等待檢測檔案更新的最大時間,預設是10秒。

·        backoff_factor:定義到達max_backoff的速度,預設因子是2,到達max_backoff後,變成每次等待max_backoff那麼長的時間才backoff一次,直到檔案有更新才會重置為backoff。比如:

如果設定成1,意味著去使能了退避演算法,每隔backoff那麼長的時間退避一次。

·        spool_size:spooler的大小,spooler中的事件數量超過這個閾值的時候會清空傳送出去(不論是否到達超時時間)。

·        idle_timeout:spooler的超時時間,如果到了超時時間,spooler也會清空傳送出去(不論是否到達容量的閾值)。

·        registry_file:記錄filebeat處理日誌檔案的位置的檔案

·        config_dir:如果要在本配置檔案中引入其他位置的配置檔案,可以寫在這裡(需要寫完整路徑),但是隻處理prospector的部分。

·        publish_async:是否採用非同步傳送模式(實驗功能)。

3、啟動

sudo /etc/init.d/filebeat start

4、停止

1)ps -ef |grepfilebeat

2)kill掉該程式

五、其他資料採集器

1logstash-forwarder(elastic)

2)scribe(FaceBook)

3)chukwa(Apache)

4)flume(Cloudera

5)fluentd

六、推薦解決方案

1Filebeat(採集資料)+Elasticsearch(建立索引)+Kibana(展示)

 

2Filebeat(採集資料)+Logstash(過濾)+Elasticsearch(建立索引)+Kibana(展示)

 

3Filebeat(採集資料)+Kafka/Redis/File/Console(資料傳輸)+應用程式(處理,儲存,展示)

 

4Filebeat(採集資料)+Logstash(過濾)+Kafka/Redis/File/Console(資料傳輸)+應用程式(處理,儲存,展示)

 

注:Redis/File/Console如何判斷增量是一個問題


相關文章