如何覆盖logstash中来自json的时间戳字段

时间:2017-02-13 09:18:47

标签: elasticsearch logstash logstash-configuration filebeat metricbeat

我有以下类型的json与我一起使用filebeat {"@timestamp":"2017-02-10T06:30:51.424Z","beat":{"hostname":"myhostname","name":"mydevice-logger","version":"5.2.0"},"fields":{"device_type":"mydevice","env":"staging"},"metricset":{"module":"system","name":"cpu","rtt":211},"system":{"cpu":{"cores":4,"idle":{"pct":0.000000},"iowait":{"pct":0.000000},"irq":{"pct":0.000000},"nice":{"pct":0.000000},"softirq":{"pct":0.000000},"steal":{"pct":0.000000},"system":{"pct":0.000000},"user":{"pct":0.000000}}},"tags":["automata","box"],"type":"metricbeat-test-log"}

进行弹性搜索

我的logstash(版本5.1.1)配置包含,输入,过滤和输出如下 -

input { 
  beats {
        port => 5046
        codec => json
  }
}

filter {
    if ...{}
    else if [type] == "metricbeat-test-log" {

      date {
        match => ["@timestamp", "ISO8601"]
      }

      }
    }

}


output {
    if ...{}
    else if [type] == "metricbeat-test-log" {
        stdout { codec => rubydebug   }
    }
} 

类型正确但日期过滤器不起作用。 @timestamp最终总是占用当前时间戳。我想用json中的原始@timestamp替换它。

2 个答案:

答案 0 :(得分:0)

您应该使用日期过滤器中的目标设置:

来自https://www.elastic.co/guide/en/logstash/5.1/plugins-filters-date.html#plugins-filters-date-target

:定位

值类型是字符串

默认值为" @ timestamp"

将匹配的时间戳存储到给定的目标字段中。如果未提供,则默认更新事件的@timestamp字段。

答案 1 :(得分:0)

我在这个旧线程https://discuss.elastic.co/t/replace-timestamp-with-actual-timestamp-from-log-file/72017之后得到了答案,它解释了上面日志行中的"@timestamp":"2017-02-10T06:30:51.424Z"是@timestamp字段及其值的JSON表示。根据建议我在filebeat中添加了json配置,它对我有用。

- input_type: log
  paths:
    - /var/logs/mylogs/*.log
  fields:
    environment: testing
  document_type: test-metric-document

  json.keys_under_root: true   # added this line 
  json.overwrite_keys: true    # added this line 

虽然我对这个解决方案不满意,因为我真正的需要是获取时间戳,logstash事件(在某些其他变量中)和登录@timestamp的时间戳。