日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Logstash 基础入门

發(fā)布時(shí)間:2025/3/15 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Logstash 基础入门 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文地址:Logstash 基礎(chǔ)入門(mén)
博客地址:http://www.extlight.com

一、前言

Logstash 是一個(gè)開(kāi)源的數(shù)據(jù)收集引擎,它具有備實(shí)時(shí)數(shù)據(jù)傳輸能力。它可以統(tǒng)一過(guò)濾來(lái)自不同源的數(shù)據(jù),并按照開(kāi)發(fā)者的制定的規(guī)范輸出到目的地。

顧名思義,Logstash 收集數(shù)據(jù)對(duì)象就是日志文件。由于日志文件來(lái)源多(如:系統(tǒng)日志、服務(wù)器 日志等),且內(nèi)容雜亂,不便于人類(lèi)進(jìn)行觀察。因此,我們可以使用 Logstash 對(duì)日志文件進(jìn)行收集和統(tǒng)一過(guò)濾,變成可讀性高的內(nèi)容,方便開(kāi)發(fā)者或運(yùn)維人員觀察,從而有效的分析系統(tǒng)/項(xiàng)目運(yùn)行的性能,做好監(jiān)控和預(yù)警的準(zhǔn)備工作等。

二、安裝

Logstash 依賴(lài) JDK1.8 ,因此在安裝之前請(qǐng)確保機(jī)器已經(jīng)安裝和配置好 JDK1.8。

