歡迎你們前往騰訊雲+社區,獲取更多騰訊海量技術實踐乾貨哦~css
本文由 michelmu發表於 雲+社區專欄
Elasticsearch是當前主流的分佈式大數據存儲和搜索引擎,能夠爲用戶提供強大的全文本檢索能力,普遍應用於日誌檢索,全站搜索等領域。Logstash做爲Elasicsearch經常使用的實時數據採集引擎,能夠採集來自不一樣數據源的數據,並對數據進行處理後輸出到多種輸出源,是Elastic Stack 的重要組成部分。本文從Logstash的工做原理,使用示例,部署方式及性能調優等方面入手,爲你們提供一個快速入門Logstash的方式。文章最後也給出了一些深刻了解Logstash的的連接,以方便你們根據須要詳細瞭解。html
Logstash簡介linux
Logstash處理過程ios
如上圖,Logstash的數據處理過程主要包括:Inputs, Filters, Outputs 三部分, 另外在Inputs和Outputs中可使用Codecs對數據格式進行處理。這四個部分均以插件形式存在,用戶經過定義pipeline配置文件,設置須要使用的input,filter,output, codec插件,以實現特定的數據採集,數據處理,數據輸出等功能git
能夠點擊每一個模塊後面的詳細參考連接瞭解該模塊的插件列表及對應功能redis
第一個示例Logstash將採用標準輸入和標準輸出做爲input和output,而且不指定filterdocker
cd logstash-6.4.0 bin/logstash -e 'input { stdin { } } output { stdout {} }'
{ "@version" => "1", "host" => "localhost", "@timestamp" => 2018-09-18T12:39:38.514Z, "message" => "hello world" }
Logstash會自動爲數據添加@version, host, @timestamp等字段json
在這個示例中Logstash從標準輸入中得到數據,僅在數據中添加一些簡單字段後將其輸出到標準輸出。centos
這個示例將採用Filebeat input插件(Elastic Stack中的輕量級數據採集程序)採集本地日誌,而後將結果輸出到標準輸出安全
filebeat.yml配置以下(paths改成日誌實際位置,不一樣版本beats配置可能略有變化,請根據狀況調整)
filebeat.prospectors: - input\_type: log paths: - /path/to/file/logstash-tutorial.log output.logstash: hosts: "localhost:5044"
啓動命令:
./filebeat -e -c filebeat.yml -d "publish"
1)建立first-pipeline.conf文件內容以下(該文件爲pipeline配置文件,用於指定input,filter, output等):
input { beats { port => "5044" } } #filter { #} output { stdout { codec => rubydebug } }
codec => rubydebug用於美化輸出[參考]
2)驗證配置(注意指定配置文件的路徑):
./bin/logstash -f first-pipeline.conf --config.test_and_exit
3)啓動命令:
./bin/logstash -f first-pipeline.conf --config.reload.automatic
--config.reload.automatic選項啓用動態重載配置功能
4)預期結果:
能夠在Logstash的終端顯示中看到,日誌文件被讀取並處理爲以下格式的多條數據
{ "@timestamp" => 2018-10-09T12:22:39.742Z, "offset" => 24464, "@version" => "1", "input_type" => "log", "beat" => { "name" => "VM_136_9_centos", "hostname" => "VM_136_9_centos", "version" => "5.6.10" }, "host" => "VM_136_9_centos", "source" => "/data/home/michelmu/workspace/logstash-tutorial.log", "message" => "86.1.76.62 - - [04/Jan/2015:05:30:37 +0000] \"GET /style2.css HTTP/1.1\" 200 4877 \"http://www.semicomplete.com/projects/xdotool/\" \"Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20140205 Firefox/24.0 Iceweasel/24.3.0\"", "type" => "log", "tags" => [ [0] "beats_input_codec_plain_applied" ] }
相對於示例2.1,該示例使用了filebeat input插件從日誌中獲取一行記錄,這也是Elastic stack獲取日誌數據最多見的一種方式。另外該示例還採用了rubydebug codec 對輸出的數據進行顯示美化。
能夠看到雖然示例2.2使用filebeat從日誌中讀取數據,並將數據輸出到標準輸出,可是日誌內容做爲一個總體被存放在message字段中,這樣對後續存儲及查詢都極爲不便。能夠爲該pipeline指定一個grok filter來對日誌格式進行處理
input { beats { port => "5044" } } filter { grok { match => { "message" => "%{COMBINEDAPACHELOG}"} } } output { stdout { codec => rubydebug } }
sudo rm data/registry sudo ./filebeat -e -c filebeat.yml -d "publish"
{ "request" => "/style2.css", "agent" => "\"Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20140205 Firefox/24.0 Iceweasel/24.3.0\"", "offset" => 24464, "auth" => "-", "ident" => "-", "input_type" => "log", "verb" => "GET", "source" => "/data/home/michelmu/workspace/logstash-tutorial.log", "message" => "86.1.76.62 - - [04/Jan/2015:05:30:37 +0000] \"GET /style2.css HTTP/1.1\" 200 4877 \"http://www.semicomplete.com/projects/xdotool/\" \"Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20140205 Firefox/24.0 Iceweasel/24.3.0\"", "type" => "log", "tags" => [ [0] "beats_input_codec_plain_applied" ], "referrer" => "\"http://www.semicomplete.com/projects/xdotool/\"", "@timestamp" => 2018-10-09T12:24:21.276Z, "response" => "200", "bytes" => "4877", "clientip" => "86.1.76.62", "@version" => "1", "beat" => { "name" => "VM_136_9_centos", "hostname" => "VM_136_9_centos", "version" => "5.6.10" }, "host" => "VM_136_9_centos", "httpversion" => "1.1", "timestamp" => "04/Jan/2015:05:30:37 +0000" }
能夠看到message中的數據被詳細解析出來了
Logstash中的一些filter能夠根據現有數據生成一些新的數據,如geoip能夠根據ip生成經緯度信息
input { beats { port => "5044" } } filter { grok { match => { "message" => "%{COMBINEDAPACHELOG}"} } geoip { source => "clientip" } } output { stdout { codec => rubydebug } }
{ "request" => "/style2.css", "agent" => "\"Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20140205 Firefox/24.0 Iceweasel/24.3.0\"", "geoip" => { "timezone" => "Europe/London", "ip" => "86.1.76.62", "latitude" => 51.5333, "continent_code" => "EU", "city_name" => "Willesden", "country_name" => "United Kingdom", "country_code2" => "GB", "country_code3" => "GB", "region_name" => "Brent", "location" => { "lon" => -0.2333, "lat" => 51.5333 }, "postal_code" => "NW10", "region_code" => "BEN", "longitude" => -0.2333 }, "offset" => 24464, "auth" => "-", "ident" => "-", "input_type" => "log", "verb" => "GET", "source" => "/data/home/michelmu/workspace/logstash-tutorial.log", "message" => "86.1.76.62 - - [04/Jan/2015:05:30:37 +0000] \"GET /style2.css HTTP/1.1\" 200 4877 \"http://www.semicomplete.com/projects/xdotool/\" \"Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20140205 Firefox/24.0 Iceweasel/24.3.0\"", "type" => "log", "tags" => [ [0] "beats_input_codec_plain_applied" ], "referrer" => "\"http://www.semicomplete.com/projects/xdotool/\"", "@timestamp" => 2018-10-09T12:37:46.686Z, "response" => "200", "bytes" => "4877", "clientip" => "86.1.76.62", "@version" => "1", "beat" => { "name" => "VM_136_9_centos", "hostname" => "VM_136_9_centos", "version" => "5.6.10" }, "host" => "VM_136_9_centos", "httpversion" => "1.1", "timestamp" => "04/Jan/2015:05:30:37 +0000" }
能夠看到根據ip派生出了許多地理位置信息數據
Logstash做爲Elastic stack的重要組成部分,其最經常使用的功能是將數據導入到Elasticssearch中。將Logstash中的數據導入到Elasticsearch中操做也很是的方便,只須要在pipeline配置文件中增長Elasticsearch的output便可。
input { beats { port => "5044" } } filter { grok { match => { "message" => "%{COMBINEDAPACHELOG}"} } geoip { source => "clientip" } } output { elasticsearch { hosts => [ "localhost:9200" ] } }
curl -XGET 'http://172.16.16.17:9200/logstash-2018.10.09/_search?pretty&q=response=200'
kibana圖示
Logstash提供了大量的Input, filter, output, codec的插件,用戶能夠根據本身的須要,使用一個或多個組件實現本身的功能,固然用戶也能夠自定義插件以實現更爲定製化的功能。自定義插件能夠參考[logstash input插件開發]
演示過如何快速使用Logstash後,如今詳細講述一下Logstash的部署方式。
logstash的目錄主要包括:根目錄、bin目錄、配置目錄、日誌目錄、插件目錄、數據目錄
不一樣安裝方式各目錄的默認位置參考[此處]
當單個Logstash沒法知足性能需求時,能夠採用橫向擴展的方式來提升Logstash的處理能力。橫向擴展的多個Logstash相互獨立,採用相同的pipeline配置,另外能夠在這多個Logstash前增長一個LoadBalance,以實現多個Logstash的負載均衡。
(2)
系統性能指標
:
(3)
JVM堆檢查
:
Logstash做爲Elastic Stack的重要組成部分,在Elasticsearch數據採集和處理過程當中扮演着重要的角色。本文經過簡單示例的演示和Logstash基礎知識的鋪陳,但願能夠幫助初次接觸Logstash的用戶對Logstash有一個總體認識,並能較爲快速上手。對於Logstash的高階使用,仍須要用戶在使用過程當中結合實際狀況查閱相關資源深刻研究。固然也歡迎你們積極交流,並對文中的錯誤提出寶貴意見。
相關閱讀
大數據基礎系列之spark的監控體系介紹
Neutron lbaas代理https實踐
【每日課程推薦】機器學習實戰!快速入門在線廣告業務及CTR相應知識
此文已由做者受權騰訊雲+社區發佈,更多原文請點擊
搜索關注公衆號「雲加社區」,第一時間獲取技術乾貨,關注後回覆1024 送你一份技術課程大禮包!
海量技術實踐經驗,盡在雲加社區!