【问题标题】:Logstash mix output when using two config files使用两个配置文件时的 Logstash 混合输出
【发布时间】:2016-12-30 10:41:31
【问题描述】:

我在 Ubuntu 中使用 logstash 1.5.6。

我在/etc/logstash/conf.d 中写了两个配置文件,指定不同的输入/输出位置:

文件 A:

input {
  file {
    type => "api"
    path => "/mnt/logs/api_log_access.log"
  }
}
filter {
  ...
}
output {
  if "_grokparsefailure" not in [tags] {
      elasticsearch {
        host => "localhost"
        protocol => "http"
        index => "api-%{+YYYY.MM.dd}"
        template => "/opt/logstash/template/api_template.json"
        template_overwrite => true
      }
  }
}

文件 B:

input {
  file {
    type => "mis"
    path => "/mnt/logs/mis_log_access.log"
  }
}
filter {
  ...
}
output {
  if "_grokparsefailure" not in [tags] {
      elasticsearch {
        host => "localhost"
        protocol => "http"
        index => "mis-%{+YYYY.MM.dd}"
        template => "/opt/logstash/template/mis_template.json"
        template_overwrite => true
      }
  }
}

但是,我可以看到来自/mnt/logs/mis_log_access.log/mnt/logs/nginx/dmt_access.log 的数据都显示在索引api-%{+YYYY.MM.dd}mis-%{+YYYY.MM.dd} 中,这不是我想要的。

配置有什么问题?谢谢。

【问题讨论】:

    标签: nginx elasticsearch logstash elastic-stack


    【解决方案1】:

    Logstash 读取配置目录中的所有文件并将它们全部合并到一个配置中。

    要使一个过滤器或输出部分仅针对一种类型的输入运行,请使用条件:

    if [type] == "api" {
       ....
    }
    

    【讨论】:

    • 谢谢!那么对于输出部分就不用改了吗?
    • 输出部分也可以被条件“保护”。
    • 有弹性的家伙plan to get rid of type attribute 如果您的源中已经有类型字段(它不会覆盖),它也将不适用。所以我建议使用 tags 或 add_field 来代替。
    • @BornToCode type 属性与这个属性不同——它是同一 Elasticsearch 索引中文档类型之间的鉴别器,而这是 Logstash 中携带的类型属性管道。这个最终可能会在 Elasticsearch 文档中显示为 type 字段,但实际的 Elasticsearch 文档 type (使用 output { elasticsearch { document_type => "..." } } - 不会自动从 Logstash type 中填充)存储时带有下划线前缀。
    【解决方案2】:

    它更好地处理输入类型的过滤器

    文件 A:

    input {
       file {
         type => "api"
         path => "/mnt/logs/api_log_access.log"
         }
     }
    
       if [type] == "api" {
         filter {
          ...
         }
       }
    

    文件 B:

    input {
     file {
        type => "mis"
        path => "/mnt/logs/mis_log_access.log"
      }
     }
    
       if [type] == "mis" {
         filter {
          ...
         }
       }
    

    文件 C: output.conf

    output {
      elasticsearch {
      hosts => ["localhost:9200"]
      index => "%{type}-%{+YYYY.MM.dd}"
      }
    }
    

    使用logstash 5.1

    【讨论】:

    • 感谢您的提示。
    • 有弹性的家伙plan to get rid of type attribute 如果您的源代码中已经有类型字段(它不会覆盖),它也将不适用。所以我建议使用 tags 或 add_field 来代替。
    • 就我而言,在 logstash 7.11.1 中,由于在 logstash 启动期间出现解析错误,我需要将建议的“if”语句放入“filter”和“output”块中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 2023-04-01
    • 2018-08-25
    • 2016-12-27
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多