我们在Elasticsearch中使用管道来模拟grok过滤器。我们遇到了以下奇怪的行为。
如果timestamp字段位于消息的开头,则grok过滤器不起作用。
## GROK NOT WORK
POST /_ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"grok": {
"field": "message",
"patterns": ["""%{TIMESTAMP_ISO8601:@timestamp} %{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration} %{TIMESTAMP_ISO8601:@timestamp}"""]
}
}
]
},
"docs": [
{
"_source": {
"message": "2019-09-29T00:39:02.91ZZ 55.3.244.1 GET /index.html 15824 0.043 "
}
}
]
}我们有以下错误:
{
"docs" : [
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "Provided Grok expressions do not match field value: [2019-09-29T00:39:02.91ZZ 55.3.244.1 GET /index.html 15824 0.043 ]"
}
],
"type" : "illegal_argument_exception",
"reason" : "Provided Grok expressions do not match field value: [2019-09-29T00:39:02.91ZZ 55.3.244.1 GET /index.html 15824 0.043 ]"
}
}
]
}在这种格式中,消息末尾的时间戳,grok过滤器工作得很好。
## GROK WORKS FINE
POST /_ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"grok": {
"field": "message",
"patterns": ["""%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration} %{TIMESTAMP_ISO8601:@timestamp}"""]
}
}
]
},
"docs": [
{
"_source": {
"message": "55.3.244.1 GET /index.html 15824 0.043 2019-09-29T00:39:02.91ZZ"
}
}
]
}提前感谢
发布于 2020-05-27 15:45:13
从你的第一个例子中,我看到了GROK表达式的两个问题:
在grok表达式的末尾有第二个时间戳(可能是复制粘贴问题?),第二个问题是时间戳末尾的时区ZZ。如果您想忽略它,可以在您的时间戳后面添加%{NOTSPACE},以忽略这些额外的时区字符。
您可以尝试以下grok:
%{TIMESTAMP_ISO8601:timestamp}%{NOTSPACE} %{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}这是在GROK调试器中测试过的,对我来说是有效的。我建议你与GROK调试器密切合作(在Kibana->Dev Tools->Grok Debugger),或者你可以找到一些在线的grok调试器。
最后,我建议你在使用"NUMBER“格式后添加:int或:float,这样就可以在ES中使用更大/更小的过滤器。
发布于 2020-06-11 01:24:13
可以使用https://regex101.com/r/eRAJg3/1匹配日期。LMK,如果有帮助的话。
然后,您可以在grok表达式中使用Oniguruma语法:
(?<ts>([\d]{4}-[\d]{2}-[\d]{2}T[\d]{2}:[\d]{2}:[\d]{2}\.[\d]{2}Z{2}))参考:https://www.elastic.co/guide/en/logstash/current/plugins-filters-grok.html#_custom_patterns
正如eladyanai指出的那样,您还应该从第一个表达式中删除时间戳的第二个实例。
为了完整起见,我将提供完整的表达式:
(?<ts>([\d]{4}-[\d]{2}-[\d]{2}T[\d]{2}:[\d]{2}:[\d]{2}\.[\d]{2}Z{2})) %{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}%{SPACE}发布于 2020-04-18 18:44:10
他们是"ZZ“后的时间,这是不兼容的ISO。
您可以添加.* --> "0个或更多个任意字符“
%{TIMESTAMP_ISO8601:@timestamp}.*
和*在最后一个时间戳模式之后,使其成为可选的
{
"pipeline": {
"processors": [
{
"grok": {
"field": "message",
"patterns": [
"%{TIMESTAMP_ISO8601:@timestamp}.* %{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration} %{TIMESTAMP_ISO8601:@timestamp}*"
]
}
}
]
},
"docs": [
{
"_source": {
"message": "2019-09-29T00:39:02.91ZZ 55.3.244.1 GET /index.html 15824 0.043 "
}
}
]结果:
{
"docs" : [
{
"doc" : {
"_index" : "_index",
"_type" : "_doc",
"_id" : "_id",
"_source" : {
"duration" : "0.043",
"request" : "/index.html",
"@timestamp" : "2019-09-29T00:39:02.91Z",
"method" : "GET",
"bytes" : "15824",
"client" : "55.3.244.1",
"message" : "2019-09-29T00:39:02.91ZZ 55.3.244.1 GET /index.html 15824 0.043 "
},
"_ingest" : {
"timestamp" : "2020-04-18T10:43:39.8725873Z"
}
}
}
]
}https://stackoverflow.com/questions/61286615
复制相似问题