• Filebeathtml

  • Outputlinux

  • Shippernginx

  • Logging(可選)正則表達式

  • Run Options(可選)redis

這個Blog主要講解Filebeat的配置部分,其餘部分後續會有新的Blog介紹。算法

Filebeat的部分主要定義prospector的列表,定義監控哪裏的日誌文件,關於如何定義的詳細信息能夠參考filebeat.yml中的註釋,下面主要介紹一些須要注意的地方。json

filebeat.prospectors:vim

- input_type: log服務器

  paths:數據結構

    - /log/channelserver.log

    #- /home/wang/data/filebeat-5.0.2-linux-x86_64/test.log

  encoding: gbk

  symlinks: true

  include_lines: ['\[.*?統計\]','\[.*?結算\]']

  document_type: channelserver

  fields_under_root: true

  fields:

    host: 192.168.10.155

 

processors:

- drop_fields:

    #fields: ["beat.hostname", "beat.name", "beat.version", "input_type", "beat"]

    fields: ["input_type", "beat", "offset", "source"]

 

output.redis:

  enabled: true

  hosts: ["192.168.10.188"]

  port: 6379

  datatype: list

  key: "filebeat"

  db: 0

 

output.file:

  enabled: true

  path: "/tmp/filebeat"

 

output.console:

  enabled: false

  • paths:指定要監控的日誌,目前按照Go語言的glob函數處理。沒有對配置目錄作遞歸處理,好比配置的若是是:

