# How to get current open shards?

**URL:** <https://discuss.elastic.co/t/how-to-get-current-open-shards/197528>\
**Category:** Elasticsearch\
**Created:** [August 30, 2019, 12:55pm UTC](https://discuss.elastic.co/t/how-to-get-current-open-shards/197528 "2019-08-30T12:55:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dmitry1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dmitry1/32/50610_2.png) [@Dmitry1](https://discuss.elastic.co/u/Dmitry1)\
**Post date:** [August 30, 2019, 12:55pm UTC](https://discuss.elastic.co/t/how-to-get-current-open-shards/197528/1 "2019-08-30T12:55:34Z")

</div>

Hello, can't find where I can to see current open shards ?  
I want to make monitoring to avoid cases like this:

`this cluster currently has [999]/[1000] maximum shards open`

I can get maximum limit - max\_shards\_per\_node  
`$ curl -X GET "${ELK_HOST}/_cluster/settings?include_defaults=true&flat_settings=true&pretty" 2>/dev/null | grep cluster.max_shards_per_node "cluster.max_shards_per_node" : "1000", $`

But can't find out how to get number of the current open shards (999).

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [August 30, 2019, 1:56pm UTC](https://discuss.elastic.co/t/how-to-get-current-open-shards/197528/2 "2019-08-30T13:56:43Z")

</div>

A very simple way to get this information is to call the [`_cat/shards`](https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-shards.html) API and count the number of lines using the `wc` shell command:

```
curl -s -XGET ${ELK_HOST}/_cat/shards | wc -l

```

That will yield a single number that represents the number of shards in your cluster.

Another way is to query the [`_cluster/stats`](https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-stats.html) API:

```
GET _cluster/stats?filter_path=indices.shards.total

```

That will return a JSON with the shard count

```
{
  "indices" : {
    "shards" : {
      "total" : 302
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![Dmitry1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dmitry1/32/50610_2.png) [@Dmitry1](https://discuss.elastic.co/u/Dmitry1)\
**Post date:** [August 30, 2019, 2:33pm UTC](https://discuss.elastic.co/t/how-to-get-current-open-shards/197528/3 "2019-08-30T14:33:06Z")

</div>

Thank you for reply, but second way is not equal first way.

on my ES (we must exclude empty line to get 999)

1. `GET /_cat/shards | grep -v '^$' | wc -l`  
returns 999

2. `GET _cluster/stats?filter_path=indices.shards.total`  
returns  
`{"indices":{"shards":{"total":790}}}`

due second way don't count UNASSIGNED shards

`GET /_cat/shards | grep -v '^$' | grep -v UNASSIGNED | wc -l`  
returns 790

so `GET _cluster/stats?filter_path=indices.shards.total` returns shards only in STARTED state and don't suit for my case

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [August 30, 2019, 2:55pm UTC](https://discuss.elastic.co/t/how-to-get-current-open-shards/197528/4 "2019-08-30T14:55:58Z")

</div>

Another option is to retrieve the cluster stats using JSON format, pipe the results into `jq` and then grab whatever you want, e.g. below I'm counting all STARTED shards:

```
curl -s -XGET ${ELK_HOST}/_cat/shards?format=json | jq ".[].state" | grep "STARTED" | wc -l

```

And to the best of my knowledge there is no single number that ES spits out from any API with that single number. To be sure of that, let's look at the source code.

1. The error is thrown from [`IndicesService.java`](https://github.com/elastic/elasticsearch/blob/55d4581ee4cd16e5b9b3146caf6d0e68e372e6a4/server/src/main/java/org/elasticsearch/indices/IndicesService.java#L1467-L1471)

2. To see how `currentOpenShards` is computed, we can then go to [`Metadata.java`](https://github.com/elastic/elasticsearch/blob/a9f351556f519e4f9b23e755a3e5da767512a66a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java#L202-L208).

As you can see, the code is iterating over the index metadata that is retrieved from the cluster state, pretty much like running the following command and count the number of shards, but only for indices with `"state" : "open"`

```
GET _cluster/state?filter_path=metadata.indices.*.settings.index.number_of*,metadata.indices.*.state

```

From that evidence, we can pretty much be sure that the single number you're looking for is nowhere to be found, but needs to be computed by one of the methods I showed above. You're free to [open a feature request](https://github.com/elastic/elasticsearch/issues) if needed.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 27, 2019, 2:56pm UTC](https://discuss.elastic.co/t/how-to-get-current-open-shards/197528/5 "2019-09-27T14:56:03Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
