Skip to content

Commit

Permalink
(FACT-1592) A key/value pair for Interface Speed/Duplex
Browse files Browse the repository at this point in the history
  • Loading branch information
jcpunk committed May 9, 2023
1 parent 83ac46d commit 199809c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
22 changes: 22 additions & 0 deletions lib/facter/resolvers/linux/networking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def add_info_from_socket_reader
@fact_list[:interfaces].keys.each do |interface_name|
mtu(interface_name, mtu_and_indexes)
dhcp(interface_name, mtu_and_indexes)
linkspeed(interface_name)
duplex(interface_name)

@log.debug("Found interface #{interface_name} with #{@fact_list[:interfaces][interface_name]}")
end
Expand All @@ -56,6 +58,26 @@ def parse_ip_command_line(line, mtu_and_indexes)
mtu_and_indexes[name] = { index: index, mtu: mtu }
end

def duplex(interface_name)
# not all interfaces support this, wifi for example does not
unless interface_name.start_with?('lo')
plex = Facter::Util::FileHelper.safe_read("/sys/class/net/#{interface_name}/duplex", nil)
@fact_list[:interfaces][interface_name][:duplex] = plex.strip if plex
end
rescue StandardError
nil
end

def linkspeed(interface_name)
# not all interfaces support this, wifi for example does not
unless interface_name.start_with?('lo')
speed = Facter::Util::FileHelper.safe_read("/sys/class/net/#{interface_name}/speed", nil)
@fact_list[:interfaces][interface_name][:speed] = speed.strip if speed
end
rescue StandardError
nil
end

def mtu(interface_name, mtu_and_indexes)
mtu = mtu_and_indexes.dig(interface_name, :mtu)
@fact_list[:interfaces][interface_name][:mtu] = mtu unless mtu.nil?
Expand Down
6 changes: 5 additions & 1 deletion spec/facter/resolvers/linux/networking_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,14 @@
{ 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::',
scope6: 'link'
scope6: 'link',
speed: 1000
}
}
end
Expand All @@ -295,6 +297,8 @@
allow(File).to receive(:exist?).with('/proc/net/if_inet6').and_return(true)
allow(Facter::Util::FileHelper).to receive(:safe_read)
.with('/proc/net/if_inet6', nil).and_return(load_fixture('proc_net_if_inet6').read)
allow(Facter::Util::FileHelper).to receive(:safe_read).with('/sys/class/net/ens160/duplex').and_return('full')
allow(Facter::Util::FileHelper).to receive(:safe_read).with('/sys/class/net/ens160/speed').and_return(1000)
end

it 'returns all the interfaces' do
Expand Down

0 comments on commit 199809c

Please sign in to comment.