Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FACT-1592) A key/value pair for Interface Speed/Duplex #2480

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 26 additions & 0 deletions lib/facter/resolvers/linux/networking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def add_info_from_socket_reader
dhcp(interface_name, mtu_and_indexes)
operstate(interface_name)
physical(interface_name)
linkspeed(interface_name)
duplex(interface_name)

@log.debug("Found interface #{interface_name} with #{@fact_list[:interfaces][interface_name]}")
end
Expand Down Expand Up @@ -62,6 +64,30 @@ def physical(ifname)
end
end

def duplex(interface_name)
return unless @fact_list[:interfaces][interface_name][:physical]

# not all interfaces support this, wifi for example causes an EINVAL (Invalid argument)
begin
plex = Facter::Util::FileHelper.safe_read("/sys/class/net/#{interface_name}/duplex", nil)
@fact_list[:interfaces][interface_name][:duplex] = plex.strip if plex
rescue StandardError => e
@log.debug("Failed to read '/sys/class/net/#{interface_name}/duplex': #{e.message}")
end
end

def linkspeed(interface_name)
return unless @fact_list[:interfaces][interface_name][:physical]

# not all interfaces support this, wifi for example causes an EINVAL (Invalid argument)
begin
speed = Facter::Util::FileHelper.safe_read("/sys/class/net/#{interface_name}/speed", nil)
joshcooper marked this conversation as resolved.
Show resolved Hide resolved
@fact_list[:interfaces][interface_name][:speed] = speed.strip if speed
rescue StandardError => e
@log.debug("Failed to read '/sys/class/net/#{interface_name}/speed': #{e.message}")
end
end

jcpunk marked this conversation as resolved.
Show resolved Hide resolved
def parse_ip_command_line(line, mtu_and_indexes)
mtu = line.match(/mtu (\d+)/)&.captures&.first&.to_i
index_tokens = line.split(':')
Expand Down
8 changes: 7 additions & 1 deletion spec/facter/resolvers/linux/networking_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
.with('/sys/class/net/lo/operstate', nil).and_return('unknown')
allow(Facter::Util::FileHelper).to receive(:safe_read)
.with('/sys/class/net/ens160/operstate', nil).and_return('up')
allow(Facter::Util::FileHelper).to receive(:safe_read)
.with('/sys/class/net/ens160/speed', nil).and_return(1000)
allow(Facter::Util::FileHelper).to receive(:safe_read)
.with('/sys/class/net/ens160/duplex', nil).and_return('full')
end

after do
Expand Down Expand Up @@ -293,14 +297,16 @@
{ address: 'fe80::250:56ff:fe9a:8481', netmask: 'ffff:ffff:ffff:ffff::',
network: 'fe80::', scope6: 'link', flags: ['permanent'] }
],
duplex: 'full',
ip6: 'fe80::250:56ff:fe9a:8481',
mac: '00:50:56:9a:61:46',
mtu: 1500,
netmask6: 'ffff:ffff:ffff:ffff::',
network6: 'fe80::',
operational_state: 'up',
physical: true,
scope6: 'link'
scope6: 'link',
speed: 1000
}
}
end
Expand Down
Loading