Skip to content

Commit 3a4c3a6

Browse files
author
Gerard Hickey
committed
Fixes to bus and mem facter modules
1 parent 537c8a8 commit 3a4c3a6

File tree

2 files changed

+79
-36
lines changed

2 files changed

+79
-36
lines changed

hanlon_microkernel/facter/hnl_mk_bus.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
%w{description product vendor version serial physical_id}.each do |fact|
3535
if results['core'].has_key? fact
3636
val = results['core'][fact]
37-
Facter.add("mk_hw_bus_core_#{fact}") do
37+
Facter.add("mk_hw_bus_#{fact}") do
3838
setcode { val }
3939
end
4040
end

hanlon_microkernel/facter/hnl_mk_mem.rb

Lines changed: 78 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,51 @@ def def_to_hash(definition, delimiter=':')
104104
end
105105

106106

107+
def calculate_bytes(label)
108+
label =~ /^(\d+)([a-zA-Z])/
109+
size = $1.to_i
110+
unit = $2.upcase
111+
case unit
112+
when 'T'
113+
size * (1024 ** 4)
114+
when 'G'
115+
size * 1073741824
116+
when 'M'
117+
size * 1048576
118+
when 'K'
119+
size * 1024
120+
end
121+
end
122+
123+
124+
# now "right size" the memory -- starting at MB
125+
def pretty_memory_size(bytes)
126+
case bytes
127+
when 0..1052266988 # 0 - a bit over 980MB
128+
size = bytes / 1048576
129+
if size < 1
130+
"#{size.round(2)}MB"
131+
else
132+
"#{size.round}MB"
133+
end
134+
when 1052266988..(1024 ** 4 - 5368709120) # 980MB - (1TB - 5GB)
135+
size = bytes / 1073741824
136+
if size < 1
137+
"#{size.round(2)}GB"
138+
else
139+
"#{size.round}GB"
140+
end
141+
else
142+
size = bytes / (1024 ** 4)
143+
if size < 1
144+
"1TB"
145+
else
146+
"#{size.round}TB"
147+
end
148+
end
149+
end
150+
151+
107152

108153
virtual_type = Facter.value('virtual')
109154
lshw_cmd = (virtual_type && virtual_type == 'kvm') ? 'lshw -disable dmi' : 'lshw'
@@ -112,52 +157,50 @@ def def_to_hash(definition, delimiter=':')
112157
# process the results from lshw -c memory
113158
memory = def_to_hash(lshw_c_memory_str)
114159

115-
begin
116-
# Create the facts for the firmware info
117-
%w{description vendor physical_id version date size capabilities capacity}.each do |fact|
118-
if memory['firmware'].has_key? fact
119-
val = memory['firmware'][fact]
120-
Facter.add("mk_hw_fw_#{fact}") do
121-
setcode { val }
122-
end
160+
# Create the facts for the firmware info
161+
%w{description vendor physical_id version date size capabilities capacity}.each do |fact|
162+
if memory['firmware'].has_key? fact
163+
val = memory['firmware'][fact]
164+
Facter.add("mk_hw_fw_#{fact}") do
165+
setcode { val }
123166
end
124167
end
168+
end
125169

126-
# Create the facts for the memory info
127-
%w{description physical_id slot size }.each do |fact|
170+
# Create the facts for the memory info
171+
if memory.has_key? 'memory'
172+
# not all systems define top level memory summary
173+
%w{description physical_id slot}.each do |fact|
128174
if memory['memory'].has_key? fact
129175
val = memory['memory'][fact]
130176
Facter.add("mk_hw_mem_#{fact}") do
131177
setcode { val }
132178
end
133179
end
134180
end
181+
end
135182

136-
slot_info = memory['memory']['bank_array'].select {|entry| entry['size']}
137-
Facter.add("mk_hw_mem_slot_info") do
138-
setcode { slot_info }
183+
# Create a special fact for memory size
184+
if memory.has_key? 'memory' and memory['memory'].has_key? 'size'
185+
val = memory['memory']['size']
186+
else
187+
# without size specified, we need to add all the banks together
188+
bytes = 0
189+
memory['memory_array'].each do |procmem|
190+
procmem.has_key? 'bank_array' and procmem['bank_array'].each do |bank|
191+
if bank.has_key? 'size'
192+
bytes += calculate_bytes(bank['size'])
193+
end
194+
end
195+
end
196+
197+
# finally... create the fact
198+
Facter.add("mk_hw_mem_size") do
199+
setcode { pretty_memory_size(bytes) }
139200
end
140-
rescue Exception => e
141-
puts "Exception: #{e}"
142201
end
143202

144-
# # next, the memory information (including firmware, system memory, and caches)
145-
# lshw_c_memory_str = %x[sudo #{lshw_cmd} -c memory 2> /dev/null]
146-
# hash_map = lshw_output_to_hash(lshw_c_memory_str, ":")
147-
# add_hash_to_facts!(hash_map, facts_map, mk_fct_excl_pattern, "mk_hw_mem", /cache_array/)
148-
# # and add a set of facts from this memory information as top-level facts in the
149-
# # facts_map so that we can use them later to tag nodes
150-
# fields_to_include = ["description", "vendor", "physical_id", "version",
151-
# "date", "size", "capabilities", "capacity"]
152-
# add_flattened_hash_to_facts!(hash_map["firmware"], facts_map,
153-
# "mk_hw_fw", fields_to_include)
154-
# fields_to_include = ["description", "physical_id", "slot", "size"]
155-
# add_flattened_hash_to_facts!(hash_map["memory"], facts_map,
156-
# "mk_hw_mem", fields_to_include)
157-
# # grab the same meta-data from the slots that aren't empty from the "bank_array"
158-
# # field of our hash_map
159-
# non_empty_slot_info = hash_map["bank_array"].select{ |slot_entry| slot_entry['size'] }
160-
# add_flattened_array_to_facts!(non_empty_slot_info, facts_map,
161-
# "mk_hw_mem_slot", fields_to_include)
162-
163-
203+
slot_info = memory['memory']['bank_array'].select {|entry| entry['size']}
204+
Facter.add("mk_hw_mem_slot_info") do
205+
setcode { slot_info }
206+
end

0 commit comments

Comments
 (0)