aboutsummaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/HostLifeGatherer.java
blob: d2b403e056776af0c4d71ab58b162d89fbf7ae16 (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
// 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;
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;


/**
 * @author olaa
 */
public class HostLifeGatherer {

    private static final Path UPTIME_PATH = Path.of("/proc/uptime");

    protected static MetricsPacket.Builder gatherHostLifeMetrics(FileWrapper fileWrapper) {
        double upTime;
        int statusCode = 0;
        String statusMessage = "OK";

        try {
            upTime = getHostLife(fileWrapper);
        } catch (IOException e) {
            upTime = 0d;
            statusCode = 1;
            statusMessage = e.getMessage();
        }

        return new MetricsPacket.Builder(ServiceId.toServiceId("host_life"))
                .timestamp(Instant.now().getEpochSecond())
                .statusMessage(statusMessage)
                .statusCode(statusCode)
                .putMetric(MetricId.toMetricId("uptime"), upTime)
                .putMetric(MetricId.toMetricId("alive"), 1)
                .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();
    }
}