Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ public void testOsPrettyName() throws IOException {
final List<String> lines = Files.readAllLines(PathUtils.get("/etc/os-release"));
for (final String line : lines) {
if (line != null && line.startsWith("PRETTY_NAME=")) {
final Matcher matcher = Pattern.compile("PRETTY_NAME=(\"?|'?)?([^\"']+)\\1").matcher(line);
assert matcher.matches() : line;
final Matcher matcher = Pattern.compile("PRETTY_NAME=(\"?|'?)?([^\"']+)\\1").matcher(line.trim());
final boolean matches = matcher.matches();
assert matches : line;
assert matcher.groupCount() == 2 : line;
final String prettyName = matcher.group(2);
assertThat(osInfo.getPrettyName(), equalTo(prettyName));
return;
Expand Down
19 changes: 9 additions & 10 deletions server/src/main/java/org/elasticsearch/monitor/os/OsProbe.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class OsProbe {
Expand Down Expand Up @@ -547,16 +549,13 @@ private String getPrettyName() throws IOException {
final Optional<String> maybePrettyNameLine =
prettyNameLines.size() == 1 ? Optional.of(prettyNameLines.get(0)) : Optional.empty();
if (maybePrettyNameLine.isPresent()) {
final String prettyNameLine = maybePrettyNameLine.get();
final String[] prettyNameFields = prettyNameLine.split("=");
assert prettyNameFields.length == 2 : prettyNameLine;
if (prettyNameFields[1].length() >= 3 &&
(prettyNameFields[1].startsWith("\"") && prettyNameFields[1].endsWith("\"")) ||
(prettyNameFields[1].startsWith("'") && prettyNameFields[1].endsWith("'"))) {
return prettyNameFields[1].substring(1, prettyNameFields[1].length() - 1);
} else {
return prettyNameFields[1];
}
// we trim since some OS contain trailing space, for example, Oracle Linux Server 6.9 has a trailing space after the quote
final String trimmedPrettyNameLine = maybePrettyNameLine.get().trim();
final Matcher matcher = Pattern.compile("PRETTY_NAME=(\"?|'?)?([^\"']+)\\1").matcher(trimmedPrettyNameLine);
final boolean matches = matcher.matches();
assert matches : trimmedPrettyNameLine;
assert matcher.groupCount() == 2 : trimmedPrettyNameLine;
return matcher.group(2);
} else {
return Constants.OS_NAME;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
Expand Down Expand Up @@ -55,12 +56,10 @@ public void testOsInfo() throws IOException {
List<String> readOsRelease() throws IOException {
assert Constants.LINUX : Constants.OS_NAME;
if (prettyName != null) {
final String quote = randomFrom("\"", "'", null);
if (quote == null) {
return Arrays.asList("NAME=" + randomAlphaOfLength(16), "PRETTY_NAME=" + prettyName);
} else {
return Arrays.asList("NAME=" + randomAlphaOfLength(16), "PRETTY_NAME=" + quote + prettyName + quote);
}
final String quote = randomFrom("\"", "'", "");
final String space = randomFrom(" ", "");
final String prettyNameLine = String.format(Locale.ROOT, "PRETTY_NAME=%s%s%s%s", quote, prettyName, quote, space);
return Arrays.asList("NAME=" + randomAlphaOfLength(16), prettyNameLine);
} else {
return Collections.singletonList("NAME=" + randomAlphaOfLength(16));
}
Expand Down