Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

OHAI-325: Populate ipaddress with a logical choice

When we have a default gateway we use its interface to populate the ipaddress
attribute. When an interface has multiple addresses be sure to choose one that
is on the same network as the default gateway.
  • Loading branch information...
commit 4750288b66d65d1a78523f5b7a9c2c4eefc295a3 1 parent b8a3b10
Bryan McLellan btm authored
Showing with 17 additions and 3 deletions.
  1. +16 −3 lib/ohai/plugins/network.rb
  2. +1 −0  ohai.gemspec
19 lib/ohai/plugins/network.rb
View
@@ -16,6 +16,8 @@
# limitations under the License.
#
+require 'ipaddress'
+
provides "network", "counters/network"
network Mash.new unless network
@@ -26,22 +28,33 @@
require_plugin "hostname"
require_plugin "#{os}::network"
-def find_ip_and_mac(addresses)
+def find_ip_and_mac(addresses, match = nil)
ip = nil; mac = nil
addresses.keys.each do |addr|
- ip = addr if addresses[addr]["family"].eql?("inet")
+ if match.nil?
+ ip = addr if addresses[addr]["family"].eql?("inet")
+ else
+ ip = addr if addresses[addr]["family"].eql?("inet") && network_contains_address(match, addr, addresses[addr][:netmask])
+ end
mac = addr if addresses[addr]["family"].eql?("lladdr")
break if (ip and mac)
end
+ Ohai::Log.debug("Found IPv4 address #{ip} with MAC #{mac} #{match.nil? ? '' : 'matching address ' + match}")
[ip, mac]
end
+def network_contains_address(address_to_match, network_ip, network_mask)
+ network = IPAddress "#{network_ip}/#{network_mask}"
+ host = IPAddress address_to_match
+ network.include?(host)
+end
+
# If we have a default interface that has addresses, populate the short-cut attributes
if network[:default_interface] and
network["interfaces"][network[:default_interface]] and
network["interfaces"][network[:default_interface]]["addresses"]
Ohai::Log.debug("Using default interface for default ip and mac address")
- im = find_ip_and_mac(network["interfaces"][network[:default_interface]]["addresses"])
+ im = find_ip_and_mac(network["interfaces"][network[:default_interface]]["addresses"], network[:default_gateway])
ipaddress im.shift
macaddress im.shift
else
1  ohai.gemspec
View
@@ -22,6 +22,7 @@ spec = Gem::Specification.new do |s|
s.add_dependency "mixlib-cli"
s.add_dependency "mixlib-config"
s.add_dependency "mixlib-log"
+ s.add_dependency "ipaddress"
s.add_development_dependency "rspec-core"
s.add_development_dependency "rspec-expectations"
s.add_development_dependency "rspec-mocks"
Please sign in to comment.
Something went wrong with that request. Please try again.