Created
January 25, 2016 17:16
-
-
Save burdandrei/de3e9fd7be9e3f9a851d to your computer and use it in GitHub Desktop.
Ohai dns(nameserves) Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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