|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.rest.action.cat; |
| 21 | +import com.google.common.collect.ImmutableMap; |
| 22 | +import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; |
| 23 | +import org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest; |
| 24 | +import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse; |
| 25 | +import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest; |
| 26 | +import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse; |
| 27 | +import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; |
| 28 | +import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; |
| 29 | +import org.elasticsearch.client.Client; |
| 30 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 31 | +import org.elasticsearch.cluster.node.DiscoveryNodes; |
| 32 | +import org.elasticsearch.common.Strings; |
| 33 | +import org.elasticsearch.common.Table; |
| 34 | +import org.elasticsearch.common.inject.Inject; |
| 35 | +import org.elasticsearch.common.settings.Settings; |
| 36 | +import org.elasticsearch.common.transport.InetSocketTransportAddress; |
| 37 | +import org.elasticsearch.rest.*; |
| 38 | +import org.elasticsearch.rest.action.support.RestActionListener; |
| 39 | +import org.elasticsearch.rest.action.support.RestResponseListener; |
| 40 | +import org.elasticsearch.rest.action.support.RestTable; |
| 41 | + |
| 42 | +import static org.elasticsearch.rest.RestRequest.Method.GET; |
| 43 | + |
| 44 | +public class RestNodeAttrsAction extends AbstractCatAction { |
| 45 | + |
| 46 | + @Inject |
| 47 | + public RestNodeAttrsAction(Settings settings, RestController controller, Client client) { |
| 48 | + super(settings, controller, client); |
| 49 | + controller.registerHandler(GET, "/_cat/nodeattrs", this); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + void documentation(StringBuilder sb) { |
| 54 | + sb.append("/_cat/nodeattrs\n"); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void doRequest(final RestRequest request, final RestChannel channel, final Client client) { |
| 59 | + final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(); |
| 60 | + clusterStateRequest.clear().nodes(true); |
| 61 | + clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local())); |
| 62 | + clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout())); |
| 63 | + |
| 64 | + client.admin().cluster().state(clusterStateRequest, new RestActionListener<ClusterStateResponse>(channel) { |
| 65 | + @Override |
| 66 | + public void processResponse(final ClusterStateResponse clusterStateResponse) { |
| 67 | + NodesInfoRequest nodesInfoRequest = new NodesInfoRequest(); |
| 68 | + nodesInfoRequest.clear().jvm(false).os(false).process(true); |
| 69 | + client.admin().cluster().nodesInfo(nodesInfoRequest, new RestActionListener<NodesInfoResponse>(channel) { |
| 70 | + @Override |
| 71 | + public void processResponse(final NodesInfoResponse nodesInfoResponse) { |
| 72 | + NodesStatsRequest nodesStatsRequest = new NodesStatsRequest(); |
| 73 | + nodesStatsRequest.clear().jvm(false).os(false).fs(false).indices(false).process(false); |
| 74 | + client.admin().cluster().nodesStats(nodesStatsRequest, new RestResponseListener<NodesStatsResponse>(channel) { |
| 75 | + @Override |
| 76 | + public RestResponse buildResponse(NodesStatsResponse nodesStatsResponse) throws Exception { |
| 77 | + return RestTable.buildResponse(buildTable(request, clusterStateResponse, nodesInfoResponse, nodesStatsResponse), channel); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + Table getTableWithHeader(final RestRequest request) { |
| 88 | + Table table = new Table(); |
| 89 | + table.startHeaders(); |
| 90 | + table.addCell("node", "default:true;alias:name;desc:node name"); |
| 91 | + table.addCell("id", "default:false;alias:id,nodeId;desc:unique node id"); |
| 92 | + table.addCell("pid", "default:false;alias:p;desc:process id"); |
| 93 | + table.addCell("host", "alias:h;desc:host name"); |
| 94 | + table.addCell("ip", "alias:i;desc:ip address"); |
| 95 | + table.addCell("port", "default:false;alias:po;desc:bound transport port"); |
| 96 | + table.addCell("attr", "default:true;alias:attr.name;desc:attribute description"); |
| 97 | + table.addCell("value","default:true;alias:attr.value;desc:attribute value"); |
| 98 | + table.endHeaders(); |
| 99 | + return table; |
| 100 | + } |
| 101 | + |
| 102 | + private Table buildTable(RestRequest req, ClusterStateResponse state, NodesInfoResponse nodesInfo, NodesStatsResponse nodesStats) { |
| 103 | + boolean fullId = req.paramAsBoolean("full_id", false); |
| 104 | + |
| 105 | + DiscoveryNodes nodes = state.getState().nodes(); |
| 106 | + Table table = getTableWithHeader(req); |
| 107 | + |
| 108 | + for (DiscoveryNode node : nodes) { |
| 109 | + NodeInfo info = nodesInfo.getNodesMap().get(node.id()); |
| 110 | + ImmutableMap<String, String> attrs = node.getAttributes(); |
| 111 | + for(String att : attrs.keySet()) { |
| 112 | + table.startRow(); |
| 113 | + table.addCell(node.name()); |
| 114 | + table.addCell(fullId ? node.id() : Strings.substring(node.getId(), 0, 4)); |
| 115 | + table.addCell(info == null ? null : info.getProcess().getId()); |
| 116 | + table.addCell(node.getHostName()); |
| 117 | + table.addCell(node.getHostAddress()); |
| 118 | + if (node.address() instanceof InetSocketTransportAddress) { |
| 119 | + table.addCell(((InetSocketTransportAddress) node.address()).address().getPort()); |
| 120 | + } else { |
| 121 | + table.addCell("-"); |
| 122 | + } |
| 123 | + table.addCell(att); |
| 124 | + table.addCell(attrs.containsKey(att) ? attrs.get(att) : null); |
| 125 | + table.endRow(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return table; |
| 130 | + } |
| 131 | +} |
0 commit comments