blob: f5a53ddab50a4b21723854a822a246dccf5c04e7 (
plain) (
blame)
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
|
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.node;
import ai.vespa.metricsproxy.metric.model.MetricsPacket;
import ai.vespa.metricsproxy.metric.model.json.YamasJsonUtil;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import static ai.vespa.metricsproxy.node.NodeMetricGatherer.ROUTING_JSON;
/**
* @author olaa
*/
public class HostLifeGatherer {
private static final Path UPTIME_PATH = Path.of("/proc/uptime");
private static final Logger logger = Logger.getLogger(HostLifeGatherer.class.getSimpleName());
protected static List<MetricsPacket.Builder> gatherHostLifeMetrics(FileWrapper fileWrapper) {
JSONObject jsonObject = new JSONObject();
double upTime;
int statusCode = 0;
String statusMessage = "OK";
try {
upTime = getHostLife(fileWrapper);
} catch (IOException e) {
upTime = 0d;
statusCode = 1;
statusMessage = e.getMessage();
}
try {
jsonObject.put("application", "host_life");
jsonObject.put("timestamp", Instant.now().getEpochSecond());
jsonObject.put("status_message", statusMessage);
jsonObject.put("status_code", statusCode);
JSONObject metrics = new JSONObject();
metrics.put("uptime", upTime);
metrics.put("alive", 1);
jsonObject.put("metrics", metrics);
jsonObject.put("routing", ROUTING_JSON);
return YamasJsonUtil.toMetricsPackets(jsonObject.toString());
} catch (JSONException e) {
logger.log(Level.WARNING, "Error writing JSON", e);
return Collections.emptyList();
}
}
private static double getHostLife(FileWrapper fileWrapper) throws IOException {
return fileWrapper.readAllLines(UPTIME_PATH)
.stream()
.mapToDouble(line -> Double.valueOf(line.split("\\s")[0]))
.findFirst()
.orElseThrow();
}
}
|