Logstash 下載

  • tar -zxvf logstash-5.6.3.tar.gz -C /usr
  • cd logstash-5.6.3
  • 三、組成結(jié)構(gòu)

    Logstash 通過(guò)管道進(jìn)行運(yùn)作,管道有兩個(gè)必需的元素,輸入和輸出,還有一個(gè)可選的元素,過(guò)濾器。

    輸入插件從數(shù)據(jù)源獲取數(shù)據(jù),過(guò)濾器插件根據(jù)用戶指定的數(shù)據(jù)格式修改數(shù)據(jù),輸出插件則將數(shù)據(jù)寫(xiě)入到目的地。如下圖:

    我們先來(lái)一個(gè)簡(jiǎn)單的案例:

  • bin/logstash -e 'input { stdin { } } output { stdout {} }'
  • 啟動(dòng) Logstash 后,再鍵入 Hello World,結(jié)果如下:

  • [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(輸入)經(jīng)過(guò) Logstash 管道(過(guò)濾)變成:2017-10-27T07:17:51.034Z localhost.localdomain Hello World (輸出)。

    在生產(chǎn)環(huán)境中,Logstash 的管道要復(fù)雜很多,可能需要配置多個(gè)輸入、過(guò)濾器和輸出插件。

    因此,需要一個(gè)配置文件管理輸入、過(guò)濾器和輸出相關(guān)的配置。配置文件內(nèi)容格式如下:

  • # 輸入
  • input {
  • ...
  • }
  • # 過(guò)濾器
  • filter {
  • ...
  • }
  • # 輸出
  • output {
  • ...
  • }
  • 根據(jù)自己的需求在對(duì)應(yīng)的位置配置?輸入插件、過(guò)濾器插件、輸出插件?和?編碼解碼插件?即可。

    四、插件用法

    在使用插件之前,我們先了解一個(gè)概念:事件。

    Logstash 每讀取一次數(shù)據(jù)的行為叫做事件。

    在 Logstach 目錄中創(chuàng)建一個(gè)配置文件,名為 logstash.conf(名字任意)。

    4.1 輸入插件

    輸入插件允許一個(gè)特定的事件源可以讀取到 Logstash 管道中,配置在 input {} 中,且可以設(shè)置多個(gè)。

    修改配置文件:

  • input {
  • # 從文件讀取日志信息
  • file {
  • path => "/var/log/messages"
  • type => "system"
  • start_position => "beginning"
  • }
  • }
  • # filter {
  • #
  • # }
  • output {
  • # 標(biāo)準(zhǔn)輸出
  • stdout { codec => rubydebug }
  • }
  • 其中,messages 為系統(tǒng)日志。

    保存文件。鍵入:

  • bin/logstash -f logstash.conf
  • 在控制臺(tái)結(jié)果如下:

  • {
  • "@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 輸出插件

    輸出插件將事件數(shù)據(jù)發(fā)送到特定的目的地,配置在 output {} 中,且可以設(shè)置多個(gè)。

    修改配置文件:

  • input {
  • # 從文件讀取日志信息
  • file {
  • path => "/var/log/error.log"
  • type => "error"
  • start_position => "beginning"
  • }
  • }
  • # filter {
  • #
  • # }
  • output {
  • # 輸出到 elasticsearch
  • elasticsearch {
  • hosts => ["192.168.2.41:9200"]
  • index => "error-%{+YYYY.MM.dd}"
  • }
  • }
  • 其中,error.log 的內(nèi)容格式如下:

  • 2017-08-04 13:57:30.378 [http-nio-8080-exec-1] ERROR c.g.a.global.ResponseResultAdvice -設(shè)備數(shù)據(jù)為空
  • com.light.pay.common.exceptions.ValidationException: 設(shè)備數(shù)據(jù)為空
  • 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 中,索引名稱(chēng)為 index 參數(shù)設(shè)置的格式。

    如果讀者不了解 Elasticsearch 基礎(chǔ)內(nèi)容,可以查看本站?《Elasticsearch 基礎(chǔ)入門(mén)》?文章或自行百度進(jìn)行知識(shí)的補(bǔ)缺。

    保存文件。鍵入:

  • bin/logstash -f logstash.conf
  • 打開(kāi)瀏覽器訪問(wèn)?http://192.168.2.41:9100?使用 head 插件查看 Elasticsearch 數(shù)據(jù),結(jié)果如下圖:

    踩坑提醒:
    file 輸入插件默認(rèn)使用 “\n” 判斷日志中每行的邊界位置。error.log 是筆者自己編輯的錯(cuò)誤日志,之前由于在復(fù)制粘貼日志內(nèi)容時(shí),忘記在內(nèi)容末尾換行,導(dǎo)致日志數(shù)據(jù)始終無(wú)法導(dǎo)入到 Elasticsearch 中。 在此,提醒各位讀者這個(gè)關(guān)鍵點(diǎn)。

    4.3 編碼解碼插件

    編碼解碼插件本質(zhì)是一種流過(guò)濾器,配合輸入插件或輸出插件使用。

    從上圖中,我們發(fā)現(xiàn)一個(gè)問(wèn)題:Java 異常日志被拆分成單行事件記錄到 Elasticsearch 中,這不符合開(kāi)發(fā)者或運(yùn)維人員的查看習(xí)慣。因此,我們需要對(duì)日志信息進(jìn)行編碼將多行事件轉(zhuǎn)成單行事件記錄起來(lái)。

    我們需要配置 Multiline codec 插件,這個(gè)插件可以將多行日志信息合并成一行,作為一個(gè)事件處理。

    Logstash 默認(rèn)沒(méi)有安裝該插件,需要開(kāi)發(fā)者自行安裝。鍵入:

  • bin/logstash-plugin install logstash-codec-multiline
  • 修改配置文件:

  • input {
  • # 從文件讀取日志信息
  • file {
  • path => "/var/log/error.log"
  • type => "error"
  • start_position => "beginning"
  • # 使用 multiline 插件
  • codec => multiline {
  • # 通過(guò)正則表達(dá)式匹配,具體配置根據(jù)自身實(shí)際情況而定
  • pattern => "^\d"
  • negate => true
  • what => "previous"
  • }
  • }
  • }
  • # filter {
  • #
  • # }
  • output {
  • # 輸出到 elasticsearch
  • elasticsearch {
  • hosts => ["192.168.2.41:9200"]
  • index => "error-%{+YYYY.MM.dd}"
  • }
  • }
  • 保存文件。鍵入:

  • bin/logstash -f logstash.conf
  • 使用 head 插件查看 Elasticsearch 數(shù)據(jù),結(jié)果如下圖:

    4.4 過(guò)濾器插件

    過(guò)濾器插件位于 Logstash 管道的中間位置,對(duì)事件執(zhí)行過(guò)濾處理,配置在 filter {},且可以配置多個(gè)。

    本次測(cè)試使用 grok 插件演示,grok 插件用于過(guò)濾雜亂的內(nèi)容,將其結(jié)構(gòu)化,增加可讀性。

    安裝:

  • 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
  • 啟動(dòng)成功后,我們輸入:

  • 55.3.244.1 GET /index.html 15824 0.043
  • 控制臺(tái)返回:

  • [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"
  • }
  • 輸入的內(nèi)容被匹配到相應(yīng)的名字中。

    五、參考資料

      • https://www.elastic.co/guide/en/logstash/current/index.html?官方文檔
      • 轉(zhuǎn)自 https://www.cnblogs.com/moonlightL/p/7760512.html

    轉(zhuǎn)載于:https://www.cnblogs.com/xd502djj/p/9244213.html

    總結(jié)

    以上是生活随笔為你收集整理的Logstash 基础入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。