Skip to content

Commit cc39d27

Browse files
author
Gerard Hickey
committed
Facter modlue for gathering cpu facts
1 parent 68921f8 commit cc39d27

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Gathers hardware-related facts from the underlying system (used by the
2+
# hnl_mk_registration_manager to gather these sorts of facts in order to
3+
# supplement the facts gathered using Facter during the node registration
4+
# process)
5+
#
6+
#
7+
8+
# add the '/usr/local/lib/ruby' directory to the LOAD_PATH
9+
# (this is where the hanlon_microkernel module files are placed by
10+
# our Dockerfile)
11+
$LOAD_PATH.unshift('/usr/local/lib/ruby')
12+
13+
require 'facter'
14+
15+
16+
17+
virtual_type = Facter.value('virtual')
18+
lshw_cmd = (virtual_type && virtual_type == 'kvm') ? 'lshw -disable dmi' : 'lshw'
19+
lshw_c_cpu_str = %x[sudo #{lshw_cmd} -c cpu 2> /dev/null]
20+
21+
# build the results into a Hash
22+
results = {}
23+
lshw_c_cpu_str.split(/\s\s\*-/).each do |definition|
24+
unless definition.empty?
25+
lines = definition.split(/\n/)
26+
item = lines.shift.tr(':', '')
27+
attribs = Hash[ lines.collect { |l| l =~ /^\s*([^:]+):\s+(.*)\s*$/; v=$2; [$1.gsub(/\s/, '_'), v] } ]
28+
results[item] = attribs
29+
end
30+
end
31+
32+
33+
34+
results.keys.each do |cpu|
35+
results[cpu].each_pair do |k, v|
36+
Facter.add("mk_hw_#{cpu}_#{k}") do
37+
setcode { v }
38+
end
39+
end
40+
end
41+
42+
43+
# process the results from lscpu
44+
facts_to_report = %w{Architecture BogoMIPS Byte_Order CPU_MHz CPU_family CPU_op-modes
45+
L1d_cache L1i_cache L2_cache L3_cache Model Stepping Vendor_ID
46+
Virtualization}
47+
%x[lscpu].split(/\n/).each do |line|
48+
line =~ /^([^:]+):\s*(.*)\s*$/
49+
if $1
50+
key = $1
51+
val = $2
52+
key.tr!(' ', '_')
53+
key.tr!('()', '')
54+
if facts_to_report.include? key
55+
Facter.add("mk_hw_lscpu_#{key}") do
56+
setcode { val }
57+
end
58+
end
59+
end
60+
end
61+

0 commit comments

Comments
 (0)