expose more information using the node info api

This commit is contained in:
kimchy 2010-05-08 23:01:38 +03:00
parent 6424d02be8
commit 462d426c53
10 changed files with 280 additions and 19 deletions

View File

@ -21,6 +21,10 @@ package org.elasticsearch.action.admin.cluster.node.info;
import org.elasticsearch.action.support.nodes.NodeOperationResponse; import org.elasticsearch.action.support.nodes.NodeOperationResponse;
import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.monitor.jvm.JvmInfo;
import org.elasticsearch.monitor.network.NetworkInfo;
import org.elasticsearch.monitor.os.OsInfo;
import org.elasticsearch.monitor.process.ProcessInfo;
import org.elasticsearch.util.collect.ImmutableMap; import org.elasticsearch.util.collect.ImmutableMap;
import org.elasticsearch.util.io.stream.StreamInput; import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput; import org.elasticsearch.util.io.stream.StreamOutput;
@ -39,17 +43,26 @@ public class NodeInfo extends NodeOperationResponse {
private Settings settings; private Settings settings;
private OsInfo os;
private ProcessInfo process;
private JvmInfo jvm;
private NetworkInfo network;
NodeInfo() { NodeInfo() {
} }
public NodeInfo(DiscoveryNode node, Map<String, String> attributes, Settings settings) { public NodeInfo(DiscoveryNode node, ImmutableMap<String, String> attributes, Settings settings,
this(node, ImmutableMap.copyOf(attributes), settings); OsInfo os, ProcessInfo process, JvmInfo jvm, NetworkInfo network) {
}
public NodeInfo(DiscoveryNode node, ImmutableMap<String, String> attributes, Settings settings) {
super(node); super(node);
this.attributes = attributes; this.attributes = attributes;
this.settings = settings; this.settings = settings;
this.os = os;
this.process = process;
this.jvm = jvm;
this.network = network;
} }
public ImmutableMap<String, String> attributes() { public ImmutableMap<String, String> attributes() {
@ -68,6 +81,38 @@ public class NodeInfo extends NodeOperationResponse {
return settings(); return settings();
} }
public OsInfo os() {
return this.os;
}
public OsInfo getOs() {
return os();
}
public ProcessInfo process() {
return process;
}
public ProcessInfo getProcess() {
return process();
}
public JvmInfo jvm() {
return jvm;
}
public JvmInfo getJvm() {
return jvm();
}
public NetworkInfo network() {
return network;
}
public NetworkInfo getNetwork() {
return network();
}
public static NodeInfo readNodeInfo(StreamInput in) throws IOException { public static NodeInfo readNodeInfo(StreamInput in) throws IOException {
NodeInfo nodeInfo = new NodeInfo(); NodeInfo nodeInfo = new NodeInfo();
nodeInfo.readFrom(in); nodeInfo.readFrom(in);
@ -83,6 +128,18 @@ public class NodeInfo extends NodeOperationResponse {
} }
attributes = builder.build(); attributes = builder.build();
settings = ImmutableSettings.readSettingsFromStream(in); settings = ImmutableSettings.readSettingsFromStream(in);
if (in.readBoolean()) {
os = OsInfo.readOsInfo(in);
}
if (in.readBoolean()) {
process = ProcessInfo.readProcessInfo(in);
}
if (in.readBoolean()) {
jvm = JvmInfo.readJvmInfo(in);
}
if (in.readBoolean()) {
network = NetworkInfo.readNetworkInfo(in);
}
} }
@Override public void writeTo(StreamOutput out) throws IOException { @Override public void writeTo(StreamOutput out) throws IOException {
@ -93,5 +150,29 @@ public class NodeInfo extends NodeOperationResponse {
out.writeUTF(entry.getValue()); out.writeUTF(entry.getValue());
} }
ImmutableSettings.writeSettingsToStream(settings, out); ImmutableSettings.writeSettingsToStream(settings, out);
if (os == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
os.writeTo(out);
}
if (process == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
process.writeTo(out);
}
if (jvm == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
jvm.writeTo(out);
}
if (network == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
network.writeTo(out);
}
} }
} }

View File

