ohai 16.10.6 → 17.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,6 +9,7 @@
9
9
  # Author:: Prabhu Das (<prabhu.das@clogeny.com>)
10
10
  # Author:: Isa Farnik (<isa@chef.io>)
11
11
  # Author:: Doug MacEachern <dougm@vmware.com>
12
+ # Author:: Lance Albertson <lance@osuosl.org>
12
13
  # Copyright:: Copyright (c) Chef Software Inc.
13
14
  # License:: Apache License, Version 2.0
14
15
  #
@@ -46,13 +47,213 @@ Ohai.plugin(:CPU) do
46
47
  cpuinfo
47
48
  end
48
49
 
49
- collect_data(:linux) do
50
+ # Convert a string that looks like range of CPUs to an array
51
+ # Given the following range: 1-7
52
+ # Convert it into an array: [1, 2, 3, 4, 5, 6, 7]
53
+ def range_str_to_a(range)
54
+ range.split(",").each_with_object([]) do |cpu, arr|
55
+ if /\d+-\d+/.match?(cpu.to_s)
56
+ arr << Range.new(*cpu.split("-").map(&:to_i)).to_a
57
+ else
58
+ arr << cpu.to_i
59
+ end
60
+ end.flatten
61
+ end
62
+
63
+ def parse_lscpu(cpu_info)
64
+ lscpu_info = Mash.new
65
+ begin
66
+ so = shell_out("lscpu")
67
+ cpu_cores = shell_out("lscpu -p=CPU,CORE,SOCKET")
68
+ if so.exitstatus == 0 && cpu_cores.exitstatus == 0
69
+ lscpu_info[:numa_node_cpus] = Mash.new
70
+ lscpu_info[:vulnerability] = Mash.new
71
+ so.stdout.each_line do |line|
72
+ case line
73
+ when /^Architecture:\s+(.+)/
74
+ lscpu_info[:architecture] = $1.to_s
75
+ when /^CPU op-mode\(s\):\s+(.+)/
76
+ lscpu_info[:cpu_opmodes] = $1.split(", ")
77
+ when /^Byte Order:\s+(.+)/
78
+ lscpu_info[:byte_order] = $1.downcase
79
+ when /^Address sizes:\s+(.+)/
80
+ lscpu_info[:address_sizes] = $1.split(", ")
81
+ when /^CPU\(s\):\s+(.+)/
82
+ lscpu_info[:cpus] = $1.to_i
83
+ when /^On-line CPU\(s\) list:\s+(.+)/
84
+ cpu_range = range_str_to_a($1)
85
+ if cpu_range == [0]
86
+ lscpu_info[:cpus_online] = 0
87
+ else
88
+ lscpu_info[:cpus_online] = cpu_range.length
89
+ end
90
+ when /^Off-line CPU\(s\) list:\s+(.+)/
91
+ cpu_range = range_str_to_a($1)
92
+ if cpu_range == [0]
93
+ lscpu_info[:cpus_offline] = 0
94
+ else
95
+ lscpu_info[:cpus_offline] = cpu_range.length
96
+ end
97
+ when /^Thread\(s\) per core:\s+(.+)/ # http://rubular.com/r/lOw2pRrw1q
98
+ lscpu_info[:threads_per_core] = $1.to_i
99
+ when /^Core\(s\) per socket:\s+(.+)/ # http://rubular.com/r/lOw2pRrw1q
100
+ lscpu_info[:cores_per_socket] = $1.to_i
101
+ when /^Socket\(s\):\s+(.+)/ # http://rubular.com/r/DIzmPtJFvK
102
+ lscpu_info[:sockets] = $1.to_i
103
+ when /^Socket\(s\) per book:\s+(.+)/
104
+ lscpu_info[:sockets_per_book] = $1.to_i
105
+ when /^Book\(s\) per drawer:\s+(.+)/
106
+ lscpu_info[:books_per_drawer] = $1.to_i
107
+ when /^Drawer\(s\):\s+(.+)/
108
+ lscpu_info[:drawers] = $1.to_i
109
+ when /^NUMA node\(s\):\s+(.+)/
110
+ lscpu_info[:numa_nodes] = $1.to_i
111
+ when /^Vendor ID:\s+(.+)/
112
+ lscpu_info[:vendor_id] = $1
113
+ when /^Machine type:\s+(.+)/
114
+ lscpu_info[:machine_type] = $1
115
+ when /^CPU family:\s+(.+)/
116
+ lscpu_info[:family] = $1
117
+ when /^Model:\s+(.+)/
118
+ lscpu_info[:model] = $1
119
+ when /^Model name:\s+(.+)/
120
+ lscpu_info[:model_name] = $1
121
+ when /^Stepping:\s+(.+)/
122
+ lscpu_info[:stepping] = $1
123
+ when /^CPU MHz:\s+(.+)/
124
+ lscpu_info[:mhz] = $1
125
+ when /^CPU static MHz:\s+(.+)/
126
+ lscpu_info[:mhz] = $1
127
+ when /^CPU max MHz:\s+(.+)/
128
+ lscpu_info[:mhz_max] = $1
129
+ when /^CPU min MHz:\s+(.+)/
130
+ lscpu_info[:mhz_min] = $1
131
+ when /^CPU dynamic MHz:\s+(.+)/
132
+ lscpu_info[:mhz_dynamic] = $1
133
+ when /^BogoMIPS:\s+(.+)/
134
+ lscpu_info[:bogomips] = $1
135
+ when /^Virtualization:\s+(.+)/
136
+ lscpu_info[:virtualization] = $1
137
+ when /^Virtualization type:\s+(.+)/
138
+ lscpu_info[:virtualization_type] = $1
139
+ when /^Hypervisor vendor:\s+(.+)/
140
+ lscpu_info[:hypervisor_vendor] = $1
141
+ when /^Dispatching mode:\s+(.+)/
142
+ lscpu_info[:dispatching_mode] = $1
143
+ when /^L1d cache:\s+(.+)/
144
+ lscpu_info[:l1d_cache] = $1
145
+ when /^L1i cache:\s+(.+)/
146
+ lscpu_info[:l1i_cache] = $1
147
+ when /^L2 cache:\s+(.+)/
148
+ lscpu_info[:l2_cache] = $1
149
+ when /^L2d cache:\s+(.+)/
150
+ lscpu_info[:l2d_cache] = $1
151
+ when /^L2i cache:\s+(.+)/
152
+ lscpu_info[:l2i_cache] = $1
153
+ when /^L3 cache:\s+(.+)/
154
+ lscpu_info[:l3_cache] = $1
155
+ when /^L4 cache:\s+(.+)/
156
+ lscpu_info[:l4_cache] = $1
157
+ when /^NUMA node(\d+) CPU\(s\):\s+(.+)/
158
+ numa_node = $1
159
+ cpus = $2
160
+ lscpu_info[:numa_node_cpus][numa_node] = range_str_to_a(cpus)
161
+ when /^Vulnerability (.+?):\s+(.+)/ # https://rubular.com/r/aKtSD1ypUlKbGm
162
+ name = $1.strip.downcase.tr(" ", "_")
163
+ description = $2.strip
164
+ lscpu_info[:vulnerability][name] = Mash.new
165
+ lscpu_info[:vulnerability][name] = description
166
+ when /^Flags:\s+(.+)/
167
+ lscpu_info[:flags] = $1.split(" ").sort
168
+ # flags are "features" on aarch64 and s390x so add it for backwards computability
169
+ lscpu_info[:features] = lscpu_info[:flags] if lscpu_info[:architecture].match?(/aarch64|s390x/)
170
+ end
171
+ end
172
+
173
+ case lscpu_info[:architecture]
174
+ when "s390x"
175
+ # Add data from /proc/cpuinfo that isn't available from lscpu
176
+ lscpu_info[:bogomips_per_cpu] = cpu_info[:bogomips_per_cpu]
177
+ lscpu_info[:version] = cpu_info["0"][:version]
178
+ lscpu_info[:identification] = cpu_info["0"][:identification]
179
+ lscpu_info[:machine] = cpu_info["0"][:machine]
180
+ lscpu_total = lscpu_info[:sockets_per_book] * lscpu_info[:cores_per_socket] * lscpu_info[:threads_per_core] * lscpu_info[:books_per_drawer] * lscpu_info[:drawers]
181
+ lscpu_real = lscpu_info[:sockets_per_book]
182
+ lscpu_cores = lscpu_info[:sockets_per_book] * lscpu_info[:cores_per_socket] * lscpu_info[:books_per_drawer] * lscpu_info[:drawers]
183
+ when "ppc64le"
184
+ # Add data from /proc/cpuinfo that isn't available from lscpu
185
+ lscpu_info[:timebase] = cpu_info[:timebase]
186
+ lscpu_info[:platform] = cpu_info[:platform]
187
+ lscpu_info[:machine_model] = cpu_info[:machine_model]
188
+ lscpu_info[:machine] = cpu_info[:machine]
189
+ lscpu_info[:firmware] = cpu_info[:firmware] if cpu_info[:firmware]
190
+ lscpu_info[:mmu] = cpu_info[:mmu] if cpu_info[:mmu]
191
+ lscpu_info[:mhz] = cpu_info["0"][:mhz]
192
+ lscpu_total = lscpu_info[:sockets] * lscpu_info[:cores_per_socket] * lscpu_info[:threads_per_core]
193
+ lscpu_real = lscpu_info[:sockets]
194
+ lscpu_cores = lscpu_info[:sockets] * lscpu_info[:cores_per_socket]
195
+ else
196
+ lscpu_total = lscpu_info[:sockets] * lscpu_info[:cores_per_socket] * lscpu_info[:threads_per_core]
197
+ lscpu_real = lscpu_info[:sockets]
198
+ lscpu_cores = lscpu_info[:sockets] * lscpu_info[:cores_per_socket]
199
+ end
200
+
201
+ # Enumerate cpus and fill out data to provide backwards compatibility data
202
+ cpu_cores.stdout.each_line do |line|
203
+ current_cpu = nil
204
+ current_core = nil
205
+ current_socket = nil
206
+
207
+ case line
208
+ # skip comments
209
+ when /^#/
210
+ next
211
+ # Parse data from "lscpu -p=CPU,CORE,SOCKET"
212
+ when /(\d+),(\d+),(\d+)/
213
+ current_cpu = $1
214
+ current_core = $2
215
+ current_socket = $3
216
+ end
217
+ lscpu_info[current_cpu] = Mash.new
218
+ lscpu_info[current_cpu][:vendor_id] = lscpu_info[:vendor_id] if lscpu_info[:vendor_id]
219
+ lscpu_info[current_cpu][:family] = lscpu_info[:family] if lscpu_info[:family]
220
+ lscpu_info[current_cpu][:model] = lscpu_info[:model] if lscpu_info[:model]
221
+ lscpu_info[current_cpu][:model_name] = lscpu_info[:model_name] if lscpu_info[:model_name]
222
+ lscpu_info[current_cpu][:stepping] = lscpu_info[:stepping] if lscpu_info[:stepping]
223
+ lscpu_info[current_cpu][:mhz] = lscpu_info[:mhz] if lscpu_info[:mhz]
224
+ lscpu_info[current_cpu][:bogomips] = lscpu_info[:bogomips] if lscpu_info[:bogomips]
225
+ # Per cpu cache_size is only really available from /proc/cpuinfo on x86
226
+ lscpu_info[current_cpu][:cache_size] = cpu_info[current_cpu][:cache_size] if cpu_info[current_cpu] && cpu_info[current_cpu][:cache_size]
227
+ lscpu_info[current_cpu][:physical_id] = current_socket
228
+ lscpu_info[current_cpu][:core_id] = current_core
229
+ lscpu_info[current_cpu][:cores] = lscpu_info[:cores_per_socket].to_s
230
+ lscpu_info[current_cpu][:flags] = lscpu_info[:flags] if lscpu_info[:flags]
231
+ lscpu_info[current_cpu][:features] = lscpu_info[:flags] if lscpu_info[:architecture].match?(/aarch64|s390x/)
232
+ if lscpu_info[:architecture] == "s390x"
233
+ lscpu_info[current_cpu][:version] = cpu_info[current_cpu][:version] if cpu_info[current_cpu][:version]
234
+ lscpu_info[current_cpu][:identification] = cpu_info[current_cpu][:identification] if cpu_info[current_cpu][:identification]
235
+ lscpu_info[current_cpu][:machine] = cpu_info[current_cpu][:machine] if cpu_info[current_cpu][:machine]
236
+ end
237
+ end
238
+ lscpu_info[:total] = lscpu_total
239
+ lscpu_info[:real] = lscpu_real
240
+ lscpu_info[:cores] = lscpu_cores
241
+ else
242
+ logger.trace("Plugin CPU: Error executing lscpu. CPU data may not be available.")
243
+ end
244
+ rescue Ohai::Exceptions::Exec # util-linux isn't installed most likely
245
+ logger.trace("Plugin CPU: Error executing lscpu. util-linux may not be installed.")
246
+ end
247
+ lscpu_info
248
+ end
249
+
250
+ def parse_cpuinfo
50
251
  cpuinfo = Mash.new
