elasticsearch 使用日期过滤器将日期解析为时间戳
嗯,环顾四周,我找不到解决问题的办法,因为它“应该”起作用,但显然不起作用。 我在Ubuntu 14.04 LTS机器上使用Logstash 1.4.2-1-2-2c0f5a1,我收到如下消息:
elasticsearch 使用日期过滤器将日期解析为时间戳,
elasticsearch,logstash,kibana,
elasticsearch,Logstash,Kibana,嗯,环顾四周,我找不到解决问题的办法,因为它“应该”起作用,但显然不起作用。 我在Ubuntu 14.04 LTS机器上使用Logstash 1.4.2-1-2-2c0f5a1,我收到如下消息: 2014-08-05 10:21:13,618 [17] INFO Class.Type - This is a log message from the class: BTW, I am also multiline 在输入配置中,我有一个多行编解码器,事件被正确解析。我还将事件文本分为几个部
2014-08-05 10:21:13,618 [17] INFO Class.Type - This is a log message from the class:
BTW, I am also multiline
在输入配置中,我有一个多行
编解码器,事件被正确解析。我还将事件文本分为几个部分,以便于阅读
最后,如Kibana所示,我获得了如下内容(JSON视图):
你可能已经注意到我在时间戳中加了“;”。原因是我希望能够使用时间戳字符串对日志进行排序,显然logstash在这方面并不擅长(例如:)
我曾尝试以多种方式使用日期
过滤器,但均未成功,而且它显然不起作用
date {
locale => "en"
match => ["logTimestamp", "YYYY-MM-dd;HH:mm:ss.SSS", "ISO8601"]
timezone => "Europe/Vienna"
target => "@timestamp"
add_field => { "debug" => "timestampMatched"}
}
由于我了解到如果字符串不严格符合ISO 8601(非常挑剔,需要T,请参阅),Joda库可能会出现问题,因此我还尝试使用mutate
将字符串转换为类似2014-08-05T10:21:13.618
的格式,然后使用“YYYY-MM-dd'T'HH:MM:ss.SSS”
。这也不起作用
date {
locale => "en"
match => ["logTimestamp", "YYYY-MM-dd;HH:mm:ss.SSS", "ISO8601"]
timezone => "Europe/Vienna"
target => "@timestamp"
add_field => { "debug" => "timestampMatched"}
}
我不想手动设置+02:00的时间,因为这会给夏令时带来问题
在任何一种情况下,事件都会转到elasticsearch,但是date
显然什么也不做,因为@timestamp
和logTimestamp
是不同的,并且没有添加debug
字段
你知道我怎样才能使logTime字符串正确排序吗?我专注于将它们转换为适当的时间戳,但也欢迎任何其他解决方案
如下所示:
当对@timestamp
进行排序时,elasticsearch可以正确地进行排序,但由于这不是“真实”的日志时间戳,而是在读取logstash事件时,我需要(显然)能够对logTimestamp
进行排序。这就是输出。显然没有那么有用:
欢迎任何帮助!如果我忘了一些有用的信息,请告诉我
更新:
以下是最终起作用的过滤器配置文件:
# Filters messages like this:
# 2014-08-05 10:21:13,618 [17] INFO Class.Type - This is a log message from the class:
# BTW, I am also multiline
# Take only type- events (type-componentA, type-componentB, etc)
filter {
# You cannot write an "if" outside of the filter!
if "type-" in [type] {
grok {
# Parse timestamp data. We need the "(?m)" so that grok (Oniguruma internally) correctly parses multi-line events
patterns_dir => "./patterns"
match => [ "message", "(?m)%{TIMESTAMP_ISO8601:logTimestampString}[ ;]\[%{DATA:logThreadId}\][ ;]%{LOGLEVEL:logLevel}[ ;]*%{GREEDYDATA:logMessage}" ]
}
# The timestamp may have commas instead of dots. Convert so as to store everything in the same way
mutate {
gsub => [
# replace all commas with dots
"logTimestampString", ",", "."
]
}
mutate {
gsub => [
# make the logTimestamp sortable. With a space, it is not! This does not work that well, in the end
# but somehow apparently makes things easier for the date filter
"logTimestampString", " ", ";"
]
}
date {
locale => "en"
match => ["logTimestampString", "YYYY-MM-dd;HH:mm:ss.SSS"]
timezone => "Europe/Vienna"
target => "logTimestamp"
}
}
}
filter {
if "type-" in [type] {
# Remove already-parsed data
mutate {
remove_field => [ "message" ]
}
}
}
我已经测试了你的
日期
过滤器。它对我有用
这是我的配置
input {
stdin{}
}
filter {
date {
locale => "en"
match => ["message", "YYYY-MM-dd;HH:mm:ss.SSS"]
timezone => "Europe/Vienna"
target => "@timestamp"
add_field => { "debug" => "timestampMatched"}
}
}
output {
stdout {
codec => "rubydebug"
}
}
我使用这个输入:
2014-08-01;11:00:22.123
输出为:
{
"message" => "2014-08-01;11:00:22.123",
"@version" => "1",
"@timestamp" => "2014-08-01T09:00:22.123Z",
"host" => "ABCDE",
"debug" => "timestampMatched"
}
因此,请确保您的logTimestamp
具有正确的值。
这可能是另一个问题。或者,您可以提供日志事件和日志存储配置以供更多讨论。谢谢。这对我很有效-日期时间格式略有不同:
# 2017-11-22 13:00:01,621 INFO [AtlassianEvent::0-BAM::EVENTS:pool-2-thread-2] [BuildQueueManagerImpl] Sent ExecutableQueueUpdate: addToQueue, agents known to be affected: []
input {
file {
path => "/data/atlassian-bamboo.log"
start_position => "beginning"
type => "logs"
codec => multiline {
pattern => "^%{TIMESTAMP_ISO8601} "
charset => "ISO-8859-1"
negate => true
what => "previous"
}
}
}
filter {
grok {
match => [ "message", "(?m)^%{TIMESTAMP_ISO8601:logtime}%{SPACE}%{LOGLEVEL:loglevel}%{SPACE}\[%{DATA:thread_id}\]%{SPACE}\[%{WORD:classname}\]%{SPACE}%{GREEDYDATA:logmessage}" ]
}
date {
match => ["logtime", "yyyy-MM-dd HH:mm:ss,SSS", "yyyy-MM-dd HH:mm:ss,SSS Z", "MMM dd, yyyy HH:mm:ss a" ]
timezone => "Europe/Berlin"
}
}
output {
elasticsearch { hosts => ["localhost:9200"] }
stdout { codec => rubydebug }
}
谢谢你在stdin上试用的提示,该死的是我没有想到这一点!于是我开始玩弄过滤器排序(在同一个
if
或not中,除其他外),并想出了一个有效的配置:)感谢您的帮助!我还将设置配置,以防对某人有用。不知何故,我在遵循此解决方案时遇到了问题,因为换行符。我收到了类似于“2014-08-01;11:00:22.123\r\”的错误,在“\r\”处格式不正确。为了解决这个问题,我遵循这个原则,非常感谢您发布工作结果,Ben的回复和您的回复使我的问题变得微不足道!