Top Level Namespace

Defined Under Namespace

Modules: Ohai Classes: String

Constant Summary

EC2_METADATA_ADDR =
"169.254.169.254"
EC2_METADATA_URL =
"http://#{EC2_METADATA_ADDR}/2008-02-01/meta-data"
EC2_USERDATA_URL =
"http://#{EC2_METADATA_ADDR}/2008-02-01/user-data"
EC2_ARRAY_VALUES =
%w(security-groups

Instance Method Summary (collapse)

Instance Method Details

- (Object) _seconds_to_human(seconds)

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.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

- (Object) arpname_to_ifname(iface, arpname)



67
68
69
70
71
72
73
# File 'lib/ohai/plugins/solaris2/network.rb', line 67

def arpname_to_ifname(iface, arpname)
  iface.keys.each do |ifn|
    return ifn if ifn.split(':')[0].eql?(arpname)
  end

  nil
end

- (Boolean) can_metadata_connect?(addr, port, timeout = 2)

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ohai/plugins/ec2.rb', line 34

def (addr, port, timeout=2)
  t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
  saddr = Socket.pack_sockaddr_in(port, addr)
  connected = false

  begin
    t.connect_nonblock(saddr)    
  rescue Errno::EINPROGRESS
    r,w,e = IO::select(nil,[t],nil,timeout)
    if !w.nil?
      connected = true
    else
      begin
        t.connect_nonblock(saddr)
      rescue Errno::EISCONN
        t.close
        connected = true
      rescue SystemCallError
      end
    end
  rescue SystemCallError
  end

  connected
end

- (Object) create_objects

Make top-level cloud hashes



24
25
26
27
28
# File 'lib/ohai/plugins/cloud.rb', line 24

def create_objects
  cloud Mash.new
  cloud[:public_ips] = Array.new
  cloud[:private_ips] = Array.new
end

- (Object) derive_bcast(ipaddr, ipmask, zero_bcast = false)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ohai/plugins/windows/network.rb', line 26

def derive_bcast(ipaddr, ipmask, zero_bcast = false)
  begin
    ipaddr_int = ipaddr.split(".").collect{ |x| x.to_i}.pack("C4").unpack("N").first
    ipmask_int = ipmask.split(".").collect{ |x| x.to_i}.pack("C4").unpack("N").first
    if zero_bcast
      bcast_int = ipaddr_int & ipmask_int
    else
      bcast_int = ipaddr_int | 2 ** 32 - ipmask_int - 1  
    end  
    bcast = [bcast_int].pack("N").unpack("C4").join(".")                                     
    return bcast
  rescue
    return nil
  end
end

- (Object) encaps_lookup(ifname)



33
34
35
36
37
38
# File 'lib/ohai/plugins/sigar/network.rb', line 33

def encaps_lookup(ifname)
  return "Ethernet" if ifname.eql?("e1000g")
  return "Ethernet" if ifname.eql?("eri")
  return "Loopback" if ifname.eql?("lo")
  "Unknown"
end

- (Boolean) excluded_setting?(setting)

Returns:

  • (Boolean)


70
71
72
# File 'lib/ohai/plugins/darwin/network.rb', line 70

def excluded_setting?(setting)
  setting.match('_sw_cksum')
end

- (Object) find_ip_and_mac(addresses)



29
30
31
32
33
34
35
36
37
# File 'lib/ohai/plugins/network.rb', line 29

def find_ip_and_mac(addresses)
  ip = nil; mac = nil
  addresses.keys.each do |addr|
    ip = addr if addresses[addr]["family"].eql?("inet")
    mac = addr if addresses[addr]["family"].eql?("lladdr")
    break if (ip and mac)
  end
  [ip, mac]
end

- (Object) get_ec2_values

Fill cloud hash with ec2 values



44
45
46
47
48
# File 'lib/ohai/plugins/cloud.rb', line 44

def get_ec2_values 
  cloud[:public_ips] << ec2['public_ipv4']
  cloud[:private_ips] << ec2['local_ipv4']
  cloud[:provider] = "ec2"
end

- (Object) get_ip_address(name, eth)

Names rackspace ip address

Parameters

name<Symbol>

Use :public_ip or :private_ip

eth<Symbol>

Interface name of public or private ip



59
60
61
62
63
# File 'lib/ohai/plugins/rackspace.rb', line 59

def get_ip_address(name, eth)
  network[:interfaces][eth][:addresses].each do |key, info|
    rackspace[name] = key if info['family'] == 'inet'
  end
end

- (Object) get_rackspace_values

Fill cloud hash with rackspace values



70
71
72
73
74
# File 'lib/ohai/plugins/cloud.rb', line 70

def get_rackspace_values 
  cloud[:public_ips] << rackspace['public_ip']
  cloud[:private_ips] << rackspace['private_ip']
  cloud[:provider] = "rackspace"
end

- (Object) get_redhatish_platform(contents)

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.



18
19
20
# File 'lib/ohai/plugins/linux/platform.rb', line 18

def get_redhatish_platform(contents)
  contents[/^Red Hat/i] ? "redhat" : contents[/(\w+)/i, 1].downcase
end

- (Object) get_redhatish_version(contents)



22
23
24
# File 'lib/ohai/plugins/linux/platform.rb', line 22

def get_redhatish_version(contents)
  contents[/Rawhide/i] ? contents[/((\d+) \(Rawhide\))/i, 1].downcase : contents[/release ([\d\.]+)/, 1]
end

- (Boolean) has_ec2_mac?

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
# File 'lib/ohai/plugins/ec2.rb', line 60

def has_ec2_mac?
  network[:interfaces].values.each do |iface|
    unless iface[:arp].nil?
      return true if iface[:arp].value?("fe:ff:ff:ff:ff:ff")
    end
  end
  false
end

- (Boolean) has_rackspace_kernel?

Checks for matching rackspace kernel name

Return

true

If kernel name matches

false

Otherwise

Returns:

  • (Boolean)


27
28
29
# File 'lib/ohai/plugins/rackspace.rb', line 27

def has_rackspace_kernel?
  kernel[:release].split('-').last.eql?("rscloud")
end

- (Boolean) has_rackspace_mac?

Checks for matching rackspace arp mac

Return

true

If mac address matches

false

Otherwise

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
# 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")
    end
  end
  false
end

- (Object) locate_interface(ifaces, ifname, mac)



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ohai/plugins/darwin/network.rb', line 74

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

- (Boolean) looks_like_ec2?

Returns:

  • (Boolean)


94
95
96
97
98
# File 'lib/ohai/plugins/ec2.rb', line 94

def looks_like_ec2?
  # Try non-blocking connect so we don't "block" if 
  # the Xen environment is *not* EC2
  has_ec2_mac? && (EC2_METADATA_ADDR,80)
end

- (Boolean) looks_like_rackspace?

Identifies the rackspace cloud

Return

true

If the rackspace cloud can be identified

false

Otherwise

Returns:

  • (Boolean)


50
51
52
# File 'lib/ohai/plugins/rackspace.rb', line 50

def looks_like_rackspace?
  has_rackspace_mac? || has_rackspace_kernel?
end

- (Object) machine_lookup(sys_type)



21
22
23
24
25
# File 'lib/ohai/plugins/windows/kernel.rb', line 21

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

- (Object) metadata(id = ''))



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ohai/plugins/ec2.rb', line 69

