首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elastic -检查给定时间范围内的所有值是否大于阈值X

Elastic -检查给定时间范围内的所有值是否大于阈值X
EN

Stack Overflow用户
提问于 2020-08-18 21:51:13
回答 1查看 368关注 0票数 0

我想使用Elastic query在Kibana中创建一个警报。我使用的是using发行版的提醒功能。我要检查过去10分钟内cpu.pct字段的所有值是否大于50,如果大于50,则发出警报。

代码语言:javascript
复制
{
"size": 500,
"query": {
    "bool": {
        "filter": [
            {
                "match_all": {
                    "boost": 1
                }
            },
            {
                "match_phrase": {
                    "client.id": {
                        "query": "42",
                        "slop": 0,
                        "zero_terms_query": "NONE",
                        "boost": 1
                    }
                }
            },
            {
                "range": {
                    "cpu.pct": {
                        "from": 10,
                        "to": null,
                        "include_lower": true,
                        "include_upper": true,
                        "boost": 1
                    }
                }
            },
            {
                "range": {
                    "@timestamp": {
                        "from": "{{period_end}}||-5m",
                        "to": "{{period_end}}",
                        "include_lower": true,
                        "include_upper": true,
                        "format": "epoch_millis",
                        "boost": 1
                    }
                }
            }
        ],
        "adjust_pure_negative": true,
        "boost": 1
    }
},
"aggregations": {
    "2": {
        "terms": {
            "field": "client.name.keyword",
            "size": 5,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": {
                "_key": "desc"
            }
        },
        "aggregations": {
            "3": {
                "terms": {
                    "field": "component.name",
                    "size": 1000,
                    "min_doc_count": 1,
                    "shard_min_doc_count": 0,
                    "show_term_doc_count_error": false,
                    "order": [
                        {
                            "1": "desc"
                        },
                        {
                            "_key": "asc"
                        }
                    ]
                },
                "aggregations": {
                    "1": {
                        "avg": {
                            "field": "cpu.pct"
                        }
                    }
                }
            }
        }
    }
}

我有以下计算平均值的查询,但这是不正确的。

反例:取值(100,100,100,100,100,0,0,0,0) |抛出预警:否(平均值: 60)

正例:取值(60、60、60、60、60、60、60、60、60、60、60、60) |抛出预警:是(平均值: 60)

怎样才能检查所有的值?

EN

回答 1

Stack Overflow用户

发布于 2020-08-18 23:31:42

我不确定您使用的是什么应用程序来触发警报。解决这种情况的一种方法是使用两个过滤器聚合:

  1. totalInLast10Min:获取最近10分钟内被索引的文档总数mins.
  2. totalInLast10MinAboveTh:这是获取最近10分钟内索引的文档总数,并且字段的值大于阈值。

如果为totalInLast10Min == totalInLast10MinAboveTh,则触发警报。

创建索引

代码语言:javascript
复制
PUT test
{
  "mappings": {
    "properties": {
      "timestamp": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      }
    }
  }
}

添加一些文档

代码语言:javascript
复制
POST test/_doc
{"cpu":20,"timestamp":"2020-08-18 20:20:00"}

POST test/_doc
{"cpu":100,"timestamp":"2020-08-18 20:21:00"}

POST test/_doc
{"cpu":90,"timestamp":"2020-08-18 20:29:00"}

查询:

代码语言:javascript
复制
GET test/_search
{
  "size": 0,
  "aggs": {
    "totalInLast10Min": {
      "filter": {
        "range": {
          "timestamp": {
            "gte": "2020-08-18 20:20:00"
          }
        }
      }
    },
    "totalInLast10MinAboveTh": {
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "timestamp": {
                  "gte": "2020-08-18 20:20:00"
                }
              }
            },
            {
              "range": {
                "cpu": {
                  "gte": 80
                }
              }
            }
          ]
        }
      }
    }
  }
}

示例结果:

代码语言:javascript
复制
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "totalInLast10MinAboveTh" : {
      "meta" : { },
      "doc_count" : 2
    },
    "totalInLast10Min" : {
      "meta" : { },
      "doc_count" : 3
    }
  }
}

基于两个aggs的计数,您可以编写何时触发警报的条件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63470114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档