ohai 8.26.1 → 13.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (188) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile +3 -4
  3. data/Rakefile +14 -0
  4. data/lib/ohai/application.rb +1 -0
  5. data/lib/ohai/config.rb +2 -88
  6. data/lib/ohai/dsl/plugin.rb +0 -4
  7. data/lib/ohai/exception.rb +0 -1
  8. data/lib/ohai/loader.rb +11 -2
  9. data/lib/ohai/log.rb +3 -1
  10. data/lib/ohai/mixin/command.rb +1 -318
  11. data/lib/ohai/mixin/do_metadata.rb +47 -0
  12. data/lib/ohai/mixin/ec2_metadata.rb +2 -29
  13. data/lib/ohai/mixin/gce_metadata.rb +0 -37
  14. data/lib/ohai/mixin/http_helper.rb +56 -0
  15. data/lib/ohai/mixin/network_constants.rb +0 -5
  16. data/lib/ohai/plugins/cloud.rb +144 -157
  17. data/lib/ohai/plugins/darwin/filesystem.rb +68 -22
  18. data/lib/ohai/plugins/digital_ocean.rb +24 -42
  19. data/lib/ohai/plugins/dmi.rb +81 -77
  20. data/lib/ohai/plugins/ec2.rb +33 -60
  21. data/lib/ohai/plugins/gce.rb +3 -1
  22. data/lib/ohai/plugins/hostname.rb +1 -16
  23. data/lib/ohai/plugins/linux/filesystem.rb +130 -81
  24. data/lib/ohai/plugins/linux/mdadm.rb +3 -45
  25. data/lib/ohai/plugins/linux/network.rb +1 -26
  26. data/lib/ohai/plugins/linux/platform.rb +4 -2
  27. data/lib/ohai/plugins/linux/virtualization.rb +1 -5
  28. data/lib/ohai/plugins/scala.rb +1 -13
  29. data/lib/ohai/plugins/solaris2/network.rb +1 -2
  30. data/lib/ohai/plugins/uptime.rb +0 -9
  31. data/lib/ohai/plugins/windows/cpu.rb +2 -1
  32. data/lib/ohai/plugins/windows/network.rb +1 -1
  33. data/lib/ohai/runner.rb +0 -5
  34. data/lib/ohai/system.rb +38 -8
  35. data/lib/ohai/version.rb +2 -2
  36. data/ohai.gemspec +1 -1
  37. data/spec/functional/application_spec.rb +0 -40
  38. data/spec/functional/plugins/powershell_spec.rb +1 -1
  39. data/spec/functional/plugins/root_group_spec.rb +1 -1
  40. data/spec/functional/plugins/windows/uptime_spec.rb +1 -1
  41. data/spec/ohai_spec.rb +1 -1
  42. data/spec/support/integration_helper.rb +17 -0
  43. data/spec/support/platform_helpers.rb +17 -0
  44. data/spec/unit/application_spec.rb +0 -1
  45. data/spec/unit/config_spec.rb +0 -111
  46. data/spec/unit/dsl/plugin_spec.rb +1 -1
  47. data/spec/unit/hints_spec.rb +1 -1
  48. data/spec/unit/loader_spec.rb +26 -1
  49. data/spec/unit/mixin/command_spec.rb +1 -69
  50. data/spec/unit/mixin/ec2_metadata_spec.rb +1 -1
  51. data/spec/unit/mixin/softlayer_metadata_spec.rb +1 -1
  52. data/spec/unit/plugin_config_spec.rb +0 -1
  53. data/spec/unit/plugins/abort_spec.rb +1 -1
  54. data/spec/unit/plugins/aix/cpu_spec.rb +1 -1
  55. data/spec/unit/plugins/aix/filesystem_spec.rb +2 -1
  56. data/spec/unit/plugins/aix/hostname_spec.rb +1 -1
  57. data/spec/unit/plugins/aix/kernel_spec.rb +1 -1
  58. data/spec/unit/plugins/aix/memory_spec.rb +1 -1
  59. data/spec/unit/plugins/aix/network_spec.rb +1 -1
  60. data/spec/unit/plugins/aix/os_spec.rb +1 -1
  61. data/spec/unit/plugins/aix/platform_spec.rb +1 -1
  62. data/spec/unit/plugins/aix/uptime_spec.rb +1 -1
  63. data/spec/unit/plugins/aix/virtualization_spec.rb +1 -1
  64. data/spec/unit/plugins/azure_spec.rb +1 -1
  65. data/spec/unit/plugins/bsd/filesystem_spec.rb +1 -1
  66. data/spec/unit/plugins/bsd/virtualization_spec.rb +1 -1
  67. data/spec/unit/plugins/c_spec.rb +1 -1
  68. data/spec/unit/plugins/chef_spec.rb +1 -2
  69. data/spec/unit/plugins/cloud_spec.rb +210 -50
  70. data/spec/unit/plugins/darwin/cpu_spec.rb +1 -1
  71. data/spec/unit/plugins/darwin/filesystem_spec.rb +62 -13
  72. data/spec/unit/plugins/darwin/hardware_spec.rb +2 -2
  73. data/spec/unit/plugins/darwin/hostname_spec.rb +1 -1
  74. data/spec/unit/plugins/darwin/kernel_spec.rb +1 -1
  75. data/spec/unit/plugins/darwin/memory_spec.rb +1 -1
  76. data/spec/unit/plugins/darwin/network_spec.rb +1 -1
  77. data/spec/unit/plugins/darwin/platform_spec.rb +1 -1
  78. data/spec/unit/plugins/darwin/system_profiler_spec.rb +2 -9
  79. data/spec/unit/plugins/darwin/virtualization_spec.rb +1 -1
  80. data/spec/unit/plugins/digital_ocean_spec.rb +39 -110
  81. data/spec/unit/plugins/dmi_spec.rb +1 -1
  82. data/spec/unit/plugins/ec2_spec.rb +10 -62
  83. data/spec/unit/plugins/elixir_spec.rb +1 -1
  84. data/spec/unit/plugins/erlang_spec.rb +1 -1
  85. data/spec/unit/plugins/eucalyptus_spec.rb +1 -1
  86. data/spec/unit/plugins/fail_spec.rb +1 -1
  87. data/spec/unit/plugins/freebsd/cpu_spec.rb +1 -1
  88. data/spec/unit/plugins/freebsd/hostname_spec.rb +1 -1
  89. data/spec/unit/plugins/freebsd/kernel_spec.rb +1 -1
  90. data/spec/unit/plugins/freebsd/os_spec.rb +1 -1
  91. data/spec/unit/plugins/freebsd/platform_spec.rb +1 -1
  92. data/spec/unit/plugins/gce_spec.rb +1 -1
  93. data/spec/unit/plugins/go_spec.rb +1 -1
  94. data/spec/unit/plugins/groovy_spec.rb +1 -1
  95. data/spec/unit/plugins/haskell_spec.rb +1 -1
  96. data/spec/unit/plugins/hostname_spec.rb +16 -41
  97. data/spec/unit/plugins/init_package_spec.rb +1 -1
  98. data/spec/unit/plugins/ip_scopes_spec.rb +18 -6
  99. data/spec/unit/plugins/java_spec.rb +1 -1
  100. data/spec/unit/plugins/joyent_spec.rb +17 -1
  101. data/spec/unit/plugins/kernel_spec.rb +1 -1
  102. data/spec/unit/plugins/linode_spec.rb +1 -1
  103. data/spec/unit/plugins/linux/block_device_spec.rb +1 -1
  104. data/spec/unit/plugins/linux/cpu_spec.rb +1 -2
  105. data/spec/unit/plugins/linux/filesystem_spec.rb +207 -201
  106. data/spec/unit/plugins/linux/fips_spec.rb +1 -1
  107. data/spec/unit/plugins/linux/hostname_spec.rb +1 -1
  108. data/spec/unit/plugins/linux/hostnamectl_spec.rb +1 -1
  109. data/spec/unit/plugins/linux/kernel_spec.rb +1 -1
  110. data/spec/unit/plugins/linux/lsb_spec.rb +1 -1
  111. data/spec/unit/plugins/linux/machineid_spec.rb +1 -1
  112. data/spec/unit/plugins/linux/mdadm_spec.rb +2 -68
  113. data/spec/unit/plugins/linux/memory_spec.rb +1 -1
  114. data/spec/unit/plugins/linux/network_spec.rb +3 -44
  115. data/spec/unit/plugins/linux/platform_spec.rb +4 -4
  116. data/spec/unit/plugins/linux/sessions_spec.rb +1 -1
  117. data/spec/unit/plugins/linux/uptime_spec.rb +1 -1
  118. data/spec/unit/plugins/linux/virtualization_spec.rb +1 -28
  119. data/spec/unit/plugins/lua_spec.rb +1 -1
  120. data/spec/unit/plugins/mono_spec.rb +1 -1
  121. data/spec/unit/plugins/netbsd/hostname_spec.rb +1 -1
  122. data/spec/unit/plugins/netbsd/kernel_spec.rb +1 -1
  123. data/spec/unit/plugins/netbsd/platform_spec.rb +1 -1
  124. data/spec/unit/plugins/network_spec.rb +1 -1
  125. data/spec/unit/plugins/nodejs_spec.rb +1 -1
  126. data/spec/unit/plugins/ohai_spec.rb +1 -1
  127. data/spec/unit/plugins/ohai_time_spec.rb +1 -1
  128. data/spec/unit/plugins/openbsd/hostname_spec.rb +1 -1
  129. data/spec/unit/plugins/openbsd/kernel_spec.rb +1 -1
  130. data/spec/unit/plugins/openbsd/platform_spec.rb +1 -1
  131. data/spec/unit/plugins/openstack_spec.rb +2 -3
  132. data/spec/unit/plugins/os_spec.rb +1 -1
  133. data/spec/unit/plugins/packages_spec.rb +1 -1
  134. data/spec/unit/plugins/passwd_spec.rb +17 -1
  135. data/spec/unit/plugins/perl_spec.rb +1 -1
  136. data/spec/unit/plugins/php_spec.rb +1 -1
  137. data/spec/unit/plugins/platform_spec.rb +1 -1
  138. data/spec/unit/plugins/powershell_spec.rb +1 -1
  139. data/spec/unit/plugins/python_spec.rb +1 -1
  140. data/spec/unit/plugins/rackspace_spec.rb +1 -2
  141. data/spec/unit/plugins/root_group_spec.rb +2 -2
  142. data/spec/unit/plugins/ruby_spec.rb +1 -1
  143. data/spec/unit/plugins/rust_spec.rb +1 -1
  144. data/spec/unit/plugins/scala_spec.rb +3 -37
  145. data/spec/unit/plugins/shard_spec.rb +1 -1
  146. data/spec/unit/plugins/shells_spec.rb +1 -1
  147. data/spec/unit/plugins/softlayer_spec.rb +1 -1
  148. data/spec/unit/plugins/solaris2/cpu_spec.rb +1 -1
  149. data/spec/unit/plugins/solaris2/dmi_spec.rb +1 -1
  150. data/spec/unit/plugins/solaris2/hostname_spec.rb +1 -1
  151. data/spec/unit/plugins/solaris2/kernel_spec.rb +1 -1
  152. data/spec/unit/plugins/solaris2/memory_spec.rb +1 -1
  153. data/spec/unit/plugins/solaris2/network_spec.rb +7 -4
  154. data/spec/unit/plugins/solaris2/platform_spec.rb +1 -1
  155. data/spec/unit/plugins/solaris2/virtualization_spec.rb +1 -1
  156. data/spec/unit/plugins/solaris2/zpools_spec.rb +1 -1
  157. data/spec/unit/plugins/ssh_host_keys_spec.rb +1 -1
  158. data/spec/unit/plugins/sysconf_spec.rb +1 -1
  159. data/spec/unit/plugins/timezone_spec.rb +1 -1
  160. data/spec/unit/plugins/virtualbox_spec.rb +1 -1
  161. data/spec/unit/plugins/vmware_spec.rb +2 -1
  162. data/spec/unit/plugins/windows/cpu_spec.rb +9 -2
  163. data/spec/unit/plugins/windows/fips_spec.rb +1 -1
  164. data/spec/unit/plugins/windows/memory_spec.rb +1 -1
  165. data/spec/unit/plugins/windows/uptime_spec.rb +1 -1
  166. data/spec/unit/plugins/windows/virtualization_spec.rb +1 -1
  167. data/spec/unit/provides_map_spec.rb +1 -1
  168. data/spec/unit/runner_spec.rb +1 -1
  169. data/spec/unit/system_spec.rb +68 -76
  170. data/spec/unit/util/file_helper_spec.rb +1 -1
  171. data/spec/unit/util/ip_helper_spec.rb +1 -1
  172. metadata +6 -20
  173. data/lib/ohai/plugins/cloud_v2.rb +0 -315
  174. data/lib/ohai/plugins/darwin/filesystem2.rb +0 -107
  175. data/lib/ohai/plugins/linux/filesystem2.rb +0 -223
  176. data/lib/ohai/plugins/linux/lspci.rb +0 -76
  177. data/lib/ohai/plugins/network_listeners.rb +0 -60
  178. data/lib/ohai/plugins/sigar/cpu.rb +0 -44
  179. data/lib/ohai/plugins/sigar/filesystem.rb +0 -50
  180. data/lib/ohai/plugins/sigar/memory.rb +0 -39
  181. data/lib/ohai/plugins/sigar/network.rb +0 -168
  182. data/lib/ohai/plugins/sigar/network_route.rb +0 -64
  183. data/lib/ohai/plugins/sigar/platform.rb +0 -30
  184. data/spec/unit/plugins/cloud_v2_spec.rb +0 -389
  185. data/spec/unit/plugins/darwin/filesystem2_spec.rb +0 -139
  186. data/spec/unit/plugins/linux/filesystem2_spec.rb +0 -437
  187. data/spec/unit/plugins/linux/lspci_spec.rb +0 -133
  188. data/spec/unit/plugins/sigar/network_route_spec.rb +0 -148
@@ -1,6 +1,8 @@
1
1
  #
2
+ # Author:: Phil Dibowitz (<phil@ipom.com>)
2
3
  # Author:: Benjamin Black (<bb@chef.io>)
3
4
  # Copyright:: Copyright (c) 2009-2016 Chef Software, Inc.
5
+ # Copyright:: Copyright (c) 2015 Facebook, Inc.
4
6
  # License:: Apache License, Version 2.0
5
7
  #
6
8
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,46 +20,90 @@
18
20
 
19
21
  Ohai.plugin(:Filesystem) do
20
22
  provides "filesystem"
23
+ provides "filesystem2"
24
+
25
+ def generate_device_view(fs)
26
+ view = {}
27
+ fs.each_value do |entry|
28
+ view[entry[:device]] = Mash.new unless view[entry[:device]]
29
+ entry.each do |key, val|
30
+ next if %w{device mount}.include?(key)
31
+ view[entry[:device]][key] = val
32
+ end
33
+ if entry[:mount]
34
+ view[entry[:device]][:mounts] = [] unless view[entry[:device]][:mounts]
35
+ view[entry[:device]][:mounts] << entry[:mount]
36
+ end
37
+ end
38
+ view
39
+ end
40
+
41
+ def generate_mountpoint_view(fs)
42
+ view = {}
43
+ fs.each_value do |entry|
44
+ next unless entry[:mount]
45
+ view[entry[:mount]] = Mash.new unless view[entry[:mount]]
46
+ entry.each do |key, val|
47
+ next if %w{mount device}.include?(key)
48
+ view[entry[:mount]][key] = val
49
+ end
50
+ if entry[:device]
51
+ view[entry[:mount]][:devices] = [] unless view[entry[:mount]][:devices]
52
+ view[entry[:mount]][:devices] << entry[:device]
53
+ end
54
+ end
55
+ view
56
+ end
21
57
 
