Skip to content

Instantly share code, notes, and snippets.

@crazed
Created December 2, 2011 21:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazed/1424959 to your computer and use it in GitHub Desktop.
Save crazed/1424959 to your computer and use it in GitHub Desktop.
useful facter plugins
if File::exists?("/sbin/ip")
Facter.add('gateway') do
setcode do
`/sbin/ip route show`.match(/^default.*/)[0].match(/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)[0]
end
end
end
if File.exists?('/usr/bin/ipmitool')
Facter.add('bmc_ip_address') do
setcode do
begin
Timeout::timeout(3) { `ipmitool lan print 1 | grep 'IP Address ' | cut -d: -f2 | sed 's/ //g'` }
rescue
nil
end
end
end
end
if File.exists?('/usr/sbin/lldpctl')
lldp = Hash.new
`lldpctl -f keyvalue`.split(/\n/).each do |line|
key, value = line.split(/=/)
lldp[key] ||= Array.new
lldp[key] << value
lldp[key].uniq!
end
# things we care about from lldp
lldp_keys = ['chassis.name', 'port.descr', 'vlan.vlan-id', 'vlan']
Facter.value('interfaces').split(/,/).each do |interface|
lldp_keys.each do |lldp_key|
fact_string = "lldp.#{interface}.#{lldp_key}"
if lldp.has_key?(fact_string)
Facter.add(fact_string.gsub('.', '_')) do
setcode do
lldp[fact_string].join(',')
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment