Checks for matching gce dmi developers.google.com/compute/docs/instances#dmi
If gce dmi matches
Otherwise
Match the lead line for an interface from iproute2 3: eth0 at eth0.11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP The '@eth0:' portion doesn't exist on primary interfaces and thus is optional in the regex
From sigar: include/sigar.h sigar_net_route_t
Adam Jacob (<adam@opscode.com>)
Copyright © 2008 Opscode, Inc.
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.
# File lib/ohai/plugins/uptime.rb, line 19 def _seconds_to_human(seconds) days = seconds.to_i / 86400 seconds -= 86400 * days hours = seconds.to_i / 3600 seconds -= 3600 * hours minutes = seconds.to_i / 60 seconds -= 60 * minutes if days > 1 return sprintf("%d days %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds) elsif days == 1 return sprintf("%d day %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds) elsif hours > 0 return sprintf("%d hours %02d minutes %02d seconds", hours, minutes, seconds) elsif minutes > 0 return sprintf("%d minutes %02d seconds", minutes, seconds) else return sprintf("%02d seconds", seconds) end end
# File lib/ohai/plugins/solaris2/network.rb, line 68 def arpname_to_ifname(iface, arpname) iface.keys.each do |ifn| return ifn if ifn.split(':')[0].eql?(arpname) end nil end
Make top-level cloud hashes
# File lib/ohai/plugins/cloud.rb, line 29 def create_objects cloud Mash.new cloud[:public_ips] = Array.new cloud[:private_ips] = Array.new end
# File lib/ohai/plugins/darwin/network.rb, line 64 def encaps_lookup(ifname) return "Loopback" if ifname.eql?("lo") return "1394" if ifname.eql?("fw") return "IPIP" if ifname.eql?("gif") return "6to4" if ifname.eql?("stf") return "dot1q" if ifname.eql?("vlan") "Unknown" end
# File lib/ohai/plugins/darwin/network.rb, line 80 def excluded_setting?(setting) setting.match('_sw_cksum') end
# File lib/ohai/plugins/ssh_host_key.rb, line 24 def extract_keytype?(content) case content[0] when "ssh-dss" [ "dsa", nil ] when "ssh-rsa" [ "rsa", nil ] when /^ecdsa/ [ "ecdsa", content[0] ] else [ nil, nil ] end end
# File lib/ohai/plugins/linux/filesystem.rb, line 23 def find_device(name) %w{/dev /dev/mapper}.each do |dir| path = File.join(dir, name) return path if File.exist?(path) end name end
# File lib/ohai/plugins/network.rb, line 65 def find_ip(family = "inet") ips=sorted_ips(family) # return if there isn't any #{family} address ! return [ nil, nil ] if ips.empty? # shortcuts to access default #{family} interface and gateway int_attr = FAMILIES[family] +"_interface" gw_attr = FAMILIES[family] + "_gateway" # If we have a default interface that has addresses, # populate the short-cut attributes ipaddress, ip6address and macaddress if network[int_attr] # working with the address(es) of the default network interface gw_if_ips = ips.select do |v| v[:iface] == network[int_attr] end if gw_if_ips.empty? Ohai::Log.warn("[#{family}] no ip address on #{network[int_attr]}") elsif network[gw_attr] and network["interfaces"][network[int_attr]] and network["interfaces"][network[int_attr]]["addresses"] if [ "0.0.0.0", "::" ].include? network[gw_attr] # link level default route Ohai::Log.debug("link level default #{family} route, picking ip from #{network[gw_attr]}") r = gw_if_ips.first else # checking network masks r = gw_if_ips.select do |v| network_contains_address(network[gw_attr], v[:ipaddress], v[:iface]) end.first if r.nil? r = gw_if_ips.first Ohai::Log.warn("[#{family}] no ipaddress/mask on #{network[int_attr]} matching the gateway #{network[gw_attr]}, picking one anyway") else Ohai::Log.debug("[#{family}] Using default interface #{network[int_attr]} and default gateway #{network[gw_attr]} to set the default ip to #{r[:ipaddress]}") end end else # return the first ip address on network[int_attr] r = gw_if_ips.first end else r = ips.first Ohai::Log.debug("[#{family}] no default interface, picking the first ipaddress") end return [ nil, nil ] if r.nil? or r.empty? [ r[:ipaddress].to_s, r[:iface] ] end
# File lib/ohai/plugins/network.rb, line 118 def find_mac_from_iface(iface) r = network["interfaces"][iface]["addresses"].select{|k,v| v["family"]=="lladdr"} r.nil? or r.first.nil? ? nil : r.first.first end
# File lib/ohai/plugins/passwd.rb, line 5 def fix_encoding(str) str.force_encoding(Encoding.default_external) if str.respond_to?(:force_encoding) str end
# File lib/ohai/plugins/sigar/network_route.rb, line 24 def flags(flags) f = "" if (flags & Sigar::RTF_UP) != 0 f += "U" end if (flags & Sigar::RTF_GATEWAY) != 0 f += "G" end if (flags & Sigar::RTF_HOST) != 0 f += "H" end f end
Fill cloud hash with azure values
# File lib/ohai/plugins/cloud.rb, line 241 def get_azure_values cloud[:vm_name] = azure["vm_name"] cloud[:public_ips] << azure['public_ip'] cloud[:public_fqdn] = azure['public_fqdn'] cloud[:public_ssh_port] = azure['public_ssh_port'] if azure['public_ssh_port'] cloud[:public_winrm_port] = azure['public_winrm_port'] if azure['public_winrm_port'] cloud[:provider] = "azure" end
Fill cloud hash with ec2 values
# File lib/ohai/plugins/cloud.rb, line 91 def get_ec2_values cloud[:public_ips] << ec2['public_ipv4'] cloud[:private_ips] << ec2['local_ipv4'] cloud[:public_ipv4] = ec2['public_ipv4'] cloud[:public_hostname] = ec2['public_hostname'] cloud[:local_ipv4] = ec2['local_ipv4'] cloud[:local_hostname] = ec2['local_hostname'] cloud[:provider] = "ec2" end
# File lib/ohai/plugins/cloud.rb, line 182 def get_eucalyptus_values cloud[:public_ips] << eucalyptus['public_ipv4'] cloud[:private_ips] << eucalyptus['local_ipv4'] cloud[:public_ipv4] = eucalyptus['public_ipv4'] cloud[:public_hostname] = eucalyptus['public_hostname'] cloud[:local_ipv4] = eucalyptus['local_ipv4'] cloud[:local_hostname] = eucalyptus['local_hostname'] cloud[:provider] = "eucalyptus" end
Fill cloud hash with gce values
# File lib/ohai/plugins/cloud.rb, line 48 def get_gce_values cloud[:public_ipv4] = [] cloud[:local_ipv4] = [] public_ips = gce['instance']["networkInterfaces"].collect do |interface| if interface.has_key?('accessConfigs') interface['accessConfigs'].collect{|ac| ac['externalIp']} end end.flatten.compact private_ips = gce['instance']["networkInterfaces"].collect do |interface| interface['ip'] end.compact cloud[:public_ips] += public_ips cloud[:private_ips] += private_ips cloud[:public_ipv4] += public_ips cloud[:public_hostname] = nil cloud[:local_ipv4] += private_ips cloud[:local_hostname] = gce['instance']['hostname'] cloud[:provider] = "gce" end
Names rackspace ipv6 address for interface
Use :public_ip or :private_ip
Interface name of public or private ip
# File lib/ohai/plugins/rackspace.rb, line 73 def get_global_ipv6_address(name, eth) network[:interfaces][eth][:addresses].each do |key, info| # check if we got an ipv6 address and if its in global scope if info['family'] == 'inet6' && info['scope'] == 'Global' rackspace[name] = key break # break when we found an address end end end
Names linode ip address
name - symbol of ohai name (e.g. :public_ip) eth - Interface name (e.g. :eth0)
Alters linode mash with new interface based on name parameter
# File lib/ohai/plugins/linode.rb, line 42 def get_ip_address(name, eth) network[:interfaces][eth][:addresses].each do |key, info| linode[name] = key if info['family'] == 'inet' end end
Fill cloud hash with linode values
# File lib/ohai/plugins/cloud.rb, line 153 def get_linode_values cloud[:public_ips] << linode['public_ip'] cloud[:private_ips] << linode['private_ip'] cloud[:public_ipv4] = linode['public_ipv4'] cloud[:public_hostname] = linode['public_hostname'] cloud[:local_ipv4] = linode['local_ipv4'] cloud[:local_hostname] = linode['local_hostname'] cloud[:provider] = "linode" end
# File lib/ohai/plugins/eucalyptus.rb, line 30 def get_mac_address(addresses) detected_addresses = addresses.detect { |address, keypair| keypair == {"family"=>"lladdr"} } if detected_addresses return detected_addresses.first else return "" end end
Fill cloud hash with openstack values
# File lib/ohai/plugins/cloud.rb, line 211 def get_openstack_values cloud[:public_ips] << openstack['public_ipv4'] cloud[:private_ips] << openstack['local_ipv4'] cloud[:public_ipv4] = openstack['public_ipv4'] cloud[:public_hostname] = openstack['public_hostname'] cloud[:local_ipv4] = openstack['local_ipv4'] cloud[:local_hostname] = openstack['local_hostname'] cloud[:provider] = openstack['provider'] end
Fill cloud hash with rackspace values
# File lib/ohai/plugins/cloud.rb, line 121 def get_rackspace_values cloud[:public_ips] << rackspace['public_ipv4'] if rackspace['public_ipv4'] cloud[:private_ips] << rackspace['local_ipv4'] if rackspace['local_ipv4'] cloud[:public_ipv4] = rackspace['public_ipv4'] cloud[:public_ipv6] = rackspace['public_ipv6'] cloud[:public_hostname] = rackspace['public_hostname'] cloud[:local_ipv4] = rackspace['local_ipv4'] cloud[:local_ipv6] = rackspace['local_ipv6'] cloud[:local_hostname] = rackspace['local_hostname'] cloud[:provider] = "rackspace" end
Adam Jacob (<adam@opscode.com>)
Copyright © 2008 Opscode, Inc.
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.
# File lib/ohai/plugins/linux/platform.rb, line 19 def get_redhatish_platform(contents) contents[/^Red Hat/] ? "redhat" : contents[/(\w+)/, 1].downcase end
# File lib/ohai/plugins/linux/platform.rb, line 23 def get_redhatish_version(contents) contents[/Rawhide/] ? contents[/((\d+) \(Rawhide\))/, 1].downcase : contents[/release ([\d\.]+)/, 1] end
Get the rackspace region
# File lib/ohai/plugins/rackspace.rb, line 85 def get_region() status, stdout, stderr = run_command(:no_status_check => true, :command => "xenstore-ls vm-data/provider_data") if status == 0 stdout.split("\n").each do |line| rackspace[:region] = line.split[2].delete('\"') if line =~ /^region/ end end rescue Ohai::Exceptions::Exec Ohai::Log.debug("Unable to find xenstore-ls, cannot capture region information for Rackspace cloud") end
# File lib/ohai/plugins/ec2.rb, line 30 def has_ec2_mac? network[:interfaces].values.each do |iface| unless iface[:arp].nil? if iface[:arp].value?("fe:ff:ff:ff:ff:ff") Ohai::Log.debug("has_ec2_mac? == true") return true end end end Ohai::Log.debug("has_ec2_mac? == false") false end
# File lib/ohai/plugins/eucalyptus.rb, line 39 def has_euca_mac? network[:interfaces].values.each do |iface| has_mac = (get_mac_address(iface[:addresses]) =~ /^[dD]0:0[dD]:/) Ohai::Log.debug("has_euca_mac? == #{!!has_mac}") return true if has_mac end Ohai::Log.debug("has_euca_mac? == false") false end
# File lib/ohai/plugins/gce.rb, line 28 def has_google_dmi? ::File.read(GOOGLE_SYSFS_DMI).include?('Google') end
Checks for matching linode kernel name
Returns true or false
# File lib/ohai/plugins/linode.rb, line 25 def has_linode_kernel? kernel[:release].split('-').last =~ /linode/ end
Checks for rackspace provider attribute
If rackspace provider attribute found
Otherwise
# File lib/ohai/plugins/rackspace.rb, line 36 def has_rackspace_metadata? status, stdout, stderr = run_command(:no_status_check => true, :command => "xenstore-read vm-data/provider_data/provider") if status == 0 stdout.strip.downcase == 'rackspace' end rescue Ohai::Exceptions::Exec false end
Helpers
# File lib/ohai/plugins/aix/network.rb, line 49 def hex_to_dec_netmask(netmask) # example '0xffff0000' -> '255.255.0.0' dec = netmask[2..3].to_i(16).to_s(10) [4,6,8].each { |n| dec = dec + "." + netmask[n..n+1].to_i(16).to_s(10) } dec end
# File lib/ohai/plugins/darwin/network.rb, line 84 def locate_interface(ifaces, ifname, mac) return ifname unless ifaces[ifname].nil? # oh well, time to go hunting! return ifname.chop if ifname.match /\*$/ ifaces.keys.each do |ifc| ifaces[ifc][:addresses].keys.each do |addr| return ifc if addr.eql? mac end end nil end
# File lib/ohai/plugins/ec2.rb, line 43 def looks_like_ec2? # Try non-blocking connect so we don't "block" if # the Xen environment is *not* EC2 hint?('ec2') || has_ec2_mac? && can_metadata_connect?(EC2_METADATA_ADDR,80) end
# File lib/ohai/plugins/eucalyptus.rb, line 50 def looks_like_euca? # Try non-blocking connect so we don't "block" if # the Xen environment is *not* EC2 hint?('eucalyptus') || has_euca_mac? && can_metadata_connect?(EC2_METADATA_ADDR,80) end
Identifies the linode cloud by preferring the hint, then
Returns true or false
# File lib/ohai/plugins/linode.rb, line 32 def looks_like_linode? hint?('linode') || has_linode_kernel? end
# File lib/ohai/plugins/windows/kernel.rb, line 23 def machine_lookup(sys_type) return "i386" if sys_type.eql?("X86-based PC") return "x86_64" if sys_type.eql?("x64-based PC") sys_type end
# File lib/ohai/plugins/network.rb, line 123 def network_contains_address(address_to_match, ipaddress, iface) # address_to_match: String # ipaddress: IPAddress # iface: String if peer = network["interfaces"][iface]["addresses"][ipaddress.to_s][:peer] IPAddress(peer) == IPAddress(address_to_match) else ipaddress.include? IPAddress(address_to_match) end end
# File lib/ohai/plugins/windows/kernel.rb, line 29 def os_lookup(sys_type) return "Unknown" if sys_type.to_s.eql?("0") return "Other" if sys_type.to_s.eql?("1") return "MSDOS" if sys_type.to_s.eql?("14") return "WIN3x" if sys_type.to_s.eql?("15") return "WIN95" if sys_type.to_s.eql?("16") return "WIN98" if sys_type.to_s.eql?("17") return "WINNT" if sys_type.to_s.eql?("18") return "WINCE" if sys_type.to_s.eql?("19") return nil end
# File lib/ohai/plugins/darwin/network.rb, line 34 def parse_media(media_string) media = Hash.new line_array = media_string.split(' ') 0.upto(line_array.length - 1) do |i| unless line_array[i].eql?("none") if line_array[i + 1] =~ /^\<([a-zA-Z\-\,]+)\>$/ media[line_array[i]] = Hash.new unless media.has_key?(line_array[i]) if media[line_array[i]].has_key?("options") $1.split(",").each do |opt| media[line_array[i]]["options"] << opt unless media[line_array[i]]["options"].include?(opt) end else media[line_array[i]]["options"] = $1.split(",") end else if line_array[i].eql?("autoselect") media["autoselect"] = Hash.new unless media.has_key?("autoselect") media["autoselect"]["options"] = [] end end else media["none"] = { "options" => [] } end end media end
# File lib/ohai/plugins/ruby.rb, line 24 def run_ruby(command) cmd = "ruby -e \"require 'rbconfig'; #{command}\"" status, stdout, stderr = run_command(:no_status_check => true, :command => cmd) stdout.strip end
# File lib/ohai/plugins/darwin/network.rb, line 73 def scope_lookup(scope) return "Node" if scope.eql?("::1") return "Link" if scope.match(/^fe80\:/) return "Site" if scope.match(/^fec0\:/) "Global" end
# File lib/ohai/plugins/network.rb, line 36 def sorted_ips(family = "inet") raise "bad family #{family}" unless [ "inet", "inet6" ].include? family # going to use that later to sort by scope scope_prio = [ "global", "site", "link", "host", "node", nil ] ipaddresses = [] # ipaddresses going to hold #{family} ipaddresses and their scope Mash[network['interfaces']].each do |iface, iface_v| iface_v['addresses'].each do |addr, addr_v| next if addr_v.nil? or not addr_v.has_key? "family" or addr_v['family'] != family ipaddresses << { :ipaddress => addr_v["prefixlen"] ? IPAddress("#{addr}/#{addr_v["prefixlen"]}") : IPAddress("#{addr}/#{addr_v["netmask"]}"), :scope => addr_v["scope"].nil? ? nil : addr_v["scope"].downcase, :iface => iface } end end # sort ip addresses by scope, by prefixlen and then by ip address # 128 - prefixlen: longest prefixes first ipaddresses.sort_by do |v| [ ( scope_prio.index(v[:scope]) or 999999 ), 128 - v[:ipaddress].prefix.to_i, ( family == "inet" ? v[:ipaddress].to_u32 : v[:ipaddress].to_u128 ) ] end end