|
@@ -21,28 +21,42 @@ package org.elasticsearch.monitor.os;
|
|
|
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
|
|
-import org.elasticsearch.common.io.stream.Streamable;
|
|
|
+import org.elasticsearch.common.io.stream.Writeable;
|
|
|
import org.elasticsearch.common.unit.ByteSizeValue;
|
|
|
import org.elasticsearch.common.xcontent.ToXContent;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.Arrays;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
-/**
|
|
|
- *
|
|
|
- */
|
|
|
-public class OsStats implements Streamable, ToXContent {
|
|
|
-
|
|
|
- long timestamp;
|
|
|
+public class OsStats implements Writeable, ToXContent {
|
|
|
|
|
|
- Cpu cpu = null;
|
|
|
+ private final long timestamp;
|
|
|
+ private final Cpu cpu;
|
|
|
+ private final Mem mem;
|
|
|
+ private final Swap swap;
|
|
|
|
|
|
- Mem mem = null;
|
|
|
+ public OsStats(long timestamp, Cpu cpu, Mem mem, Swap swap) {
|
|
|
+ this.timestamp = timestamp;
|
|
|
+ this.cpu = Objects.requireNonNull(cpu, "cpu must not be null");
|
|
|
+ this.mem = Objects.requireNonNull(mem, "mem must not be null");;
|
|
|
+ this.swap = Objects.requireNonNull(swap, "swap must not be null");;
|
|
|
+ }
|
|
|
|
|
|
- Swap swap = null;
|
|
|
+ public OsStats(StreamInput in) throws IOException {
|
|
|
+ this.timestamp = in.readVLong();
|
|
|
+ this.cpu = new Cpu(in);
|
|
|
+ this.mem = new Mem(in);
|
|
|
+ this.swap = new Swap(in);
|
|
|
+ }
|
|
|
|
|
|
- OsStats() {
|
|
|
+ @Override
|
|
|
+ public void writeTo(StreamOutput out) throws IOException {
|
|
|
+ out.writeVLong(timestamp);
|
|
|
+ cpu.writeTo(out);
|
|
|
+ mem.writeTo(out);
|
|
|
+ swap.writeTo(out);
|
|
|
}
|
|
|
|
|
|
public long getTimestamp() {
|
|
@@ -65,9 +79,9 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
static final String CPU = "cpu";
|
|
|
static final String PERCENT = "percent";
|
|
|
static final String LOAD_AVERAGE = "load_average";
|
|
|
- static final String LOAD_AVERAGE_1M = new String("1m");
|
|
|
- static final String LOAD_AVERAGE_5M = new String("5m");
|
|
|
- static final String LOAD_AVERAGE_15M = new String("15m");
|
|
|
+ static final String LOAD_AVERAGE_1M = "1m";
|
|
|
+ static final String LOAD_AVERAGE_5M = "5m";
|
|
|
+ static final String LOAD_AVERAGE_15M = "15m";
|
|
|
|
|
|
static final String MEM = "mem";
|
|
|
static final String SWAP = "swap";
|
|
@@ -86,105 +100,29 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
builder.startObject(Fields.OS);
|
|
|
builder.field(Fields.TIMESTAMP, getTimestamp());
|
|
|
- if (cpu != null) {
|
|
|
- builder.startObject(Fields.CPU);
|
|
|
- builder.field(Fields.PERCENT, cpu.getPercent());
|
|
|
- if (cpu.getLoadAverage() != null && Arrays.stream(cpu.getLoadAverage()).anyMatch(load -> load != -1)) {
|
|
|
- builder.startObject(Fields.LOAD_AVERAGE);
|
|
|
- if (cpu.getLoadAverage()[0] != -1) {
|
|
|
- builder.field(Fields.LOAD_AVERAGE_1M, cpu.getLoadAverage()[0]);
|
|
|
- }
|
|
|
- if (cpu.getLoadAverage()[1] != -1) {
|
|
|
- builder.field(Fields.LOAD_AVERAGE_5M, cpu.getLoadAverage()[1]);
|
|
|
- }
|
|
|
- if (cpu.getLoadAverage()[2] != -1) {
|
|
|
- builder.field(Fields.LOAD_AVERAGE_15M, cpu.getLoadAverage()[2]);
|
|
|
- }
|
|
|
- builder.endObject();
|
|
|
- }
|
|
|
- builder.endObject();
|
|
|
- }
|
|
|
-
|
|
|
- if (mem != null) {
|
|
|
- builder.startObject(Fields.MEM);
|
|
|
- builder.byteSizeField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, mem.getTotal());
|
|
|
- builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, mem.getFree());
|
|
|
- builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, mem.getUsed());
|
|
|
-
|
|
|
- builder.field(Fields.FREE_PERCENT, mem.getFreePercent());
|
|
|
- builder.field(Fields.USED_PERCENT, mem.getUsedPercent());
|
|
|
-
|
|
|
- builder.endObject();
|
|
|
- }
|
|
|
-
|
|
|
- if (swap != null) {
|
|
|
- builder.startObject(Fields.SWAP);
|
|
|
- builder.byteSizeField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, swap.getTotal());
|
|
|
- builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, swap.getFree());
|
|
|
- builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, swap.getUsed());
|
|
|
- builder.endObject();
|
|
|
- }
|
|
|
-
|
|
|
+ cpu.toXContent(builder, params);
|
|
|
+ mem.toXContent(builder, params);
|
|
|
+ swap.toXContent(builder, params);
|
|
|
builder.endObject();
|
|
|
return builder;
|
|
|
}
|
|
|
|
|
|
- public static OsStats readOsStats(StreamInput in) throws IOException {
|
|
|
- OsStats stats = new OsStats();
|
|
|
- stats.readFrom(in);
|
|
|
- return stats;
|
|
|
- }
|
|
|
+ public static class Cpu implements Writeable, ToXContent {
|
|
|
|
|
|
- @Override
|
|
|
- public void readFrom(StreamInput in) throws IOException {
|
|
|
- timestamp = in.readVLong();
|
|
|
- cpu = in.readOptionalStreamable(Cpu::new);
|
|
|
- if (in.readBoolean()) {
|
|
|
- mem = Mem.readMem(in);
|
|
|
- }
|
|
|
- if (in.readBoolean()) {
|
|
|
- swap = Swap.readSwap(in);
|
|
|
- }
|
|
|
- }
|
|
|
+ private final short percent;
|
|
|
+ private final double[] loadAverage;
|
|
|
|
|
|
- @Override
|
|
|
- public void writeTo(StreamOutput out) throws IOException {
|
|
|
- out.writeVLong(timestamp);
|
|
|
- out.writeOptionalStreamable(cpu);
|
|
|
- if (mem == null) {
|
|
|
- out.writeBoolean(false);
|
|
|
- } else {
|
|
|
- out.writeBoolean(true);
|
|
|
- mem.writeTo(out);
|
|
|
+ public Cpu(short systemCpuPercent, double[] systemLoadAverage) {
|
|
|
+ this.percent = systemCpuPercent;
|
|
|
+ this.loadAverage = systemLoadAverage;
|
|
|
}
|
|
|
- if (swap == null) {
|
|
|
- out.writeBoolean(false);
|
|
|
- } else {
|
|
|
- out.writeBoolean(true);
|
|
|
- swap.writeTo(out);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static class Cpu implements Streamable {
|
|
|
-
|
|
|
- short percent = -1;
|
|
|
- double[] loadAverage = null;
|
|
|
-
|
|
|
- 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();
|
|
|
+ public Cpu(StreamInput in) throws IOException {
|
|
|
+ this.percent = in.readShort();
|
|
|
if (in.readBoolean()) {
|
|
|
- loadAverage = in.readDoubleArray();
|
|
|
+ this.loadAverage = in.readDoubleArray();
|
|
|
} else {
|
|
|
- loadAverage = null;
|
|
|
+ this.loadAverage = null;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -206,12 +144,49 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
public double[] getLoadAverage() {
|
|
|
return loadAverage;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
+ builder.startObject(Fields.CPU);
|
|
|
+ builder.field(Fields.PERCENT, getPercent());
|
|
|
+ if (getLoadAverage() != null && Arrays.stream(getLoadAverage()).anyMatch(load -> load != -1)) {
|
|
|
+ builder.startObject(Fields.LOAD_AVERAGE);
|
|
|
+ if (getLoadAverage()[0] != -1) {
|
|
|
+ builder.field(Fields.LOAD_AVERAGE_1M, getLoadAverage()[0]);
|
|
|
+ }
|
|
|
+ if (getLoadAverage()[1] != -1) {
|
|
|
+ builder.field(Fields.LOAD_AVERAGE_5M, getLoadAverage()[1]);
|
|
|
+ }
|
|
|
+ if (getLoadAverage()[2] != -1) {
|
|
|
+ builder.field(Fields.LOAD_AVERAGE_15M, getLoadAverage()[2]);
|
|
|
+ }
|
|
|
+ builder.endObject();
|
|
|
+ }
|
|
|
+ builder.endObject();
|
|
|
+ return builder;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public static class Swap implements Streamable {
|
|
|
+ public static class Swap implements Writeable, ToXContent {
|
|
|
+
|
|
|
+ private final long total;
|
|
|
+ private final long free;
|
|
|
|
|
|
- long total = -1;
|
|
|
- long free = -1;
|
|
|
+ public Swap(long total, long free) {
|
|
|
+ this.total = total;
|
|
|
+ this.free = free;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Swap(StreamInput in) throws IOException {
|
|
|
+ this.total = in.readLong();
|
|
|
+ this.free = in.readLong();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void writeTo(StreamOutput out) throws IOException {
|
|
|
+ out.writeLong(total);
|
|
|
+ out.writeLong(free);
|
|
|
+ }
|
|
|
|
|
|
public ByteSizeValue getFree() {
|
|
|
return new ByteSizeValue(free);
|
|
@@ -225,40 +200,30 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
return new ByteSizeValue(total);
|
|
|
}
|
|
|
|
|
|
- public static Swap readSwap(StreamInput in) throws IOException {
|
|
|
- Swap swap = new Swap();
|
|
|
- swap.readFrom(in);
|
|
|
- return swap;
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
- public void readFrom(StreamInput in) throws IOException {
|
|
|
- total = in.readLong();
|
|
|
- free = in.readLong();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void writeTo(StreamOutput out) throws IOException {
|
|
|
- out.writeLong(total);
|
|
|
- out.writeLong(free);
|
|
|
+ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
+ builder.startObject(Fields.SWAP);
|
|
|
+ builder.byteSizeField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, getTotal());
|
|
|
+ builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, getFree());
|
|
|
+ builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, getUsed());
|
|
|
+ builder.endObject();
|
|
|
+ return builder;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static class Mem implements Streamable {
|
|
|
+ public static class Mem implements Writeable, ToXContent {
|
|
|
|
|
|
- long total = -1;
|
|
|
- long free = -1;
|
|
|
+ private final long total;
|
|
|
+ private final long free;
|
|
|
|
|
|
- public static Mem readMem(StreamInput in) throws IOException {
|
|
|
- Mem mem = new Mem();
|
|
|
- mem.readFrom(in);
|
|
|
- return mem;
|
|
|
+ public Mem(long total, long free) {
|
|
|
+ this.total = total;
|
|
|
+ this.free = free;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void readFrom(StreamInput in) throws IOException {
|
|
|
- total = in.readLong();
|
|
|
- free = in.readLong();
|
|
|
+ public Mem(StreamInput in) throws IOException {
|
|
|
+ this.total = in.readLong();
|
|
|
+ this.free = in.readLong();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -276,7 +241,7 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
}
|
|
|
|
|
|
public short getUsedPercent() {
|
|
|
- return calculatePercentage(getUsed().bytes(), getTotal().bytes());
|
|
|
+ return calculatePercentage(getUsed().bytes(), total);
|
|
|
}
|
|
|
|
|
|
public ByteSizeValue getFree() {
|
|
@@ -284,11 +249,23 @@ public class OsStats implements Streamable, ToXContent {
|
|
|
}
|
|
|
|
|
|
public short getFreePercent() {
|
|
|
- return calculatePercentage(getFree().bytes(), getTotal().bytes());
|
|
|
+ return calculatePercentage(free, total);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
+ builder.startObject(Fields.MEM);
|
|
|
+ builder.byteSizeField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, getTotal());
|
|
|
+ builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, getFree());
|
|
|
+ builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, getUsed());
|
|
|
+ builder.field(Fields.FREE_PERCENT, getFreePercent());
|
|
|
+ builder.field(Fields.USED_PERCENT, getUsedPercent());
|
|
|
+ builder.endObject();
|
|
|
+ return builder;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static short calculatePercentage(long used, long max) {
|
|
|
+ public static short calculatePercentage(long used, long max) {
|
|
|
return max <= 0 ? 0 : (short) (Math.round((100d * used) / max));
|
|
|
}
|
|
|
}
|