Skip to content

Instantly share code, notes, and snippets.

@h0tw1r3
Created Nov 6, 2018
Embed
What would you like to do?
nic fact for puppet gathers speed, duplex, state, slaves, device module, carrier, etc
# Fact: nic { speed, state, duplex, ... }
#
# Copyright: Jeffrey Clark
# License: GPLv3
require 'pathname'
nic = Hash.new
ifaces = Facter.value(:interfaces)
if ifaces and Facter.value(:kernel) == 'Linux'
ifaces.split(',').each do |iface|
iface_path = "/sys/class/net/#{iface}"
if File.exists?("#{iface_path}/speed")
nic_iface = Hash.new
sys_path = Pathname.new("#{iface_path}").realpath
if sys_path.to_s =~ /\/virtual\//
nic_iface['virtual'] = true
end
device_path = sys_path + 'device'
if device_path.exist?
device_path = device_path.realpath
nic_iface['device'] = {
'subsystem' => (device_path+'subsystem').realpath.basename.to_s
}
driver_path = device_path + 'driver'
if driver_path.exist?
nic_iface['device']['module'] = (driver_path+'module').realpath.basename.to_s
end
end
master_path = sys_path + 'master'
if master_path.exist?
nic_iface['master'] = master_path.realpath.basename.to_s
end
slaves = sys_path.children.select { |child| child.basename.fnmatch('lower_*') }
if slaves.count > 0
nic_iface['slaves'] = slaves.map { |child|
child.basename.to_s[6..-1]
}
end
['dormant', 'speed', 'operstate', 'carrier', 'duplex', 'carrier_changes'].each { |x|
begin
result = IO.read("#{iface_path}/#{x}").chomp
begin
Float(result)
nic_iface[x] = result.to_i
rescue
nic_iface[x] = result
end
rescue
end
}
nic[iface] = nic_iface
end
end
end
Facter.add(:nic) do
confine :kernel => "Linux"
setcode do
nic
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment