Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

elasticsearch - How to get number of current open shards in elsticsearch cluster?

I can't find where to get the number of 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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Yet another option is to query the _cluster/stats API:

curl -s -XGET ${ELK_HOST}/_cluster/stats?filter_path=indices.shards.total

That will return a JSON with the shard count

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

To my knowledge there is no single number that ES spits out from any API with the 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...