Logstash 與 Beats 入門


公號:碼農充電站pro
主頁:https://codeshellme.github.io

Logstash 是一款免費開放的服務器端數據處理管道,能夠從多個來源采集並轉換數據,然后將數據發送到后端存儲中。

在這里插入圖片描述

1,Logstash 處理流程

Logstash 的處理流程分為三個階段,這三個階段合稱為一個 Pipeline

  • Inputs:將輸入數據轉換成 Events。
  • Filters:處理修改 Events。
  • Outputs:將 Events 輸出到存儲。

在這里插入圖片描述

同時在輸入/輸出階段可以對數據進行編解碼處理。

用戶通過配置文件告訴 Logstash 如何處理數據。

2,Logstash 插件

Logstash 的每個處理階段都由一個或多個插件來完成,Logstash 目前支持 200 多個插件

Logstash 的一些常用插件:

  • Inputs 階段
    • stdin、file
    • beats、log4j
    • elasticsearch、jdbc、kafka、rabbitmq、redis
    • jmx、http、websocket、tcp、udp
  • Filters 階段
    • mutate
    • metrics
    • ruby
    • csv
  • Outputs 階段
    • email、pageduty
    • elasticsearch、kafka、mongodb
    • http、tcp、websocket
  • 編碼處理
    • line、multiline
    • json、dots

3,Logstash Queue

Logstash 在實際處理數據時,會先將輸入數據放入隊列中,作為緩沖。

在這里插入圖片描述

Logstash Queue 分為兩種:

  • Memory Queue(默認方式):放在內存中;如果意外宕機數據會丟失。
  • Persistent Queue:會進行持久化;意外宕機數據不會丟失。
    • 可通過 queue.max_bytes 參數設置隊列能存放的數據大小,默認為 1G。

4,Logstash 使用示例

Logstash 通過 -e 參數在命令行指定一個 pipeline,通過 -f 參數指定一個配置文件。

4.1,-e 參數

通過 -e 在命令行指定一個 pipeline

logstash -e "input{stdin{codec=>line}}output{stdout{codec=>rubydebug}}"
logstash -e "input{stdin{codec=>json}}output{stdout{codec=>rubydebug}}"
logstash -e "input{stdin{codec=>line}}output{stdout{codec=>dots}}"

4.2,-f 參數

數據示例,一個 movies.csv 文件:

movieId,title,genres
1,Toy Story (1995),Adventure|Animation|Children|Comedy|Fantasy
2,Jumanji (1995),Adventure|Children|Fantasy
3,Grumpier Old Men (1995),Comedy|Romance
4,Waiting to Exhale (1995),Comedy|Drama|Romance
5,Father of the Bride Part II (1995),Comedy
6,Heat (1995),Action|Crime|Thriller
7,Sabrina (1995),Comedy|Romance
8,Tom and Huck (1995),Adventure|Children
9,Sudden Death (1995),Action
10,GoldenEye (1995),Action|Adventure|Thriller
11,"American President, The (1995)",Comedy|Drama|Romance
12,Dracula: Dead and Loving It (1995),Comedy|Horror
13,Balto (1995),Adventure|Animation|Children
14,Nixon (1995),Drama
15,Cutthroat Island (1995),Action|Adventure|Romance
16,Casino (1995),Crime|Drama
17,Sense and Sensibility (1995),Drama|Romance
18,Four Rooms (1995),Comedy
19,Ace Ventura: When Nature Calls (1995),Comedy
20,Money Train (1995),Action|Comedy|Crime|Drama|Thriller

配置文件 logstash.conf

input {        # 定義 inputs
  file {       # 一個 file input
    path => "/path/movies.csv"
    start_position => "beginning"
    sincedb_path => "/dev/null"
  }
}

filter {       # 定義 filters
  csv {        # 一個 csv filter
    separator => ","
    columns => ["id","content","genre"]
  }

  mutate {     # 一個 mutate filter
    split => { "genre" => "|" }
    remove_field => ["path", "host","@timestamp","message"]
  }

  mutate {     # 一個 mutate filter
    split => ["content", "("]
    add_field => { "title" => "%{[content][0]}"}
    add_field => { "year" => "%{[content][1]}"}
  }

  mutate {     # 一個 mutate filter
    convert => {"year" => "integer"}
    strip => ["title"]
    remove_field => ["path", "host","@timestamp","message","content"]
  }
}

output {             # 定義 outputs
   elasticsearch {   # 一個 elasticsearch output
     hosts => "http://localhost:9200"
     index => "movies"
     document_id => "%{id}"
   }
   stdout {}         # 一個 stdout output
}

5,Beats 介紹

在這里插入圖片描述

Beats 是一個輕量型數據采集器,能夠方便的與 Logstash 和 ElasticSearch 配合使用。

Beats 是基於 Golang 開發的。

在這里插入圖片描述

Beats 官方提供了一些具體的 beats 供我們使用:

(本節完。)


歡迎關注作者公眾號,獲取更多技術干貨。

碼農充電站pro


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM