|
@@ -36,7 +36,7 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
|
|
|
long timestamp;
|
|
|
|
|
|
- double loadAverage = -1;
|
|
|
+ Cpu cpu = null;
|
|
|
|
|
|
Mem mem = null;
|
|
|
|
|
@@ -49,10 +49,7 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
return timestamp;
|
|
|
}
|
|
|
|
|
|
- public double getLoadAverage() {
|
|
|
- return loadAverage;
|
|
|
- }
|
|
|
-
|
|
|
+ public Cpu getCpu() { return cpu; }
|
|
|
|
|
|
public Mem getMem() {
|
|
|
return mem;
|
|
@@ -65,6 +62,8 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
static final class Fields {
|
|
|
static final XContentBuilderString OS = new XContentBuilderString("os");
|
|
|
static final XContentBuilderString TIMESTAMP = new XContentBuilderString("timestamp");
|
|
|
+ static final XContentBuilderString CPU = new XContentBuilderString("cpu");
|
|
|
+ static final XContentBuilderString PERCENT = new XContentBuilderString("percent");
|
|
|
static final XContentBuilderString LOAD_AVERAGE = new XContentBuilderString("load_average");
|
|
|
|
|
|
static final XContentBuilderString MEM = new XContentBuilderString("mem");
|
|
@@ -85,7 +84,12 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
builder.startObject(Fields.OS);
|
|
|
builder.field(Fields.TIMESTAMP, getTimestamp());
|
|
|
- builder.field(Fields.LOAD_AVERAGE, getLoadAverage());
|
|
|
+ if (cpu != null) {
|
|
|
+ builder.startObject(Fields.CPU);
|
|
|
+ builder.field(Fields.PERCENT, cpu.getPercent());
|
|
|
+ builder.field(Fields.LOAD_AVERAGE, cpu.getLoadAverage());
|
|
|
+ builder.endObject();
|
|
|
+ }
|
|
|
|
|
|
if (mem != null) {
|
|
|
builder.startObject(Fields.MEM);
|
|
@@ -120,7 +124,7 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
@Override
|
|
|
public void readFrom(StreamInput in) throws IOException {
|
|
|
timestamp = in.readVLong();
|
|
|
- loadAverage = in.readDouble();
|
|
|
+ cpu = in.readOptionalStreamable(Cpu::new);
|
|
|
if (in.readBoolean()) {
|
|
|
mem = Mem.readMem(in);
|
|
|
}
|
|
@@ -132,7 +136,7 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
@Override
|
|
|
public void writeTo(StreamOutput out) throws IOException {
|
|
|
out.writeVLong(timestamp);
|
|
|
- out.writeDouble(loadAverage);
|
|
|
+ out.writeOptionalStreamable(cpu);
|
|
|
if (mem == null) {
|
|
|
out.writeBoolean(false);
|
|
|
} else {
|
|
@@ -147,6 +151,39 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static class Cpu implements Streamable {
|
|
|
+ short percent = -1;
|
|
|
+ double loadAverage = -1;
|
|
|
+
|
|
|
+ Cpu() {}
|
|
|
+
|
|
|
+ public static Cpu readCpu(StreamInput in) throws IOException {
|
|
|
+ Cpu cpu = new Cpu();
|
|
|
+ cpu.readFrom(in);
|
|
|
+ return cpu;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void readFrom(StreamInput in) throws IOException {
|
|
|
+ percent = in.readShort();
|
|
|
+ loadAverage = in.readDouble();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void writeTo(StreamOutput out) throws IOException {
|
|
|
+ out.writeShort(percent);
|
|
|
+ out.writeDouble(loadAverage);
|
|
|
+ }
|
|
|
+
|
|
|
+ public short getPercent() {
|
|
|
+ return percent;
|
|
|
+ }
|
|
|
+
|
|
|
+ public double getLoadAverage() {
|
|
|
+ return loadAverage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static class Swap implements Streamable {
|
|
|
|
|
|
long total = -1;
|
|
@@ -230,5 +267,4 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
private static short calculatePercentage(long used, long max) {
|
|
|
return max <= 0 ? 0 : (short) (Math.round((100d * used) / max));
|
|
|
}
|
|
|
-
|
|
|
}
|