Parent

Object

Constants

FAMILIES
IPROUTE_INT_REGEX

Match the lead line for an interface from iproute2 3: eth0.11@eth0: <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

SIGAR_ROUTE_METHODS

From sigar: include/sigar.h sigar_net_route_t

Public Instance Methods

_seconds_to_human(seconds) click to toggle source

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.

# 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
arpname_to_ifname(iface, arpname) click to toggle source
# 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
create_objects() click to toggle source

Make top-level cloud hashes

# File lib/ohai/plugins/cloud.rb, line 27
def create_objects
  cloud Mash.new
  cloud[:public_ips] = Array.new
  cloud[:private_ips] = Array.new
end
encaps_lookup(encap) click to toggle source
# File lib/ohai/plugins/linux/network.rb, line 22
def encaps_lookup(encap)
  return "Loopback" if encap.eql?("Local Loopback") || encap.eql?("loopback")
  return "PPP" if encap.eql?("Point-to-Point Protocol")
  return "SLIP" if encap.eql?("Serial Line IP")
  return "VJSLIP" if encap.eql?("VJ Serial Line IP")
  return "IPIP" if encap.eql?("IPIP Tunnel")
  return "6to4" if encap.eql?("IPv6-in-IPv4")
  return "Ethernet" if encap.eql?("ether")
  encap
end
excluded_setting?(setting) click to toggle source
# File lib/ohai/plugins/darwin/network.rb, line 80
def excluded_setting?(setting)
  setting.match('_sw_cksum')
end
find_ip(family = "inet") click to toggle source
# File lib/ohai/plugins/network.rb, line 65
def find_ip(family = "inet")
  r=sorted_ips(family)

  # return if there isn't any #{family} address !
  return [ nil, nil ] if r.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
  if network[int_attr]

    # network[int_attr] exists, the choosen ip must be exist on this interface
    r = r.select do |v|
      v[:iface] == network[int_attr]
    end
    if r.empty?
      Ohai::Log.warn("[#{family}] no ip 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 = r.first
      else
        r = r.select do |v|
          network_contains_address(network[gw_attr], v[:ipaddress], v[:iface])
        end.first
        if r.nil?
          Ohai::Log.warn("[#{family}] no ipaddress/mask on #{network[int_attr]} matching the gateway #{network[gw_attr]}")
        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 = r.first
    end
  else
    r = r.first
    Ohai::Log.info("[#{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
find_mac_from_iface(iface) click to toggle source
# File lib/ohai/plugins/network.rb, line 116
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
fix_encoding(str) click to toggle source
# 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
flags(flags) click to toggle source
# 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
get_ec2_values() click to toggle source

Fill cloud hash with ec2 values

# File lib/ohai/plugins/cloud.rb, line 47
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
get_eucalyptus_values() click to toggle source
# File lib/ohai/plugins/cloud.rb, line 138
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
get_global_ipv6_address(name, eth) click to toggle source

Names rackspace ipv6 address for interface

Parameters

name<Symbol>

Use :public_ip or :private_ip

eth<Symbol>

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
get_ip_address(name, eth) click to toggle source

Names rackspace ip address

Parameters

name<Symbol>

Use :public_ip or :private_ip

eth<Symbol>

Interface name of public or private ip

# File lib/ohai/plugins/rackspace.rb, line 59
def get_ip_address(name, eth)
  network[:interfaces][eth][:addresses].each do |key, info|
    if info['family'] == 'inet'
      rackspace[name] = key 
      break # break when we found an address
    end
  end
end
get_linode_values() click to toggle source

Fill cloud hash with linode values

# File lib/ohai/plugins/cloud.rb, line 109
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
get_mac_address(addresses) click to toggle source
# 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
get_openstack_values() click to toggle source

Fill cloud hash with openstack values

# File lib/ohai/plugins/cloud.rb, line 167
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
get_rackspace_values() click to toggle source

Fill cloud hash with rackspace values

# File lib/ohai/plugins/cloud.rb, line 77
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
get_redhatish_platform(contents) click to toggle source

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.

# File lib/ohai/plugins/linux/platform.rb, line 19
def get_redhatish_platform(contents)
  contents[/^Red Hat/] ? "redhat" : contents[/(\w+)/, 1].downcase
end
get_redhatish_version(contents) click to toggle source
# 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_region() click to toggle source

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
has_ec2_mac?() click to toggle source
# 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
has_euca_mac?() click to toggle source
# 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
has_linode_kernel?() click to toggle source

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
has_rackspace_kernel?() click to toggle source

Checks for matching rackspace kernel name

Return

true

If kernel name matches

false

Otherwise

# File lib/ohai/plugins/rackspace.rb, line 27
def has_rackspace_kernel?
  kernel[:release].split('-').last.eql?("rscloud")
end
has_rackspace_mac?() click to toggle source

Checks for matching rackspace arp mac

Return

true

If mac address matches

false

Otherwise

# File lib/ohai/plugins/rackspace.rb, line 36
def has_rackspace_mac?
  network[:interfaces].values.each do |iface|
    unless iface[:arp].nil?
      return true if iface[:arp].value?("00:00:0c:07:ac:01") or iface[:arp].value?("00:00:0c:9f:f0:01")
    end
  end
  false
end
is_dsa_or_rsa?(file) click to toggle source
# File lib/ohai/plugins/ssh_host_key.rb, line 24
def is_dsa_or_rsa?(file)
  case IO.read(file).split[0]
  when "ssh-dss"
    "dsa"
  when "ssh-rsa"
    "rsa"
  else
    nil
  end
end
locate_interface(ifaces, ifname, mac) click to toggle source
# 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
looks_like_ec2?() click to toggle source
# 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
looks_like_euca?() click to toggle source
# 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
looks_like_linode?() click to toggle source

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
looks_like_rackspace?() click to toggle source

Identifies the rackspace cloud

Return

true

If the rackspace cloud can be identified

false

Otherwise

# File lib/ohai/plugins/rackspace.rb, line 50
def looks_like_rackspace?
  hint?('rackspace') || has_rackspace_mac? || has_rackspace_kernel?
end
machine_lookup(sys_type) click to toggle source
# 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
network_contains_address(address_to_match, ipaddress, iface) click to toggle source
# File lib/ohai/plugins/network.rb, line 121
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
on_ec2?() click to toggle source

Is current cloud ec2?

Return

true

If ec2 Hash is defined

false

Otherwise

# File lib/ohai/plugins/cloud.rb, line 42
def on_ec2?
  ec2 != nil
end
on_eucalyptus?() click to toggle source

Is current cloud eucalyptus?

Return

true

If eucalyptus Hash is defined

false

Otherwise

# File lib/ohai/plugins/cloud.rb, line 134
def on_eucalyptus?
  eucalyptus != nil
end
on_linode?() click to toggle source

Is current cloud linode?

Return

true

If linode Hash is defined

false

Otherwise

# File lib/ohai/plugins/cloud.rb, line 104
def on_linode?
  linode != nil
end
on_openstack?() click to toggle source

Is current cloud openstack-based?

Return

true

If openstack Hash is defined

false

Otherwise

# File lib/ohai/plugins/cloud.rb, line 162
def on_openstack?
  openstack != nil
end
on_rackspace?() click to toggle source

Is current cloud rackspace?

Return

true

If rackspace Hash is defined

false

Otherwise

# File lib/ohai/plugins/cloud.rb, line 72
def on_rackspace?
  rackspace != nil
end
os_lookup(sys_type) click to toggle source
# 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
parse_media(media_string) click to toggle source
# 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
run_ruby(command) click to toggle source
# 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
scope_lookup(scope) click to toggle source
# 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
sorted_ips(family = "inet") click to toggle source
# 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.