issueELKlogstash时间戳差8个小时
liuzhihang问题说明
原始配置:
1 2 3 4 5 6 7 8 9 10
| elasticsearch { # manage_template => false template_overwrite => true template => "/opt/export/app/logstash-6.4.2/bin/dynamic_templates.json" user => xxxxxxx password => xxxxxxx index => "%{sys_name}-%{+YYYY.MM.dd}" hosts => ["172.19.3.51:9200","172.19.3.52:9200"] }
|
在使用logstash输出内容要es中时, 指定index为系统名称+时间(年月日), 时间会自动匹配’@timestamp’字段并格式化, 但是在实际使用过程中, 发现在上午八点之前的消息会被创建到昨天的索引里面.查阅相关资料, 有介绍在时间戳上面增加8个小时的方式, 也可以使用. 这里结合自己业务使用的其他方式.
解决方案
-
主要报送内容为filebeat的日志信息, 日志统一有时间戳, 格式如下:
[trans-mediapay]-[2018-12-19 02:00:00:187]-[queryThreadPool-14]-[]-[WeBankServiceImpl.java:101]-[INFO ]-[测试2点的日志]
-
解析时间戳的时间
- 先匹配整体日志, 获取’log_time’字段
- 匹配’log_time’字段
- 生成元数据 ‘[@metadata][index_suffix]’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| filter {
grok { match => { "message" => "\[%{DATA:sys_name}\]-\[%{DATA:log_time}\]-\[%{DATA:thread_name}\]-\[%{DATA:trace_id}\]-\[%{DATA:class_name}\]-\[%{DATA:log_level}\]-%{GREEDYDATA:log_msg}" } } grok{ match => { "log_time" => ["%{INT:index_year}-%{INT:index_mouth}-%{INT:index_day}"]} } mutate { add_field => { "[@metadata][index_suffix]" => "%{index_year}.%{index_mouth}.%{index_day}" } remove_field => ["host","beat","tags","[beat][name]","[beat][version]","prospector","@version","offset","input","y_index","M_index","d_index"] }
}
|
- 输出时使用元数据, 该字段不会出现在es的字段中
1 2 3 4 5 6 7 8 9
| elasticsearch { template_overwrite => true template => "/opt/export/app/logstash-6.4.2/bin/dynamic_templates.json" user => xxxxxxx password => xxxxxxx index => "%{sys_name}-%{[@metadata][index_suffix]}" hosts => ["xxxx:9200","xxxx:9200"] }
|