正在加载

监控 Elasticsearch 集群的状态

Elastic Stack Serverless

您可以轻松配置一个基本的监视器来监控 Elasticsearch 集群的健康状况

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

定义计划最简单的方式是指定一个间隔。例如,以下计划每 10 秒运行一次

 PUT _watcher/watch/cluster_health_watch {
  "trigger" : {
    "schedule" : { "interval" : "10s" }
  }
}
  1. 通常,计划的配置频率较低。此示例将间隔设置为 10 秒,以便您可以轻松看到正在触发的监视器。由于此监视器运行频率如此之高,因此在完成实验后,请不要忘记 删除监视器

要获取集群的状态,您可以调用 集群健康 API

 GET _cluster/health?pretty 

要将健康状况加载到监视器中,您只需添加一个 HTTP 输入,调用集群健康 API

 PUT _watcher/watch/cluster_health_watch {
  "trigger" : {
    "schedule" : { "interval" : "10s" }
  },
  "input" : {
    "http" : {
      "request" : {
        "host" : "localhost",
        "port" : 9200,
        "path" : "/_cluster/health"
      }
    }
  }
}

如果您正在使用安全功能,那么您还需要提供一些身份验证凭据作为监视器配置的一部分

 PUT _watcher/watch/cluster_health_watch {
  "trigger" : {
    "schedule" : { "interval" : "10s" }
  },
  "input" : {
    "http" : {
      "request" : {
        "host" : "localhost",
        "port" : 9200,
        "path" : "/_cluster/health",
        "auth": {
          "basic": {
            "username": "elastic",
            "password": "x-pack-test-password"
          }
        }
      }
    }
  }
}

最好创建一个具有使用此类监视器配置所需最低权限的用户。

根据集群的配置方式,在监视器可以访问集群之前,可能需要其他设置,例如密钥库、信任库或证书。有关更多信息,请参阅 Watcher 设置

如果您检查监视器历史记录,您会看到每次执行监视器时,集群状态都会作为 watch_record 的一部分进行记录。

例如,以下请求从监视器历史记录中检索最后十个监视器记录

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

条件 评估您已加载到监视器中的数据,并确定是否需要采取任何操作。由于您已定义一个将集群状态加载到监视器中的输入,因此您可以定义一个检查该状态的条件。

例如,您可以添加一个条件来检查状态是否为 RED。

 PUT _watcher/watch/cluster_health_watch {
  "trigger" : {
    "schedule" : { "interval" : "10s" }
  },
  "input" : {
    "http" : {
      "request" : {
       "host" : "localhost",
       "port" : 9200,
       "path" : "/_cluster/health"
      }
    }
  },
  "condition" : {
    "compare" : {
      "ctx.payload.status" : { "eq" : "red" }
    }
  }
}
  1. 通常,计划的配置频率较低。此示例将间隔设置为 10 秒,以便您可以轻松看到正在触发的监视器。

如果您检查监视器历史记录,您会看到每次执行监视器时,条件结果都会作为 watch_record 的一部分进行记录。

要检查是否满足条件,您可以运行以下查询。

 GET .watcher-history*/_search?pretty {
  "query" : {
    "match" : { "result.condition.met" : true }
  }
}

在监视器历史记录中记录 watch_records 很好,但 Watcher 的真正威力在于能够响应警报采取一些措施。监视器的 操作 定义了当监视器条件为 true 时该怎么做——您可以发送电子邮件、调用第三方 webhook,或者在满足监视器条件时将文档写入 Elasticsearch 索引或日志中。

例如,您可以添加一个操作,以便在状态为 RED 时索引集群状态信息。

 PUT _watcher/watch/cluster_health_watch {
  "trigger" : {
    "schedule" : { "interval" : "10s" }
  },
  "input" : {
    "http" : {
      "request" : {
       "host" : "localhost",
       "port" : 9200,
       "path" : "/_cluster/health"
      }
    }
  },
  "condition" : {
    "compare" : {
      "ctx.payload.status" : { "eq" : "red" }
    }
  },
  "actions" : {
    "send_email" : {
      "email" : {
        "to" : "username@example.org",
        "subject" : "Cluster Status Warning",
        "body" : "Cluster status is RED"
      }
    }
  }
}

为了让 Watcher 发送电子邮件,您必须在 elasticsearch.yml 配置文件中配置一个电子邮件帐户,然后重新启动 Elasticsearch。要添加电子邮件帐户,请设置 xpack.notification.email.account 属性。

例如,以下代码段配置了一个名为 work 的 Gmail 帐户

xpack.notification.email.account:
  work:
    profile: gmail
    email_defaults:
      from: <email>
    smtp:
      auth: true
      starttls.enable: true
      host: smtp.gmail.com
      port: 587
      user: <username>
      password: <password>
  1. <email> 替换为您要从中发送通知的电子邮件地址。
  2. <username> 替换为您的 Gmail 用户名(通常是您的 Gmail 地址)。
  3. <password> 替换为您的 Gmail 密码。
注意

如果您为您的电子邮件帐户启用了高级安全选项,您需要采取其他步骤才能从 Watcher 发送电子邮件。有关更多信息,请参阅 配置电子邮件帐户

您可以检查监视器历史记录或 status_index,以查看是否执行了该操作。

 GET .watcher-history*/_search?pretty {
  "query" : {
    "match" : { "result.condition.met" : true }
  }
}

由于 cluster_health_watch 被配置为每 10 秒运行一次,因此请确保在完成实验后将其删除。否则,您将无限期地给自己发送垃圾邮件。

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

 DELETE _watcher/watch/cluster_health_watch 
© . All rights reserved.