aboutsummaryrefslogtreecommitdiffstats
path: root/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/spec/retrievers/DiskRetriever.java
blob: 8fe9e04db306f881552c7865399109bfda009111 (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
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
package com.yahoo.vespa.hosted.node.verification.spec.retrievers;

import com.yahoo.vespa.hosted.node.verification.commons.CommandExecutor;
import com.yahoo.vespa.hosted.node.verification.commons.OutputParser;
import com.yahoo.vespa.hosted.node.verification.commons.ParseInstructions;
import com.yahoo.vespa.hosted.node.verification.commons.ParseResult;
import com.yahoo.vespa.hosted.node.verification.spec.retrievers.HardwareInfo.DiskType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by olaa on 30/06/2017.
 */
public class DiskRetriever implements HardwareRetriever {
    private static final String DISK_CHECK_TYPE = "lsblk -d -o name,rota";
    private static final String DISK_CHECK_SIZE = "df -BG | grep -v tmpfs | awk '{s+=$2} END {print s-1}'";
    private static final String DISK_NAME = "sda";
    private static final String DISK_TYPE_REGEX_SPLIT = "\\s+";
    private static final int DISK_TYPE_SEARCH_ELEMENT_INDEX = 0;
    private static final int DISK_TYPE_RETURN_ELEMENT_INDEX = 1;
    private static final String DISK_SIZE_SEARCH_WORD = ".*\\d+.*";
    private static final String DISK_SIZE_REGEX_SPLIT = "\\s+";
    private static final int DISK_SIZE_SEARCH_ELEMENT_INDEX = 0;
    private static final int DISK_SIZE_RETURN_ELEMENT_INDEX = 0;
    private static final Logger logger = Logger.getLogger(DiskRetriever.class.getName());
    private final HardwareInfo hardwareInfo;
    private final CommandExecutor commandExecutor;


    public DiskRetriever(HardwareInfo hardwareInfo, CommandExecutor commandExecutor) {
        this.hardwareInfo = hardwareInfo;
        this.commandExecutor = commandExecutor;
    }

    public void updateInfo() {
        try {
            updateDiskType();
            updateDiskSize();
        } catch (IOException e) {
            logger.log(Level.WARNING, "Failed to retrieve disk info", e);
        }
    }

    protected void updateDiskType() throws IOException {
        ArrayList<String> commandOutput = commandExecutor.executeCommand(DISK_CHECK_TYPE);
        ParseResult parseResult = parseDiskType(commandOutput);
        setDiskType(parseResult);
    }

    protected void updateDiskSize() throws IOException {
        ArrayList<String> commandOutput = commandExecutor.executeCommand(DISK_CHECK_SIZE);
        ParseResult parseResult = parseDiskSize(commandOutput);
        setDiskSize(parseResult);
    }

    protected ParseResult parseDiskType(ArrayList<String> commandOutput) {
        ArrayList<String> searchWords = new ArrayList<>(Arrays.asList(DISK_NAME));
        ParseInstructions parseInstructions = new ParseInstructions(DISK_TYPE_SEARCH_ELEMENT_INDEX, DISK_TYPE_RETURN_ELEMENT_INDEX, DISK_TYPE_REGEX_SPLIT, searchWords);
        return OutputParser.parseSingleOutput(parseInstructions, commandOutput);
    }

    protected void setDiskType(ParseResult parseResult) {
        hardwareInfo.setDiskType(DiskType.UNKNOWN);
        if (!parseResult.getSearchWord().equals(DISK_NAME)) {
            return;
        }
        String fastDiskSymbol = "0";
        String nonFastDiskSymbol = "1";
        if (parseResult.getValue().equals(fastDiskSymbol)) {
            hardwareInfo.setDiskType(DiskType.FAST);
        } else if (parseResult.getValue().equals(nonFastDiskSymbol)) {
            hardwareInfo.setDiskType(DiskType.SLOW);
        }
    }

    protected ParseResult parseDiskSize(ArrayList<String> commandOutput) {
        ArrayList<String> searchWords = new ArrayList<>(Arrays.asList(DISK_SIZE_SEARCH_WORD));
        ParseInstructions parseInstructions = new ParseInstructions(DISK_SIZE_SEARCH_ELEMENT_INDEX, DISK_SIZE_RETURN_ELEMENT_INDEX, DISK_SIZE_REGEX_SPLIT, searchWords);
        return OutputParser.parseSingleOutput(parseInstructions, commandOutput);
    }

    protected void setDiskSize(ParseResult parseResult) {
        try {
            String sizeValue = parseResult.getValue().replaceAll("[^\\d.]", "");
            double diskSize = Double.parseDouble(sizeValue);
            hardwareInfo.setMinDiskAvailableGb(diskSize);
        } catch (NumberFormatException | NullPointerException e) {
            return;
        }
    }

}