向 Watcher 操作添加条件编辑

当触发 Watcher 时,其条件决定是否执行 Watcher 操作。在每个操作中,您还可以为每个操作添加一个条件。这些附加条件允许单个警报根据其各自的条件执行不同的操作。以下 Watcher 将始终在从输入搜索中找到匹配项时发送电子邮件,但仅在搜索结果中包含超过 5 个匹配项时才触发 notify_pager 操作。

PUT _watcher/watch/log_event_watch
{
  "trigger" : {
    "schedule" : { "interval" : "5m" }
  },
  "input" : {
    "search" : {
      "request" : {
        "indices" : "log-events",
        "body" : {
          "size" : 0,
          "query" : { "match" : { "status" : "error" } }
        }
      }
    }
  },
  "condition" : {
    "compare" : { "ctx.payload.hits.total" : { "gt" : 0 } }
  },
  "actions" : {
    "email_administrator" : {
      "email" : {
        "to" : "[email protected]",
        "subject" : "Encountered {{ctx.payload.hits.total}} errors",
        "body" : "Too many error in the system, see attached data",
        "attachments" : {
          "attached_data" : {
            "data" : {
              "format" : "json"
            }
          }
        },
        "priority" : "high"
      }
    },
    "notify_pager" : {
      "condition": { 
        "compare" : { "ctx.payload.hits.total" : { "gt" : 5 } }
      },
      "webhook" : {
        "method" : "POST",
        "host" : "pager.service.domain",
        "port" : 1234,
        "path" : "/{{watch_id}}",
        "body" : "Encountered {{ctx.payload.hits.total}} errors"
      }
    }
  }
}

一个仅适用于 notify_pager 操作的 condition,它将其执行限制为条件成功时(在本例中至少为 5 个匹配项)。