How to get current open shards?

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).

A very simple way to get this information is to call the _cat/shards 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 API:

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

That will return a JSON with the shard count

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

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

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

  2. To see how currentOpenShards is computed, we can then go to Metadata.java.

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 if needed.

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