使用 Elasticsearch 5 进行 Grafana 4 模板化

Grafana 4 Templating with Elasticsearch 5

编辑:请参阅下面的解决方案

目前 Grafana 中的模板存在问题 - 试图从我通过 Logstash 的 Graphite 插件输入到 Elasticsearch 的一些数据中获取主机名下拉列表,这样我就可以在 Grafana 中构建动态模板。

版本是 Grafana 4.1.2 + Elasticsearch/Logstash 5.2.1

根据 grafana 网站上的文档,我尝试使用的 Grafana 中的术语查询如下 - http://docs.grafana.org/features/datasources/elasticsearch/

{"find": "terms", "field": "host_name"}

如果该字段是数字类型字段,这会很好地工作 - 例如,我在 metric_value 的模板中得到结果,但这似乎不适用于 text/string 字段。我想知道这是否可能是由于我构建或摄取字段的方式所致 - 您可以在下面看到这些字段的 I"m trying to achieve this - note, I've tried "keyword" 和 "text" 类型,它们似乎都不起作用。

这是我正在使用的 Logstash 输入过滤器 - 基本上试图将石墨样式指标拆分为单独的字段 -

input {
  graphite {
    type => graphite
    port => 2003
    id => "graphite_input"
  }
}

filter {
        if [type] == "graphite" {
                grok {
                        match => [ "message", "\Aicinga2\.%{MONGO_WORDDASH:host_name:keyword}\.%{WORD:metric_type:keyword}\.%{NOTSPACE:metric_name:keyword}\.value%{SPACE}%{NUMBER:metric_value:float}%{SPACE}%{POSINT:timestamp:date}" ]
                }
        }

}

output { 
        if [type] == "graphite" {
                elasticsearch {
                        index => "graphite-%{+YYYY.MM}"
                        hosts => ["localhost"]
                }
        }

}

还有一个我正在编制索引的示例文档(取自 kibana)

{
  "_index": "graphite-2017.02",
  "_type": "graphite",
  "_id": "XYZdflksdf",
  "_score": null,
  "_source": {
    "@timestamp": "2017-02-21T00:17:16.000Z",
    "metric_name": "interface-eth0.snmp-interface.perfdata.eth0_in_discard",
    "port": 37694,
    "icinga2.XXXYYY.services.interface-eth0.snmp-interface.perfdata.eth0_in_discard.value": 357237,
    "@version": "1",
    "host": "192.168.1.1",
    "metric_type": "services",
    "metric_value": 357237,
    "message": "icinga2.XXXYYY.services.interface-eth0.snmp-interface.perfdata.eth0_in_discard.value 357237 1487636236",
    "type": "graphite",
    "host_name": "XXXYYY",
    "timestamp": "1487636236"
  },
  "fields": {
    "@timestamp": [
      1487636236000
    ]
  },
  "sort": [
    1487636236000
  ]
}

我现在已经自己解决了这个问题。字符串字段需要定义为 not_analyzed 才能显示在 Grafana 仪表板中。

这是您可以使用的示例模板: 注意:你必须手动安装它,看起来 logstash 出于某种原因不会将它安装到 elasticsearch 中(也许是一个错误?) 像这样安装(假设路径是 /etc/logstash/graphite-new.json:

curl -XPUT 'http://localhost:9200/_template/graphite-*' -d@/etc/logstash/graphite-new.json

模板:

{ 
    "template" : "graphite-*", 
    "settings" : { "index.refresh_interval" : "60s" }, 
    "mappings" : { 
        "_default_" : { 
            "_all" : { "enabled" : false }, 
            "dynamic_templates" : [{ 
              "message_field" : { 
                "match" : "message", 
                "match_mapping_type" : "string", 
                "mapping" : { "type" : "string", "index" : "not_analyzed" } 
              } 
            }, { 
              "string_fields" : { 
                "match" : "*", 
                "match_mapping_type" : "string", 
                "mapping" : { "type" : "string", "index" : "not_analyzed" } 
              } 
            }], 
            "properties" : { 
                "@timestamp" : { "type" : "date", "format" : "dateOptionalTime" }, 
                "@version" : { "type" : "integer", "index" : "not_analyzed" }, 
                "metric_name" : { "type" : "string", "index" : "not_analyzed" }, 
                "host" : { "type" : "string", "index" : "not_analyzed" }, 
                "host_name" : { "type" : "string", "index" : "not_analyzed" }, 
                "metric_type" : { "type" : "string", "index" : "not_analyzed" } 
            } 
        } 
    } 
}

我还在 logstash 过滤器中定义了这个:

if [type] == "graphite" {
        elasticsearch {
                index => "graphite-%{+YYYY.MM}"
                hosts => ["localhost"]
                template => "/etc/logstash/graphite-new.json"
        }
}