1. 程式人生 > >ELK(實時日誌分析平臺)搭建必備基礎知識-------logstash

ELK(實時日誌分析平臺)搭建必備基礎知識-------logstash

預熱:基礎知識

       Logstash 是一個開源的資料收集引擎,它具有備實時資料傳輸能力。它可以統一過濾來自不同源的資料,並按照開發者的制定的規範輸出到目的地。 顧名思義,Logstash 收集資料物件就是日誌檔案。由於日誌檔案來源多(如:系統日誌、伺服器 日誌等),且內容雜亂,不便於人類進行觀察。因此,我們可以使用 Logstash 對日誌檔案進行收集和統一過濾,變成可讀性高的內容,方便開發者或運維人員觀察,從而有效的分析系統/專案執行的效能,做好監控和預警的準備工作等。

1,安裝

   下載:

wget https://artifacts.elastic.co/downloads/logstash/logstash-6.5.3.tar.gz

   解壓:

tar -zxvf logstash-6.5.3.tar.gz

2,配置檔案結構

#輸入(must)
input {
  #beats {
    #port => 5044
  #}
  #從檔案讀取日誌資訊
  #file {
    #path => "/var/log/messages"
    #type => "system"
    #start_position => "beginning"
   #}

}
#過濾(可選)
filter {
  

}
#輸出(must)
output {
  elasticsearch {
    hosts => ["http://localhost:9200"]
    #index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
    index => "test-log"
    #user => "elastic"
    #password => "changeme"
  }
}

2.1,input基本講解

輸入支援的型別有很多,官方文件記載

常用的有

input {
  #從filebeat裡面取值
  beats {
    port => 5044
  }
  #從檔案讀取日誌資訊
  #file {
    #path => "/var/log/messages"
    #type => "system"
    #start_position => "beginning"
   #}

}

 2.2,output基本講解

output同樣支援很多元件:

常用的輸出元件有:

output {
  #輸出到elasticseartch
  elasticsearch {
    hosts => ["http://localhost:9200"]
    #index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
    index => "test-log"
    #user => "elastic"
    #password => "changeme"
  }
  #file {
   #path => /usr/local/test-2013-05-29.txt
   #codec => line { format => "custom format: %{message}"}
   #gzip => false #Gzip the output stream before writing to disk.
  #}
  #stdout { codec => json } #輸出到控制檯 json格式
}

2.3,filter 基本講解

logstash支援多種過濾器,

常用的過濾器如下:

grok :用於匹配log,生成模組化資料,匹配成功的話,會在返回的欄位中多幾個自定義的模組化欄位

filter {
  grok {
    match => { "message" => "%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %
     {NUMBER:bytes} %{NUMBER:duration}" }# 55.3.244.1 GET /index.html 15824 0.043
  }

}

 example log:  55.3.244.1 GET /index.html 15824 0.043

返回的json資料:

{
    "offset":10,
    "@version":"1",
    "beat":{
        "name":"izuf6136vql89ybnyypmbaz",
        "hostname":"izuf6136vql89ybnyypmbaz",
        "version":"6.5.3"
    },
    "message":"55.3.244.1 GET /index.html 15824 0.043",
    "request":"/index.html",
    "input":{
        "type":"log"
    },
    "host":{
        "id":"963c2c41b08343f7b063dddac6b2e486",
        "name":"izuf6136vql89ybnyypmbaz",
        "os":{
            "version":"7 (Core)",
            "codename":"Core",
            "platform":"centos",
            "family":"redhat"
        },
        "containerized":true,
        "architecture":"x86_64"
    },
    "prospector":{
        "type":"log"
    },
    "bytes":"15824",
    "duration":"0.043",
    "source":"/usr/local/logs/catalina.out",
    "client":"55.3.244.1",
    "@timestamp":"2018-12-22T08:44:09.372Z",
    "tags":[
        "beats_input_codec_plain_applied"
    ],
    "method":"GET",
    "meta":{
        "cloud":{
            "availability_zone":"cn-shanghai-d",
            "region":"cn-shanghai",
            "instance_id":"i-uf6136vql89ybnyypmba",
            "provider":"ecs"
        }
    }
}

3,啟動

bin/logstash -f logstash.conf

更多配置:https://www.elastic.co/guide/en/logstash/current/plugins-filters-grok.html