如何在Logstash中获取部分Filebeat源文件名
我有一个Filebeat实例(7.5.0版,在Windows服务器上运行)监视本地文件夹中的日志文件,并将此数据发送到Logstash(7.5.0版,在Docker容器中运行)。在Logstash中,我想提取一个文件夹名(最后一个),并将其添加为一个字段 一个具体的例子是,从两个日志条目中,一个来自文件如何在Logstash中获取部分Filebeat源文件名,logstash,logstash-grok,filebeat,Logstash,Logstash Grok,Filebeat,我有一个Filebeat实例(7.5.0版,在Windows服务器上运行)监视本地文件夹中的日志文件,并将此数据发送到Logstash(7.5.0版,在Docker容器中运行)。在Logstash中,我想提取一个文件夹名(最后一个),并将其添加为一个字段 一个具体的例子是,从两个日志条目中,一个来自文件d:\\Logs\\Foo\\Bar\\lorem\\currentlog.txt,另一个来自文件d:\\Logs\\Foo\\Bar\\ipsum\\currentlog.txt,我想分别提取值
d:\\Logs\\Foo\\Bar\\lorem\\currentlog.txt
,另一个来自文件d:\\Logs\\Foo\\Bar\\ipsum\\currentlog.txt
,我想分别提取值lorem
和ipsum
为此,我设置了以下(简化示例):
我已尝试将匹配更改为位于log.file.path
属性上,但这给了我相同的\u grokparsefailure
标记
我也很确定这在早期安装的Filebeat/Logstash(可能是一个或两个主要版本)上起作用,但我记不清了
所以问题是:为什么Logstash不能从Filebeat源中提取文件夹名?还有什么方法可以进一步调试这个grok问题吗?上面的配置不起作用的原因是复杂的,但我最终设法解决了: 首先,没有来自Filebeat的
source
字段(我很确定以前有一些版本,但情况不同),这显然会导致grok过滤器不成功
接下来,当我尝试搜索log.file.path
字段时,我使用了错误的语法。访问嵌套字段的正确方法如下:[log][file][path]
最后,尽管输出显示log.file.path
的值为“d:\\Logs\\Foo\\Bar\\ipsum\\currentlog.txt”
,但显然在输出管道的某个位置添加了双反斜杠。因此,当我将正则表达式更改为单反斜杠匹配而不是双反斜杠匹配时,它正确地从“d:\Logs\Foo\Bar\ipsum\currentlog.txt”
因此,我的最终管道配置如下所示:
{
"@version" => "1",
"tags" => [
[0]"beats_input_codec_plain_applied",
[1]"_grokparsefailure"
],
"host" => {
"name" => "test"
},
"message" => "Another line in the log",
"agent" => {
"id" => "e00d2f50-b10c-406a-a4fa-be381d15b869",
"ephemeral_id" => "28dfe105-b936-40de-bc97-16c4a9196e30",
"hostname" => "my-host",
"name" => "test",
"type" => "filebeat",
"version" => "7.5.0"
},
"@timestamp" => 2019 - 12 - 16T14: 04: 09.064Z,
"ecs" => {
"version" => "1.1.0"
},
"log" => {
"file" => {
"path" => "d:\\Logs\\Foo\\Bar\\ipsum\\currentlog.txt"
},
"offset" => 21
},
"input" => {
"type" => "log"
}
}
input {
pipeline { address => "test" }
}
filter {
grok {
match => { "[log][file][path]" => ".*(\\|\/).*(\\|\/)(?<product>.*)(\\|\/).*"}
}
}
output {
stdout { codec => rubydebug }
}
输入{
管道{address=>“test”}
}
滤器{
格罗克{
match=>{“[log][file][path]”“=>”*(\\\\\\\/).*(\\\\\\/)(?*)(\\\\\\/).*”}
}
}
输出{
stdout{codec=>rubydebug}
}
现在我成功地获得了提取到product
字段的路径中最后一个文件夹的名称,没有\u grokparsefailure
标记
input {
pipeline { address => "test" }
}
filter {
grok {
match => { "[log][file][path]" => ".*(\\|\/).*(\\|\/)(?<product>.*)(\\|\/).*"}
}
}
output {
stdout { codec => rubydebug }
}