Skip to content

Commit 1df2d30

Browse files
committed
Add OS name to _nodes and _cluster/nodes
we currently don't expose this. This adds the following to the OS section of `_nodes`: ``` "os": { "name": "Mac OS X", ... } ``` and the following to the OS section of `_cluster/stats`: ``` "os": { ... "names": [ { "name": "Mac OS X", "count": 1 } ], ... }, ``` Closes #11807
1 parent 2e07d0b commit 1df2d30

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

core/src/main/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsNodes.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,17 +303,22 @@ public static class OsStats implements ToXContent, Streamable {
303303

304304
int availableProcessors;
305305
long availableMemory;
306-
ObjectIntHashMap<OsInfo.Cpu> cpus;
306+
final ObjectIntHashMap<String> names;
307+
final ObjectIntHashMap<OsInfo.Cpu> cpus;
307308

308309
public OsStats() {
309310
cpus = new ObjectIntHashMap<>();
311+
names = new ObjectIntHashMap<>();
310312
}
311313

312314
public void addNodeInfo(NodeInfo nodeInfo) {
313315
availableProcessors += nodeInfo.getOs().availableProcessors();
314316
if (nodeInfo.getOs() == null) {
315317
return;
316318
}
319+
if (nodeInfo.getOs().getName() != null) {
320+
names.addTo(nodeInfo.getOs().getName(), 1);
321+
}
317322
if (nodeInfo.getOs().cpu() != null) {
318323
cpus.addTo(nodeInfo.getOs().cpu(), 1);
319324
}
@@ -339,8 +344,13 @@ public void readFrom(StreamInput in) throws IOException {
339344
availableProcessors = in.readVInt();
340345
availableMemory = in.readLong();
341346
int size = in.readVInt();
342-
cpus = new ObjectIntHashMap<>(size);
343-
for (; size > 0; size--) {
347+
names.clear();
348+
for (int i = 0; i < size; i++) {
349+
names.addTo(in.readString(), in.readVInt());
350+
}
351+
size = in.readVInt();
352+
cpus.clear();
353+
for (int i = 0; i < size; i++) {
344354
cpus.addTo(OsInfo.Cpu.readCpu(in), in.readVInt());
345355
}
346356
}
@@ -349,12 +359,16 @@ public void readFrom(StreamInput in) throws IOException {
349359
public void writeTo(StreamOutput out) throws IOException {
350360
out.writeVInt(availableProcessors);
351361
out.writeLong(availableMemory);
362+
out.writeVInt(names.size());
363+
for (ObjectIntCursor<String> name : names) {
364+
out.writeString(name.key);
365+
out.writeVInt(name.value);
366+
}
352367
out.writeVInt(cpus.size());
353368
for (ObjectIntCursor<OsInfo.Cpu> c : cpus) {
354369
c.key.writeTo(out);
355370
out.writeVInt(c.value);
356371
}
357-
358372
}
359373

360374
public static OsStats readOsStats(StreamInput in) throws IOException {
@@ -365,6 +379,8 @@ public static OsStats readOsStats(StreamInput in) throws IOException {
365379

366380
static final class Fields {
367381
static final XContentBuilderString AVAILABLE_PROCESSORS = new XContentBuilderString("available_processors");
382+
static final XContentBuilderString NAME = new XContentBuilderString("name");
383+
static final XContentBuilderString NAMES = new XContentBuilderString("names");
368384
static final XContentBuilderString MEM = new XContentBuilderString("mem");
369385
static final XContentBuilderString TOTAL = new XContentBuilderString("total");
370386
static final XContentBuilderString TOTAL_IN_BYTES = new XContentBuilderString("total_in_bytes");
@@ -379,6 +395,15 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
379395
builder.byteSizeField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, availableMemory);
380396
builder.endObject();
381397

398+
builder.startArray(Fields.NAMES);
399+
for (ObjectIntCursor<String> name : names) {
400+
builder.startObject();
401+
builder.field(Fields.NAME, name.key);
402+
builder.field(Fields.COUNT, name.value);
403+
builder.endObject();
404+
}
405+
builder.endArray();
406+
382407
builder.startArray(Fields.CPU);
383408
for (ObjectIntCursor<OsInfo.Cpu> cpu : cpus) {
384409
builder.startObject();

core/src/main/java/org/elasticsearch/monitor/os/OsInfo.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public class OsInfo implements Streamable, Serializable, ToXContent {
3939

4040
int availableProcessors;
4141

42+
String name = null;
43+
4244
Cpu cpu = null;
4345

4446
Mem mem = null;
@@ -88,8 +90,13 @@ public Swap getSwap() {
8890
return swap();
8991
}
9092

93+
public String getName() {
94+
return name;
95+
}
96+
9197
static final class Fields {
9298
static final XContentBuilderString OS = new XContentBuilderString("os");
99+
static final XContentBuilderString NAME = new XContentBuilderString("name");
93100
static final XContentBuilderString REFRESH_INTERVAL = new XContentBuilderString("refresh_interval");
94101
static final XContentBuilderString REFRESH_INTERVAL_IN_MILLIS = new XContentBuilderString("refresh_interval_in_millis");
95102
static final XContentBuilderString AVAILABLE_PROCESSORS = new XContentBuilderString("available_processors");
@@ -112,6 +119,9 @@ static final class Fields {
112119
@Override
113120
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
114121
builder.startObject(Fields.OS);
122+
if (name != null) {
123+
builder.field(Fields.NAME, name);
124+
}
115125
builder.timeValueField(Fields.REFRESH_INTERVAL_IN_MILLIS, Fields.REFRESH_INTERVAL, refreshInterval);
116126
builder.field(Fields.AVAILABLE_PROCESSORS, availableProcessors);
117127
if (cpu != null) {

core/src/main/java/org/elasticsearch/monitor/os/OsService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.monitor.os;
2121

22+
import org.apache.lucene.util.Constants;
2223
import org.elasticsearch.common.component.AbstractComponent;
2324
import org.elasticsearch.common.inject.Inject;
2425
import org.elasticsearch.common.settings.Settings;
@@ -46,6 +47,7 @@ public OsService(Settings settings, OsProbe probe) {
4647
this.info = probe.osInfo();
4748
this.info.refreshInterval = refreshInterval.millis();
4849
this.info.availableProcessors = Runtime.getRuntime().availableProcessors();
50+
this.info.name = Constants.OS_NAME;
4951
osStatsCache = new OsStatsCache(refreshInterval, probe.osStats());
5052
logger.debug("Using probe [{}] with refresh_interval [{}]", probe, refreshInterval);
5153
}

docs/reference/cluster/stats.asciidoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ Will return, for example:
8686
"total": "8gb",
8787
"total_in_bytes": 8589934592
8888
},
89+
"names": [
90+
{
91+
"name": "Mac OS X",
92+
"count": 1
93+
}
94+
],
8995
"cpu": [
9096
{
9197
"vendor": "Intel",

0 commit comments

Comments
 (0)