1. Logstash是位于Data和Elasticsearch之间的一个中间件。Logstash是一个功能强大的工具,可与各种部署集成。 它提供了大量插件。 它从数据源实时地把数据进行采集,可帮助您解析,丰富,转换和缓冲来自各种来源的数据,并最终把数据传入到Elasticsearch之中。 如果您的数据需要Beats中没有的其他处理,则需要将Logstash添加到部署中。Logstash部署于ingest node之中。

    0.1 默认情况下,Logstash在管道(pipeline)阶段之间使用内存中有界队列(输入到过滤器和过滤器到输出)来缓冲事件。 如果Logstash不安全地终止,则存储在内存中的所有事件都将丢失。 为防止数据丢失,您可以使Logstash通过使用持久队列将正在进行的事件持久化到磁盘上。可以通过在logstash.yml文件中设置queue.type:persisted属性来启用持久队列,该文件位于LOGSTASH_HOME/config文件夹下。 logstash.yml是一个配置文件,其中包含与Logstash相关的设置。 默认情况下,文件存储在LOGSTASH_HOME/data/queue中。 您可以通过在logstash.yml中设置path.queue属性来覆盖它。
  2. 在使用logstash之前,必须要先安装JAVA
  3. 下载地址:https://artifacts.elastic.co/downloads/logstash/logstash-7.3.0.tar.gz (里面的版本号可以根据实际情况进行修改)
  4. 运行最基本的Logstash管道
cd logstash-7.3.0
bin/logstash -e 'input { stdin { } } output { stdout {} }'
  1. 创建logstash.conf文件来运行管道
