Skip to content

Instantly share code, notes, and snippets.

@burdandrei
Created January 25, 2016 17:16
Show Gist options
  • Save burdandrei/de3e9fd7be9e3f9a851d to your computer and use it in GitHub Desktop.
Save burdandrei/de3e9fd7be9e3f9a851d to your computer and use it in GitHub Desktop.
Ohai dns(nameserves) Plugin
require 'json'
Ohai.plugin(:Nameservers) do
provides "network/nameservers"
depends "network"
collect_data do
Ohai::Log.debug('Parsing resolv.conf for nameservers.')
nameservers = []
# Find all the nameserver values in /etc/resolv.conf
File.open("/etc/resolv.conf", "r").each_line do |line|
if line =~ /^nameserver\s*(\S*)/
nameservers << $1
end
end
# If we can't get any result (bad host config?) default to a
# public DNS server that is likely to be reachable.
if nameservers.empty?
Ohai::Log.debug('No nameservers found in resolv.conf.')
nameservers << '8.8.8.8'
end
network[:nameservers] = nameservers
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment