6

Logstash-Beats入门

 2 years ago
source link: https://codeshellme.github.io/2021/03/es-logstash-beats/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

公号:码农充电站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
  • 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 供我们使用:

(本节完。)


欢迎关注作者公众号,获取更多技术干货。


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK