logstash 时区问题

场景

日志信息按天写入索引,output 代码如下

output {
    elasticsearch {
        hosts => ["192.168.23.20:9200","192.168.23.21:9200","192.168.23.22:9200","192.168.23.23:9200","192.168.23.24:9200"]
        index => "songtest-%{service}-%{+YYYY.MM.dd}"
    }
}

问题

发现写入时差存在问题,原本应2019.1.2 的日志应该写入 log-2019.01.02索引中结果发现实际写入该索引的日志区间为 2019.1.2 8:00:00 到 2019.1.3 8:00:00。
问题出在logstash 使用的是UTC 时间 和我们CST 时间相差8小时,当logstash 解析道timestamp 时间戳时会做减8h的操作,操作索引变更时间整体延后8小时

解决方案

增加一个index_date 字段 代替默认date pulgin %{+YYYY.MM.dd},上代码

filter{
# 1. 增加一个字段,计算timestamp+8小时
ruby {
    code => "event.set('index_date', event.get('@timestamp').time.localtime+ 8*60*60 )"
}
# 2. 用mutate插件先转换为string类型,gsub只处理string类型的数据,在用正则匹配,最终得到想要的日期
mutate {
    convert => ["index_date", "string"]
    gsub => ["index_date", "T([\S\s]*?)Z", ""]
    gsub => ["index_date", "-", "."]
}

}

output {
    elasticsearch {
        hosts => ["192.168.23.20:9200","192.168.23.21:9200","192.168.23.22:9200","192.168.23.23:9200","192.168.23.24:9200"]
        index => "songtest-%{service}-%{index_date}"
    }

posted @ 2023-01-03 21:02  萱乐庆foreverlove  阅读(238)  评论(0编辑  收藏  举报