# logstash.conf文件内容
input {
stdin{ }
} output {
stdout {
codec => rubydebug
} # 运行
./bin/logstash -f logstash.conf (path_to_logstash_conf_file)

提示:在运行Logstash时使用-r标志可让您在更改和保存配置后自动重新加载配置。 在测试新配置时,这将很有用,因为您可以对其进行修改,这样就不必在每次更改配置时都手动启动Logstash。

  1. 获得所有的plugins
bin/logstash-plugin list
  1. input读取csv文件

input {
file {
path => "/Users/liuxg/data/cars.csv"
start_position => "beginning"
sincedb_path => "null"
}

在input中,定义了一个文件,它的path指向csv文件的位置。start_position指向beginning。如果对于一个实时的数据源来说,它通常是ending,这样表示它每次都是从最后拿到那个数据。sincedb_path通常指向一个文件。这个文件保存上次操作的位置。设置为/dev/null,表明不存储这个数据

7. 在Logstash中,按照顺序执行的处理方式被叫做一个pipeline。一个pipeline含有一个按照顺序执行的逻辑数据流。pipeline从input里获取数据,并传送给一个队列,并接着传入到一些worker去处理

  1. 官方提供的lostash关于apache,nginx应用的日志处理样本,网站: https://github.com/elastic/examples/tree/master/Common Data Formats
# apache_logstash.conf
input {
stdin { }
} filter {
grok {
match => {
"message" => '%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "%{WORD:verb} %{DATA:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}'
}
} date {
match => [ "timestamp", "dd/MMM/YYYY:HH:mm:ss Z" ]
locale => en
} geoip {
source => "clientip"
} useragent {
source => "agent"
target => "useragent"
}
} output {
stdout {
codec => dots {}
} elasticsearch {
index => "apache_elastic_example"
template => "./apache_template.json"
template_name => "apache_elastic_example"
template_overwrite => true
}
}
# apache_template.json
{ "template": "apache_elastic_example",
"settings": {
"index.refresh_interval": "5s"
},
"mappings": {
"_default_": {
"dynamic_templates": [
{
"message_field": {
"mapping": {
"norms": false,
"type": "text"
},
"match_mapping_type": "string",
"match": "message"
}
},
{
"string_fields": {
"mapping": {
"norms": false,
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"match_mapping_type": "string",
"match": "*"
}
}
],
"properties": {
"geoip": {
"dynamic": true,
"properties": {
"location": {
"type": "geo_point"
},
"ip": {
"type": "ip"
},
"continent_code": {
"type": "keyword"
},
"country_name": {
"type": "keyword"
}
},
"type": "object"
},
"@version": {
"type": "keyword"
}
}
}
}
}
# nginx_logstash.conf
input {
stdin { }
} filter {
grok {
match => {
"message" => '%{IPORHOST:remote_ip} - %{DATA:user_name} \[%{HTTPDATE:time}\] "%{WORD:request_action} %{DATA:request} HTTP/%{NUMBER:http_version}" %{NUMBER:response} %{NUMBER:bytes} "%{DATA:referrer}" "%{DATA:agent}"'
}
} date {
match => [ "time", "dd/MMM/YYYY:HH:mm:ss Z" ]
locale => en
} geoip {
source => "remote_ip"
target => "geoip"
} useragent {
source => "agent"
target => "user_agent"
}
} output {
stdout {
codec => dots {}
}
elasticsearch {
index => "nginx_elastic_stack_example"
document_type => "logs"
template => "./nginx_template.json"
template_name => "nginx_elastic_stack_example"
template_overwrite => true
}
}
# nginx_template.json
{ "template": "nginx_elastic_stack_example",
"settings": {
"index.refresh_interval": "5s"
},
"mappings": {
"_default_": {
"dynamic_templates": [
{
"message_field": {
"mapping": {
"index": "analyzed",
"norms": false,
"type": "string"
},
"match_mapping_type": "string",
"match": "message"
}
},
{
"string_fields": {
"mapping": {
"norms": false,
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"match_mapping_type": "string",
"match": "*"
}
}
],
"properties": {
"geoip": {
"dynamic": true,
"properties": {
"location": {
"type": "geo_point"
}
},
"type": "object"
},
"bytes": {
"type": "float"
},
"request": {
"type": "keyword"
}
},
"_all": {
"enabled": true
}
}
}
}
# nginx_json_logstash.conf
input {
stdin {
codec => json
}
} filter { date {
match => ["time", "dd/MMM/YYYY:HH:mm:ss Z" ]
locale => en
} geoip {
source => "remote_ip"
target => "geoip"
} useragent {
source => "agent"
target => "user_agent"
} grok {
match => [ "request" , "%{WORD:request_action} %{DATA:request1} HTTP/%{NUMBER:http_version}" ]
}
} output {
stdout {
codec => dots {}
} elasticsearch {
index => "nginx_json_elastic_stack_example"
document_type => "logs"
template => "./nginx_json_template.json"
template_name => "nginx_json_elastic_stack_example"
template_overwrite => true
} }
# nginx_json_template.json
{
"index_patterns": "nginx_json_elastic",
"settings": {
"index.refresh_interval": "5s"
},
"mappings": {
"doc": {
"dynamic_templates": [
{
"message_field": {
"mapping": {
"norms": false,
"type": "text"
},
"match_mapping_type": "string",
"match": "message"
}
},
{
"string_fields": {
"mapping": {
"type": "text",
"norms": false,
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"match_mapping_type": "string",
"match": "*"
}
}
],
"properties": {
"geoip": {
"dynamic": true,
"properties": {
"location": {
"type": "geo_point"
}
},
"type": "object"
},
"request": {
"type": "keyword"
}
}
}
}
}
  1. 处理多个input
# multi-input.conf
input {
file {
path => "/data/multi-input/apache.log"
start_position => "beginning"
sincedb_path => "/dev/null"
# ignore_older => 100000
type => "apache"
}
} input {
file {
path => "/data/multi-input/apache-daily-access.log"
start_position => "beginning"
sincedb_path => "/dev/null"
type => "daily"
}
} filter {
grok {
match => {
"message" => '%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "%{WORD:verb} %{DATA:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}'
}
} if [type] == "apache" {
mutate {
add_tag => ["apache"]
}
} if [type] == "daily" {
mutate {
add_tag => ["daily"]
}
}
} output {
stdout {
codec => rubydebug
} if "apache" in [tags] {
elasticsearch {
index => "apache_log"
template => "/data/apache_template.json"
template_name => "apache_elastic_example"
template_overwrite => true
}
} if "daily" in [tags] {
elasticsearch {
index => "apache_daily"
template => "/data/apache_template.json"
template_name => "apache_elastic_example"
template_overwrite => true
}
}
} # 运行
./bin/logstash -f multi-input.conf

使用了两个input。它们分别对应不同的log文件。对于这两个input,使用了不同的type来表示:apache和daily。尽管它们的格式是一样的,它们共同使用同样的一个grok filter,但是还是想分别对它们进行处理。为此,添加了一个tag。也可以添加一个field来进行区别。在output的部分,根据在filter部分设置的tag来对它们输出到不同的index里。

daily的事件最早被处理及输出,接着apache的数据才开始处理.

  1. 处理多个配置文件(conf)

    一个pipeline含有一个逻辑的数据流,它从input接收数据,并把它们传入到队列里,经过worker的处理,最后输出到output。这个output可以是Elasticsearch或其它
  • 多个pipeline

两个不同的conf配置文件

# apache.conf
input {
file {
path => "/data/multi-input/apache.log"
start_position => "beginning"
sincedb_path => "/dev/null"
# ignore_older => 100000
type => "apache"
}
} filter {
grok {
match => {
"message" => '%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "%{WORD:verb} %{DATA:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}'
}
}
} output {
stdout {
codec => rubydebug
} elasticsearch {
index => "apache_log"
template => "/data/apache_template.json"
template_name => "apache_elastic_example"
template_overwrite => true
}
} # daily.conf
input {
file {
path => "/data/multi-pipeline/apache-daily-access.log"
start_position => "beginning"
sincedb_path => "/dev/null"
type => "daily"
}
} filter {
grok {
match => {
"message" => '%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "%{WORD:verb} %{DATA:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}'
}
}
} output {
stdout {
codec => rubydebug
} elasticsearch {
index => "apache_daily"
template => "/data/multi-pipeline/apache_template.json"
template_name => "apache_elastic_example"
template_overwrite => true
}
}

在logstash的安装目录下的config文件目录下,修改pipelines.yml文件.

# pipelines.yml
- pipeline.id: daily
pipeline.workers: 1
pipeline.batch.size: 1
path.config: "/data/multi-pipeline/daily.conf" - pipeline.id: apache
queue.type: persisted
path.config: "/data/multi-pipeline/apache.conf"

启动,注意:不使用-f参数指定配置文件

/bin/logstash

在终端中可以看到有两个piple在同时运行。

  • 一个pipeline

    修改位于Logstash安装目录下的config子目录里的pipleline.yml文件
# pipelines.yml
- pipeline.id: my_logs
queue.type: persisted
path.config: "/data/multi-pipeline/*.conf"

这里把所有位于/data/multi-pipeline/下的所有的conf文件都放于一个pipeline里。

启动,注意:不使用-f参数指定配置文件

/bin/logstash

在终端中会看到两个同样的输出,这是因为把两个.conf文件放于一个pipleline里运行,那么有两个stdout的输出分别位于两个.conf文件了。

apache_log里有20条数据,它包括两个log文件里所有的事件,这是因为它们都是一个pipleline。同样可以在apache_daily看到同样的20条数据。

采用这种方式意味着会把两个不同的配置文件获取的日志输出到同一个索引中。合并数据的话可以使用这种方式。

  1. 把MySQL数据导入到Elasticsearch中

官方文档地址: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-jdbc.html#plugins-inputs-jdbc-parameters

  • MySQL安装,准备一些测试数据
  • Logstash安装

    根据mysql的版本信息下载相应的JDBC connector驱动,下载网站: https://dev.mysql.com/downloads/connector/j/

    下载完这个Connector后,把这个connector存入到Logstash安装目录下的logstash-core/lib/jars/子目录中。

    最终地址是这样的:logstash-7.3.0/logstash-core/lib/jars/mysql-connector-java-8.0.17.jar
  • Logstash 配置
# sales.conf
input {
jdbc {
jdbc_connection_string => "jdbc:mysql://localhost:3306/data"
jdbc_user => "root"
jdbc_password => "YourMyQLPassword"
jdbc_validate_connection => true
jdbc_driver_library => ""
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
parameters => { "Product_id" => "Product1" }
statement => "SELECT * FROM SalesJan2009 WHERE Product = :Product_id"
}
} output {
stdout {
} elasticsearch {
index => "sales"
hosts => "localhost:9200"
document_type => "_doc"
}
}

替换jdbc_user和jdbc_password为自己的MySQL账号的用户名及密码。特别值得指出的是jdbc_driver_library按elastic的文档是可以放入JDBC驱动的路径及驱动名称。实践证明如果这个驱动不在JAVA的classpath里,是不能被正确地加载。

正因为这样的原因,在上一步里把驱动mysql-connector-java-8.0.17.jar放入到Logstash的jar目录里,所以这里就直接填入空字符串。

  • 运行Logstash加载数据
./bin/logstash --debug -f sales.conf

注意:在MySQL中删除数据的话则不会自动同步删除es中的数据,需要另作处理

logstash知识点的更多相关文章

  1. 为什么你学不会递归?告别递归,谈谈我的一些经验 关于集合中一些常考的知识点总结 .net辗转java系列(一)视野 彻底理解cookie,session,token

    为什么你学不会递归?告别递归,谈谈我的一些经验   可能很多人在大一的时候,就已经接触了递归了,不过,我敢保证很多人初学者刚开始接触递归的时候,是一脸懵逼的,我当初也是,给我的感觉就是,递归太神奇了! ...

  2. ELK系列四:Logstash的在ELK架构中的使用和简单的输入

    1.ELK架构中Logstash的位置: 1.1.小规模集群部署(学习者适用的架构) 简单的只有Logstash.Elasticsearch.Kibana,由Logstash收集日志或者流量信息,过滤 ...

  3. 五十八.Kibana使用 、 Logstash配置扩展插件

    1.导入数据 批量导入数据并查看    1.1 导入数据   1) 使用POST方式批量导入数据,数据格式为json,url 编码使用data-binary导入含有index配置的json文件 ]# ...

  4. Canal:同步mysql增量数据工具,一篇详解核心知识点

    老刘是一名即将找工作的研二学生,写博客一方面是总结大数据开发的知识点,一方面是希望能够帮助伙伴让自学从此不求人.由于老刘是自学大数据开发,博客中肯定会存在一些不足,还希望大家能够批评指正,让我们一起进 ...

  5. Logstash 的命令行入门 ( 附上相关实验步骤 )

    Logstash 的命令行入门 ( 附上相关实验步骤 ) 在之前的博客中,我们已经在 Macbook Big Sur 环境下安装了 ELK 的相关软件,并且已经可以成功运行对应的模块: 如果没有安装的 ...

  6. ASP.NET Core 中的那些认证中间件及一些重要知识点

    前言 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系列(一,二,三)奠定一下基础. 有关于 Authentication 的知识太广,所以本篇介绍几个在 A ...

  7. Logstash实践: 分布式系统的日志监控

    文/赵杰 2015.11.04 1. 前言 服务端日志你有多重视? 我们没有日志 有日志,但基本不去控制需要输出的内容 经常微调日志,只输出我们想看和有用的 经常监控日志,一方面帮助日志微调,一方面及 ...

  8. logstash file输入,无输出原因与解决办法

    1.现象 很多同学在用logstash input 为file的时候,经常会出现如下问题:配置文件无误,logstash有时一直停留在等待输入的界面 2.解释 logstash作为日志分析的管道,在实 ...

  9. ASP.NET MVC开发:Web项目开发必备知识点

    最近加班加点完成一个Web项目,使用Asp.net MVC开发.很久以前接触的Asp.net开发还是Aspx形式,什么Razor引擎,什么MVC还是这次开发才明白,可以算是新手. 对新手而言,那进行A ...

随机推荐

  1. 微信开发在Pc端调用公众号粉丝发送过来的图片素材

    因为项目要在PC端搞一个微信墙功能,就是把粉丝发送过来的上墙内容给展示出来,但因为微信对微信素材进行了防盗链加密处理,所以在非微信页面上直接引用在微信服务器上的图片的链接是无法显示的,只会显示一张微信 ...

  2. 【ASP.NET Core】自定义的配置源

    本文的主题是简单说说如何实现 IConfigurationSource.IConfigurationProvider 接口来自定义一个配置信息的来源,后面老周给的示例是实现用 CSV 文件进行应用配置 ...

  3. 什么?让每一个开源项目更安全?啊?还有IDE工具?难道是它?

    背景 入编程界6年来,大大小小的安全漏洞是真滴听了不少,xxx通过日志入侵了,xxxx通过请求入侵了,等等等等. 近期fastJson又报安全漏洞,敢巧自己又"被"跳槽到了新公司, ...

  4. 提名 Apache ShardingSphere Committer,说说方法

    文章首发在公众号(龙台的技术笔记),之后同步到博客园和个人网站:xiaomage.info 就在前几天,收到了 Apache ShardingSphere Vote 我成为 Committer 的邮件 ...

  5. 沁恒CH32V103C8T6(二): Linux RISC-V编译和烧录环境配置

    目录 沁恒CH32V103C8T6(一): 核心板焊接和Windows开发环境配置 沁恒CH32V103C8T6(二): Linux RISC-V编译和烧录环境配置 硬件准备 CH32V103 开发板 ...

  6. vue发布自定义组件到npm

    一.使用 vue create currentdatetime创建项目(可查考https://cli.vuejs.org/zh/guide/creating-a-project.html),创建成功后 ...

  7. JavaScript数组方法总结及手写

    目录 手写数组衍生方法 1.检测是否为数组 2.类数组转化为数组 3.数组扁平化 4.数组去重 5.数组使用Math.max 手写数组内置方法 1. Array.prototype.filter 2. ...

  8. grafana监控配置

    一.配置开启smtp服务 1.编辑grafana配置文件grafana.ini [smtp] enabled = true host = smtp.163.com:25 user = 157xxxx3 ...

  9. MySQL基本操作笔记

    一.数值类型 1.常量(1)字符串常量 ASCII字符串常量占一个字节 例如:'Hello Word' Unicode字符串常量占两个字节 例如:N'Hello Word' mysql> sel ...

  10. linux常见命令chgrp/chown/chmod

    linux文件权限有读(r-4)写(w-2)执行(x-1) linux文件的所有方式有拥有者(user),属组(group),其他人(others) 改变文件属组命令 -- chgrp 修改/data ...