【问题标题】:I need an Elasticsearch query to display data in Grafana我需要一个 Elasticsearch 查询来在 Grafana 中显示数据
【发布时间】:2021-01-13 08:35:12
【问题描述】:

假设我有文档 a、b、c、d,其中包含字段站点名称、设备名称、接口名称和利用率。我需要为每个站点名称的每个接口名称显示设备名称的最大利用率。

这里是示例数据:

**Site  Device  Interface Name           Utilization**
TYO    tyo-gb1  TenGigabitEthernet1      33,23,699
TYO    tyo-gb1  TenGigabitEthernet1      38,92,992
TYO    tyo-gb2  TenGigabitEthernet2      98,824
TYO    tyo-gb2  TenGigabitEthernet2      49,187
SYD    syd-gb1   GigabitEthernet1        52,800
SYD    syd-gb1   GigabitEthernet1        71,572
STLD   stld-gb1  GigabitEthernet1        1,62,886
STLD   stld-gb1  GigabitEthernet1        40,977

我需要这样显示:

  **Site    Device  Interface Name           Utilization**
    TYO    tyo-gb1  TenGigabitEthernet1      38,92,992
    TYO    tyo-gb2  TenGigabitEthernet2      98,824
    SYD    syd-gb1   GigabitEthernet1        71,572
    STLD   stld-gb1  GigabitEthernet1        1,62,886

提前致谢!

【问题讨论】:

    标签: elasticsearch grafana elasticsearch-dsl


    【解决方案1】:

    你可以使用这个查询

         {
            "size": 0,
            "_source": false,
            "stored_fields": "_none_",
            "aggregations": {
                "groupby": {
                    "composite": {
                        "size": 1000,
                        "sources": [
                            {
                                "Site": {
                                    "terms": {
                                        "field": "Site",
                                        "missing_bucket": true,
                                        "order": "asc"
                                    }
                                }
                            },
                            {
                                "Device": {
                                    "terms": {
                                        "field": "Device",
                                        "missing_bucket": true,
                                        "order": "asc"
                                    }
                                }
                            },
                            {
                                "Interface Name": {
                                    "terms": {
                                        "field": "Interface Name",
                                        "missing_bucket": true,
                                        "order": "asc"
                                    }
                                }
                            }
                        ]
                    },
                    "aggregations": {
                        "Utilization Sum": {
                            "sum": {
                                "field": "Utilization"
                            }
                        }
                    }
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      数据摄取

      POST test_nagendra/_doc
        {
          "site_name": "TYO",
          "device_name": "tyo-gb1",
          "interface_name": "TenGigabitEthernet1",
          "utilization": 3323699
        }
        
        POST test_nagendra/_doc
        {
          "site_name": "TYO",
          "device_name": "tyo-gb1",
          "interface_name": "TenGigabitEthernet1",
          "utilization": 3892992
        }
        
        POST test_nagendra/_doc
        {
          "site_name": "TYO",
          "device_name": "tyo-gb2",
          "interface_name": "TenGigabitEthernet2",
          "utilization": 98824
        }
        
        POST test_nagendra/_doc
        {
          "site_name": "TYO",
          "device_name": "tyo-gb2",
          "interface_name": "TenGigabitEthernet2",
          "utilization": 49187
        }
        
        POST test_nagendra/_doc
        {
          "site_name": "SYD",
          "device_name": "syd-gb1",
          "interface_name": "GigabitEthernet1",
          "utilization": 52800
        }
        
        POST test_nagendra/_doc
        {
          "site_name": "SYD",
          "device_name": "syd-gb1",
          "interface_name": "GigabitEthernet1",
          "utilization": 71572
        }
        
        POST test_nagendra/_doc
        {
          "site_name": "STLD",
          "device_name": "stld-gb1",
          "interface_name": "GigabitEthernet1",
          "utilization": 162886
        }
        
        POST test_nagendra/_doc
        {
          "site_name": "STLD",
          "device_name": "stld-gb1",
          "interface_name": "GigabitEthernet1",
          "utilization": 40977
        }
      

      查询

      POST test_nagendra/_search
      {
        "size": 0,
        "aggs": {
          "sites": {
            "terms": {
              "field": "site_name.keyword",
              "size": 10
            },
            "aggs": {
              "devices": {
                "terms": {
                  "field": "device_name.keyword",
                  "size": 10
                },
                "aggs": {
                  "interfaces": {
                    "terms": {
                      "field": "interface_name.keyword",
                      "size": 10
                    },
                    "aggs": {
                      "max_utilization": {
                        "max": {
                          "field": "utilization"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
      

      回应

      {
        "took" : 3,
        "timed_out" : false,
        "_shards" : {
          "total" : 1,
          "successful" : 1,
          "skipped" : 0,
          "failed" : 0
        },
        "hits" : {
          "total" : {
            "value" : 8,
            "relation" : "eq"
          },
          "max_score" : null,
          "hits" : [ ]
        },
        "aggregations" : {
          "sites" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "TYO",
                "doc_count" : 4,
                "devices" : {
                  "doc_count_error_upper_bound" : 0,
                  "sum_other_doc_count" : 0,
                  "buckets" : [
                    {
                      "key" : "tyo-gb1",
                      "doc_count" : 2,
                      "interfaces" : {
                        "doc_count_error_upper_bound" : 0,
                        "sum_other_doc_count" : 0,
                        "buckets" : [
                          {
                            "key" : "TenGigabitEthernet1",
                            "doc_count" : 2,
                            "max_utilization" : {
                              "value" : 3892992.0
                            }
                          }
                        ]
                      }
                    },
                    {
                      "key" : "tyo-gb2",
                      "doc_count" : 2,
                      "interfaces" : {
                        "doc_count_error_upper_bound" : 0,
                        "sum_other_doc_count" : 0,
                        "buckets" : [
                          {
                            "key" : "TenGigabitEthernet2",
                            "doc_count" : 2,
                            "max_utilization" : {
                              "value" : 98824.0
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "key" : "STLD",
                "doc_count" : 2,
                "devices" : {
                  "doc_count_error_upper_bound" : 0,
                  "sum_other_doc_count" : 0,
                  "buckets" : [
                    {
                      "key" : "stld-gb1",
                      "doc_count" : 2,
                      "interfaces" : {
                        "doc_count_error_upper_bound" : 0,
                        "sum_other_doc_count" : 0,
                        "buckets" : [
                          {
                            "key" : "GigabitEthernet1",
                            "doc_count" : 2,
                            "max_utilization" : {
                              "value" : 162886.0
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "key" : "SYD",
                "doc_count" : 2,
                "devices" : {
                  "doc_count_error_upper_bound" : 0,
                  "sum_other_doc_count" : 0,
                  "buckets" : [
                    {
                      "key" : "syd-gb1",
                      "doc_count" : 2,
                      "interfaces" : {
                        "doc_count_error_upper_bound" : 0,
                        "sum_other_doc_count" : 0,
                        "buckets" : [
                          {
                            "key" : "GigabitEthernet1",
                            "doc_count" : 2,
                            "max_utilization" : {
                              "value" : 71572.0
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-23
        • 2021-11-02
        • 2016-03-27
        • 2012-08-21
        • 2022-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多