如何在logstash中创建条件字段?

时间:2015-10-28 06:47:21

标签: logstash

我有一个名为"优先级"它具有整数值,如1到 10.这里1是最低的,10是最高的。如何显示字符串 值,而不是数字,使这个领域更容易理解。因此,新字段应包含3个值,即

low ---> if priority lies in 1 to 3
Medium---> if priority lies in 4-7
high---->if priority lies in 8-10

2 个答案:

答案 0 :(得分:3)

首先,您需要确保您的优先级值为int类型。之后,您可以使用logstash's conditionals将值更改为字符串。

示例配置:

input { stdin {} }
filter {
    grok { match => { "message" => [" %{NUMBER:priority:int}" ] } }
    if [priority] < 4 {
        mutate { replace => { "priority" => "low" } } 
    } else if [priority] < 8 {
        mutate { replace => { "priority" => "medium" } }
    } else {
        mutate { replace => { "priority" => "high" } }
    }
}
output {stdout { codec => rubydebug }}

请注意grok filter将您的号码解析为int类型的字段。

%{NUMBER:priority:int}

这意味着,无论您从哪里获得优先级字段,都需要注意它包含一个int。否则,您可以通过比较字符串值来完成此操作。

答案 1 :(得分:0)

我建议您保留两个字段:数字和字符串(漂亮)版本。这样,您可以根据数字运行高效查询并在图表上显示字符串等。

更多信息here