如何在Logstash中设置Elasticsearch输出模板

Jak*_*uld 3 elasticsearch logstash kibana elastic-stack

我对Kibana和ELK(Elasticsearch,Logstash和Kibana)堆栈比较陌生,并且设置非常好,但是遇到了我认为很奇怪的问题,需要一些帮助来了解正在发生的事情。

我正在使用ELK堆栈处理一些Apache日志,但是我有自己的自定义类型设置。因此,我需要明确指定字段类型,而不是让Logstash(或者是Kibana?)猜测数据映射是什么。

通过阅读Logstash文档,似乎很明显,我可以templateoutput.elasticsearch此处显示的配置块中设置值:

output {
  elasticsearch {
    hosts => ["localhost:9200"]
    index => "logstash-apache"
    document_id => "%{[@metadata][fingerprint]}"
    manage_template => false
    template => "/path/to/logstash/logstash-apache.json"
    template_name => "logstash-apache"
    template_overwrite => true
  }
  stdout {
    codec => rubydebug
  }
}
Run Code Online (Sandbox Code Playgroud)

100%确认我设置了正确的路径。但是由于某种原因,如果我使用它,启动Logstash并使其完成任务,那么我指定的映射logstash-apache.json就不会显示出来。Kibana中的索引logstash-apache也是如此,所以应该正确吗?

所以我现在要做的是像这样将映射模板直接预加载到Elasticsearch中:

curl -ss -XPUT "http://localhost:9200/_template/logstash-apache/" -H 'Content-Type: application/json' -d @"/path/to/logstash/logstash-apache.json";
Run Code Online (Sandbox Code Playgroud)

而且显然效果很好,并且数据得到了正确的映射…但是,执行此类操作相当麻烦。仅将所有内容都来自logstash-apache.conf我设置的文件会更清洁。

那我在做什么错?如何通过该自定义映射模板使用该模板,logstash-apache.conf而不必跳过curl命令的额外循环?

Val*_*Val 13

问题是您已将设置manage_templatefalse,从而完全禁用了此模板创建功能,并且需要像现在一样手动创建模板。

因此,您的output部分应该看起来像这样,并且您应该很好:

  elasticsearch {
    hosts => ["localhost:9200"]
    index => "logstash-apache"
    document_id => "%{[@metadata][fingerprint]}"
    manage_template => true                              <-- change this line
    template => "/path/to/logstash/logstash-apache.json"
    template_name => "logstash-apache"
    template_overwrite => true
  }
Run Code Online (Sandbox Code Playgroud)

  • 基本上,`manage_template`可以打开/关闭模板管理。如果打开模板管理,`template`给出模板的路径。template_name是在ES中保存模板的名称。和`template_overwrite`告诉Logstash是否以相同的名称存在已经覆盖了模板。 (2认同)
  • 感谢您的进一步澄清。我现在明白了。但我现在所说的——在这个问题和这个答案的背景下——是官方文档在这种情况下似乎有点混乱。[`manage_template`](https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html#plugins-outputs-elasticsearch-manage_template) 和 [`template`](https ://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html#plugins-outputs-elasticsearch-template) 在官方文档中声明的目的并没有真正解释它们的联系。 (2认同)