Мне нужен запрос Elasticsearch для отображения данных в Grafana

#elasticsearch #grafana #elasticsearch-dsl

#elasticsearch #grafana #elasticsearch-dsl

Вопрос:

Представьте, что у меня есть документы a, b, c, d, в которых есть поля site_name, device_name, Interface_name и использование. Мне нужно отобразить максимальное использование для device_name для каждого Interface_name для каждого site_name.

Вот пример данных:

 **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
 

Заранее спасибо!

Ответ №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
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}