From fc9d090fb349a69f1a025b9b808f1ade2ef6247a Mon Sep 17 00:00:00 2001 From: olaaun Date: Mon, 7 Aug 2017 15:29:06 +0800 Subject: Interns/verification fix 3 (#3047) * Fixed failing ping parsing * Replace regex with InetAddresses * NetRetriever now also accepts interfaces other than eth. --- .../node/verification/commons/OutputParser.java | 39 -------------------- .../spec/noderepo/NodeRepoJsonModel.java | 28 +++++++-------- .../verification/spec/retrievers/NetRetriever.java | 41 ++++++++++++---------- 3 files changed, 37 insertions(+), 71 deletions(-) (limited to 'node-maintainer/src/main/java/com') diff --git a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/commons/OutputParser.java b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/commons/OutputParser.java index 09060bdebdf..0df3ff31dae 100644 --- a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/commons/OutputParser.java +++ b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/commons/OutputParser.java @@ -37,45 +37,6 @@ public class OutputParser { return parseResults.get(0); } - public static ArrayList parseOutPutWithSkips(ParseInstructions parseInstructions, ArrayList commandOutput) { - ArrayList results = new ArrayList<>(); - int searchElementIndex = parseInstructions.getSearchElementIndex(); - int valueElementIndex = parseInstructions.getValueElementIndex(); - String skipWord = parseInstructions.getSkipWord(); - ArrayList searchWords = parseInstructions.getSearchWords(); - for (int i = 0; i < commandOutput.size(); i++) { - String line = commandOutput.get(i); - String[] lineSplit = line.trim().split(parseInstructions.getSplitRegex()); - if (lineSplit.length <= Math.max(searchElementIndex, valueElementIndex)) { - continue; - } - if (lineSplit[searchElementIndex].equals(skipWord)) { - i = skipToIndex(i, parseInstructions, commandOutput); - continue; - } - String searchWordCandidate = lineSplit[searchElementIndex]; - boolean searchWordCandidateMatch = matchingSearchWord(searchWords, searchWordCandidate); - if (searchWordCandidateMatch) { - String value = lineSplit[valueElementIndex]; - results.add(new ParseResult(searchWordCandidate, value.trim())); - } - } - return results; - } - - protected static int skipToIndex(int index, ParseInstructions parseInstructions, ArrayList commandOutput) { - String skipUntilKeyword = parseInstructions.getSkipUntilKeyword(); - int returnIndex = commandOutput.size(); - for (int i = index; i < commandOutput.size(); i++) { - String line = commandOutput.get(i); - if (line.equals(skipUntilKeyword)) { - returnIndex = i; - break; - } - } - return returnIndex - 1; - } - private static boolean matchingSearchWord(ArrayList searchWords, String searchWordCandidate) { return searchWords.stream().anyMatch(w -> Pattern.compile(w).matcher(searchWordCandidate).matches()); } diff --git a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/spec/noderepo/NodeRepoJsonModel.java b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/spec/noderepo/NodeRepoJsonModel.java index 3b7f2058ca3..f708057f6a5 100644 --- a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/spec/noderepo/NodeRepoJsonModel.java +++ b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/spec/noderepo/NodeRepoJsonModel.java @@ -2,9 +2,15 @@ package com.yahoo.vespa.hosted.node.verification.spec.noderepo; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.net.InetAddresses; import com.yahoo.vespa.hosted.node.verification.spec.retrievers.HardwareInfo; import com.yahoo.vespa.hosted.node.verification.spec.retrievers.HardwareInfo.DiskType; +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.util.stream.Stream; + /** * Created by olaa on 05/07/2017. * Object with the information node repositories has about the node. @@ -44,23 +50,17 @@ public class NodeRepoJsonModel { } public String getIpv6Address() { - String ipv6Regex = "^((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*::((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*|((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4})){7}$"; - for (String ipAddress : ipAddresses) { - if (ipAddress.matches(ipv6Regex)) { - return ipAddress; - } - } - return null; + return Stream.of(ipAddresses) + .map(InetAddresses::forString) + .filter(ip -> ip instanceof Inet6Address) + .findFirst().map(InetAddress::getHostAddress).orElse(null); } public String getIpv4Address() { - String ipv4Regex = "^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$"; - for (String ipAddress : ipAddresses) { - if (ipAddress.matches(ipv4Regex)) { - return ipAddress; - } - } - return null; + return Stream.of(ipAddresses) + .map(InetAddresses::forString) + .filter(ip -> ip instanceof Inet4Address) + .findFirst().map(InetAddress::getHostAddress).orElse(null); } public double getMinDiskAvailableGb() { diff --git a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/spec/retrievers/NetRetriever.java b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/spec/retrievers/NetRetriever.java index 6bf5d7b300d..c775ee7a846 100644 --- a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/spec/retrievers/NetRetriever.java +++ b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/spec/retrievers/NetRetriever.java @@ -16,25 +16,23 @@ import java.util.logging.Logger; * Created by olaa on 30/06/2017. */ public class NetRetriever implements HardwareRetriever { - private static final String NET_FIND_INTERFACE = "/sbin/ifconfig"; + private static final String NET_FIND_INTERFACE = "/sbin/ifconfig | awk 'BEGIN {RS=\"\\n\\n\"; } { if ( $1 != \"lo\") {print} }'"; private static final String NET_CHECK_INTERFACE_SPEED = "/sbin/ethtool"; private static final String SEARCH_WORD_INTERFACE_IP4 = "inet"; private static final String SEARCH_WORD_INTERFACE_IPV6 = "inet6"; - private static final String SEARCH_WORD_INTERFACE_NAME = "eth."; + private static final String SEARCH_WORD_INTERFACE_NAME = "interface name"; private static final String SEARCH_WORD_INTERFACE_SPEED = "Speed"; private static final String INTERFACE_NAME_REGEX_SPLIT = "\\s+"; - private static final String INTERFACE_NAME_SKIP_WORD = "lo"; - private static final String INTERFACE_NAME_SKIP_UNTIL_WORD = ""; - private static final int INTERFACE_NAME_SEARCH_ELEMENT_INDEX = 0; - private static final int INTERFACE_NAME_RETURN_ELEMENT_INDEX = 0; + private static final int INTERFACE_SEARCH_ELEMENT_INDEX = 0; + private static final int INTERFACE_RETURN_ELEMENT_INDEX = 0; private static final String INTERFACE_SPEED_REGEX_SPLIT = ":"; private static final int INTERFACE_SPEED_SEARCH_ELEMENT_INDEX = 0; private static final int INTERFACE_SPEED_RETURN_ELEMENT_INDEX = 1; - private static final String PING_NET_COMMAND = "ping6 -c 1 www.yahoo.com | grep transmitted"; - private static final String PING_SEARCH_WORD = "loss,"; + private static final String PING_NET_COMMAND = "ping6 -c 1 -q www.yahoo.com | grep -oP '\\d+(?=% packet loss)'"; + private static final String PING_SEARCH_WORD = "\\d+\\.?\\d*"; private static final String PING_SPLIT_REGEX_STRING = "\\s+"; - private static final int PING_SEARCH_ELEMENT_INDEX = 7; - private static final int PING_RETURN_ELEMENT_INDEX = 5; + private static final int PING_SEARCH_ELEMENT_INDEX = 0; + private static final int PING_RETURN_ELEMENT_INDEX = 0; private static final Logger logger = Logger.getLogger(NetRetriever.class.getName()); private final HardwareInfo hardwareInfo; private final CommandExecutor commandExecutor; @@ -65,17 +63,16 @@ public class NetRetriever implements HardwareRetriever { } protected ArrayList parseNetInterface(ArrayList commandOutput) { - ArrayList searchWords = new ArrayList<>(Arrays.asList(SEARCH_WORD_INTERFACE_IP4, SEARCH_WORD_INTERFACE_IPV6, SEARCH_WORD_INTERFACE_NAME)); - ParseInstructions parseInstructions = new ParseInstructions(INTERFACE_NAME_SEARCH_ELEMENT_INDEX, INTERFACE_NAME_RETURN_ELEMENT_INDEX, INTERFACE_NAME_REGEX_SPLIT, searchWords); - parseInstructions.setSkipWord(INTERFACE_NAME_SKIP_WORD); - parseInstructions.setSkipUntilKeyword(INTERFACE_NAME_SKIP_UNTIL_WORD); - ArrayList parseResults = OutputParser.parseOutPutWithSkips(parseInstructions, commandOutput); + ArrayList searchWords = new ArrayList<>(Arrays.asList(SEARCH_WORD_INTERFACE_IP4, SEARCH_WORD_INTERFACE_IPV6)); + ParseInstructions parseInstructions = new ParseInstructions(INTERFACE_SEARCH_ELEMENT_INDEX, INTERFACE_RETURN_ELEMENT_INDEX, INTERFACE_NAME_REGEX_SPLIT, searchWords); + ArrayList parseResults = OutputParser.parseOutput(parseInstructions, commandOutput); + parseResults.add(findInterfaceName(commandOutput)); return parseResults; } protected void findInterfaceSpeed(ArrayList parseResults) { try { - String interfaceName = findInterfaceName(parseResults); + String interfaceName = getInterfaceName(parseResults); String command = NET_CHECK_INTERFACE_SPEED + " " + interfaceName; ArrayList commandOutput = commandExecutor.executeCommand(command); ParseResult parseResult = parseInterfaceSpeed(commandOutput); @@ -85,7 +82,15 @@ public class NetRetriever implements HardwareRetriever { } } - protected String findInterfaceName(ArrayList parseResults) { + protected ParseResult findInterfaceName(ArrayList commandOutput) { + try { + return new ParseResult(SEARCH_WORD_INTERFACE_NAME, commandOutput.get(0).trim().split(" ")[0]); + } catch (NullPointerException e) { + return new ParseResult("invalid", "invalid"); + } + } + + protected String getInterfaceName(ArrayList parseResults) { for (ParseResult parseResult : parseResults) { if (!parseResult.getSearchWord().matches(SEARCH_WORD_INTERFACE_NAME)) continue; return parseResult.getValue(); @@ -121,7 +126,7 @@ public class NetRetriever implements HardwareRetriever { if (!parseResult.getSearchWord().matches(PING_SEARCH_WORD)) { throw new IOException("Failed to parse ping output."); } - return parseResult; + return new ParseResult(PING_SEARCH_WORD, parseResult.getValue()); } protected void updateHardwareInfoWithNet(ArrayList parseResults) { -- cgit v1.2.3