将 nginx 访问日志字节转换为 Kibana4 中的数字

Converting nginx access log bytes to number in Kibana4

我想使用我的 nginx 访问日志中的数据创建发送字节总数的可视化。尝试创建 "Metric" 可视化时,我无法将 bytes 字段用作总和,因为它是字符串类型。

而且我无法在设置下更改它。

如何将此字段类型更改为 number/bytes 类型?

这是我的 nginx 访问日志的 logstash 配置

filter {
  if [type] == "nginx-access" {
    grok {
      match => { "message" => "%{NGINXACCESS}" }
    }
    geoip {
      source => "clientip"
    }
    useragent {
      source => "agent"
      target => "useragent"
    }
  }
}

由于每个 logstash 索引都是作为索引创建的,我想我需要在这里更改它。

我试过添加

mutate {
  convert => { "bytes" => "integer" }
}

不过好像没什么区别

字段类型使用mappings配置,在索引级别配置,几乎不能改变。使用 Logstash,因为每天都会创建一个新索引,所以如果您不想更改这些 映射 要么等到第二天,要么删除当前索引。

默认情况下,这些 映射 由 Elasticsearch 根据索引 JSON 文档的语法和应用的索引模板自动生成:

# Type String
{"bytes":"123"}

# Type Integer
{"bytes":123}

最后有2种解法:

  • 调整 Logstash,使其生成一个整数并让 Elasticsearch 猜测字段类型 → 使用 mutate/convert 过滤器
  • 调整 Elasticsearch,强制文档类型 nginx-access 的字段 bytes 为类型 integer → 使用索引模板:

索引模板API:

PUT _template/logstash-nginx-access
{
  "order": 1,
  "template": "logstash-*",
  "mappings": {
    "nginx-access": {
      "properties": {
        "bytes": {
          "type": "integer"
        }
      }
    }
  }
}