ELK之Logstash

版本與Elasticsearch一致

**1、下載并安裝適用于 Linux 的存檔編輯
**Elasticsearch v8.4.1 的 Linux 存檔可以按如下方式下載和安裝:

wget https://artifacts.elastic.co/downloads/logstash/logstash-7.17.6-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.17.6-linux-x86_64.tar.gz.sha512
shasum -a 512 -c logstash-7.17.6-linux-x86_64.tar.gz.sha512
tar -xzf logstash-7.17.6-linux-x86_64.tar.gz
cd logstash-7.17.6/

2、logstash.yml配置文件

  • 基礎(chǔ)配置:

#該值為true時(shí),開(kāi)啟轉(zhuǎn)義(避免寫(xiě)法格式錯(cuò)誤導(dǎo)致浪費(fèi)太多時(shí)間)
config.support_escapes: true
<meta name="wolai" content="bAHmkmE9rVVWdBhyjXgUDd1666174563784">

配合ES開(kāi)啟認(rèn)證:

xpack.monitoring.enabled: true
xpack.monitoring.elasticsearch.username: logstash_system > xpack.monitoring.elasticsearch.password: *
xpack.monitoring.elasticsearch.hosts: ["http://ip:9200"]

如開(kāi)啟認(rèn)證,pipeline output如使用es,需配置user/password(elastic/****)

  • 配置說(shuō)明如下:

    ------------ Node identity ------------
    #節(jié)點(diǎn)名稱(chēng),默認(rèn)主機(jī)名
    node.name: test

    ------------ Data path ------------------
    #數(shù)據(jù)存儲(chǔ)路徑,默認(rèn)LOGSTASH_HOME/data
    path.data:

    ------------ Pipeline Settings --------------
    #pipeline ID,默認(rèn)main
    pipeline.id: main

    #輸出通道的工作workers數(shù)據(jù)量,默認(rèn)cpu核心數(shù)
    pipeline.workers:

    How many events to retrieve from inputs before sending to filters+workers
    #單個(gè)工作線程在嘗試執(zhí)行其過(guò)濾器和輸出之前將從輸入收集的最大事件數(shù)量,默認(rèn)125
    pipeline.batch.size: 125

    #將較小的批處理分派給管道之前,等待的毫秒數(shù),默認(rèn)50ms
    pipeline.batch.delay: 50

    #此值為true時(shí),即使內(nèi)存中仍然有運(yùn)行中事件,也會(huì)強(qiáng)制Logstash在關(guān)機(jī)期間退出
    #默認(rèn)flase
    pipeline.unsafe_shutdown: false

    #管道事件排序
    #選項(xiàng)有;auto,true,false,默認(rèn)auto
    pipeline.ordered: auto

    ------------ Pipeline Configuration Settings --------------
    #配置文件路徑
    path.config:

    #主管道的管道配置字符串
    config.string:

    #該值為true時(shí),檢查配置是否有效,然后退出,默認(rèn)false
    config.test_and_exit: false

    #該值為true時(shí),會(huì)定期檢查配置是否已更改,并在更改后重新加載配置,默認(rèn)false
    config.reload.automatic: false

    #檢查配置文件更改的時(shí)間間隔,默認(rèn)3s
    config.reload.interval: 3s

    #該值為true時(shí),將完整編譯的配置顯示為調(diào)試日志消息,默認(rèn)false
    config.debug: false

    #該值為true時(shí),開(kāi)啟轉(zhuǎn)義
    config.support_escapes: false

    ------------ HTTP API Settings -------------
    #是否開(kāi)啟http訪問(wèn),默認(rèn)true
    http.enabled: true

    #綁定主機(jī)地址,可以是ip,主機(jī)名,默認(rèn)127.0.0.1
    http.host: 127.0.0.1

    #服務(wù)監(jiān)聽(tīng)端口,可以是單個(gè)端口,也可以是范圍端口,默認(rèn)9600-9700
    http.port: 9600-9700

    ------------ Module Settings ---------------
    #模塊定義,必須為數(shù)組
    #模塊變量名格式必須為var.PLUGIN_TYPE.PLUGIN_NAME.KEY
    modules:
    - name: MODULE_NAME
    var.PLUGINTYPE1.PLUGINNAME1.KEY1: VALUE
    var.PLUGINTYPE1.PLUGINNAME1.KEY2: VALUE
    var.PLUGINTYPE2.PLUGINNAME1.KEY1: VALUE
    var.PLUGINTYPE3.PLUGINNAME3.KEY1: VALUE

    ------------ Queuing Settings --------------
    #事件緩沖的內(nèi)部排隊(duì)模型,可選項(xiàng):memory,persisted,默認(rèn)memory
    queue.type: memory

    #啟用持久隊(duì)列(queue.type: persisted)后將在其中存儲(chǔ)數(shù)據(jù)文件的目錄路徑
    #默認(rèn)path.data/queue
    path.queue:

    #啟用持久隊(duì)列(queue.type: persisted)時(shí)使用的頁(yè)面數(shù)據(jù)文件的大小
    #默認(rèn)64mb
    queue.page_capacity: 64mb

    #啟用持久隊(duì)列(queue.type: persisted)后,隊(duì)列中未讀事件的最大數(shù)量
    #默認(rèn)0
    queue.max_events: 0

    #啟用持久隊(duì)列(queue.type: persisted)后,隊(duì)列的總?cè)萘?,單位字?jié),默認(rèn)1024mb
    queue.max_bytes: 1024mb

    #啟用持久隊(duì)列(queue.type: persisted)后,在強(qiáng)制檢查點(diǎn)之前的最大ACKed事件數(shù),默認(rèn)1024
    queue.checkpoint.acks: 1024

    #啟用持久隊(duì)列(queue.type: persisted)后,在強(qiáng)制檢查點(diǎn)之前的最大書(shū)面事件數(shù),默認(rèn)1024
    queue.checkpoint.writes: 1024

    If using queue.type: persisted, the interval in milliseconds when a checkpoint is forced on the head page
    Default is 1000, 0 for no periodic checkpoint.

    #啟用持久隊(duì)列(queue.type: persisted)后,執(zhí)行檢查點(diǎn)的時(shí)間間隔,單位ms,默認(rèn)1000ms
    queue.checkpoint.interval: 1000

    ------------ Dead-Letter Queue Settings --------------
    #是否啟用插件支持的DLQ功能的標(biāo)志,默認(rèn)false
    dead_letter_queue.enable: false

    #dead_letter_queue.enable為true時(shí),每個(gè)死信隊(duì)列的最大大小
    #若死信隊(duì)列的大小超出該值,則被刪除,默認(rèn)1024mb
    dead_letter_queue.max_bytes: 1024mb

    #死信隊(duì)列存儲(chǔ)路徑,默認(rèn)path.data/dead_letter_queue
    path.dead_letter_queue:

    ------------ Debugging Settings --------------
    #日志輸出級(jí)別,選項(xiàng):fatal,error,warn,info,debug,trace,默認(rèn)info
    log.level: info

    #日志格式,選項(xiàng):json,plain,默認(rèn)plain
    log.format:

    #日志路徑,默認(rèn)LOGSTASH_HOME/logs
    path.logs:

    ------------ Other Settings --------------
    #插件存儲(chǔ)路徑
    path.plugins: []

    #是否啟用每個(gè)管道在不同日志文件中的日志分隔
    #默認(rèn)false
    pipeline.separate_logs: false

3、pipeline配置

  • 多個(gè)管道

    若是須要在同一進(jìn)程中運(yùn)行多個(gè)管道,Logstash提供了一種經(jīng)過(guò)名為pipelines.yml的配置文件完成此操做的方法,這個(gè)文件必須放在path.settings文件夾,并遵循此結(jié)構(gòu):

    pipeline.id: my-pipeline_1

    path.config: "/etc/path/to/p1.config"

    pipeline.workers: 3

    pipeline.id: my-other-pipeline
    path.config: "/etc/different/path/p2.cfg"
    queue.type: persisted

    該文件在YAML中格式化并包含一個(gè)字典列表,其中每一個(gè)字典描述一個(gè)管道,每一個(gè)鍵/值對(duì)指定該管道的設(shè)置。這個(gè)示例展現(xiàn)了兩個(gè)不一樣的管道,它們由ID和配置路徑描述,對(duì)于第一個(gè)管道,pipeline.workers的值被設(shè)置為3,而在另外一個(gè)則啟用持久隊(duì)列特性,在pipelines.yml文件中未顯式設(shè)置的設(shè)置值將退回到pipelines.yml設(shè)置文件中指定的默認(rèn)值。

    性能在沒(méi)有參數(shù)的狀況下啟動(dòng)Logstash時(shí),它將讀取pipelines.yml文件并實(shí)例化文件中指定的全部管道,另外一方面,當(dāng)你使用-e-f時(shí),Logstash會(huì)忽略pipelines.yml文件,并記錄對(duì)此的警告。

4、啟動(dòng)命令

sudo -u elasticsearch ./bin/logstash

# 指定文件運(yùn)行
sudo -u elasticsearch ./bin/logstash -f aaa.config

# -t 指定文件運(yùn)行,驗(yàn)證文件是否存在問(wèn)題
sudo -u elasticsearch ./bin/logstash -f aaa.config -t

nohup sudo -u elasticsearch ./logstash/bin/logstash  > /www/logs/logstash.log &1 &

nohup sudo -u elasticsearch ./bin/logstash  > /www/logs/logstash.log &1 &

5、使用logstash過(guò)濾出特定格式的日志

日志內(nèi)容:[2018-11-24 08:33:43,253][ERROR][http-nio-8080-exec-4][com.hh.test.logs.LogsApplication][code:200,msg:測(cè)試錄入錯(cuò)誤日志,param:{}]

filter {
  if "nova" in [tags]{
    grok {
      # 篩選過(guò)濾
      match => {
        "message" => "(?<date>\d{4}\-\d{2}\-\d{2}\s\d{2}:\d{2}:\d{2},\d{3})\]\[(?<level>[A-Z]{4,5})\]\[(?<thread>[A-Za-z0-9/\-]{4,40})\]\[(?<class>[A-Za-z0-9/.]{4,40})\]\[(?<msg>.*)"
      }
    mutate {
      remove_field => [
        "message",
      ]
    }
    # 不匹配正則則刪除,匹配正則用=~
    if [level] !~ "(ERROR|WARN|INFO)" {
      # 刪除日志
      drop {}
    }
  }
}

正則表達(dá)式在線的調(diào)試庫(kù),以供參考: http://grokdebug.herokuapp.com/
需注意:”-“ 破折號(hào),需要轉(zhuǎn)移不然會(huì)出現(xiàn)解析異常

6、異常問(wèn)題

  • 配置文件格式問(wèn)題

    "LogStash::ConfigurationError", :message=>"Expected one of #, input, filter, output at line 1

    • 方式1:配置文件設(shè)置
    config.support_escapes: true
    
    • 方式2:?jiǎn)栴}需區(qū)分部署環(huán)境系統(tǒng)(windows/linux)

      1. windows

        文件編碼格式問(wèn)題,改為UTF-8無(wú)BOM模式(可參考相關(guān)鏈接:https://blog.csdn.net/qq_32131499/article/details/91338972 );

        如不行,可考慮參考:http://www.04007.cn/article/835.html

      2. linux

        如第一步還不生效,可以直接用vi命令編寫(xiě)簡(jiǎn)單的配置驗(yàn)證。大有可能是從windows創(chuàng)建提交導(dǎo)致該問(wèn)題。

      3. 如果未能定位,則考慮通過(guò)命令行 logstash -f **.config -t 指定文件執(zhí)行,看看是否成功。

  • 如果開(kāi)啟pipeline,也出現(xiàn)提示文件格式化問(wèn)題

    • 先確認(rèn)日志中是否出現(xiàn)“Ignoring the 'pipelines.yml' file because modules or command line options are specified”,如出現(xiàn)上述警告內(nèi)容,代表未成功開(kāi)啟pipeline配置導(dǎo)致。

      比如:

      logstash.yml開(kāi)啟pipeline 單通道配置與 pipeline.yml配置同時(shí)開(kāi)啟

    • 如上述未能解決,參照第一個(gè)問(wèn)題。

  • 如果出現(xiàn)某個(gè)通道數(shù)據(jù)抓取不到?

    • 考慮抓取的目錄是否有權(quán)限訪問(wèn)??赏ㄟ^(guò)切換logstash運(yùn)行用戶去訪問(wèn)該文件,來(lái)確認(rèn)有權(quán)限。
    • 如果不能訪問(wèn),可通過(guò)root用戶運(yùn)行,確認(rèn)數(shù)據(jù)是否同步成功。如同步成功,則授權(quán)目錄權(quán)限為755即可達(dá)到目的。

??參考文獻(xiàn):

http://www.noobyard.com/article/p-ucnfmacz-gk.html

https://www.elastic.co/guide/en/logstash/7.17/introduction.html

https://blog.51cto.com/u_15047490/4228036

樣例:

input {
    file {
        path => "/www/log/java-web/log_info.log"
        type => "java-web-info"
        start_position => "beginning"
        # 多行輸入
        codec => multiline {
            pattern => "^%{TIMESTAMP_ISO8601} "
            negate => true
            what => previous
        }
    }

    file {
        path => "/www/log/java-web/log_error.log"
        type => "java-web-error"
        start_position => "beginning"
        # 多行輸入
        codec => multiline {
            pattern => "^%{TIMESTAMP_ISO8601} "
            negate => true
            what => previous
        }
    }
}

filter {

    if [type] == "java-web-info" {
    grok {
        match => {
            "message" => "(?<date>\d{4}\-\d{2}\-\d{2}\s\d{2}:\d{2}:\d{2}.\d{3})\s(?<level>[A-Z]{4,5})\s{1,2}(?<pid>\d{1,20})\s\-{1,5}\s\[(?<thread>[A-Za-z0-9./-]{4,40})\]\s(?<method>[A-Z
a-z0-9.\\(\\:/\-\\)]{0,200}):\s(?<msg>.*)"
        }
    }

    # 過(guò)濾debug
    if [level] !~ "(ERROR|WARN|INFO)" {
        drop {}
    }
    # 過(guò)濾指定內(nèi)容
    if [thread] == "com.alibaba.nacos.naming.push.receiver" {
        drop {}
    }

    }
}

output {

    if [type] == "java-web-info" {
        elasticsearch {
            hosts => ["127.0.0.1:9200"]
            index => "java-web-info-%{+YYYY.MM.dd}"
            #開(kāi)啟認(rèn)證后,需配置
            #user => "elastic"
            #password => "******"
        }
    }
    
    if [type] == "java-web-error" {
        elasticsearch {
            hosts => ["127.0.0.1:9200"]
            index => "java-web-error-%{+YYYY.MM.dd}"
            #開(kāi)啟認(rèn)證后,需配置
            #user => "elastic"
            #password => "******"
        }
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容