Created
February 3, 2016 18:33
-
-
Save cholcombe973/2c3f6cbb0d9acf0b3a07 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_link_speed(network_interface): | |
""" | |
This will find the link speed for a given network device. Returns None | |
if an error occurs. | |
:param network_interface: | |
:return: LinkSpeed or None if an error occurs. | |
""" | |
speed_path = os.path.join(os.sep, 'sys', 'class', 'net', | |
network_interface, 'speed') | |
# I'm not sure where else we'd check if this doesn't exist | |
if not os.path.exists(speed_path): | |
return LinkSpeed.UNKNOWN | |
try: | |
with open(speed_path, 'r') as sysfs: | |
speed = sysfs.readlines() | |
# Did we actually read anything? | |
if not speed: | |
return LinkSpeed.UNKNOWN | |
# Try to find a sysctl match for this particular speed | |
for name, member in LinkSpeed.__members__.items(): | |
if member.value == int(speed[0].strip()): | |
return member | |
# Default to UNKNOWN if we can't find a match | |
return LinkSpeed.UNKNOWN | |
except IOError as e: | |
log("Unable to open {path} because of error: {error}".format( | |
path=speed_path, | |
error=e.message, | |
)) | |
return LinkSpeed.UNKNOWN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment