Skip to content

Commit

Permalink
Add health status parameter to cat indices API
Browse files Browse the repository at this point in the history
This commit adds a health status parameter to the cat indices API for
filtering on indices that match the specified status (green|yellow|red).

Relates #20393
  • Loading branch information
jasontedor committed Sep 13, 2016
1 parent 01dbf4b commit 7c76445
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.cluster.health.ClusterIndexHealth;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
Expand Down Expand Up @@ -314,16 +315,32 @@ protected Table getTableWithHeader(final RestRequest request) {
}

// package private for testing
Table buildTable(RestRequest request, Index[] indices, ClusterHealthResponse health, IndicesStatsResponse stats, MetaData indexMetaDatas) {
Table buildTable(RestRequest request, Index[] indices, ClusterHealthResponse response, IndicesStatsResponse stats, MetaData indexMetaDatas) {
final String healthParam = request.param("health");
final ClusterHealthStatus status;
if (healthParam != null) {
status = ClusterHealthStatus.fromString(healthParam);
} else {
status = null;
}

Table table = getTableWithHeader(request);

for (final Index index : indices) {
final String indexName = index.getName();
ClusterIndexHealth indexHealth = health.getIndices().get(indexName);
ClusterIndexHealth indexHealth = response.getIndices().get(indexName);
IndexStats indexStats = stats.getIndices().get(indexName);
IndexMetaData indexMetaData = indexMetaDatas.getIndices().get(indexName);
IndexMetaData.State state = indexMetaData.getState();

if (status != null) {
if (state == IndexMetaData.State.CLOSE ||
(indexHealth == null && !ClusterHealthStatus.RED.equals(status)) ||
!indexHealth.getStatus().equals(status)) {
continue;
}
}

table.startRow();
table.addCell(state == IndexMetaData.State.OPEN ? (indexHealth == null ? "red*" : indexHealth.getStatus().toString().toLowerCase(Locale.ROOT)) : null);
table.addCell(state.toString().toLowerCase(Locale.ROOT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.elasticsearch.rest.RestController;
import org.elasticsearch.search.suggest.completion.CompletionStats;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.rest.FakeRestRequest;

import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -107,7 +108,7 @@ public void testBuildTable() {
clusterState.getClusterName().value(), indicesStr, clusterState, 0, 0, 0, TimeValue.timeValueMillis(1000L)
);

final Table table = action.buildTable(null, indices, clusterHealth, randomIndicesStatsResponse(indices), metaData);
final Table table = action.buildTable(new FakeRestRequest(), indices, clusterHealth, randomIndicesStatsResponse(indices), metaData);

// now, verify the table is correct
int count = 0;
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/cat/indices.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Which indices are yellow?

[source,sh]
--------------------------------------------------
% curl localhost:9200/_cat/indices | grep ^yell
% curl localhost:9200/_cat/indices?health=yellow
yellow open wiki 2 1 6401 1115 151.4mb 151.4mb
yellow open twitter 5 1 11434 0 32mb 32mb
--------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"type": "list",
"description" : "Comma-separated list of column names to display"
},
"health": {
"type" : "string",
"description" : "A health status (\"green\", \"yellow\", or \"red\" to filter only indices matching the specified health status"
},
"help": {
"type": "boolean",
"description": "Return help information",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,47 @@
)
$/
---
"Test cat indices using health status":

- do:
cluster.health: {}

- set: { number_of_data_nodes: count }

- do:
indices.create:
index: foo
body:
settings:
number_of_shards: "1"
number_of_replicas: "0"
- do:
indices.create:
index: bar
body:
settings:
number_of_shards: "1"
number_of_replicas: $count

- do:
cat.indices:
health: green
h: index

- match:
$body: |
/^(foo)$/
- do:
cat.indices:
health: yellow
h: index

- match:
$body: |
/^(bar)$/
---
"Test cat indices using wildcards":

Expand Down

0 comments on commit 7c76445

Please sign in to comment.