22
58
  collect_data(:darwin) do
23
59
  fs = Mash.new
24
-
25
60
  block_size = 0
61
+ # on new versions of OSX, -i is default, on old versions it's not, so
62
+ # specifying it gets consistent output
26
63
  so = shell_out("df -i")
27
- so.stdout.lines do |line|
64
+ so.stdout.each_line do |line|
28
65
  case line
29
66
  when /^Filesystem\s+(\d+)-/
30
67
  block_size = $1.to_i
31
68
  next
32
69
  when /^(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\%)\s+(\d+)\s+(\d+)\s+(\d+%)\s+(.+)$/
33
- filesystem = $1
34
- fs[filesystem] = Mash.new
35
- fs[filesystem][:block_size] = block_size
36
- # To match linux, these should be strings, but we don't want
37
- # to break back compat so we'll leave them as they are. In filesystem2
38
- # we make them consistent.
39
- fs[filesystem][:kb_size] = $2.to_i / (1024 / block_size)
40
- fs[filesystem][:kb_used] = $3.to_i / (1024 / block_size)
41
- fs[filesystem][:kb_available] = $4.to_i / (1024 / block_size)
42
- fs[filesystem][:percent_used] = $5
43
- fs[filesystem][:inodes_used] = $6
44
- fs[filesystem][:inodes_available] = $7
45
- fs[filesystem][:total_inodes] = ($6.to_i + $7.to_i).to_s
46
- fs[filesystem][:mount] = $9
70
+ key = "#{$1},#{$9}"
71
+ fs[key] = Mash.new
72
+ fs[key][:block_size] = block_size
73
+ fs[key][:device] = $1
74
+ fs[key][:kb_size] = ($2.to_i / (1024 / block_size)).to_s
75
+ fs[key][:kb_used] = ($3.to_i / (1024 / block_size)).to_s
76
+ fs[key][:kb_available] = ($4.to_i / (1024 / block_size)).to_s
77
+ fs[key][:percent_used] = $5
78
+ fs[key][:inodes_used] = $6
79
+ fs[key][:inodes_available] = $7
80
+ fs[key][:total_inodes] = ($6.to_i + $7.to_i).to_s
81
+ fs[key][:inodes_percent_used] = $8
82
+ fs[key][:mount] = $9
47
83
  end
48
84
  end
49
85
 
50
86
  so = shell_out("mount")
51
87
  so.stdout.lines do |line|
52
88
  if line =~ /^(.+?) on (.+?) \((.+?), (.+?)\)$/
53
- filesystem = $1
54
- fs[filesystem] = Mash.new unless fs.has_key?(filesystem)
55
- fs[filesystem][:mount] = $2
56
- fs[filesystem][:fs_type] = $3
57
- fs[filesystem][:mount_options] = $4.split(/,\s*/)
89
+ key = "#{$1},#{$2}"
90
+ fs[key] = Mash.new unless fs.has_key?(key)
91
+ fs[key][:mount] = $2
92
+ fs[key][:fs_type] = $3
93
+ fs[key][:mount_options] = $4.split(/,\s*/)
58
94
  end
59
95
  end
60
96
 
61
- filesystem fs
97
+ by_pair = fs
98
+ by_device = generate_device_view(fs)
99
+ by_mountpoint = generate_mountpoint_view(fs)
100
+
101
+ fs_data = Mash.new
102
+ fs_data["by_device"] = by_device
103
+ fs_data["by_mountpoint"] = by_mountpoint
104
+ fs_data["by_pair"] = by_pair
105
+
106
+ filesystem fs_data
107
+ filesystem2 fs_data
62
108
  end
63
109
  end
@@ -1,4 +1,5 @@
1
1
  #
2
+ # Author:: Dylan Page (<dpage@digitalocean.com>)
2
3
  # Author:: Stafford Brunk (<stafford.brunk@gmail.com>)
3
4
  # License:: Apache License, Version 2.0
4
5
  #
@@ -14,65 +15,46 @@
14
15
  # See the License for the specific language governing permissions and
15
16
  # limitations under the License.
16
17
 
17
- require "ohai/util/ip_helper"
18
+ require "ohai/mixin/do_metadata"
19
+ require "ohai/mixin/http_helper"
18
20
 
19
21
  Ohai.plugin(:DigitalOcean) do
20
- include Ohai::Util::IpHelper
21
-
22
- DIGITALOCEAN_FILE = "/etc/digitalocean" unless defined?(DIGITALOCEAN_FILE)
22
+ include Ohai::Mixin::DOMetadata
23
+ include Ohai::Mixin::HttpHelper
23
24
 
24
25
  provides "digital_ocean"
25
- depends "network/interfaces"
26
26
 
27
- def extract_droplet_ip_addresses
28
- addresses = Mash.new({ "v4" => [], "v6" => [] })
29
- network[:interfaces].each_value do |iface|
30
- iface[:addresses].each do |address, details|
31
- next if details[:family] == "lladdr" || loopback?(address)
27
+ depends "dmi"
32
28
 
33
- ip = IPAddress(address)
34
- type = digital_ocean_address_type(ip)
35
- address_hash = build_address_hash(ip, details)
36
- addresses[type] << address_hash
29
+ # look for digitalocean string in dmi bios data
30
+ def has_do_dmi?
31
+ begin
32
+ # detect a vendor of "DigitalOcean"
33
+ if dmi[:bios][:all_records][0][:Vendor] == "DigitalOcean"
34
+ Ohai::Log.debug("Plugin DigitalOcean: has_do_dmi? == true")
35
+ return true
37
36
  end
37
+ rescue NoMethodError
38
+ # dmi[:bios][:all_records][0][:Vendor] may not exist
38
39
  end
39
- addresses
40
- end
41
-
42
- def build_address_hash(ip, details)
43
- address_hash = Mash.new({
44
- "ip_address" => ip.address,
45
- "type" => private_address?(ip.address) ? "private" : "public",
46
- })
47
-
48
- if ip.ipv4?
49
- address_hash["netmask"] = details[:netmask]
50
- elsif ip.ipv6?
51
- address_hash["cidr"] = ip.prefix
52
- end
53
- address_hash
54
- end
55
-
56
- def digital_ocean_address_type(ip)
57
- ip.ipv4? ? "v4" : "v6"
40
+ Ohai::Log.debug("Plugin DigitalOcean: has_do_dmi? == false")
41
+ return false
58
42
  end
59
43
 
60
44
  def looks_like_digital_ocean?
61
- hint?("digital_ocean") || File.exist?(DIGITALOCEAN_FILE)
45
+ return true if hint?("digital_ocean")
46
+ return true if has_do_dmi? && can_socket_connect?(Ohai::Mixin::DOMetadata::DO_METADATA_ADDR, 80)
47
+ return false
62
48
  end
63
49
 
64
50
  collect_data do
65
51
  if looks_like_digital_ocean?
66
52
  Ohai::Log.debug("Plugin Digitalocean: looks_like_digital_ocean? == true")
67
53
  digital_ocean Mash.new
68
- hint = hint?("digital_ocean") || {}
69
- hint.each { |k, v| digital_ocean[k] = v unless k == "ip_addresses" }
70
-
71
- # Extract actual ip addresses
72
- # The networks sub-hash is structured similarly to how
73
- # Digital Ocean's v2 API structures things:
74
- # https://developers.digitalocean.com/#droplets
75
- digital_ocean[:networks] = extract_droplet_ip_addresses
54
+ fetch_metadata.each do |k, v|
55
+ next if k == "vendor_data" # this may have sensitive data we shouldn't store
56
+ digital_ocean[k] = v
57
+ end
76
58
  else
77
59
  Ohai::Log.debug("Plugin Digitalocean: No hints present for and doesn't look like digitalocean")
78
60
  false
@@ -44,86 +44,90 @@ Ohai.plugin(:DMI) do
44
44
  dmi_record = nil
45
45
  field = nil
46
46
 