/var/log/* /*.log

則只會去/var/log目錄的全部子目錄中尋找以」.log」結尾的文件,而不會尋找/var/log目錄下以」.log」結尾的文件。

  • encoding:指定被監控的文件的編碼類型,使用plain和utf-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

則在Kibana看到的內容以下:

這裏寫圖片描述

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

fields: 
level: debug 
fields_under_root: true

則在Kibana看到的內容以下:

這裏寫圖片描述

  • ignore_older:能夠指定Filebeat忽略指定時間段之外修改的日誌內容,好比2h(兩個小時)或者5m(5分鐘)。

  • close_older:若是一個文件在某個時間段內沒有發生過更新,則關閉監控的文件handle。默認1h。

  • force_close_files:Filebeat會在沒有到達close_older以前一直保持文件的handle,若是在這個時間窗內刪除文件會有問題,因此能夠把force_close_files設置爲true,只要filebeat檢測到文件名字發生變化,就會關掉這個handle。

  • scan_frequency:Filebeat以多快的頻率去prospector指定的目錄下面檢測文件更新(好比是否有新增文件),若是設置爲0s,則Filebeat會盡量快地感知更新(佔用的CPU會變高)。默認是10s。

  • document_type:設定Elasticsearch輸出時的document的type字段,也能夠用來給日誌進行分類。

  • harvester_buffer_size:每一個harvester監控文件時,使用的buffer的大小。

  • max_bytes:日誌文件中增長一行算一個日誌事件,max_bytes限制在一第二天志事件中最多上傳的字節數,多出的字節會被丟棄。

  • multiline:適用於日誌中每一條日誌佔據多行的狀況,好比各類語言的報錯信息調用棧。這個配置的下面包含以下配置:

pattern:多行日誌開始的那一行匹配的pattern
negate:是否須要對pattern條件轉置使用,不翻轉設爲true,反轉設置爲falsematch:匹配pattern後,與前面(before)仍是後面(after)的內容合併爲一條日誌
max_lines:合併的最多行數(包含匹配pattern的那一行)timeout:到了timeout以後,即便沒有匹配一個新的pattern(發生一個新的事件),也把已經匹配的日誌事件發送出去1234512345
 
  • tail_files:若是設置爲true,Filebeat從文件尾開始監控文件新增內容,把新增的每一行文件做爲一個事件依次發送,而不是從文件開始處從新發送全部內容。

  • backoff:Filebeat檢測到某個文件到了EOF以後,每次等待多久再去檢測文件是否有更新,默認爲1s。

  • max_backoff:Filebeat檢測到某個文件到了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:是否採用異步發送模式(實驗功能)。

 

Filebeat的基本原理其實就是有一羣prospector,每一個prospector手下管着一批harvester,每一個harvester負責監視一個日誌文件,把變更的內容由spooler彙總以後交給Logstash或者Elasticsearch。若是想快速搭建一個能夠工做的Filebeat,無非有三點:

1.說明要監視哪些文件

filebeat:
  prospectors:
    -      paths:
        - "/var/log/lmz.log"1234512345
 

2.說明收集的日誌發給誰

若是直接發送給Elasticsearch,能夠設置以下:

output:
 

若是須要發給Logstash,能夠註釋掉Elasticsearch的配置,而後設置Logstash以下:

output:
 

固然,也須要在Logstash的配置中指明要從該端口(5044)監聽來自Filebeat的數據:

/etc/logstash/conf.d/beats-input.conf:
input {
  beats {
    port => 5044
  }
}123456123456
 

這裏的配置舉例是把Filebeat、Logstash、Elasticsearch安裝在了一臺機器上,實際使用中確定是分開部署的,須要根據實際狀況修改配置文件中的IP地址及端口號。

3.讓Elasticsearch知道如何處理每一個日誌事件。

默認的Elasticsearch須要的index template在安裝Filebeat的時候已經提供,路徑爲/etc/filebeat/filebeat.template.json,可使用以下命令裝載該模板:

$ curl -XPUT 'http://localhost:9200/_template/filebeat?pretty' -d@/etc/filebeat/filebeat.template.json11
 

固然,也不能忘了,每次修改完Filebeat的配置後,須要重啓Filebeat才能讓改動的配置生效

 

 

1.下載和安裝

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

目前最新版本 1.3.0 
這裏選擇 Linux 64-BIT 即方式一 
方式一:源碼

wget https://download.elastic.co/beats/filebeat/filebeat-1.3.0-x86_64.tar.gztar -zxvf filebeat-1.3.0-x86_64.tar.gz1212
 

方式二:deb

curl -L -O https://download.elastic.co/beats/filebeat/filebeat_1.3.0_amd64.debsudo dpkg -i filebeat_1.3.0_amd64.deb1212
 

方式三:rpm

curl -L -O https://download.elastic.co/beats/filebeat/filebeat-1.3.0-x86_64.rpmsudo rpm -vi filebeat-1.3.0-x86_64.rpm1212
 

方式四:MAC

curl -L -O https://download.elastic.co/beats/filebeat/filebeat-1.3.0-darwin.tgztar -xzvf filebeat-1.3.0-darwin.tgz1212
 

2.配置Filebeat

環境說明: 
1)elasticsearch和logstash 在不一樣的服務器上,只發送數據給logstash 
2)監控nginx日誌 
3)監控支付日誌 
4)監控訂單日誌

2.1配置

編輯filebeat.yml

vim filebeat.yml11
 

默認監控日誌配置

filebeat: 
  prospectors:
  -      
        - /var/log/*.log      input_type: log 123456123456
 

按照要求修改成

filebeat: 
  prospectors:
  -      
        - /www/wwwLog/www.lanmps.com_old/*.log        - /www/wwwLog/www.lanmps.com/*.log      input_type: log 
      document_type: nginx-access-www.lanmps.com
 -      paths:
        - /www/wwwRUNTIME/www.lanmps.com/order/*.log      input_type: log 
      document_type: order-www.lanmps.com
  -      paths:
        - /www/wwwRUNTIME/www.lanmps.com/pay/*.log      input_type: log 
      document_type: pay-www.lanmps.com

  #elasticsearch:
  #   hosts: ["localhost:9200"]
   
    hosts: ["10.1.5.65:5044"]

...其餘部分沒有改動,不須要修改1234567891011121314151617181920212223242512345678910111213141516171819202122232425
 

2.2 說明

  1. paths:指定要監控的日誌,目前按照Go語言的glob函數處理。沒有對配置目錄作遞歸處理,好比配置的若是是:

/var/log/* /*.log11
 

則只會去/var/log目錄的全部子目錄中尋找以」.log」結尾的文件,而不會尋找/var/log目錄下以」.log」結尾的文件。 
2. input_type:指定文件的輸入類型log(默認)或者stdin。 
3. document_type:設定Elasticsearch輸出時的document的type字段,也能夠用來給日誌進行分類。

把 elasticsearch和其下的全部都註釋掉(這裏Filebeat是新安裝的,只註釋這2處便可)

output:  #elasticsearch:  #   hosts: ["localhost:9200"]123123
 

開啓 logstash(刪除這兩行前的#號),並把localhost改成logstash服務器地址

 logstash:
    hosts: ["10.1.5.65:5044"]1212
 

若是開啓logstash了,那麼Logstash配置中要設置監聽端口 5044: 
這個是默認文件位置,若是不存在請自行查找

vim /etc/logstash/conf.d/beats-input.conf11
 

增長端口

input {
  beats {
    port => 5044
  }
}1234512345
 

3.啓動

3.1 測試

-d "Publish"11
 

若是能看到一堆東西輸出,表示正在向elasticsearch或logstash發送日誌。 
若是是elasticsearch能夠瀏覽:http://localhost:9200/_search?pretty 若是有新內容返回,表示ok 
測試正常後,Ctrl+C結束

3.2啓動

nohup ./filebeat -e -c filebeat.yml >/dev/null 2>&1 &11
 

上面會轉入後臺運行

3.3中止

查找進程 ID

ps -ef |grep filebeat11
 

KILL他

kill -9  id11
 

3.X kibana設置

若是使用 kibana 作日誌分析, 
在kibana裏,建立一個索引,注意pattern爲:filebeat-* 
這裏寫圖片描述

4.高級配置說明

http://kibana.logstash.es/content/beats/file.html

http://blog.csdn.net/a464057216/article/details/51233375

5.其餘說明

5.1Elasticsearch知道如何處理每一個日誌事件

默認的Elasticsearch須要的index template在安裝Filebeat的時候已經提供,路徑爲/etc/filebeat/filebeat.template.json,可使用以下命令裝載該模板:

curl -XPUT 'http://localhost:9200/_template/filebeat?pretty' -d@/etc/filebeat/filebeat.template.json11
 

若是運行成功則返回以下,表示模板已被接收

{
  "acknowledged" : true}123123
 

每次修改Filebeat配置,重啓Filebeat才能生效