diff options
author | Ola Aunrønning <olaa@verizonmedia.com> | 2019-09-05 10:49:23 +0200 |
---|---|---|
committer | Ola Aunrønning <olaa@verizonmedia.com> | 2019-09-05 10:49:23 +0200 |
commit | d84dddd3d6544c5f0f640a908e673cf90ce3667b (patch) | |
tree | 6295dc547c30c7d7be33029174ddf3390af58e9d /metrics-proxy/src/main | |
parent | e21eed87ff09cd6a5b1a848568786db648f35e08 (diff) |
Read /proc for uptime
Diffstat (limited to 'metrics-proxy/src/main')
-rw-r--r-- | metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/FileWrapper.java | 6 | ||||
-rw-r--r-- | metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/HostLifeGatherer.java | 20 |
2 files changed, 8 insertions, 18 deletions
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/FileWrapper.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/FileWrapper.java index b21f2a004b9..aa01802883e 100644 --- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/FileWrapper.java +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/FileWrapper.java @@ -4,6 +4,7 @@ package ai.vespa.metricsproxy.node; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.time.Instant; import java.util.List; import java.util.stream.Stream; @@ -12,8 +13,9 @@ import java.util.stream.Stream; */ public class FileWrapper { - List<String> readAllLines(Path path) throws IOException { - return Files.readAllLines(path); + long getFileAgeInSeconds(Path path) throws IOException { + Instant lastModifiedTime = Files.getLastModifiedTime(path).toInstant(); + return Instant.now().getEpochSecond() - lastModifiedTime.getEpochSecond(); } Stream<Path> walkTree(Path path) throws IOException { diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/HostLifeGatherer.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/HostLifeGatherer.java index d2b403e0567..8fdfc93022c 100644 --- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/HostLifeGatherer.java +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/HostLifeGatherer.java @@ -1,7 +1,6 @@ // 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.Metric; import ai.vespa.metricsproxy.metric.model.ConsumerId; import ai.vespa.metricsproxy.metric.model.MetricId; import ai.vespa.metricsproxy.metric.model.MetricsPacket; @@ -10,9 +9,7 @@ import ai.vespa.metricsproxy.metric.model.ServiceId; import java.io.IOException; import java.nio.file.Path; import java.time.Instant; -import java.util.List; import java.util.Set; -import java.util.logging.Logger; /** @@ -20,17 +17,17 @@ import java.util.logging.Logger; */ public class HostLifeGatherer { - private static final Path UPTIME_PATH = Path.of("/proc/uptime"); + private static final Path UPTIME_PATH = Path.of("/proc"); protected static MetricsPacket.Builder gatherHostLifeMetrics(FileWrapper fileWrapper) { - double upTime; + long upTime; int statusCode = 0; String statusMessage = "OK"; try { - upTime = getHostLife(fileWrapper); + upTime = fileWrapper.getFileAgeInSeconds(UPTIME_PATH); } catch (IOException e) { - upTime = 0d; + upTime = 0; statusCode = 1; statusMessage = e.getMessage(); } @@ -44,13 +41,4 @@ public class HostLifeGatherer { .addConsumers(Set.of(ConsumerId.toConsumerId("Vespa"))); } - - - private static double getHostLife(FileWrapper fileWrapper) throws IOException { - return fileWrapper.readAllLines(UPTIME_PATH) - .stream() - .mapToDouble(line -> Double.valueOf(line.split("\\s")[0])) - .findFirst() - .orElseThrow(); - } } |