Module: Facter::Memory

Defined in:
lib/facter/util/memory.rb

Class Method Summary collapse

Class Method Details

.mem_free(kernel = Facter.value(:kernel)) ⇒ Object



80
81
82
83
# File 'lib/facter/util/memory.rb', line 80

def self.mem_free(kernel = Facter.value(:kernel))
  output = mem_free_info(kernel)
  scale_mem_free_value output, kernel
end

.mem_free_info(kernel = Facter.value(:kernel)) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/facter/util/memory.rb', line 85

def self.mem_free_info(kernel = Facter.value(:kernel))
  case kernel
  when /OpenBSD/i, /SunOS/i, /Dragonfly/i
    vmstat_find_free_memory()
  when /FreeBSD/i
    vmstat_find_free_memory(["-H"])
  when /Darwin/i
    vmstat_darwin_find_free_memory()
  when /AIX/i
    svmon_aix_find_free_memory()
  end
end

.mem_size(kernel = Facter.value(:kernel)) ⇒ Object



109
110
111
112
# File 'lib/facter/util/memory.rb', line 109

def self.mem_size(kernel = Facter.value(:kernel))
  output = mem_size_info(kernel)
  scale_mem_size_value output, kernel
end

.mem_size_info(kernel = Facter.value(:kernel)) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/facter/util/memory.rb', line 114

def self.mem_size_info(kernel = Facter.value(:kernel))
  case kernel
  when /Dragonfly/i, /FreeBSD/i, /OpenBSD/i
    Facter::Util::POSIX.sysctl("hw.physmem")
  when /Darwin/i
    Facter::Util::POSIX.sysctl("hw.memsize")
  when /AIX/i
    if Facter::Core::Execution.exec("/usr/bin/svmon -O unit=KB") =~ /^memory\s+(\d+)\s+/
      $1
    end
  end
end

.meminfo_number(tag) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/facter/util/memory.rb', line 8

def self.meminfo_number(tag)
  memsize = ""
  size = [0]
  File.readlines("/proc/meminfo").each do |l|
    size = $1.to_f if l =~ /^#{tag}:\s+(\d+)\s+\S+/
    if tag == "MemFree" &&
        l =~ /^(?:Buffers|Cached):\s+(\d+)\s+\S+/
      size += $1.to_f
    end
  end
  size / 1024.0
end

.parse_swap(output, kernel = Facter.value(:kernel), size_or_free = :size) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/facter/util/memory.rb', line 163

def self.parse_swap (output, kernel = Facter.value(:kernel), size_or_free = :size)
  value_in_mb = 0.0
  value = 0
  is_size = size_or_free == :size
  unless output.nil?
    output.each_line do |line|
      value += parse_swap_line(line, kernel, is_size)
    end
  end      
  value_in_mb = scale_swap_value(value, kernel)
end

.parse_swap_line(line, kernel, is_size) ⇒ Object

There is a lot of duplication here because of concern over being able to add new platforms in a reasonable manner. For all of these platforms the first regex corresponds to the swap size value and the second corresponds to the swap free value, but this may not always be the case. In Ruby 1.9.3 it is possible to give these names, but sadly 1.8.7 does not support this.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/facter/util/memory.rb', line 181

def self.parse_swap_line(line, kernel, is_size)
  case kernel
  when /AIX/i
    if line =~ /^\/\S+\s.*\s+(\S+)MB\s+(\S+)MB/
      (is_size) ? $1.to_i : $2.to_i
    else
      0
    end
  when /OpenBSD/i
    if line =~ /^total: (\d+) (\d+)-blocks allocated, (\d+) used, (\d+) available$/
      (is_size) ? ($1.to_i * $2.to_i) : ($4.to_i * $2.to_i)
    else
      0
    end
  when /FreeBSD/i
    if line =~ /\S+\s+(\d+)\s+\d+\s+(\d+)\s+\d+%$/
      (is_size) ? $1.to_i : $2.to_i
    else
      0
    end
  when /Darwin/i
    if line =~ /total\s=\s(\S+)M\s+used\s=\s\S+M\s+free\s=\s(\S+)M\s/
      (is_size) ? $1.to_i : $2.to_i
    else
      0
    end
  when /SunOS/i
    if line =~ /^\S+\s.*\s+(\d+)\s+(\d+)$/
      (is_size) ? $1.to_i : $2.to_i
    else
      0
    end
  end
end

.scale_mem_free_value(value, kernel) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/facter/util/memory.rb', line 98

