开始使用 Watcher

edit

开始使用 Watcher

edit

要设置一个监视器以开始发送警报:

安排监视并定义输入

edit

一个监视器计划控制监视器触发的频率。 监视器输入获取您要评估的数据。

要定期搜索日志数据并将结果加载到监视器中,您可以使用间隔计划和搜索输入。例如,以下监视器每10秒搜索一次logs索引中的错误:

PUT _watcher/watch/log_error_watch
{
  "trigger" : {
    "schedule" : { "interval" : "10s" } 
  },
  "input" : {
    "search" : {
      "request" : {
        "indices" : [ "logs" ],
        "body" : {
          "query" : {
            "match" : { "message": "error" }
          }
        }
      }
    }
  }
}

调度通常配置为运行频率较低。此示例将间隔设置为10秒,以便您可以轻松看到触发的监视。由于此监视运行如此频繁,请不要忘记在完成实验后删除监视

如果您查看监视历史记录,您会看到监视每10秒被触发一次。然而,搜索没有返回任何结果,因此没有任何内容加载到监视负载中。

例如,以下请求从监视历史记录中检索最后十个监视执行(监视记录):

GET .watcher-history*/_search?pretty
{
  "sort" : [
    { "result.execution_time" : "desc" }
  ]
}

添加一个条件

edit

一个条件评估您加载到监视器中的数据,并确定是否需要采取任何行动。现在您已经将日志错误加载到监视器中,您可以定义一个条件来检查是否发现了任何错误。

例如,以下比较条件仅检查搜索输入是否返回了任何结果。

PUT _watcher/watch/log_error_watch
{
  "trigger" : { "schedule" : { "interval" : "10s" }},
  "input" : {
    "search" : {
      "request" : {
        "indices" : [ "logs" ],
        "body" : {
          "query" : {
            "match" : { "message": "error" }
          }
        }
      }
    }
  },
  "condition" : {
    "compare" : { "ctx.payload.hits.total" : { "gt" : 0 }} 
  }
}

The compare 条件允许您轻松地与执行上下文中的值进行比较。

为了使此比较条件评估为true,您需要向logs索引添加一个包含错误的事件。例如,以下请求向logs索引添加了一个404错误:

POST logs/_doc
{
  "timestamp": "2015-05-17T18:12:07.613Z",
  "request": "GET index.html",
  "status_code": 404,
  "message": "Error: File not found"
}

一旦添加此事件,下次手表执行其条件时,条件将评估为true。每次手表执行时,条件结果都会作为watch_record的一部分记录下来,因此您可以通过搜索手表历史记录来验证条件是否已满足:

GET .watcher-history*/_search?pretty
{
  "query" : {
    "bool" : {
      "must" : [
        { "match" : { "result.condition.met" : true }},
        { "range" : { "result.execution_time" : { "gte" : "now-10s" }}}
      ]
    }
  }
}

配置一个操作

edit

在观看历史记录中记录观看记录是很好的,但Watcher的真正强大之处在于能够在满足观看条件时执行某些操作。一个观看的操作定义了当观看条件评估为true时要执行的操作。您可以发送电子邮件、调用第三方webhook、将文档写入Elasticsearch索引,或将消息记录到标准Elasticsearch日志文件中。

例如,当检测到错误时,以下操作会将消息写入Elasticsearch日志。

PUT _watcher/watch/log_error_watch
{
  "trigger" : { "schedule" : { "interval" : "10s" }},
  "input" : {
    "search" : {
      "request" : {
        "indices" : [ "logs" ],
        "body" : {
          "query" : {
            "match" : { "message": "error" }
          }
        }
      }
    }
  },
  "condition" : {
    "compare" : { "ctx.payload.hits.total" : { "gt" : 0 }}
  },
  "actions" : {
    "log_error" : {
      "logging" : {
        "text" : "Found {{ctx.payload.hits.total}} errors in the logs"
      }
    }
  }
}

删除手表

edit

由于log_error_watch配置为每10秒运行一次,请确保在完成实验后删除它。否则,此示例监视产生的噪音将使您难以查看监视历史记录和日志文件中的其他内容。

要移除监视器,请使用删除监视器 API

DELETE _watcher/watch/log_error_watch

所需的安全权限

edit

要允许用户创建和操作监视器,请为他们分配watcher_admin安全角色。监视器管理员还可以查看监视器、监视器历史记录和触发的监视器。

要允许用户查看监视器和监视历史记录,请为他们分配 watcher_user 安全角色。监视器用户不能创建或操作监视器;他们只允许执行只读监视操作。

下一步去哪里

edit
  • 参见Watcher 的工作原理以获取更多关于观察器结构和观察器生命周期的信息。
  • 参见示例观察器以获取更多设置观察器的示例。
  • 参见 Elastic Examples 仓库中的示例观察器,以获取更多您可以用作构建自定义观察器起点的示例。