我们知道Logstash的架构如下:
它的整个 pipleline 分为三个部分:
这里的 input 可以支持多个 input,同时多个 worker 可以处理 filter 及 output:
在今天的介绍中,我们来介绍一下如何使用多个input。
为了说明问题的方便,我把所需要用到的文件都传到 github 地址https://github.com/liu-xiao-guo/logstash_multi-input。我们可以通过如下的方式来下载这些文件:
git clone https://github.com/liu-xiao-guo/logstash_multi-input
Logstash 的配置文件如下:
multi-input.conf
input { file { path => "/Users/liuxg/data/multi-input/apache.log" start_position => "beginning" sincedb_path => "/dev/null" # ignore_older => 100000 type => "apache" }} input { file { path => "/Users/liuxg/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 => "/Users/liuxg/data/apache_template.json" template_name => "apache_elastic_example" template_overwrite => true } } if "daily" in [tags] { elasticsearch { index => "apache_daily" template => "/Users/liuxg/data/apache_template.json" template_name => "apache_elastic_example" template_overwrite => true } } }
为了说明问题的方便,我们使用了两个 input。它们分别对应不同的 log 文件。对于这两个 input,我们也使用了不同的 type 来表示:apache和 daily。尽管它们的格式是一样的,它们共同使用同样的一个 grok filter,但是我们还是想分别对它们进行处理。为此,我们添加了一个 tag。我们也可以添加一个 field 来进行区别。在 output 的部分,我们根据在 filter 部分设置的 tag来对它们输出到不同的 index里。
我们可以通过如下的命令来运行:
$ pwd/Users/liuxg/elastic/logstash-7.3.0bogon:logstash-7.3.0 liuxg$ sudo ./bin/logstash -f ~/data/multi-input/multi-input.conf
当你们运行这个例子的时候,你们需要根据自己存放 multi-input.conf 文件的位置改变而改变上面的命令。
运行的结果如下:
根据显示的结果可以看出来 daily 的事件最早被处理及输出。接着 apache 的数据才开始处理。在实际的应用中,我们可能有不同的数据源,比如来自其它 beats 的监听某个端口的数据。
我们可以在 Kibana 中看到我们最终的 index 数据:
包含文章发布时段最新活动,前往ES产品介绍页,可查找ES当前活动统一入口
Elasticsearch Service自建迁移特惠政策>>
Elasticsearch Service 新用户特惠狂欢,最低4折首购优惠 >>
Elasticsearch Service 企业首购特惠,助力企业复工复产>>
关注“腾讯云大数据”公众号,技术交流、最新活动、服务专享一站Get~
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有