Module | Facter::Memory |
In: |
lib/facter/util/memory.rb
|
memory.rb Support module for memory related facts
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation (version 2 of the License) This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA
# File lib/facter/util/memory.rb, line 19 19: def self.meminfo_number(tag) 20: memsize = "" 21: Thread::exclusive do 22: size, scale = [0, ""] 23: File.readlines("/proc/meminfo").each do |l| 24: size, scale = [$1.to_f, $2] if l =~ /^#{tag}:\s+(\d+)\s+(\S+)/ 25: # MemoryFree == memfree + cached + buffers 26: # (assume scales are all the same as memfree) 27: if tag == "MemFree" && 28: l =~ /^(?:Buffers|Cached):\s+(\d+)\s+(?:\S+)/ 29: size += $1.to_f 30: end 31: end 32: memsize = scale_number(size, scale) 33: end 34: 35: memsize 36: end
# File lib/facter/util/memory.rb, line 38 38: def self.scale_number(size, multiplier) 39: suffixes = ['', 'kB', 'MB', 'GB', 'TB'] 40: 41: s = suffixes.shift 42: while s != multiplier 43: s = suffixes.shift 44: end 45: 46: while size > 1024.0 47: size /= 1024.0 48: s = suffixes.shift 49: end 50: 51: return "%.2f %s" % [size, s] 52: end