diff options
author | Maciej Kisielewski <maciej.kisielewski@canonical.com> | 2017-02-13 13:52:31 +0100 |
---|---|---|
committer | Sylvain Pineau <sylvain.pineau@canonical.com> | 2017-04-03 09:39:22 +0200 |
commit | 303bd47780db18e7dd830523ca5faa32b6605b12 (patch) | |
tree | 0dbb7090da352f035f3cbb204c01d6ddcefa2d42 /bin | |
parent | 8acbfca96557c740e491af5da52f6ea2dd75533f (diff) |
bring back ethtool
And use it as the main tool for determining NIC's speed. If it's not available try mii-tool. This should give us maximum resilience. Fixes: LP: #1662726 Signed-off-by: Maciej Kisielewski <maciej.kisielewski@canonical.com>
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/network | 57 |
1 files changed, 37 insertions, 20 deletions
diff --git a/bin/network b/bin/network index ae9cd38..88fd931 100755 --- a/bin/network +++ b/bin/network @@ -280,29 +280,46 @@ class Interface(socket.socket): @property def max_speed(self): - # Parse mii-tool data for max speed - # search for numbers in the line starting with 'capabilities' - # return largest number as max_speed + speeds = [0] + # parse ethtool output, look for things like: + # 100baseSX, 40000baseNX, 10000baseT try: - info = check_output(['mii-tool', '-v', self.interface], - universal_newlines=True, - stderr=STDOUT).split('\n') - except FileNotFoundError: - logging.warning('mii-tool not found! Unable to get max speed') - ethinfo = None - except CalledProcessError as e: - logging.error('mii-tool returned an error!') - logging.error(e.output) - ethinfo = None - finally: - regex = re.compile(r'(\d+)(base)([A-Z]+)') - speeds = [0] - for line in filter(lambda l: 'capabilities' in l, info): - for s in line.split(' '): - hit = regex.search(s) + ethinfo = check_output(['ethtool', self.interface], + universal_newlines=True, + stderr=STDOUT).split(' ') + expression = '(\\d+)(base)([A-Z]+)|(\d+)(Mb/s)' + + regex = re.compile(expression) + if ethinfo: + for i in ethinfo: + hit = regex.search(i) if hit: speeds.append(int(re.sub("\D", "", hit.group(0)))) - return max(speeds) + except CalledProcessError as e: + logging.error('ethtool returned an error!') + logging.error(e.output) + except FileNotFoundError: + logging.warning('ethtool not found! Trying mii-tool') + # Parse mii-tool data for max speed + # search for numbers in the line starting with 'capabilities' + # return largest number as max_speed + try: + info = check_output(['mii-tool', '-v', self.interface], + universal_newlines=True, + stderr=STDOUT).split('\n') + regex = re.compile(r'(\d+)(base)([A-Z]+)') + speeds = [0] + for line in filter(lambda l: 'capabilities' in l, info): + for s in line.split(' '): + hit = regex.search(s) + if hit: + speeds.append(int(re.sub("\D", "", hit.group(0)))) + except FileNotFoundError: + logging.warning('mii-tool not found! Unable to get max speed') + except CalledProcessError as e: + logging.error('mii-tool returned an error!') + logging.error(e.output) + return max(speeds) @property def macaddress(self): |