【问题标题】:Implement watcher to send mail if no input in past n seconds如果在过去 n 秒内没有输入,则实现 watcher 发送邮件
【发布时间】:2017-01-16 00:35:49
【问题描述】:

我正在尝试为以下场景设置观察者

如果过去 n 小时内没有文档插入索引,则发出警报。这样我们就可以检查我们的节点和弹性搜索之间的连接故障是否存在任何问题。

为了实现这样的场景,我使用以下 json 进行复制。

curl -XPUT 'http://10.x.x.1:9200/_watcher/watch/log_error_watch' -d '{
  "trigger" : { "schedule" : { "interval" : "10s" } },
  "input" : {
    "search" : {
      "request" : {
        "indices" : [ "test" ],
        "body" : {
          "query" : {
            "match_all" : {}
          }
        }
      }
    }
  },
  "condition": {
    "array_compare": {
      "ctx.payload.aggregations.test.buckets" : { 
        "path": "doc_count" ,
        "lt": { 
          "value": 1, 
          "quantifier": "some" 
        }
      }
    }
  },
  "actions" : {
    "log_error" : {
      "logging" : {
        "text" : "No data for past 10 seconds"
      }
    }
  }
}'

但它没有将警报消息放入日志中。有人可以帮我解决条件部分的逻辑。

【问题讨论】:

  • 您的搜索输入查询没有任何聚合。为什么要使用这种检查聚合的条件? ctx.payload.aggregations.test.buckets

标签: elasticsearch elasticsearch-plugin


【解决方案1】:

改用这个条件:

  "condition": {
    "compare": {
      "ctx.payload.hits.total": {
        "gt": 0
      }
    }
  }

【讨论】:

  • 如果点击数大于 0,这将发送警报。即如果在 10 秒内插入任何文件,我会收到警报。 .然而,我正在寻找相反的情况。如果 10 秒内没有文件发送警报。
  • "ctx.payload.hits.total" 将计算 10 秒内的总点击数。否则它将从头开始计算索引中的总记录
  • 它没有计算任何东西。您在输入中运行查询,在您的情况下,我看到它是match_all。该查询的结果将传递给条件。因此,您运行 match_all 并检查条件是否返回任何结果。就是这样。
  • 我尝试设置这个观察者。仅当有消息时才会触发警报。我正在寻找场景,当没有消息时它应该发布警报
  • “每当有消息时”是什么意思?
【解决方案2】:
{
    "trigger": {
      "schedule": {
        "interval": "5m"
      }
    },
    "input": {
      "search": {
        "request": {
          "search_type": "query_then_fetch",
          "indices": [
            "filebeat-*"
          ],
          "rest_total_hits_as_int": true,
          "body": {
            "size": 0, 
   "query": {
     "bool": {
       "filter": [
         {
           "terms": {
             "product": [
               "ABC",
               "XYZ"
             ]
           }
         },
         {
           "term": {
             "env": "PROD"
           }
         },
         { "range": {
        "@timestamp": {
        "gte": "now-1m",
        "lte": "now",
        "format": "strict_date_optional_time"
             }
         }}
       ]
     }
   },
   "aggs": {
        "NAME": {
          "terms": {
            "field": "hostname",
            "size": 100,
            "min_doc_count": 0,
            "include": ".*PROD.*"
      }
     }
   }
          }
        }
      }
    },
    "condition": {
      "array_compare": {
        "ctx.payload.aggregations.NAME.buckets": {
          "path": "doc_count",
          "eq": {
              "value": 0
          }
        }
      }
    },
    "actions": {
      "send_email": {
        "email": {
          "profile": "standard",
          "to": [
            "email@email.com"
          ],
          "subject": "No Logs in Kibana for 5 minutes",
          "body": {
            "html": """<h2><font color="red">Team, <br>Logs are not coming for below environment for 5 Minutes</br></font></h2>
  <table>
  <table border="1">
    <tr>
      <th>Environment</th>
      <th>Doc Count</th>
    </tr>
  {{#ctx.payload.aggregations.NAME.buckets}}
  <tr>
      <td>{{key}}</td>
      <td>{{doc_count}}</td>
  </tr>
  {{/ctx.payload.aggregations.NAME.buckets}}
  </table>"""
          }
        }
      }
    }
  }

【讨论】:

  • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
猜你喜欢
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多