51
252
  real_cpu = Mash.new
52
253
  cpu_number = 0
53
254
  current_cpu = nil
54
255
 
55
- file_open("/proc/cpuinfo").each do |line|
256
+ file_open("/proc/cpuinfo").each_line do |line|
56
257
  case line
57
258
  when /processor\s+:\s(.+)/
58
259
  cpuinfo[$1] = Mash.new
@@ -68,7 +269,11 @@ Ohai.plugin(:CPU) do
68
269
  when /cpu family\s+:\s(.+)/
69
270
  cpuinfo[current_cpu]["family"] = $1
70
271
  when /model\s+:\s(.+)/
71
- cpuinfo[current_cpu]["model"] = $1
272
+ model = $1
273
+ cpuinfo[current_cpu]["model"] = model
274
+ # ppc has "model" at the end of /proc/cpuinfo. In addition it should always include a include a dash or "IBM".
275
+ # So let's put this in cpu/model on ppc
276
+ cpuinfo["machine_model"] = model if model.match?(/-|IBM/)
72
277
  when /stepping\s+:\s(.+)/
73
278
  cpuinfo[current_cpu]["stepping"] = $1
74
279
  when /physical id\s+:\s(.+)/
@@ -94,6 +299,24 @@ Ohai.plugin(:CPU) do
94
299
  cpuinfo["bogomips_per_cpu"] = $1
95
300
  when /features\s+:\s(.+)/
96
301
  cpuinfo["features"] = $1.split
302
+ # ppc64le
303
+ when /revision\s+:\s(.+)/
304
+ cpuinfo[current_cpu]["model"] = $1
305
+ when /cpu\s+:\s(.+)/
306
+ cpuinfo[current_cpu]["model_name"] = $1
307
+ when /clock\s+:\s(.+)/
308
+ cpuinfo[current_cpu]["mhz"] = $1
309
+ when /timebase\s+:\s(.+)/
310
+ cpuinfo["timebase"] = $1
311
+ when /platform\s+:\s(.+)/
312
+ cpuinfo["platform"] = $1
313
+ when /machine\s+:\s(.+)/
314
+ cpuinfo["machine"] = $1
315
+ when /firmware\s+:\s(.+)/
316
+ cpuinfo["firmware"] = $1
317
+ when /MMU\s+:\s(.+)/
318
+ cpuinfo["mmu"] = $1
319
+ # s390x
97
320
  when /processor\s(\d):\s(.+)/
98
321
  current_cpu = $1
99
322
  cpu_number += 1
@@ -108,40 +331,28 @@ Ohai.plugin(:CPU) do
108
331
  end
109
332
  end
110
333
 
111
- cpu cpuinfo
112
-
113
- cpu[:total] = cpu_number
114
-
115
334
  # use data we collected unless cpuinfo is lacking core information
116
335
  # which is the case on older linux distros
117
- if !real_cpu.empty? && cpu["0"]["cores"]
118
- cpu[:real] = real_cpu.keys.length
119
- cpu[:cores] = real_cpu.keys.length * cpu["0"]["cores"].to_i
336
+ if !real_cpu.empty? && cpuinfo["0"]["cores"]
337
+ logger.trace("Plugin CPU: Error executing lscpu. CPU data may not be available.")
338
+ cpuinfo[:real] = real_cpu.keys.length
339
+ cpuinfo[:cores] = real_cpu.keys.length * cpuinfo["0"]["cores"].to_i
120
340
  else
121
- begin
122
- logger.trace("Plugin CPU: Falling back to aggregate data from lscpu as real cpu & core data is missing in /proc/cpuinfo")
123
- so = shell_out("lscpu")
124
- if so.exitstatus == 0
125
- lscpu_data = Mash.new
126
- so.stdout.each_line do |line|
127
- case line
128
- when /^Thread\(s\) per core:\s(.+)/ # http://rubular.com/r/lOw2pRrw1q
129
- lscpu_data[:threads] = $1.to_i
130
- when /^Core\(s\) per socket:\s(.+)/ # http://rubular.com/r/lOw2pRrw1q
131
- lscpu_data[:cores] = $1.to_i
132
- when /^Socket\(s\):\s(.+)/ # http://rubular.com/r/DIzmPtJFvK
133
- lscpu_data[:sockets] = $1.to_i
134
- end
135
- end
136
- cpu[:total] = lscpu_data[:sockets] * lscpu_data[:cores] * lscpu_data[:threads]
137
- cpu[:real] = lscpu_data[:sockets]
138
- cpu[:cores] = lscpu_data[:sockets] * lscpu_data[:cores]
139
- else
140
- logger.trace("Plugin CPU: Error executing lscpu. CPU data may not be available.")
141
- end
142
- rescue Ohai::Exceptions::Exec # util-linux isn't installed most likely
143
- logger.trace("Plugin CPU: Error executing lscpu. util-linux may not be installed.")
144
- end
341
+ logger.trace("Plugin CPU: real cpu & core data is missing in /proc/cpuinfo and lscpu")
342
+ end
343
+ cpuinfo[:total] = cpu_number
344
+ cpuinfo
345
+ end
346
+
347
+ collect_data(:linux) do
348
+ cpuinfo = parse_cpuinfo
349
+ lscpu = parse_lscpu(cpuinfo)
350
+
351
+ # If we don't have any data from lscpu then get it from /proc/cpuinfo
352
+ if lscpu.empty?
353
+ cpu cpuinfo
354
+ else
355
+ cpu lscpu
145
356
  end
146
357
  end
147
358
 
@@ -288,7 +499,7 @@ Ohai.plugin(:CPU) do
288
499
 
289
500
  cpu[:total] = shell_out("pmcycles -m").stdout.lines.length
290
501
 
291
- # The below is only relevent on an LPAR
502
+ # The below is only relevant on an LPAR
292
503
  if shell_out("uname -W").stdout.strip == "0"
293
504
 
294
505
  # At least one CPU will be available, but we'll wait to increment this later.
@@ -29,7 +29,7 @@ Ohai.plugin(:Eucalyptus) do
29
29
  provides "eucalyptus"
30
30
  depends "network/interfaces"
31
31
 
32
- MAC_MATCH = /^[dD]0:0[dD]:/.freeze
32
+ MAC_MATCH = /^[dD]0:0[dD]:/.freeze unless defined?(MAC_MATCH)
33
33
 
34
34
  # returns the mac address from the collection of all address types
35
35
  def get_mac_address(addresses)
@@ -98,14 +98,6 @@ Ohai.plugin(:Filesystem) do
98
98
  view
99
99
  end
100
100
 
101
- def generate_deprecated_windows_view(fs)
102
- view = generate_mountpoint_view(fs)
103
- view.each do |mp, entry|
104
- view[mp].delete("devices")
105
- end
106
- view
107
- end
108
-
109
101
  def parse_common_df(out)
110
102
  fs = {}
111
103
  out.each_line do |line|
@@ -466,9 +458,7 @@ Ohai.plugin(:Filesystem) do
466
458
  fs_data["by_mountpoint"] = by_mountpoint
467
459
  fs_data["by_pair"] = by_pair
468
460
 
469
- # @todo in Chef 17 the filesystem2 part of this goes away
470
461
  filesystem fs_data
471
- filesystem2 fs_data
472
462
  end
473
463
 
474
464
  collect_data(:darwin) do
@@ -613,9 +603,7 @@ Ohai.plugin(:Filesystem) do
613
603
  fs_data["by_mountpoint"] = by_mountpoint
614
604
  fs_data["by_pair"] = by_pair
615
605
 
616
- # @todo in Chef 17 the filesystem2 plugin goes away
617
606
  filesystem fs_data
618
- filesystem2 fs_data
619
607
  end
620
608
 
621
609
  collect_data(:aix) do
@@ -705,9 +693,7 @@ Ohai.plugin(:Filesystem) do
705
693
  fs_data["by_mountpoint"] = by_mountpoint
706
694
  fs_data["by_pair"] = by_pair
707
695
 
708
- # @todo in Chef 17 the filesystem2 plugin goes away here
709
696
  filesystem fs_data
710
- filesystem2 fs_data
711
697
  end
712
698
 
713
699
  collect_data(:windows) do
@@ -726,9 +712,10 @@ Ohai.plugin(:Filesystem) do
726
712
  fs_data["by_mountpoint"] = by_mountpoint
727
713
  fs_data["by_pair"] = by_pair
728
714
 
729
- # Set the filesystem data - Windows didn't do the conversion when everyone
730
- # else did, so 15 will have both be the new API and 16 will drop the old API
731
- filesystem generate_deprecated_windows_view(fs)
715
+ # Chef 16 added 'filesystem2'
716
+ # In Chef 17 we made 'filesystem' and 'filesystem2' match (both new-style)
717
+ # In Chef 18 we will drop 'filesystem2'
718
+ filesystem fs_data
732
719
  filesystem2 fs_data
733
720
  end
734
721
  end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright:: Copyright (c) Chef Software Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ #
19
+ Ohai.plugin(:Habitat) do
20
+ provides "habitat"
21
+
22
+ def habitat_binary
23
+ @habitat_binary ||= which("hab")
24
+ end
25
+
26
+ def fetch_habitat_version
27
+ shell_out([habitat_binary, "-V"]).stdout.gsub(/hab\s*/, "").strip
28
+ rescue Ohai::Exceptions::Exec
29
+ logger.trace("Plugin Habitat: Unable to determine the installed version of Habitat, skipping collection.")
30
+ end
31
+
32
+ def fetch_habitat_packages
33
+ shell_out([habitat_binary, "pkg", "list", "--all"]).stdout.split.sort.select { |pkg| pkg.match?(%r{.*/.*/.*/.*}) }
34
+ rescue Ohai::Exceptions::Exec
35
+ logger.trace("Plugin Habitat: Unable to determine the installed Habitat packages, skipping collection.")
36
+ end
37
+
38
+ def load_habitat_service_via_cli(status_stdout)
39
+ # package type desired state elapsed (s) pid group
40
+ # core/httpd/2.4.35/20190307151146 standalone up up 158169 1410 httpd.default
41
+ @services = []
42
+ status_stdout.each_line do |line|
43
+ fields = line.split(/\s+/)
44
+ next unless fields[0].match?(%r{.*/.*/.*/.*}) # ignore header line
45
+
46
+ service = {}
47
+ service[:identity] = fields[0]
48
+ service[:topology] = fields[1]
49
+ service[:state_desired] = fields[2]
50
+ service[:state_actual] = fields[2]
51
+ (@services).push(service)
52
+ end
53
+ @services
54
+ end
55
+
56
+ def fetch_habitat_services
57
+ services_shell_out = shell_out([habitat_binary, "svc", "status"]).stdout
58
+ load_habitat_service_via_cli(services_shell_out) if services_shell_out
59
+ rescue Ohai::Exceptions::Exec
60
+ logger.trace("Plugin Habitat: Unable to determine the installed Habitat services, skipping collection.")
61
+ end
62
+
63
+ collect_data(:default) do
64
+ if habitat_binary
65
+ habitat Mash.new
66
+ habitat["version"] = fetch_habitat_version
67
+ habitat["packages"] = fetch_habitat_packages
68
+ habitat["services"] = fetch_habitat_services
69
+ else
70
+ logger.trace("Plugin Habitat: Could not find hab binary. Skipping plugin.")
71
+ end
72
+ end
73
+ end
@@ -26,7 +26,11 @@
26
26
  # limitations under the License.
27
27
  #
28
28
 
29
+ require_relative "../mixin/network_helper"
30
+
29
31
  Ohai.plugin(:Hostname) do
32
+ include Ohai::Mixin::NetworkHelper
33
+
30
34
  provides "domain", "hostname", "fqdn", "machinename"
31
35
 
32
36
  # hostname : short hostname
@@ -42,17 +46,8 @@ Ohai.plugin(:Hostname) do
42
46
  end
43
47
 
44
48
  # forward and reverse lookup to canonicalize FQDN (hostname -f equivalent)
45
- # this is ipv6-safe, works on ruby 1.8.7+
46
49
  def resolve_fqdn
47
- require "socket" unless defined?(Socket)
48
- require "ipaddr" unless defined?(IPAddr)
49
-
50
- hostname = from_cmd("hostname")
51
- addrinfo = Socket.getaddrinfo(hostname, nil).first
52
- iaddr = IPAddr.new(addrinfo[3])
53
- Socket.gethostbyaddr(iaddr.hton)[0]
54
- rescue
55
- nil
50
+ canonicalize_hostname_with_retries(from_cmd("hostname"))
56
51
  end
57
52
 
58
53
  def collect_domain
@@ -98,58 +93,21 @@ Ohai.plugin(:Hostname) do
98
93
  collect_data(:darwin) do
99
94
  hostname from_cmd("hostname -s")
100
95
  machinename from_cmd("hostname")
101
- begin
102
- our_fqdn = resolve_fqdn
103
- # Sometimes... very rarely, but sometimes, 'hostname --fqdn' falsely
104
- # returns a blank string. WTF.
105
- if our_fqdn.nil? || our_fqdn.empty?
106
- logger.trace("Plugin Hostname: hostname returned an empty string, retrying once.")
107
- our_fqdn = resolve_fqdn
108
- end
109
-
110
- if our_fqdn.nil? || our_fqdn.empty?
111
- logger.trace("Plugin Hostname: hostname returned an empty string twice and will" +
112
- "not be set.")
113
- else
114
- fqdn our_fqdn
115
- end
116
- rescue
117
- logger.trace(
118
- "Plugin Hostname: hostname returned an error, probably no domain set"
119
- )
120
- end
96
+ fqdn resolve_fqdn
121
97
  domain collect_domain
122
98
  end
123
99
 
124
100
  collect_data(:freebsd) do
125
101
  hostname from_cmd("hostname -s")
126
102
  machinename from_cmd("hostname")
127
- fqdn from_cmd("hostname -f")
103
+ fqdn resolve_fqdn
128
104
  collect_domain
129
105
  end
130
106
 
131
107
  collect_data(:linux) do
132
108
  hostname from_cmd("hostname -s")
133
109
  machinename from_cmd("hostname")
134
- begin
135
- our_fqdn = from_cmd("hostname --fqdn")
136
- # Sometimes... very rarely, but sometimes, 'hostname --fqdn' falsely
137
- # returns a blank string. WTF.
138
- if our_fqdn.nil? || our_fqdn.empty?
139
- logger.trace("Plugin Hostname: hostname --fqdn returned an empty string, retrying " +
140
- "once.")
141
- our_fqdn = from_cmd("hostname --fqdn")
142
- end
143
-
144
- if our_fqdn.nil? || our_fqdn.empty?
145
- logger.trace("Plugin Hostname: hostname --fqdn returned an empty string twice and " +
146
- "will not be set.")
147
- else
148
- fqdn our_fqdn
149
- end
150
- rescue
151
- logger.trace("Plugin Hostname: hostname --fqdn returned an error, probably no domain set")
152
- end
110
+ fqdn resolve_fqdn
153
111
  domain collect_domain
154
112
  end
155
113
 
@@ -169,22 +127,7 @@ Ohai.plugin(:Hostname) do
169
127
 
170
128
  hostname host["dnshostname"].to_s
171
129
  machinename host["name"].to_s
172
-
173
- info = Socket.gethostbyname(Socket.gethostname)
174
- if /.+?\.(.*)/.match?(info.first)
175
- fqdn info.first
176
- else
177
- # host is not in dns. optionally use:
178
- # C:\WINDOWS\system32\drivers\etc\hosts
179
- info[3..info.length].reverse_each do |addr|
180
- hostent = Socket.gethostbyaddr(addr)
181
- if /.+?\.(.*)/.match?(hostent.first)
182
- fqdn hostent.first
183
- break
184
- end
185
- end
186
- fqdn info.first unless fqdn
187
- end
130
+ fqdn canonicalize_hostname_with_retries(Socket.gethostname)
188
131
  domain collect_domain
189
132
  end
190
133
  end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Author:: Song Liu <song@kernel.org>
4
+ # Copyright:: Copyright (c) 2021 Facebook, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ Ohai.plugin(:Livepatch) do
21
+ provides "livepatch"
22
+
23
+ collect_data(:linux) do
24
+ if file_exist?("/sys/kernel/livepatch")
25
+ patches = Mash.new
26
+ dir_glob("/sys/kernel/livepatch/*").each do |livepatch_dir|
27
+ dir = File.basename(livepatch_dir)
28
+ patches[dir] = Mash.new
29
+ %w{enabled transition}.each do |check|
30
+ if file_exist?("/sys/kernel/livepatch/#{dir}/#{check}")
31
+ file_open("/sys/kernel/livepatch/#{dir}/#{check}") { |f| patches[dir][check] = f.read_nonblock(1024).strip }
32
+ end
33
+ end
34
+ livepatch patches
35
+ end
36
+ end
37
+ end
38
+ end