# Cluster Health

原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.0/cluster-health.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/cluster-health.html)

译文链接 : [http://www.apache.wiki/display/Elasticsearch/Cluster+Health](http://www.apache.wiki/display/Elasticsearch/Cluster+Health)

贡献者 : [那伊抹微笑](/display/~wangyangting)

**cluster health**（集群健康）**API** 可以获取一个集群健康的简单状态。例如，在一个单节点的集群上，存在一个有 **5** 个分片和 **1** 个副本的索引，如下操作 : 

```
curl -XGET 'localhost:9200/_cluster/health?pretty'
```

响应如下 : 

```
{
  "cluster_name" : "testcluster",
  "status" : "yellow",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 5,
  "active_shards" : 5,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 5,
  "delayed_unassigned_shards": 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch": 0,
  "task_max_waiting_in_queue_millis": 0,
  "active_shards_percent_as_number": 50.0
}
```

该 **API** 也可以针对一个或多个索引执行请求以获得指定索引的健康状态 : 

```
curl -XGET 'localhost:9200/_cluster/health/test1,test2?pretty'
```

集群健康的状态是 : **green**，**yellow** 或 **red**。从分片的角度来说，**red** 状态的索引表示集群中指定的分片没有分配，**yellow** 意味着主分片已经分配成功但是副本还没有分配，**green** 意味着所有的分片已经分配成功。索引级别的状态由最差的分片状态所控制。集群状态由最差的索引状态所控制。

该 **API** 其中的一个好处是有等到直到集群达到了某个标记的健康级别的阀值的能力。例如，为了集群达到 **yellow** 级别下面将等待 **50** 秒（如果在 **50** 秒之前达到了 **green** 或 **yellow** 状态，它将在这时候返回）: 

```
curl -XGET 'localhost:9200/_cluster/health?wait_for_status=yellow&timeout=50s&pretty'
```

## Request Parameters

该集群 **API** 接受下列的请求参数 : 

| 请求参数 | 描述 |
| --- | --- |
| level | Can be one of `cluster`, `indices` or `shards`. Controls the details level of the health information returned. Defaults to `cluster`. |
| wait_for_status | One of `green`, `yellow` or `red`. Will wait (until the timeout provided) until the status of the cluster changes to the one provided or better, i.e. `green` &gt; `yellow` &gt; `red`. By default, will not wait for any status. |
| wait_for_no_relocating_shards | A boolean value which controls whether to wait (until the timeout provided) for the cluster to have no shard relocations. Defaults to false, which means it will not wait for relocating shards. |
| wait_for_active_shards | A number controlling to how many active shards to wait for, `all` to wait for all shards in the cluster to be active, or `0` to not wait. Defaults to `0`. |
| wait_for_nodes | The request waits until the specified number `N` of nodes is available. It also accepts `&gt;=N`, `&lt;=N`, `&gt;N` and `&lt;N`. Alternatively, it is possible to use `ge(N)`, `le(N)`, `gt(N)` and `lt(N)` notation. |
| timeout | A time based parameter controlling how long to wait if one of the wait_for_XXX are provided. Defaults to `30s`. |
| local | If `true` returns the local node information and does not provide the state from master node. Default: `false`. |

下面是一个获取集群中 shards（分片）级别的集群健康的示例 : 

```
curl -XGET 'localhost:9200/_cluster/health/twitter?level=shards&pretty'
```