spu*_*der 38 logstash logstash-configuration
我有日志文件进入ELK堆栈.我想复制一个字段(foo)以便在其上执行各种突变,但是字段(foo)并不总是存在.
如果foo不存在,则仍会创建bar,但会为其分配文字字符串 "%{foo}"
如果字段存在,我怎样才能执行变异?
我正在尝试做这样的事情.
if ["foo"] {
mutate {
add_field => "bar" => "%{foo}
}
}
Run Code Online (Sandbox Code Playgroud)
Ofr*_*viv 81
要检查字段foo是否存在:
1)对于数字类型字段,使用:
if ([foo]) {
...
}
Run Code Online (Sandbox Code Playgroud)
2)对于除数字之外的类型,如布尔值,字符串使用:
if ("" in [foo]) {
...
}
Run Code Online (Sandbox Code Playgroud)
Ala*_*ins 33
"foo"是一个文字字符串.
[foo]是一个领域.
# technically anything that returns 'true', so good for numbers and basic strings:
if [foo] {
}
# contains a value
if [foo] =~ /.+/ {
}
Run Code Online (Sandbox Code Playgroud)
小智 18
在Logstash 2.2.2上,该("" in [field])构造似乎不适合我.
if ![field] { }
Run Code Online (Sandbox Code Playgroud)
对于非数字字段.
Seb*_*ian 10
现在是 2020 年,以上答案都不完全正确。自 2014 年以来,我一直在使用 logstash,过滤器中的表达式曾经、现在和将来都会成为一件事......
例如,您可能有一个带有falsevalue的布尔字段,并且使用上述解决方案,您可能不知道false是该字段的值还是表达式的结果值,因为该字段不存在。
我认为所有版本的 logstash 都支持[@metadata]字段。也就是说,一个字段对输出插件不可见并且仅处于过滤状态。所以这就是我必须解决的方法:
filter {
mutate {
# we use a "temporal" field with a predefined arbitrary known value that
# lives only in filtering stage.
add_field => { "[@metadata][testField_check]" => "unknown arbitrary value" }
# we copy the field of interest into that temporal field.
# If the field doesn't exist, copy is not executed.
copy => { "testField" => "[@metadata][testField_check]" }
}
# now we now if testField didn't exists, our field will have
# the initial arbitrary value
if [@metadata][testField_check] == "unknown arbitrary value" {
# just for debugging purpouses...
mutate { add_field => { "FIELD_DID_NOT_EXISTED" => true }}
} else {
# just for debugging purpouses...
mutate { add_field => { "FIELD_DID_ALREADY_EXISTED" => true }}
}
}
Run Code Online (Sandbox Code Playgroud)
我一直在为 logstash 中的表达式苦苦挣扎。我的旧解决方案一直工作到版本 7。这是用于布尔字段,例如:
filter {
# if the field does not exists, `convert` will create it with "false" string. If
# the field exists, it will be the boolean value converted into string.
mutate { convert => { "field" => "string" } }
# This condition breaks on logstash > 7 (see my bug report). Before version 7,
# this condition will be true if a boolean field didn't exists.
if ![field] {
mutate { add_field => { "field" => false } }
}
# at this stage, we are sure field exists, so make it boolean again
mutate { convert => { "field" => "boolean" } }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
60630 次 |
| 最近记录: |