Add the hostname to the node info

This commit is contained in:
Nicolas Lalevée 2011-10-31 14:50:18 +01:00 committed by Shay Banon
parent 41a9dc1e6d
commit 4693b1e875
3 changed files with 45 additions and 2 deletions

View File

@ -44,6 +44,9 @@ public class NodeInfo extends NodeOperationResponse {
private ImmutableMap<String, String> serviceAttributes;
@Nullable
private String hostname;
private Settings settings;
private OsInfo os;
@ -61,10 +64,11 @@ public class NodeInfo extends NodeOperationResponse {
NodeInfo() {
}
public NodeInfo(DiscoveryNode node, ImmutableMap<String, String> serviceAttributes, Settings settings,
public NodeInfo(String hostname, DiscoveryNode node, ImmutableMap<String, String> serviceAttributes, Settings settings,
OsInfo os, ProcessInfo process, JvmInfo jvm, NetworkInfo network,
TransportInfo transport, @Nullable HttpInfo http) {
super(node);
this.hostname = hostname;
this.serviceAttributes = serviceAttributes;
this.settings = settings;
this.os = os;
@ -75,6 +79,22 @@ public class NodeInfo extends NodeOperationResponse {
this.http = http;
}
/**
* System's hostname. <code>null</code> in case of UnknownHostException
*/
@Nullable
public String hostname() {
return this.hostname;
}
/**
* System's hostname. <code>null</code> in case of UnknownHostException
*/
@Nullable
public String getHostname() {
return hostname();
}
/**
* The service attributes of the node.
*/
@ -184,6 +204,9 @@ public class NodeInfo extends NodeOperationResponse {
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
if (in.readBoolean()) {
hostname = in.readUTF();
}
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
int size = in.readVInt();
for (int i = 0; i < size; i++) {
@ -214,6 +237,12 @@ public class NodeInfo extends NodeOperationResponse {
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
if (hostname == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeUTF(hostname);
}
out.writeVInt(serviceAttributes.size());
for (Map.Entry<String, String> entry : serviceAttributes.entrySet()) {
out.writeUTF(entry.getKey());

View File

@ -19,6 +19,8 @@
package org.elasticsearch.node.service;
import java.net.InetAddress;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
@ -27,6 +29,7 @@ import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.network.NetworkUtils;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.discovery.Discovery;
import org.elasticsearch.http.HttpServer;
@ -51,6 +54,9 @@ public class NodeService extends AbstractComponent {
private volatile ImmutableMap<String, String> serviceAttributes = ImmutableMap.of();
@Nullable
private String hostname;
@Inject
public NodeService(Settings settings, MonitorService monitorService, Discovery discovery, ClusterService clusterService, TransportService transportService, IndicesService indicesService) {
super(settings);
@ -59,6 +65,10 @@ public class NodeService extends AbstractComponent {
this.transportService = transportService;
this.indicesService = indicesService;
discovery.setNodeService(this);
InetAddress address = NetworkUtils.getLocalAddress();
if (address != null) {
this.hostname = address.getHostName();
}
}
public void setHttpServer(@Nullable HttpServer httpServer) {
@ -91,7 +101,7 @@ public class NodeService extends AbstractComponent {
}
public NodeInfo info() {
return new NodeInfo(clusterService.state().nodes().localNode(), serviceAttributes, settings,
return new NodeInfo(hostname, clusterService.state().nodes().localNode(), serviceAttributes, settings,
monitorService.osService().info(), monitorService.processService().info(),
monitorService.jvmService().info(), monitorService.networkService().info(),
transportService.info(), httpServer == null ? null : httpServer.info());

View File

@ -73,6 +73,10 @@ public class RestNodesInfoAction extends BaseRestHandler {
builder.field("name", nodeInfo.node().name(), XContentBuilder.FieldCaseConversion.NONE);
builder.field("transport_address", nodeInfo.node().address().toString());
if (nodeInfo.hostname() != null) {
builder.field("hostname", nodeInfo.hostname(), XContentBuilder.FieldCaseConversion.NONE);
}
for (Map.Entry<String, String> nodeAttribute : nodeInfo.serviceAttributes().entrySet()) {
builder.field(nodeAttribute.getKey(), nodeAttribute.getValue());
}