Skip to content

Commit

Permalink
Add _cat/health.
Browse files Browse the repository at this point in the history
% curl localhost:9200/_cat/health\?v=1\&ts=0
cluster  status nodeTotal nodeData shards pri relo init unassign
kluster green          1        1     20  20    0    0        0

% curl localhost:9200/_cat/health\?ts=0
kluster green 1 1 20 20 0 0 0

% curl localhost:9200/_cat/health\?v=1
epoch      time     cluster  status nodeTotal nodeData shards pri relo init unassign
1383341092 16:24:52 kluster green          1        1     20  20    0    0        0

% curl localhost:9200/_cat/health
1383341119 16:25:19 kluster green 1 1 20 20 0 0 0

Closes #4050.
  • Loading branch information
drewr committed Nov 1, 2013
1 parent 48f4ba0 commit f9a1726
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 7 deletions.
Expand Up @@ -70,12 +70,7 @@
import org.elasticsearch.rest.action.admin.indices.warmer.get.RestGetWarmerAction;
import org.elasticsearch.rest.action.admin.indices.warmer.put.RestPutWarmerAction;
import org.elasticsearch.rest.action.bulk.RestBulkAction;
import org.elasticsearch.rest.action.cat.RestAllocationAction;
import org.elasticsearch.rest.action.cat.RestShardsAction;
import org.elasticsearch.rest.action.cat.RestIndicesAction;
import org.elasticsearch.rest.action.cat.RestMasterAction;
import org.elasticsearch.rest.action.cat.RestNodesAction;
import org.elasticsearch.rest.action.cat.RestRecoveryAction;
import org.elasticsearch.rest.action.cat.*;
import org.elasticsearch.rest.action.delete.RestDeleteAction;
import org.elasticsearch.rest.action.deletebyquery.RestDeleteByQueryAction;
import org.elasticsearch.rest.action.explain.RestExplainAction;
Expand Down Expand Up @@ -189,6 +184,7 @@ protected void configure() {

bind(RestSearchAction.class).asEagerSingleton();
bind(RestSearchScrollAction.class).asEagerSingleton();
bind(RestClearScrollAction.class).asEagerSingleton();
bind(RestMultiSearchAction.class).asEagerSingleton();

bind(RestValidateQueryAction.class).asEagerSingleton();
Expand All @@ -205,6 +201,6 @@ protected void configure() {
// Fully qualified to prevent interference with rest.action.count.RestCountAction
bind(org.elasticsearch.rest.action.cat.RestCountAction.class).asEagerSingleton();
bind(RestRecoveryAction.class).asEagerSingleton();
bind(RestClearScrollAction.class).asEagerSingleton();
bind(RestHealthAction.class).asEagerSingleton();
}
}
108 changes: 108 additions & 0 deletions src/main/java/org/elasticsearch/rest/action/cat/RestHealthAction.java
@@ -0,0 +1,108 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.rest.action.cat;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Table;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.table.TimestampedTable;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestTable;

import java.io.IOException;
import java.util.Locale;

import static org.elasticsearch.rest.RestRequest.Method.GET;

public class RestHealthAction extends BaseRestHandler {

@Inject
public RestHealthAction(Settings settings, Client client, RestController controller) {
super(settings, client);
controller.registerHandler(GET, "/_cat/health", this);
}

@Override
public void handleRequest(final RestRequest request, final RestChannel channel) {
ClusterHealthRequest clusterHealthRequest = new ClusterHealthRequest();
final boolean timeStamp = request.paramAsBoolean("ts", true);

client.admin().cluster().health(clusterHealthRequest, new ActionListener<ClusterHealthResponse>() {
@Override
public void onResponse(final ClusterHealthResponse health) {
try {
channel.sendResponse(RestTable.buildResponse(buildTable(health, timeStamp), request, channel));
} catch (Throwable t) {
onFailure(t);
}
}

@Override
public void onFailure(Throwable e) {
try {
channel.sendResponse(new XContentThrowableRestResponse(request, e));
} catch (IOException e1) {
logger.error("Failed to send failure response", e1);
}
}
});
}

private Table buildTable (final ClusterHealthResponse health, boolean timeStamp) {
Table t;

if (timeStamp) {
t = new TimestampedTable();
} else {
t = new Table();
}

if (null != health) {
t.startHeaders();
t.addCell("cluster");
t.addCell("status");
t.addCell("nodeTotal", "text-align:right;");
t.addCell("nodeData", "text-align:right;");
t.addCell("shards", "text-align:right;");
t.addCell("pri", "text-align:right;");
t.addCell("relo", "text-align:right;");
t.addCell("init", "text-align:right;");
t.addCell("unassign", "text-align:right;");
t.endHeaders();

t.startRow();
t.addCell(health.getClusterName());
t.addCell(health.getStatus().name().toLowerCase(Locale.ROOT));
t.addCell(health.getNumberOfNodes());
t.addCell(health.getNumberOfDataNodes());
t.addCell(health.getActiveShards());
t.addCell(health.getActivePrimaryShards());
t.addCell(health.getRelocatingShards());
t.addCell(health.getInitializingShards());
t.addCell(health.getUnassignedShards());
t.endRow();
}
return t;
}
}

0 comments on commit f9a1726

Please sign in to comment.