def self.scale_mem_free_value (value, kernel)
   case kernel
   when /OpenBSD/i, /FreeBSD/i, /SunOS/i, /Dragonfly/i, /AIX/i
     value.to_f / 1024.0
   when /Darwin/i
     value.to_f / 1024.0 / 1024.0
   else
     value.to_f
   end
end

.scale_mem_size_value(value, kernel) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/facter/util/memory.rb', line 127

def self.scale_mem_size_value(value, kernel)
  case kernel
  when /OpenBSD/i, /FreeBSD/i, /Darwin/i, /Dragonfly/i
    value.to_f / 1024.0 / 1024.0
  when /AIX/i
    value.to_f / 1024.0
  else
    value.to_f
  end
end

.scale_number(size, multiplier) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/facter/util/memory.rb', line 21

def self.scale_number(size, multiplier)
  suffixes = ['', 'kB', 'MB', 'GB', 'TB']

  s = suffixes.shift
  while s != multiplier
    s = suffixes.shift
  end

  while size > 1024.0
    size /= 1024.0
    s = suffixes.shift
  end

  "%.2f %s" % [size, s]
end

.scale_swap_value(value, kernel) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/facter/util/memory.rb', line 216

def self.scale_swap_value(value, kernel)
  case kernel
  when /FreeBSD/i
    value.to_f / 1024.0
  when /OpenBSD/i
    value.to_f / 1024.0 / 1024.0
  when /SunOS/i
    value.to_f / 2 / 1024.0
  else
    value.to_f
  end
end

.svmon_aix_find_free_memoryObject

on AIX use svmon to get the free memory: it’s the third value on the line starting with memory svmon can be run by non root users



75
76
77
78
# File 'lib/facter/util/memory.rb', line 75

def self.svmon_aix_find_free_memory()
  Facter::Core::Execution.exec("/usr/bin/svmon -O unit=KB") =~ /^memory\s+\d+\s+\d+\s+(\d+)\s+/
  $1
end

.swap_free(kernel = Facter.value(:kernel)) ⇒ Object



143
144
145
146
# File 'lib/facter/util/memory.rb', line 143

def self.swap_free(kernel = Facter.value(:kernel))
  output = swap_info(kernel)
  parse_swap output, kernel, :free if output
end

.swap_info(kernel = Facter.value(:kernel)) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/facter/util/memory.rb', line 148

def self.swap_info(kernel = Facter.value(:kernel))
  case kernel
  when /AIX/i
    (Facter.value(:id) == "root") ? Facter::Core::Execution.exec('swap -l 2>/dev/null') : nil
  when /OpenBSD/i
    Facter::Core::Execution.exec('swapctl -s')
  when /FreeBSD/i
    Facter::Core::Execution.exec('swapinfo -k')
  when /Darwin/i
    Facter::Util::POSIX.sysctl('vm.swapusage')
  when /SunOS/i
    Facter::Core::Execution.exec('/usr/sbin/swap -l 2>/dev/null')
  end
end

.swap_size(kernel = Facter.value(:kernel)) ⇒ Object



138
139
140
141
# File 'lib/facter/util/memory.rb', line 138

def self.swap_size(kernel = Facter.value(:kernel))
  output = swap_info(kernel)
  parse_swap output, kernel, :size if output
end

.vmstat_darwin_find_free_memoryObject

Darwin had to be different. It’s generally opaque with how much RAM it is using, and this figure could be improved upon too I fear. Parses the output of “vm_stat”, takes the pages free & pages speculative and multiples that by the page size (also given in output). Ties in with what activity monitor outputs for free memory.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/facter/util/memory.rb', line 51

def self.vmstat_darwin_find_free_memory()

  memfree = 0
  pagesize = 0
  memspecfree = 0

  vmstats = Facter::Core::Execution.exec('vm_stat')
  vmstats.each_line do |vmline|
    case
    when vmline =~ /page\ssize\sof\s(\d+)\sbytes/
      pagesize = $1.to_i
    when vmline =~ /^Pages\sfree:\s+(\d+)\./
      memfree = $1.to_i
    when vmline =~ /^Pages\sspeculative:\s+(\d+)\./
      memspecfree = $1.to_i
    end
  end

  freemem = ( memfree + memspecfree ) * pagesize
end

.vmstat_find_free_memory(args = []) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/facter/util/memory.rb', line 37

def self.vmstat_find_free_memory(args = [])
  cmd = 'vmstat'
  cmd += (' ' + args.join(' ')) unless args.empty?
  row = Facter::Core::Execution.exec(cmd).split("\n")[-1]
  if row =~ /^\s*\d+\s*\d+\s*\d+\s*\d+\s*(\d+)/
    memfree = $1
  end
end