def (id='')
  OpenURI.open_uri("#{EC2_METADATA_URL}/#{id}").read.split("\n").each do |o|
    key = "#{id}#{o.gsub(/\=.*$/, '/')}"
    if key[-1..-1] != '/'
      ec2[key.gsub(/\-|\//, '_').to_sym] = 
        if EC2_ARRAY_VALUES.include? key
          OpenURI.open_uri("#{EC2_METADATA_URL}/#{key}").read.split("\n")
        else
          OpenURI.open_uri("#{EC2_METADATA_URL}/#{key}").read
        end
    else
      (key)
    end
  end
end

- (Boolean) on_ec2?

Is current cloud ec2?

Return

true

If ec2 Hash is defined

false

Otherwise

Returns:

  • (Boolean)


39
40
41
# File 'lib/ohai/plugins/cloud.rb', line 39

def on_ec2?
  ec2 != nil
end

- (Boolean) on_rackspace?

Is current cloud rackspace?

Return

true

If rackspace Hash is defined

false

Otherwise

Returns:

  • (Boolean)


65
66
67
# File 'lib/ohai/plugins/cloud.rb', line 65

def on_rackspace?
  rackspace != nil
end

- (Object) os_lookup(sys_type)



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ohai/plugins/windows/kernel.rb', line 27

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

- (Object) parse_media(media_string)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ohai/plugins/darwin/network.rb', line 25

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

- (Object) run_ruby(command)



24
25
26
27
28
# 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

- (Object) scope_lookup(scope)



64
65
66
67
68
# File 'lib/ohai/plugins/darwin/network.rb', line 64

def scope_lookup(scope)
  return "Link" if scope.match(/^fe80\:/)
  return "Site" if scope.match(/^fec0\:/)
  "Global"
end

- (Object) userdata



85
86
87
88
89
90
91
92
# File 'lib/ohai/plugins/ec2.rb', line 85

def userdata()
  ec2[:userdata] = nil
  # assumes the only expected error is the 404 if there's no user-data
  begin
    ec2[:userdata] = OpenURI.open_uri("#{EC2_USERDATA_URL}/").read
  rescue OpenURI::HTTPError
  end
end