level = 2;
}
}
} catch (Exception e) {
try {
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
channel.sendResponse(new XContentRestResponse(request, PRECONDITION_FAILED, builder.startObject().field("error", e.getMessage()).endObject()));
} catch (IOException e1) {
logger.error("Failed to send failure response", e1);
}
return;
}
final int fLevel = level;
client.admin().cluster().health(clusterHealthRequest, new ActionListener<ClusterHealthResponse>() {
@Override public void onResponse(ClusterHealthResponse response) {
try {
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field(Fields.CLUSTER_NAME, response.clusterName());
builder.field(Fields.STATUS, response.status().name().toLowerCase());
builder.field(Fields.TIMED_OUT, response.timedOut());
builder.field(Fields.NUMBER_OF_NODES, response.numberOfNodes());
builder.field(Fields.NUMBER_OF_DATA_NODES, response.numberOfDataNodes());
builder.field(Fields.ACTIVE_PRIMARY_SHARDS, response.activePrimaryShards());
builder.field(Fields.ACTIVE_SHARDS, response.activeShards());
builder.field(Fields.RELOCATING_SHARDS, response.relocatingShards());
builder.field(Fields.INITIALIZING_SHARDS, response.initializingShards());
builder.field(Fields.UNASSIGNED_SHARDS, response.unassignedShards());
if (!response.validationFailures().isEmpty()) {
builder.startArray(Fields.VALIDATION_FAILURES);
for (String validationFailure : response.validationFailures()) {
builder.value(validationFailure);
}
// if we don't print index level information, still print the index validation failures
// so we know why the status is red
if (fLevel == 0) {
for (ClusterIndexHealth indexHealth : response) {
builder.startObject(indexHealth.index());
if (!indexHealth.validationFailures().isEmpty()) {
builder.startArray(Fields.VALIDATION_FAILURES);
for (String validationFailure : indexHealth.validationFailures()) {
builder.value(validationFailure);
}
builder.endArray();
}
builder.endObject();
}
}
builder.endArray();
}
if (fLevel > 0) {
builder.startObject(Fields.INDICES);
for (ClusterIndexHealth indexHealth : response) {
builder.startObject(indexHealth.index(), XContentBuilder.FieldCaseConversion.NONE);
builder.field(Fields.STATUS, indexHealth.status().name().toLowerCase());
builder.field(Fields.NUMBER_OF_SHARDS, indexHealth.numberOfShards());
builder.field(Fields.NUMBER_OF_REPLICAS, indexHealth.numberOfReplicas());
builder.field(Fields.ACTIVE_PRIMARY_SHARDS, indexHealth.activePrimaryShards());
builder.field(Fields.ACTIVE_SHARDS, indexHealth.activeShards());
builder.field(Fields.RELOCATING_SHARDS, indexHealth.relocatingShards());
builder.field(Fields.INITIALIZING_SHARDS, indexHealth.initializingShards());
builder.field(Fields.UNASSIGNED_SHARDS, indexHealth.unassignedShards());
if (!indexHealth.validationFailures().isEmpty()) {
builder.startArray(Fields.VALIDATION_FAILURES);
for (String validationFailure : indexHealth.validationFailures()) {
builder.value(validationFailure);
}
builder.endArray();
}
if (fLevel > 1) {
builder.startObject(Fields.SHARDS);
for (ClusterShardHealth shardHealth : indexHealth) {
builder.startObject(Integer.toString(shardHealth.id()));
builder.field(Fields.STATUS, shardHealth.status().name().toLowerCase());
builder.field(Fields.PRIMARY_ACTIVE, shardHealth.primaryActive());
builder.field(Fields.ACTIVE_SHARDS, shardHealth.activeShards());
builder.field(Fields.RELOCATING_SHARDS, shardHealth.relocatingShards());
builder.field(Fields.INITIALIZING_SHARDS, shardHealth.initializingShards());
builder.field(Fields.UNASSIGNED_SHARDS, shardHealth.unassignedShards());
builder.endObject();
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
channel.sendResponse(new XContentRestResponse(request, RestStatus.OK, builder));
} catch (Exception e) {
onFailure(e);
}