logstash使用的是标准时间UTC时间,我们在生成索引的时候常会使用xx-%{+YYYY.MM.dd}这样的格式去建立索引,这个时间其实是读取的@timestamp时间,所以就会产生一个问题:今天的08:00之前的数据会在昨天的索引里面。网上方法很多,现提供一种方法
增加一个区域,去读取本地时间
ruby {
code => "event.set('index_time',event.timestamp.time.localtime.strftime('%Y.%m.%d'))"
}
在output中调用
index => "bosslove-test-%{index_time}"
其实我觉得,这个时区问题不是什么问题,我们在搜索索引的时候都是类似于xx-2018-02*这样来搜索的。
其次我们kibana读取的是浏览器时间,即使你从日志里面抓取了时间也最好改成标准时间
grok {
match => { "message" => "(.*%{LOGLEVEL:loglevel}.*) (?<time>\d{4}[./-]\d{0,2}[./-]\d{0,2} (\d+:){2}\d+) (?<detail>.*)" }
}
date { match => [ "time", "yyyy/MM/dd HH:mm:ss" ]
target => "time"
}
我通过data参数再次把抓取到的日志时间转变为UTC时间
以下是elastic中的展示

以下是kibana中的展示

会自动改成东八区时间
另外搜索日期的时候要严格按照数据格式搜索

UTC时间格式搜索不然会报错
Discover: failed to parse date field [2018-08-09 04:56:00.664] with format [strict_date_optional_time||epoch_millis]
网友评论