@@ -104,6 +104,51 @@ def def_to_hash(definition, delimiter=':')
104
104
end
105
105
106
106
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
+
107
152
108
153
virtual_type = Facter . value ( 'virtual' )
109
154
lshw_cmd = ( virtual_type && virtual_type == 'kvm' ) ? 'lshw -disable dmi' : 'lshw'
@@ -112,52 +157,50 @@ def def_to_hash(definition, delimiter=':')
112
157
# process the results from lshw -c memory
113
158
memory = def_to_hash ( lshw_c_memory_str )
114
159
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 }
123
166
end
124
167
end
168
+ end
125
169
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 |
128
174
if memory [ 'memory' ] . has_key? fact
129
175
val = memory [ 'memory' ] [ fact ]
130
176
Facter . add ( "mk_hw_mem_#{ fact } " ) do
131
177
setcode { val }
132
178
end
133
179
end
134
180
end
181
+ end
135
182
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 ) }
139
200
end
140
- rescue Exception => e
141
- puts "Exception: #{ e } "
142
201
end
143
202
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