是时候用 ES 拯救发际线啦

logstash和es时区问题

Logstash | 作者 zhuw0312 | 发布于2018年04月18日 | 阅读数:5274

大神我这边有个时区的问题一直没法儿解决,能帮个忙嘛?
从网上找的下面的语句。但是实际使用的时候,发现有的日志时间统一,有的时间不统一。

filter { 
  date {  
    match => ["message","UNIX_MS"]  
    target => "@timestamp"     
  }  
  ruby {   
    code => "event.set('timestamp', event.get('@timestamp').time.localtime)"   
  }  
  ruby {  
    code => "event.set('@timestamp',event.get('timestamp'))"  
  }  
  mutate {  
    remove_field => ["timestamp"]  
gsub =>  ["message","SLBHealthCheck","KeepAliveClient"]
  }  
}
 
xxxx.png
已邀请:

strglee

赞同来自: vip429463267

  ruby {   
code => "event.set('timestamp', event.get('@timestamp').time.utc+8*60*60)"
}

strglee

赞同来自: panbuhei

应该是获取服务器时间的时候时区除了问题
 
在logstash filter 解决
1. 增加一个字段,计算timestamp 8小时
ruby { 
code => "event.set('timestamp', event.get('@timestamp').time.utc+8*60*60)"
}
# 新增索引日期,解决地区时差问题



2. 用mutate插件先转换为string类型,gsub只处理string类型的数据,在用正则匹配,最终得到想要的日期
# 定义索引名时间 
mutate {
convert => ["timestamp", "string"]
gsub => ["timestamp", "T([\S\s]*?)Z", ""]
gsub => ["timestamp", "-", "."]
}

3.output配置
elasticsearch { 
hosts => ["localhost:9200"]
index => "index_%{timestamp}"
}

 
 
 

zhuw0312 - 一介煮夫。

赞同来自:

@strglee
图中2位置的时间是一样了,但是4月19号的日志怎么会出现在4月18号的index里呢?
 

zhuw0312 - 一介煮夫。

赞同来自:

这样是证明时区正常了么?@strglee

zqc0512 - andy zhou

赞同来自:

差8个小时,时区的问题……

ELK_Funs

赞同来自:

兄弟,你问题怎么解决的,我现在的情况和你类似。我的情况是这样的:
我的索引是每天一个,但是每天轮转索引的时候,是在早上8点钟。就是晚了8个小时,不是在0点进行的。0-8点这个时间段的日志都是存储在昨天的index中的
 

要回复问题请先登录注册