出于某种原因,logstash(版本1.5)无法处理具有此异常的日志:
{:timestamp =>" 2016-04-26T09:20:12.141000-0400",:message =>"无法解析字段中的日期",: field =>" time",:value =>" 2016-04-26T09:20:03.520-04:00",:exception => java.lang.IllegalArgumentException:格式无效:" 2016-04-26T09:20:03.520-04:00"在" T09:20:03.520-04:00",:level =>:warn}
json中的我的时间字段是:
"time":"25-04-2016 04:21:06.786"
我的logstash配置是:
filter {
if [type] == "json" {
json {
source => "message"
}
date {
match => [ "time", "dd-MM-yyyy HH:mm:ss", "dd-MM-yyyy HH:mm:ss:SSS", "dd-MM-yyyy HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss,SSS" ]
}
}
}
在Elasticsearch方面,我看到了这个例外:
无法解析日期字段[25-04-2016 04:48:14.305],尝试了日期格式[dateOptionalTime]和时间戳号与locale [] java.lang.IllegalArgumentException:格式无效:" 25-04-2016 04:48:14.305"在" 16 04:48:14.305"
时格式不正确我该如何解决这个问题?
答案 0 :(得分:1)
我有同样的问题; Logstash会很乐意做它的工作,但是Elasticsearch会抱怨同样的错误。您会看到使用正确的变量生成了“ @timestamp”。关键是要理解这种错误,它有点像以下内容:
[2020-07-22 12:27:40,814][DEBUG][action.bulk ] [logs-p03] [logstash-2020.07.22][0] failed to execute bulk item (index) index {[logstash-2020.07.22][logs] ... org.elasticsearch.index.mapper.MapperParsingException: failed to parse [shibidp_timestamp]
...
Caused by: org.elasticsearch.index.mapper.MapperParsingException: failed to parse date field [20200722T002739Z], tried both date format [dateOptionalTime], and timestamp number with locale []
...
Caused by: java.lang.IllegalArgumentException: Invalid format: "20200722T002739Z" is malformed at "2739Z"
这表明映射是错误的...让我们看看:
GET http://127.0.0.1:9200/logstash-2020.07.22/_mapping
...
"shibidp_severity" : {
"type" : "string",
"index" : "not_analyzed",
"fields" : {
"raw" : {
"type" : "string",
"index" : "not_analyzed",
"ignore_above" : 256
}
}
},
"shibidp_timestamp" : {
"type" : "date",
"format" : "dateOptionalTime"
},
...
我添加了shibidp_severity只是为了说明如何映射大多数(字符串)。该模板不包含与shibidp_timestamp匹配的任何内容,也不包含任何其他映射为“ date”类型且格式为“ dateOptionalTime”的其他字段
映射到此的字段(查看GET http://127.0.0.1:9200/logstash-2020.07.22/_mapping)
此行为似乎与动态日期检测有关
https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-field-mapping.html
https://www.elastic.co/guide/en/elasticsearch/reference/1.7/mapping-dynamic-mapping.html
在我的情况下,此动态映射导致了一些竞争状况(遇到的第一个“ shibidp_timestamp”值将通知当日索引的映射。让我们看一下区别:
$ (d="2020.07.18"; curl -s http://127.0.0.1:9200/logstash-$d/_mapping | jq -c '.["logstash-'$d'"].mappings.logs.properties.shibidp_timestamp')
{"fields":{"raw":{"ignore_above":256,"index":"not_analyzed","type":"string"}},"index":"not_analyzed","type":"string"}
$ (d="2020.07.19"; curl -s http://127.0.0.1:9200/logstash-$d/_mapping | jq -c '.["logstash-'$d'"].mappings.logs.properties.shibidp_timestamp')
{"format":"dateOptionalTime","type":"date"}
这显示了在两个不同的每日索引中shibidp_timestamp的映射。前者是我们遇到类似字符串的东西时得到的东西。后者是我们在看起来像时间戳的时候得到的东西(但是无法解析为时间戳)
您可以采取一些措施来解决此问题:
答案 1 :(得分:0)
您的@timestamp
有偏移量(timzeone值),您需要将其添加到您的配置中。请参阅此链接:https://www.elastic.co/guide/en/logstash/current/plugins-filters-date.html
Z
时区偏移或身份
Z
:时区偏移量结构为HHmm(小时和分钟偏离Zulu / UTC)。示例:-0700。ZZ
:时区偏移量结构为HH:mm(小时和分钟偏移之间的冒号)。示例:-07:00。ZZZ
:时区身份。示例:America / Los_Angeles。注意:有效ID列在Joda.org available time zones page。所以你的模式看起来应该是这样的:
YYYY-MM-dd HH:mm:ss.SSSZ