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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
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.parser.OutputParser;
import com.yahoo.vespa.hosted.node.verification.commons.parser.ParseInstructions;
import com.yahoo.vespa.hosted.node.verification.commons.parser.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.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Retrieves disk space and type, and stores the result in a HardwareInfo instance
*
* @author olaaun
* @author sgrostad
*/
public class DiskRetriever implements HardwareRetriever {
private static final String DISK_CHECK_TYPE = "lsblk -d -o name,rota";
private static final String DISK_CHECK_SIZE = "pvdisplay --units G | grep 'PV Size'";
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 = "Size";
private static final String DISK_SIZE_REGEX_SPLIT = "\\s+";
private static final int DISK_SIZE_SEARCH_ELEMENT_INDEX = 1;
private static final int DISK_SIZE_RETURN_ELEMENT_INDEX = 2;
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;
}
@Override
public void updateInfo() {
updateDiskType();
updateDiskSize();
}
protected void updateDiskType() {
try {
List<String> commandOutput = commandExecutor.executeCommand(DISK_CHECK_TYPE);
ParseResult parseResult = parseDiskType(commandOutput);
setDiskType(parseResult);
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to retrieve disk type", e);
}
}
protected void updateDiskSize() {
try {
List<String> commandOutput = commandExecutor.executeCommand(DISK_CHECK_SIZE);
List<ParseResult> parseResult = parseDiskSize(commandOutput);
setDiskSize(parseResult);
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to retrieve disk size", e);
}
}
protected ParseResult parseDiskType(List<String> commandOutput) throws IOException {
List<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);
ParseResult parseResult = OutputParser.parseSingleOutput(parseInstructions, commandOutput);
if (!parseResult.getSearchWord().equals(DISK_NAME)) {
throw new IOException("Parsing for disk type failed");
}
return parseResult;
}
protected void setDiskType(ParseResult parseResult) {
hardwareInfo.setDiskType(DiskType.UNKNOWN);
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 List<ParseResult> parseDiskSize(List<String> commandOutput) {
List<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.parseOutput(parseInstructions, commandOutput);
}
protected void setDiskSize(List<ParseResult> parseResults) {
double diskSize = 0;
try {
for (ParseResult parseResult : parseResults) {
String sizeValue = parseResult.getValue().replaceAll("[^\\d.]", "");
diskSize += Double.parseDouble(sizeValue);
}
} catch (NumberFormatException | NullPointerException e) {
logger.log(Level.WARNING, "Parse results contained an invalid PV size - ", parseResults);
} finally {
hardwareInfo.setMinDiskAvailableGb(diskSize);
}
}
}
|