【问题标题】:Conditionals or variables within a logstash blocklogstash 块中的条件或变量
【发布时间】:2015-11-09 10:59:16
【问题描述】:

在某些logstash配置中,我想有条件地设置配置块中一个字段的值,而不必重复整个块。

例如,我有两个设置非常相似的http 输出:

output {
    if "foo" in [tags] {
        http {
            url => "http://example.com/x"
            http_method => "post"
            follow_redirects => true
            keepalive => false
            connect_timeout => 20
            cookies => true
        }
    } else {
        http {
            url => "http://example.com/y"
            http_method => "post"
            follow_redirects => true
            keepalive => false
            connect_timeout => 20
            cookies => true
        }
    }
}

避免重复这两个块的内容以及只更改我感兴趣的单个字段的最佳方法是什么?我希望我可以设置一个变量并在块内使用它,或者在块内使用if,但找不到任何一个示例。

我正在寻找类似以下的东西(这是无效的配置):

output {
    http {
        if "foo" in [tags] {
            url => "http://example.com/x"
        } else {
            url => "http://example.com/y"
        }
        http_method => "post"
        follow_redirects => true
        keepalive => false
        connect_timeout => 20
        cookies => true
    }
}

【问题讨论】:

    标签: logstash logstash-configuration


    【解决方案1】:

    在您的 filter{} 部分设置一个变量(mutate->add_field,也许),然后在您的输出节中引用该变量,例如:

    url => "%{myField}"
    

    如果您不想将变量存储在 elasticsearch 中,请在两个地方都使用元数据,例如:

    [@metadata][myField]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      • 2016-12-04
      • 1970-01-01
      • 2020-02-17
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多