Get the name of a network interface
This recipe gets the name of the first real network interface. I use it to add if_ and if_errcoll_ graphs to Munin.
#
# if_name.rb
#
# gets the name of the first real ethernet interface
#
Facter.add(:if_name) do
confine :kernel => :linux
setcode do
if_name = nil
output = %x{/sbin/ifconfig}
output.each_line do |line|
if line =~ /(^\S+)\s+Link encap:Ethernet/
tmp = $1
unless tmp =~ /^lo\d+/
if_name = tmp
break
end
end
end
if_name
end
end
Facter.add(:if_name) do
confine :kernel => %w{FreeBSD NetBSD OpenBSD}
setcode do
if_name = nil
output = %x{/sbin/ifconfig}
output.each_line do |line|
if line =~ /(\S+):\sflags.*<.*UP.*>/
tmp = $1
unless tmp =~ /^lo\d+/
if_name = tmp
break
end
end
end
if_name
end
end