Path: | lib/ohai/plugins/network.rb |
Last Update: | Thu Jan 17 19:03:16 +0000 2013 |
Author: | Adam Jacob (<adam@opscode.com>) |
Copyright: | Copyright (c) 2008 Opscode, Inc. |
License: | Apache License, Version 2.0 |
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
FAMILIES | = | { "inet" => "default", "inet6" => "default_inet6" |
# File lib/ohai/plugins/network.rb, line 65 65: def find_ip(family = "inet") 66: r=sorted_ips(family) 67: 68: # return if there isn't any #{family} address ! 69: return [ nil, nil ] if r.empty? 70: 71: # shortcuts to access default #{family} interface and gateway 72: int_attr = FAMILIES[family] +"_interface" 73: gw_attr = FAMILIES[family] + "_gateway" 74: 75: # If we have a default interface that has addresses, 76: # populate the short-cut attributes 77: if network[int_attr] 78: 79: # network[int_attr] exists, the choosen ip must be exist on this interface 80: r = r.select do |v| 81: v[:iface] == network[int_attr] 82: end 83: if r.empty? 84: Ohai::Log.warn("[#{family}] no ip on #{network[int_attr]}") 85: elsif network[gw_attr] and 86: network["interfaces"][network[int_attr]] and 87: network["interfaces"][network[int_attr]]["addresses"] 88: if [ "0.0.0.0", "::" ].include? network[gw_attr] 89: # link level default route 90: Ohai::Log.debug("link level default #{family} route, picking ip from #{network[gw_attr]}") 91: r = r.first 92: else 93: r = r.select do |v| 94: network_contains_address(network[gw_attr], v[:ipaddress], v[:iface]) 95: end.first 96: if r.nil? 97: Ohai::Log.warn("[#{family}] no ipaddress/mask on #{network[int_attr]} matching the gateway #{network[gw_attr]}") 98: else 99: Ohai::Log.debug("[#{family}] Using default interface #{network[int_attr]} and default gateway #{network[gw_attr]} to set the default ip to #{r[:ipaddress]}") 100: end 101: end 102: else 103: # return the first ip address on network[int_attr] 104: r = r.first 105: end 106: else 107: r = r.first 108: Ohai::Log.info("[#{family}] no default interface, picking the first ipaddress") 109: end 110: 111: return [ nil, nil ] if r.nil? or r.empty? 112: 113: [ r[:ipaddress].to_s, r[:iface] ] 114: end
# File lib/ohai/plugins/network.rb, line 116 116: def find_mac_from_iface(iface) 117: r = network["interfaces"][iface]["addresses"].select{|k,v| v["family"]=="lladdr"} 118: r.nil? or r.first.nil? ? nil : r.first.first 119: end
# File lib/ohai/plugins/network.rb, line 121 121: def network_contains_address(address_to_match, ipaddress, iface) 122: # address_to_match: String 123: # ipaddress: IPAddress 124: # iface: String 125: if peer = network["interfaces"][iface]["addresses"][ipaddress.to_s][:peer] 126: IPAddress(peer) == IPAddress(address_to_match) 127: else 128: ipaddress.include? IPAddress(address_to_match) 129: end 130: end
# File lib/ohai/plugins/network.rb, line 36 36: def sorted_ips(family = "inet") 37: raise "bad family #{family}" unless [ "inet", "inet6" ].include? family 38: 39: # going to use that later to sort by scope 40: scope_prio = [ "global", "site", "link", "host", "node", nil ] 41: 42: ipaddresses = [] 43: # ipaddresses going to hold #{family} ipaddresses and their scope 44: Mash[network['interfaces']].each do |iface, iface_v| 45: iface_v['addresses'].each do |addr, addr_v| 46: next if addr_v.nil? or not addr_v.has_key? "family" or addr_v['family'] != family 47: ipaddresses << { 48: :ipaddress => addr_v["prefixlen"] ? IPAddress("#{addr}/#{addr_v["prefixlen"]}") : IPAddress("#{addr}/#{addr_v["netmask"]}"), 49: :scope => addr_v["scope"].nil? ? nil : addr_v["scope"].downcase, 50: :iface => iface 51: } 52: end 53: end 54: 55: # sort ip addresses by scope, by prefixlen and then by ip address 56: # 128 - prefixlen: longest prefixes first 57: ipaddresses.sort_by do |v| 58: [ ( scope_prio.index(v[:scope]) or 999999 ), 59: 128 - v[:ipaddress].prefix.to_i, 60: ( family == "inet" ? v[:ipaddress].to_u32 : v[:ipaddress].to_u128 ) 61: ] 62: end 63: end