47
- so = shell_out("dmidecode")
48
- # ==== EXAMPLE RECORD: ====
49
- #Handle 0x0000, DMI type 0, 24 bytes
50
- #BIOS Information
51
- # Vendor: American Megatrends Inc.
52
- # Version: 080012
53
- # ... similar lines trimmed
54
- # Characteristics:
55
- # ISA is supported
56
- # PCI is supported
57
- # ... similar lines trimmed
58
- so.stdout.lines do |line|
59
- next if blank_line.match(line)
60
- line = line.encode(line.encoding, :universal_newline => true)
61
-
62
- if dmidecode_version = dmidecode_version_line.match(line)
63
- dmi[:dmidecode_version] = dmidecode_version[1]
64
-
65
- elsif smbios_version = smbios_version_line.match(line)
66
- dmi[:smbios_version] = smbios_version[1]
67
-
68
- elsif structures = structures_line.match(line)
69
- dmi[:structures] = Mash.new
70
- dmi[:structures][:count] = structures[1]
71
- dmi[:structures][:size] = structures[2]
72
-
73
- elsif table_location = table_location_line.match(line)
74
- dmi[:table_location] = table_location[1]
75
-
76
- elsif handle = handle_line.match(line)
77
- # Don't overcapture for now (OHAI-260)
78
- unless Ohai::Common::DMI::ID_TO_CAPTURE.include?(handle[2].to_i)
79
- dmi_record = nil
80
- next
81
- end
82
-
83
- dmi_record = { :type => Ohai::Common::DMI.id_lookup(handle[2]) }
84
-
85
- dmi[dmi_record[:type]] = Mash.new unless dmi.has_key?(dmi_record[:type])
86
- dmi[dmi_record[:type]][:all_records] = [] unless dmi[dmi_record[:type]].has_key?(:all_records)
87
- dmi_record[:position] = dmi[dmi_record[:type]][:all_records].length
88
- dmi[dmi_record[:type]][:all_records].push(Mash.new)
89
- dmi[dmi_record[:type]][:all_records][dmi_record[:position]][:record_id] = handle[1]
90
- dmi[dmi_record[:type]][:all_records][dmi_record[:position]][:size] = handle[2]
91
- field = nil
92
-
93
- elsif type = type_line.match(line)
94
- if dmi_record.nil?
95
- Ohai::Log.debug("Plugin DMI: unexpected data line found before header; discarding:\n#{line}")
96
- next
97
- end
98
- dmi[dmi_record[:type]][:all_records][dmi_record[:position]][:application_identifier] = type[1]
99
-
100
- elsif data = data_line.match(line)
101
- if dmi_record.nil?
102
- Ohai::Log.debug("Plugin DMI: unexpected data line found before header; discarding:\n#{line}")
103
- next
104
- end
105
- dmi[dmi_record[:type]][:all_records][dmi_record[:position]][data[1]] = data[2]
106
- field = data[1]
47
+ begin
48
+ so = shell_out("dmidecode")
49
+ # ==== EXAMPLE RECORD: ====
50
+ #Handle 0x0000, DMI type 0, 24 bytes
51
+ #BIOS Information
52
+ # Vendor: American Megatrends Inc.
53
+ # Version: 080012
54
+ # ... similar lines trimmed
55
+ # Characteristics:
56
+ # ISA is supported
57
+ # PCI is supported
58
+ # ... similar lines trimmed
59
+ so.stdout.lines do |line|
60
+ next if blank_line.match(line)
61
+ line = line.encode(line.encoding, :universal_newline => true)
62
+
63
+ if dmidecode_version = dmidecode_version_line.match(line)
64
+ dmi[:dmidecode_version] = dmidecode_version[1]
65
+
66
+ elsif smbios_version = smbios_version_line.match(line)
67
+ dmi[:smbios_version] = smbios_version[1]
68
+
69
+ elsif structures = structures_line.match(line)
70
+ dmi[:structures] = Mash.new
71
+ dmi[:structures][:count] = structures[1]
72
+ dmi[:structures][:size] = structures[2]
73
+
74
+ elsif table_location = table_location_line.match(line)
75
+ dmi[:table_location] = table_location[1]
76
+
77
+ elsif handle = handle_line.match(line)
78
+ # Don't overcapture for now (OHAI-260)
79
+ unless Ohai::Common::DMI::ID_TO_CAPTURE.include?(handle[2].to_i)
80
+ dmi_record = nil
81
+ next
82
+ end
83
+
84
+ dmi_record = { :type => Ohai::Common::DMI.id_lookup(handle[2]) }
85
+
86
+ dmi[dmi_record[:type]] = Mash.new unless dmi.has_key?(dmi_record[:type])
87
+ dmi[dmi_record[:type]][:all_records] = [] unless dmi[dmi_record[:type]].has_key?(:all_records)
88
+ dmi_record[:position] = dmi[dmi_record[:type]][:all_records].length
89
+ dmi[dmi_record[:type]][:all_records].push(Mash.new)
90
+ dmi[dmi_record[:type]][:all_records][dmi_record[:position]][:record_id] = handle[1]
91
+ dmi[dmi_record[:type]][:all_records][dmi_record[:position]][:size] = handle[2]
92
+ field = nil
93
+
94
+ elsif type = type_line.match(line)
95
+ if dmi_record .nil?
96
+ Ohai::Log.debug("Plugin DMI: unexpected data line found before header; discarding:\n#{line}")
97
+ next
98
+ end
99
+ dmi[dmi_record[:type]][:all_records][dmi_record[:position]][:application_identifier] = type[1]
100
+
101
+ elsif data = data_line.match(line)
102
+ if dmi_record .nil?
103
+ Ohai::Log.debug("Plugin DMI: unexpected data line found before header; discarding:\n#{line}")
104
+ next
105
+ end
106
+ dmi[dmi_record[:type]][:all_records][dmi_record[:position]][data[1]] = data[2]
107
+ field = data[1]
108
+
109
+ elsif extended_data = extended_data_line.match(line)
110
+ if dmi_record .nil?
111
+ Ohai::Log.debug("Plugin DMI: unexpected extended data line found before header; discarding:\n#{line}")
112
+ next
113
+ end
114
+ if field .nil?
115
+ Ohai::Log.debug("Plugin DMI: unexpected extended data line found outside data section; discarding:\n#{line}")
116
+ next
117
+ end
118
+ # overwrite "raw" value with a new Mash
119
+ dmi[dmi_record[:type]][:all_records][dmi_record[:position]][field] = Mash.new unless dmi[dmi_record[:type]][:all_records][dmi_record[:position]][field].class.to_s == "Mash"
120
+ dmi[dmi_record[:type]][:all_records][dmi_record[:position]][field][extended_data[1]] = nil
121
+
122
+ else
123
+ Ohai::Log.debug("Plugin DMI: unrecognized output line; discarding:\n#{line}")
107
124
 
108
- elsif extended_data = extended_data_line.match(line)
109
- if dmi_record.nil?
110
- Ohai::Log.debug("Plugin DMI: unexpected extended data line found before header; discarding:\n#{line}")
111
- next
112
- end
113
- if field.nil?
114
- Ohai::Log.debug("Plugin DMI: unexpected extended data line found outside data section; discarding:\n#{line}")
115
- next
116
125
  end
117
- # overwrite "raw" value with a new Mash
118
- dmi[dmi_record[:type]][:all_records][dmi_record[:position]][field] = Mash.new unless dmi[dmi_record[:type]][:all_records][dmi_record[:position]][field].class.to_s == "Mash"
119
- dmi[dmi_record[:type]][:all_records][dmi_record[:position]][field][extended_data[1]] = nil
120
-
121
- else
122
- Ohai::Log.debug("Plugin DMI: unrecognized output line; discarding:\n#{line}")
123
-
124
126
  end
125
- end
126
127
 
127
- Ohai::Common::DMI.convenience_keys(dmi)
128
+ Ohai::Common::DMI.convenience_keys(dmi)
129
+ rescue Ohai::Exceptions::Exec
130
+ Ohai::Log.debug('Plugin DMI: Could not shell_out "dmidecode". Skipping data')
131
+ end
128
132
  end
129
133
  end
@@ -21,93 +21,66 @@
21
21
  # How we detect EC2 from easiest to hardest & least reliable
22
22
  # 1. Ohai ec2 hint exists. This always works
23
23
  # 2. Xen hypervisor UUID starts with 'ec2'. This catches Linux HVM & paravirt instances
24
- # 3. DMI bios version data mentions amazon. This catches HVM instances in a VPC on the Xen based hypervisor
25
- # 3. DMI bios vendor data mentions amazon. This catches HVM instances in a VPC on the non-Xen based hypervisor
24
+ # 3. DMI data mentions amazon. This catches HVM instances in a VPC
26
25
  # 4. Kernel data mentioned Amazon. This catches Windows HVM & paravirt instances
27
26
 
28
- Ohai.plugin(:EC2) do
29
- require "ohai/mixin/ec2_metadata"
30
- require "base64"
27
+ require "ohai/mixin/ec2_metadata"
28
+ require "ohai/mixin/http_helper"
29
+ require "base64"
31
30
 
31
+ Ohai.plugin(:EC2) do
32
32
  include Ohai::Mixin::Ec2Metadata
33
+ include Ohai::Mixin::HttpHelper
33
34
 
34
35
  provides "ec2"
35
36
 
36
- # look for amazon string in dmi vendor bios data within the sys tree.
37
- # this works even if the system lacks dmidecode use by the Dmi plugin
38
- # this gets us detection of new Xen-less HVM instances that are within a VPC
39
- # @return [Boolean] do we have Amazon DMI data?
40
- def has_ec2_amazon_dmi?
41
- # detect a version of '4.2.amazon'
42
- if file_val_if_exists("/sys/class/dmi/id/bios_vendor") =~ /Amazon/
43
- Ohai::Log.debug("Plugin EC2: has_ec2_amazon_dmi? == true")
44
- true
45
- else
46
- Ohai::Log.debug("Plugin EC2: has_ec2_amazon_dmi? == false")
47
- false
48
- end
49
- end
37
+ depends "dmi"
38
+ depends "kernel"
50
39
 
