生活随笔
收集整理的這篇文章主要介紹了
Logstash 基础入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文地址:Logstash 基礎入門
博客地址:http://www.extlight.com
一、前言
Logstash 是一個開源的數據收集引擎,它具有備實時數據傳輸能力。它可以統一過濾來自不同源的數據,并按照開發者的制定的規范輸出到目的地。
顧名思義,Logstash 收集數據對象就是日志文件。由于日志文件來源多(如:系統日志、服務器 日志等),且內容雜亂,不便于人類進行觀察。因此,我們可以使用 Logstash 對日志文件進行收集和統一過濾,變成可讀性高的內容,方便開發者或運維人員觀察,從而有效的分析系統/項目運行的性能,做好監控和預警的準備工作等。
二、安裝
Logstash 依賴 JDK1.8 ,因此在安裝之前請確保機器已經安裝和配置好 JDK1.8。
Logstash 下載
tar -zxvf logstash-5.6.3.tar.gz -C /usr cd logstash-5.6.3 三、組成結構
Logstash 通過管道進行運作,管道有兩個必需的元素,輸入和輸出,還有一個可選的元素,過濾器。
輸入插件從數據源獲取數據,過濾器插件根據用戶指定的數據格式修改數據,輸出插件則將數據寫入到目的地。如下圖:
我們先來一個簡單的案例:
bin/logstash -e 'input { stdin { } } output { stdout {} }' 啟動 Logstash 后,再鍵入 Hello World,結果如下:
[root@localhost logstash-5.6.3]# bin/logstash -e 'input { stdin { } } output { stdout {} }' Sending Logstash's logs to /usr/logstash-5.6.3/logs which is now configured via log4j2.properties [2017-10-27T00:17:43,438][INFO ][logstash.modules.scaffold] Initializing module {:module_name=>"fb_apache", :directory=>"/usr/logstash-5.6.3/modules/fb_apache/configuration"} [2017-10-27T00:17:43,440][INFO ][logstash.modules.scaffold] Initializing module {:module_name=>"netflow", :directory=>"/usr/logstash-5.6.3/modules/netflow/configuration"} [2017-10-27T00:17:43,701][INFO ][logstash.pipeline ] Starting pipeline {"id"=>"main", "pipeline.workers"=>1, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>5, "pipeline.max_inflight"=>125} [2017-10-27T00:17:43,744][INFO ][logstash.pipeline ] Pipeline main started The stdin plugin is now waiting for input: [2017-10-27T00:17:43,805][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600} Hello World 2017-10-27T07:17:51.034Z localhost.localdomain Hello World Hello World(輸入)經過 Logstash 管道(過濾)變成:2017-10-27T07:17:51.034Z localhost.localdomain Hello World (輸出)。
在生產環境中,Logstash 的管道要復雜很多,可能需要配置多個輸入、過濾器和輸出插件。
因此,需要一個配置文件管理輸入、過濾器和輸出相關的配置。配置文件內容格式如下:
# 輸入 input { ... } # 過濾器 filter { ... } # 輸出 output { ... } 根據自己的需求在對應的位置配置?輸入插件、過濾器插件、輸出插件?和?編碼解碼插件?即可。
四、插件用法
在使用插件之前,我們先了解一個概念:事件。
Logstash 每讀取一次數據的行為叫做事件。
在 Logstach 目錄中創建一個配置文件,名為 logstash.conf(名字任意)。
4.1 輸入插件
輸入插件允許一個特定的事件源可以讀取到 Logstash 管道中,配置在 input {} 中,且可以設置多個。
修改配置文件:
input { file { path => "/var/log/messages" type => "system" start_position => "beginning" } } output { stdout { codec => rubydebug } } 其中,messages 為系統日志。
保存文件。鍵入:
bin/logstash -f logstash.conf 在控制臺結果如下:
{ "@version" => "1", "host" => "localhost.localdomain", "path" => "/var/log/messages", "@timestamp" => 2017-10-29T07:30:02.601Z, "message" => "Oct 29 00:30:01 localhost systemd: Starting Session 16 of user root.", "type" => "system" } ...... 4.2 輸出插件
輸出插件將事件數據發送到特定的目的地,配置在 output {} 中,且可以設置多個。
修改配置文件:
input { file { path => "/var/log/error.log" type => "error" start_position => "beginning" } } output { elasticsearch { hosts => ["192.168.2.41:9200"] index => "error-%{+YYYY.MM.dd}" } } 其中,error.log 的內容格式如下:
2017-08-04 13:57:30.378 [http-nio-8080-exec-1] ERROR c.g.a.global.ResponseResultAdvice -設備數據為空 com.light.pay.common.exceptions.ValidationException: 設備數據為空 at com.light.pay.common.validate.Check.isTrue(Check.java:31) at com.light.attendance.controllers.cloudApi.DevicePushController.deviceInfoPush(DevicePushController.java:44) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) 2017-08-04 13:57:44.495 [http-nio-8080-exec-2] ERROR c.g.a.global.ResponseResultAdvice -Failed to invoke remote method: pushData, provider: dubbo://192.168.2.100:20880/com.light.attendance.api.DevicePushApi?application=salary-custom&default.check=false&default.timeout=30000&dubbo=2.8.4&interface=com.light.attendance.api.DevicePushApi&methods=getAllDevices,getDeviceById,pushData&organization=com.light.attendance&ow ...... 配置文件中使用 elasticsearch 輸出插件。輸出的日志信息將被保存到 Elasticsearch 中,索引名稱為 index 參數設置的格式。
如果讀者不了解 Elasticsearch 基礎內容,可以查看本站?《Elasticsearch 基礎入門》?文章或自行百度進行知識的補缺。
保存文件。鍵入:
bin/logstash -f logstash.conf 打開瀏覽器訪問?http://192.168.2.41:9100?使用 head 插件查看 Elasticsearch 數據,結果如下圖:
踩坑提醒:
file 輸入插件默認使用 “\n” 判斷日志中每行的邊界位置。error.log 是筆者自己編輯的錯誤日志,之前由于在復制粘貼日志內容時,忘記在內容末尾換行,導致日志數據始終無法導入到 Elasticsearch 中。 在此,提醒各位讀者這個關鍵點。
4.3 編碼解碼插件
編碼解碼插件本質是一種流過濾器,配合輸入插件或輸出插件使用。
從上圖中,我們發現一個問題:Java 異常日志被拆分成單行事件記錄到 Elasticsearch 中,這不符合開發者或運維人員的查看習慣。因此,我們需要對日志信息進行編碼將多行事件轉成單行事件記錄起來。
我們需要配置 Multiline codec 插件,這個插件可以將多行日志信息合并成一行,作為一個事件處理。
Logstash 默認沒有安裝該插件,需要開發者自行安裝。鍵入:
bin/logstash-plugin install logstash-codec-multiline 修改配置文件:
input { file { path => "/var/log/error.log" type => "error" start_position => "beginning" codec => multiline { pattern => "^\d" negate => true what => "previous" } } } output { elasticsearch { hosts => ["192.168.2.41:9200"] index => "error-%{+YYYY.MM.dd}" } } 保存文件。鍵入:
bin/logstash -f logstash.conf 使用 head 插件查看 Elasticsearch 數據,結果如下圖:
4.4 過濾器插件
過濾器插件位于 Logstash 管道的中間位置,對事件執行過濾處理,配置在 filter {},且可以配置多個。
本次測試使用 grok 插件演示,grok 插件用于過濾雜亂的內容,將其結構化,增加可讀性。
安裝:
bin/logstash-plugin install logstash-filter-grok 修改配置文件:
input { stdin {} } filter { grok { match => { "message" => "%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER :duration}" } } } output { stdout { codec => "rubydebug" } } 保存文件。鍵入:
bin/logstash -f logstash.conf 啟動成功后,我們輸入:
55.3.244.1 GET /index.html 15824 0.043 控制臺返回:
[root@localhost logstash-5.6.3]# bin/logstash -f logstash.conf Sending Logstash's logs to /root/logstash-5.6.3/logs which is now configured via log4j2.properties [2017-10-30T08:23:20,456][INFO ][logstash.modules.scaffold] Initializing module {:module_name=>"fb_apache", :directory=>"/root/logstash-5.6.3/modules/fb_apache/configuration"} [2017-10-30T08:23:20,459][INFO ][logstash.modules.scaffold] Initializing module {:module_name=>"netflow", :directory=>"/root/logstash-5.6.3/modules/netflow/configuration"} [2017-10-30T08:23:21,447][INFO ][logstash.pipeline ] Starting pipeline {"id"=>"main", "pipeline.workers"=>1, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>5, "pipeline.max_inflight"=>125} The stdin plugin is now waiting for input: [2017-10-30T08:23:21,516][INFO ][logstash.pipeline ] Pipeline main started [2017-10-30T08:23:21,573][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600} 55.3.244.1 GET /index.html 15824 0.043 { "duration" => "0.043", "request" => "/index.html", "@timestamp" => 2017-10-30T15:23:23.912Z, "method" => "GET", "bytes" => "15824", "@version" => "1", "host" => "localhost.localdomain", "client" => "55.3.244.1", "message" => "55.3.244.1 GET /index.html 15824 0.043" } 輸入的內容被匹配到相應的名字中。
五、參考資料
- https://www.elastic.co/guide/en/logstash/current/index.html?官方文檔
-
- 轉自 https://www.cnblogs.com/moonlightL/p/7760512.html
轉載于:https://www.cnblogs.com/xd502djj/p/9244213.html
總結
以上是生活随笔為你收集整理的Logstash 基础入门的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。