Top Level Namespace

Defined Under Namespace

Modules: Ohai Classes: RightScale, String

Constant Summary collapse

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"

Instance Method Summary collapse

Instance Method Details

#_seconds_to_human(seconds) ⇒ Object

Author

Adam Jacob (<[email protected]>)

Copyright

Copyright © 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

#add_server(key, data) ⇒ Object

this is a hack to convert the RS_<server> tokens into something more intuative. Hopefully this will be removed when we stop using EC2 userdata.



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

def add_server(key, data)
  rightscale[:server] = Mash.new unless rightscale.has_key?(:server)
  server_names = {
    "RS_sketchy" => "sketchy",
    "RS_syslog" => "syslog",
    "RS_lumberjack" => "lumberjack",
    "RS_server" => "core"
  }
  rightscale[:server][server_names[key]] = data unless (server_names[key] == nil)
end

#arpname_to_ifname(iface, arpname) ⇒ Object



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

#can_metadata_connect?(addr, port, timeout = 2) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#derive_bcast(ipaddr, ipmask, zero_bcast = false) ⇒ Object



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

#encaps_lookup(ifname) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/ohai/plugins/linux/network.rb', line 23

def encaps_lookup(encap)
  return "Loopback" if encap.eql?("Local 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")
  encap
end

#excluded_setting?(setting) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#find_ip_and_mac(addresses) ⇒ Object



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

#get_dataObject

TODO: future def get_data_json

file = File.open(RightScale::MetaDataFile)
json = ""
file.each { |line| json << line }  
data = JSON.parse(json)
data["rightscale"].each { |key, data| rightscale[key] = data }

end



80
81
82
83
84
85
86
87
88
# File 'lib/ohai/plugins/rightscale.rb', line 80

def get_data
  data_array = File.open(RightScale::MetaDataFile)
  data_array.each do |d|
    key, data = d.split('=')
    key.strip!
    data.strip!
    rightscale[key.sub(/RS_/,'')] = data unless add_server(key,data) 
  end
end

#get_data_from_ec2_user_dateObject

add all ‘RS_’ tokens in userdata, but perform translation for server names



50
51
52
53
54
55
56
# File 'lib/ohai/plugins/rightscale.rb', line 50

def get_data_from_ec2_user_date
  data_array = ec2[:userdata].split('&')
  data_array.each do |d|
    key, data = d.split('=')
    rightscale[key.sub(/RS_/,'')] = data unless add_server(key,data) 
  end
end

#get_ec2_valuesObject



29
30
31
32
33
34
# File 'lib/ohai/plugins/cloud.rb', line 29

def get_ec2_values
  cloud[:instance_uid] = ec2['instance_id']
  cloud[:public_ip] = ec2['public_ipv4']
  cloud[:private_ip] = ec2['local_ipv4']
  cloud[:provider] = "ec2"
end

#get_rackspace_valuesObject



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

def get_rackspace_values
  cloud[:instance_uid] = rackspace['instance_uid']
  cloud[:public_ip] = rackspace['public_ip']
  cloud[:private_ip] = rackspace['private_ip']
  cloud[:provider] = "rackspace"
end

#has_ec2_mac?Boolean

Returns:

  • (Boolean)


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

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

#locate_interface(ifaces, ifname, mac) ⇒ Object



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

#looks_like_ec2?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/ohai/plugins/ec2.rb', line 89

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

#machine_lookup(sys_type) ⇒ Object



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

#metadata(id = '') ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ohai/plugins/ec2.rb', line 68

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] =
        OpenURI.open_uri("#{EC2_METADATA_URL}/#{key}").gets
    else
      (key)
    end
  end
end

#on_ec2?Boolean

EC2

Returns:

  • (Boolean)


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

def on_ec2?
  ec2 != nil
end

#on_rackspace?Boolean

rackspace

Returns:

  • (Boolean)


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

def on_rackspace?
  rackspace != nil
end

#on_rightscale_ec2_platform?Boolean

Returns:

  • (Boolean)


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

def on_rightscale_ec2_platform?
  return false if (ec2 == nil || ec2[:userdata].match(/RS_/) == nil) # only ec2 supported
  true
end

#on_rightscale_platform?Boolean

Generic cloud

Returns:

  • (Boolean)


67
68
69
# File 'lib/ohai/plugins/rightscale.rb', line 67

def on_rightscale_platform?
  File.exists?(RightScale::MetaDataFile)
end

#os_lookup(sys_type) ⇒ Object



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

#parse_media(media_string) ⇒ Object



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

#scope_lookup(scope) ⇒ Object



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

#userdataObject



80
81
82
83
84
85
86
87
# File 'lib/ohai/plugins/ec2.rb', line 80

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}/").gets
  rescue OpenURI::HTTPError
  end
end