51
- # look for amazon string in dmi bios version data within the sys tree.
52
- # this works even if the system lacks dmidecode use by the Dmi plugin
40
+ # look for amazon string in dmi bios data
53
41
  # this gets us detection of HVM instances that are within a VPC
54
- # @return [Boolean] do we have Amazon DMI data?
55
- def has_ec2_xen_dmi?
42
+ def has_ec2_dmi?
56
43
  # detect a version of '4.2.amazon'
57
- if file_val_if_exists("/sys/class/dmi/id/bios_version") =~ /amazon/
58
- Ohai::Log.debug("Plugin EC2: has_ec2_xen_dmi? == true")
59
- true
44
+ if get_attribute(:dmi, :bios, :all_records, 0, :Version) =~ /amazon/
45
+ Ohai::Log.debug("Plugin EC2: has_ec2_dmi? == true")
46
+ return true
60
47
  else
61
- Ohai::Log.debug("Plugin EC2: has_ec2_xen_dmi? == false")
62
- false
48
+ Ohai::Log.debug("Plugin EC2: has_ec2_dmi? == false")
49
+ return false
63
50
  end
64
51
  end
65
52
 
66
- # looks for a xen UUID that starts with ec2 from within the Linux sys tree
67
- # @return [Boolean] do we have a Xen UUID or not?
53
+ # looks for a xen UUID that starts with ec2
54
+ # this gets us detection of Linux HVM and Paravirt hosts
68
55
  def has_ec2_xen_uuid?
69
- if file_val_if_exists("/sys/hypervisor/uuid") =~ /^ec2/
70
- Ohai::Log.debug("Plugin EC2: has_ec2_xen_uuid? == true")
71
- return true
72
- end
73
- Ohai::Log.debug("Plugin EC2: has_ec2_xen_uuid? == false")
74
- false
75
- end
76
-
77
- # looks at the identifying number WMI value to see if it starts with ec2.
78
- # this is actually the same value we're looking at in has_ec2_xen_uuid? on
79
- # linux hosts
80
- # @return [Boolean] do we have a Xen Identifying Number or not?
81
- def has_ec2_identifying_number?
82
- if RUBY_PLATFORM =~ /mswin|mingw32|windows/
83
- require "wmi-lite/wmi"
84
- wmi = WmiLite::Wmi.new
85
- if wmi.first_of("Win32_ComputerSystemProduct")["identifyingnumber"] =~ /^ec2/
86
- Ohai::Log.debug("Plugin EC2: has_ec2_identifying_number? == true")
56
+ if ::File.exist?("/sys/hypervisor/uuid")
57
+ if ::File.read("/sys/hypervisor/uuid") =~ /^ec2/
58
+ Ohai::Log.debug("Plugin EC2: has_ec2_xen_uuid? == true")
87
59
  return true
88
60
  end
89
- else
90
- Ohai::Log.debug("Plugin EC2: has_ec2_identifying_number? == false")
91
- false
92
61
  end
62
+ Ohai::Log.debug("Plugin EC2: has_ec2_xen_uuid? == false")
63
+ return false
93
64
  end
94
65
 
95
- # return the contents of a file if the file exists
96
- # @param path[String] abs path to the file
97
- # @return [String] contents of the file if it exists
98
- def file_val_if_exists(path)
99
- if ::File.exist?(path)
100
- ::File.read(path)
66
+ # looks for the Amazon.com Organization in Windows Kernel data
67
+ # this gets us detection of Windows systems
68
+ def has_amazon_org?
69
+ # detect an Organization of 'Amazon.com'
70
+ if get_attribute(:kernel, :os_info, :organization) =~ /Amazon/
71
+ Ohai::Log.debug("Plugin EC2: has_amazon_org? == true")
72
+ return true
73
+ else
74
+ Ohai::Log.debug("Plugin EC2: has_amazon_org? == false")
75
+ return false
101
76
  end
102
77
  end
103
78
 
104
- # a single check that combines all the various detection methods for EC2
105
- # @return [Boolean] Does the system appear to be on EC2
106
79
  def looks_like_ec2?
107
80
  return true if hint?("ec2")
108
81
 
109
82
  # Even if it looks like EC2 try to connect first
110
- if has_ec2_xen_uuid? || has_ec2_amazon_dmi? || has_ec2_xen_dmi? || has_ec2_identifying_number?
83
+ if has_ec2_xen_uuid? || has_ec2_dmi? || has_amazon_org?
111
84
  return true if can_socket_connect?(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80)
112
85
  end
113
86
  end