@ -25,6 +25,7 @@ import org.elasticsearch.action.support.nodes.NodeOperationRequest;
import org.elasticsearch.action.support.nodes.TransportNodesOperationAction; import org.elasticsearch.action.support.nodes.TransportNodesOperationAction;
import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.monitor.MonitorService;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService; import org.elasticsearch.transport.TransportService;
import org.elasticsearch.util.MapBuilder; import org.elasticsearch.util.MapBuilder;
@ -37,15 +38,19 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicReferenceArray; import java.util.concurrent.atomic.AtomicReferenceArray;
/** /**
* @author kimchy (Shay Banon) * @author kimchy (shay.banon)
*/ */
public class TransportNodesInfo extends TransportNodesOperationAction<NodesInfoRequest, NodesInfoResponse, TransportNodesInfo.NodeInfoRequest, NodeInfo> { public class TransportNodesInfo extends TransportNodesOperationAction<NodesInfoRequest, NodesInfoResponse, TransportNodesInfo.NodeInfoRequest, NodeInfo> {
private final MonitorService monitorService;
private volatile ImmutableMap<String, String> nodeAttributes = ImmutableMap.of(); private volatile ImmutableMap<String, String> nodeAttributes = ImmutableMap.of();
@Inject public TransportNodesInfo(Settings settings, ClusterName clusterName, ThreadPool threadPool, @Inject public TransportNodesInfo(Settings settings, ClusterName clusterName, ThreadPool threadPool,
ClusterService clusterService, TransportService transportService) { ClusterService clusterService, TransportService transportService,
MonitorService monitorService) {
super(settings, clusterName, threadPool, clusterService, transportService); super(settings, clusterName, threadPool, clusterService, transportService);
this.monitorService = monitorService;
} }
public synchronized void putNodeAttribute(String key, String value) { public synchronized void putNodeAttribute(String key, String value) {
@ -92,7 +97,9 @@ public class TransportNodesInfo extends TransportNodesOperationAction<NodesInfoR
} }
@Override protected NodeInfo nodeOperation(NodeInfoRequest nodeInfoRequest) throws ElasticSearchException { @Override protected NodeInfo nodeOperation(NodeInfoRequest nodeInfoRequest) throws ElasticSearchException {
return new NodeInfo(clusterService.state().nodes().localNode(), nodeAttributes, settings); return new NodeInfo(clusterService.state().nodes().localNode(), nodeAttributes, settings,
monitorService.osService().info(), monitorService.processService().info(),
monitorService.jvmService().info(), monitorService.networkService().info());
} }
@Override protected boolean accumulateExceptions() { @Override protected boolean accumulateExceptions() {

View File

@ -19,11 +19,15 @@
package org.elasticsearch.monitor; package org.elasticsearch.monitor;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.ElasticSearchException; import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.monitor.jvm.JvmMonitorService; import org.elasticsearch.monitor.jvm.JvmMonitorService;
import org.elasticsearch.monitor.jvm.JvmService;
import org.elasticsearch.monitor.memory.MemoryMonitorService; import org.elasticsearch.monitor.memory.MemoryMonitorService;
import org.elasticsearch.monitor.network.NetworkService;
import org.elasticsearch.monitor.os.OsService;
import org.elasticsearch.monitor.process.ProcessService;
import org.elasticsearch.util.component.AbstractLifecycleComponent; import org.elasticsearch.util.component.AbstractLifecycleComponent;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.settings.Settings; import org.elasticsearch.util.settings.Settings;
/** /**
@ -35,10 +39,39 @@ public class MonitorService extends AbstractLifecycleComponent<MonitorService> {
private final JvmMonitorService jvmMonitorService; private final JvmMonitorService jvmMonitorService;
@Inject public MonitorService(Settings settings, MemoryMonitorService memoryMonitorService, JvmMonitorService jvmMonitorService) { private final OsService osService;
private final ProcessService processService;
private final JvmService jvmService;
private final NetworkService networkService;
@Inject public MonitorService(Settings settings, MemoryMonitorService memoryMonitorService, JvmMonitorService jvmMonitorService,
OsService osService, ProcessService processService, JvmService jvmService, NetworkService networkService) {
super(settings); super(settings);
this.memoryMonitorService = memoryMonitorService; this.memoryMonitorService = memoryMonitorService;
this.jvmMonitorService = jvmMonitorService; this.jvmMonitorService = jvmMonitorService;
this.osService = osService;
this.processService = processService;
this.jvmService = jvmService;
this.networkService = networkService;
}
public OsService osService() {
return this.osService;
}
public ProcessService processService() {
return this.processService;
}
public JvmService jvmService() {
return this.jvmService;
}
public NetworkService networkService() {
return this.networkService;
} }
@Override protected void doStart() throws ElasticSearchException { @Override protected void doStart() throws ElasticSearchException {

View File

@ -22,6 +22,8 @@ package org.elasticsearch.monitor.jvm;
import org.elasticsearch.util.io.stream.StreamInput; import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput; import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable; import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.xcontent.ToXContent;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
@ -34,7 +36,7 @@ import java.util.Map;
/** /**
* @author kimchy (shay.banon) * @author kimchy (shay.banon)
*/ */
public class JvmInfo implements Streamable, Serializable { public class JvmInfo implements Streamable, Serializable, ToXContent {
private static JvmInfo INSTANCE; private static JvmInfo INSTANCE;
@ -181,6 +183,16 @@ public class JvmInfo implements Streamable, Serializable {
return systemProperties; return systemProperties;
} }
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("jvm");
builder.field("pid", pid);
builder.field("vm_name", vmName);
builder.field("vm_version", vmVersion);
builder.field("vm_vendor", vmVendor);
builder.field("start_time", startTime);
builder.endObject();
}
public static JvmInfo readJvmInfo(StreamInput in) throws IOException { public static JvmInfo readJvmInfo(StreamInput in) throws IOException {
JvmInfo jvmInfo = new JvmInfo(); JvmInfo jvmInfo = new JvmInfo();
jvmInfo.readFrom(in); jvmInfo.readFrom(in);

View File

@ -22,6 +22,8 @@ package org.elasticsearch.monitor.network;
import org.elasticsearch.util.io.stream.StreamInput; import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput; import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable; import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.xcontent.ToXContent;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
@ -29,7 +31,7 @@ import java.io.Serializable;
/** /**
* @author kimchy (shay.banon) * @author kimchy (shay.banon)
*/ */
public class NetworkInfo implements Streamable, Serializable { public class NetworkInfo implements Streamable, Serializable, ToXContent {
public static final Interface NA_INTERFACE = new Interface(); public static final Interface NA_INTERFACE = new Interface();
@ -43,6 +45,24 @@ public class NetworkInfo implements Streamable, Serializable {
return primaryInterface(); return primaryInterface();
} }
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("network");
if (primary != NA_INTERFACE) {
builder.startObject("primary_interface");
builder.field("address", primary.address());
builder.field("name", primary.name());
builder.field("mac_address", primary.macAddress());
builder.endObject();
}
builder.endObject();
}
public static NetworkInfo readNetworkInfo(StreamInput in) throws IOException {
NetworkInfo info = new NetworkInfo();
info.readFrom(in);
return info;
}
@Override public void readFrom(StreamInput in) throws IOException { @Override public void readFrom(StreamInput in) throws IOException {
primary = Interface.readNetworkInterface(in); primary = Interface.readNetworkInterface(in);
} }

View File

@ -46,8 +46,8 @@ public class SigarNetworkProbe extends AbstractComponent implements NetworkProbe
NetworkInfo networkInfo = new NetworkInfo(); NetworkInfo networkInfo = new NetworkInfo();
try { try {
NetInterfaceConfig netInterfaceConfig = sigar.getNetInterfaceConfig(); NetInterfaceConfig netInterfaceConfig = sigar.getNetInterfaceConfig(null);
networkInfo.primary = new NetworkInfo.Interface(netInterfaceConfig.getName(), netInterfaceConfig.getAddress(), netInterfaceConfig.getAddress()); networkInfo.primary = new NetworkInfo.Interface(netInterfaceConfig.getName(), netInterfaceConfig.getAddress(), netInterfaceConfig.getHwaddr());
} catch (SigarException e) { } catch (SigarException e) {
// ignore // ignore
} }

View File

@ -23,6 +23,8 @@ import org.elasticsearch.util.SizeValue;
import org.elasticsearch.util.io.stream.StreamInput; import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput; import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable; import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.xcontent.ToXContent;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
@ -30,7 +32,7 @@ import java.io.Serializable;
/** /**
* @author kimchy (shay.banon) * @author kimchy (shay.banon)
*/ */
public class OsInfo implements Streamable, Serializable { public class OsInfo implements Streamable, Serializable, ToXContent {
Cpu cpu = null; Cpu cpu = null;
@ -65,6 +67,35 @@ public class OsInfo implements Streamable, Serializable {
return swap(); return swap();
} }
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("os");
if (cpu != null) {
builder.startObject("cpu");
builder.field("vendor", cpu.vendor());
builder.field("model", cpu.model());
builder.field("mhz", cpu.mhz());
builder.field("total_cores", cpu.totalCores());
builder.field("total_sockets", cpu.totalSockets());
builder.field("cores_per_socket", cpu.coresPerSocket());
builder.field("cache_size", cpu.cacheSize().toString());
builder.field("cache_size_in_bytes", cpu.cacheSize().bytes());
builder.endObject();
}
if (mem != null) {
builder.startObject("mem");
builder.field("total", mem.total().toString());
builder.field("total_in_bytes", mem.total().bytes());
builder.endObject();
}
if (swap != null) {
builder.startObject("swap");
builder.field("total", swap.total().toString());
builder.field("total_in_bytes", swap.total().bytes());
builder.endObject();
}
builder.endObject();
}
public static OsInfo readOsInfo(StreamInput in) throws IOException { public static OsInfo readOsInfo(StreamInput in) throws IOException {
OsInfo info = new OsInfo(); OsInfo info = new OsInfo();
info.readFrom(in); info.readFrom(in);
@ -182,6 +213,62 @@ public class OsInfo implements Streamable, Serializable {
} }
public String vendor() {
return this.vendor;
}
public String getVendor() {
return vendor();
}
public String model() {
return model;
}
public String getModel() {
return model;
}
public int mhz() {
return mhz;
}
public int getMhz() {
return mhz;
}
public int totalCores() {
return totalCores;
}
public int getTotalCores() {
return totalCores();
}
public int totalSockets() {
return totalSockets;
}
public int getTotalSockets() {
return totalSockets();
}
public int coresPerSocket() {
return coresPerSocket;
}
public int getCoresPerSocket() {
return coresPerSocket();
}
public SizeValue cacheSize() {
return new SizeValue(cacheSize);
}
public SizeValue getCacheSize() {
return cacheSize();
}
public static Cpu readCpu(StreamInput in) throws IOException { public static Cpu readCpu(StreamInput in) throws IOException {
Cpu cpu = new Cpu(); Cpu cpu = new Cpu();
cpu.readFrom(in); cpu.readFrom(in);

View File

@ -22,6 +22,8 @@ package org.elasticsearch.monitor.process;
import org.elasticsearch.util.io.stream.StreamInput; import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput; import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable; import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.xcontent.ToXContent;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
@ -29,7 +31,7 @@ import java.io.Serializable;
/** /**
* @author kimchy (shay.banon) * @author kimchy (shay.banon)
*/ */
public class ProcessInfo implements Streamable, Serializable { public class ProcessInfo implements Streamable, Serializable, ToXContent {
private long id; private long id;
@ -55,6 +57,12 @@ public class ProcessInfo implements Streamable, Serializable {
return id(); return id();
} }
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("process");
builder.field("id", id);
builder.endObject();
}
public static ProcessInfo readProcessInfo(StreamInput in) throws IOException { public static ProcessInfo readProcessInfo(StreamInput in) throws IOException {
ProcessInfo info = new ProcessInfo(); ProcessInfo info = new ProcessInfo();
info.readFrom(in); info.readFrom(in);

View File

@ -89,6 +89,19 @@ public class RestNodesInfoAction extends BaseRestHandler {
builder.endObject(); builder.endObject();
} }
if (nodeInfo.os() != null) {
nodeInfo.os().toXContent(builder, request);
}
if (nodeInfo.process() != null) {
nodeInfo.process().toXContent(builder, request);
}
if (nodeInfo.jvm() != null) {
nodeInfo.jvm().toXContent(builder, request);
}
if (nodeInfo.network() != null) {
nodeInfo.network().toXContent(builder, request);
}
builder.endObject(); builder.endObject();
} }
builder.endObject(); builder.endObject();

View File

@ -111,13 +111,13 @@ public class SizeValue implements Serializable, Streamable {
String suffix = "b"; String suffix = "b";
if (bytes >= SizeUnit.C3) { if (bytes >= SizeUnit.C3) {
value = gbFrac(); value = gbFrac();
suffix = "gb"; suffix = "g";
} else if (bytes >= SizeUnit.C2) { } else if (bytes >= SizeUnit.C2) {
value = mbFrac(); value = mbFrac();
suffix = "mb"; suffix = "m";
} else if (bytes >= SizeUnit.C1) { } else if (bytes >= SizeUnit.C1) {
value = kbFrac(); value = kbFrac();
suffix = "kb"; suffix = "k";
} }
return Strings.format1Decimals(value, suffix); return Strings.format1Decimals(value, suffix);
} }