forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OsStats.java
441 lines (357 loc) · 13 KB
/
OsStats.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
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.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import java.io.IOException;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
/**
*
*/
public class OsStats implements Streamable, Serializable, ToXContent {
public static final double[] EMPTY_LOAD = new double[0];
long timestamp;
double[] loadAverage = EMPTY_LOAD;
long uptime = -1;
Cpu cpu = null;
Mem mem = null;
Swap swap = null;
OsStats() {
}
public long timestamp() {
return timestamp;
}
public long getTimestamp() {
return timestamp();
}
public double[] loadAverage() {
return loadAverage;
}
public double[] getLoadAverage() {
return loadAverage();
}
public TimeValue uptime() {
return new TimeValue(uptime, TimeUnit.SECONDS);
}
public TimeValue getUptime() {
return uptime();
}
public Cpu cpu() {
return this.cpu;
}
public Cpu getCpu() {
return cpu();
}
public Mem mem() {
return this.mem;
}
public Mem getMem() {
return mem();
}
public Swap swap() {
return this.swap;
}
public Swap getSwap() {
return swap();
}
static final class Fields {
static final XContentBuilderString OS = new XContentBuilderString("os");
static final XContentBuilderString TIMESTAMP = new XContentBuilderString("timestamp");
static final XContentBuilderString UPTIME = new XContentBuilderString("uptime");
static final XContentBuilderString UPTIME_IN_MILLIS = new XContentBuilderString("uptime_in_millis");
static final XContentBuilderString LOAD_AVERAGE = new XContentBuilderString("load_average");
static final XContentBuilderString LOAD_AVERAGE_1m = new XContentBuilderString("1m");
static final XContentBuilderString LOAD_AVERAGE_5m = new XContentBuilderString("5m");
static final XContentBuilderString LOAD_AVERAGE_15m = new XContentBuilderString("15m");
static final XContentBuilderString CPU = new XContentBuilderString("cpu");
static final XContentBuilderString SYS = new XContentBuilderString("sys");
static final XContentBuilderString USER = new XContentBuilderString("user");
static final XContentBuilderString USAGE = new XContentBuilderString("usage");
static final XContentBuilderString IDLE = new XContentBuilderString("idle");
static final XContentBuilderString STOLEN = new XContentBuilderString("stolen");
static final XContentBuilderString MEM = new XContentBuilderString("mem");
static final XContentBuilderString SWAP = new XContentBuilderString("swap");
static final XContentBuilderString FREE = new XContentBuilderString("free");
static final XContentBuilderString FREE_IN_BYTES = new XContentBuilderString("free_in_bytes");
static final XContentBuilderString USED = new XContentBuilderString("used");
static final XContentBuilderString USED_IN_BYTES = new XContentBuilderString("used_in_bytes");
static final XContentBuilderString FREE_PERCENT = new XContentBuilderString("free_percent");
static final XContentBuilderString USED_PERCENT = new XContentBuilderString("used_percent");
static final XContentBuilderString ACTUAL_FREE = new XContentBuilderString("actual_free");
static final XContentBuilderString ACTUAL_FREE_IN_BYTES = new XContentBuilderString("actual_free_in_bytes");
static final XContentBuilderString ACTUAL_USED = new XContentBuilderString("actual_used");
static final XContentBuilderString ACTUAL_USED_IN_BYTES = new XContentBuilderString("actual_used_in_bytes");
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(Fields.OS);
builder.field(Fields.TIMESTAMP, timestamp);
if (uptime != -1) {
builder.timeValueField(Fields.UPTIME_IN_MILLIS, Fields.UPTIME, uptime);
}
if (loadAverage.length > 0) {
if (params.param("load_average_format", "array").equals("hash")) {
builder.startObject(Fields.LOAD_AVERAGE);
builder.field(Fields.LOAD_AVERAGE_1m, loadAverage[0]);
builder.field(Fields.LOAD_AVERAGE_5m, loadAverage[1]);
builder.field(Fields.LOAD_AVERAGE_15m, loadAverage[2]);
builder.endObject();
} else {
builder.startArray(Fields.LOAD_AVERAGE);
for (double value : loadAverage) {
builder.value(value);
}
builder.endArray();
}
}
if (cpu != null) {
builder.startObject(Fields.CPU);
builder.field(Fields.SYS, cpu.sys());
builder.field(Fields.USER, cpu.user());
builder.field(Fields.IDLE, cpu.idle());
builder.field(Fields.USAGE, cpu.user() + cpu.sys());
builder.field(Fields.STOLEN, cpu.stolen());
builder.endObject();
}
if (mem != null) {
builder.startObject(Fields.MEM);
builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, mem.free);
builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, mem.used);
builder.field(Fields.FREE_PERCENT, mem.freePercent());
builder.field(Fields.USED_PERCENT, mem.usedPercent());
builder.byteSizeField(Fields.ACTUAL_FREE_IN_BYTES, Fields.ACTUAL_FREE, mem.actualFree);
builder.byteSizeField(Fields.ACTUAL_USED_IN_BYTES, Fields.ACTUAL_USED, mem.actualUsed);
builder.endObject();
}
if (swap != null) {
builder.startObject(Fields.SWAP);
builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, swap.used);
builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, swap.free);
builder.endObject();
}
builder.endObject();
return builder;
}
public static OsStats readOsStats(StreamInput in) throws IOException {
OsStats stats = new OsStats();
stats.readFrom(in);
return stats;
}
@Override
public void readFrom(StreamInput in) throws IOException {
timestamp = in.readVLong();
loadAverage = new double[in.readVInt()];
for (int i = 0; i < loadAverage.length; i++) {
loadAverage[i] = in.readDouble();
}
uptime = in.readLong();
if (in.readBoolean()) {
cpu = Cpu.readCpu(in);
}
if (in.readBoolean()) {
mem = Mem.readMem(in);
}
if (in.readBoolean()) {
swap = Swap.readSwap(in);
}
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(timestamp);
out.writeVInt(loadAverage.length);
for (double val : loadAverage) {
out.writeDouble(val);
}
out.writeLong(uptime);
if (cpu == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
cpu.writeTo(out);
}
if (mem == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
mem.writeTo(out);
}
if (swap == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
swap.writeTo(out);
}
}
public static class Swap implements Streamable, Serializable {
long free = -1;
long used = -1;
public ByteSizeValue free() {
return new ByteSizeValue(free);
}
public ByteSizeValue getFree() {
return free();
}
public ByteSizeValue used() {
return new ByteSizeValue(used);
}
public ByteSizeValue getUsed() {
return used();
}
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 {
free = in.readLong();
used = in.readLong();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeLong(free);
out.writeLong(used);
}
}
public static class Mem implements Streamable, Serializable {
long free = -1;
short freePercent = -1;
long used = -1;
short usedPercent = -1;
long actualFree = -1;
long actualUsed = -1;
public static Mem readMem(StreamInput in) throws IOException {
Mem mem = new Mem();
mem.readFrom(in);
return mem;
}
@Override
public void readFrom(StreamInput in) throws IOException {
free = in.readLong();
freePercent = in.readShort();
used = in.readLong();
usedPercent = in.readShort();
actualFree = in.readLong();
actualUsed = in.readLong();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeLong(free);
out.writeShort(freePercent);
out.writeLong(used);
out.writeShort(usedPercent);
out.writeLong(actualFree);
out.writeLong(actualUsed);
}
public ByteSizeValue used() {
return new ByteSizeValue(used);
}
public ByteSizeValue getUsed() {
return used();
}
public short usedPercent() {
return usedPercent;
}
public short getUsedPercent() {
return usedPercent();
}
public ByteSizeValue free() {
return new ByteSizeValue(free);
}
public ByteSizeValue getFree() {
return free();
}
public short freePercent() {
return freePercent;
}
public short getFreePercent() {
return freePercent();
}
public ByteSizeValue actualFree() {
return new ByteSizeValue(actualFree);
}
public ByteSizeValue getActualFree() {
return actualFree();
}
public ByteSizeValue actualUsed() {
return new ByteSizeValue(actualUsed);
}
public ByteSizeValue getActualUsed() {
return actualUsed();
}
}
public static class Cpu implements Streamable, Serializable {
short sys = -1;
short user = -1;
short idle = -1;
short stolen = -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 {
sys = in.readShort();
user = in.readShort();
idle = in.readShort();
stolen = in.readShort();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeShort(sys);
out.writeShort(user);
out.writeShort(idle);
out.writeShort(stolen);
}
public short sys() {
return sys;
}
public short getSys() {
return sys();
}
public short user() {
return user;
}
public short getUser() {
return user();
}
public short idle() {
return idle;
}
public short getIdle() {
return idle();
}
public short stolen() {
return stolen;
}
public short getStolen() {
return stolen();
}
}
}