summaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/jdisc/state/CoredumpGatherer.java
blob: c82f0cb436f3a3ef4f241d3cedfb4b73f4e7c592 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.state;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yahoo.vespa.defaults.Defaults;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.time.Instant;
import java.util.stream.Stream;

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

    private static final ObjectMapper jsonMapper = new ObjectMapper();

    private static final Path COREDUMP_PATH = Path.of(Defaults.getDefaults().underVespaHome("var/crash/processing"));

    public static JsonNode gatherCoredumpMetrics(FileWrapper fileWrapper) {
        int coredumps = getNumberOfCoredumps(fileWrapper);
        ObjectNode packet = jsonMapper.createObjectNode();
        packet.put("status_code", coredumps == 0 ? 0 : 1);
        packet.put("status_msg", coredumps == 0 ? "OK" : String.format("Found %d coredump(s)", coredumps));
        packet.put("timestamp", Instant.now().getEpochSecond());
        packet.put("application", "system-coredumps-processing");
        return packet;
    }

    private static int getNumberOfCoredumps(FileWrapper fileWrapper) {
        try (Stream<Path> stream = fileWrapper.walkTree(COREDUMP_PATH)){
            return (int) stream
                    .filter(fileWrapper::isRegularFile)
                    .count();
        } catch (NoSuchFileException e) {
            return 0;
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
}