Sha256: 1dd7cd216974dd3ad2c5b9b9fc01cf25459f62583bbbcbf53061f50ffc6684e4

Contents?: true

Size: 1.93 KB

Versions: 25

Compression:

Stored size: 1.93 KB

Contents

#
# Author:: Patrick Collins (<pat@burned.com>)
# Copyright:: Copyright (c) 2013 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

Ohai.plugin(:Memory) do
  provides 'memory'

  collect_data(:darwin) do
    memory Mash.new

    installed_memory = shell_out("sysctl -n hw.memsize").stdout.to_i / 1024 / 1024.0
    memory[:total] = "#{installed_memory.to_i}MB"

    total_consumed = 0
    active = 0
    inactive = 0
    vm_stat = shell_out("vm_stat").stdout
    vm_stat_match = /page size of (\d+) bytes/.match(vm_stat)
    page_size = if vm_stat_match and vm_stat_match[1]
      vm_stat_match[1].to_i
    else
      4096
    end
    vm_stat.split("\n").each do |line|
      ['wired down', 'active', 'inactive'].each do |match|
        unless line.index("Pages #{match}:").nil?
          pages = line.split.last.to_i
          megabyte_val = (pages * page_size) / 1024 / 1024.0
          total_consumed += megabyte_val
          case match
          when 'wired down'
            active += megabyte_val.to_i
          when 'active'
            active += megabyte_val.to_i
          when 'inactive'
            inactive += megabyte_val.to_i
          end
        end
      end
    end

    memory[:active] = "#{active}MB" if active > 0
    memory[:inactive] = "#{inactive}MB" if inactive > 0

    free_memory = installed_memory - total_consumed
    memory[:free] = "#{free_memory.to_i}MB" if total_consumed > 0
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
ohai-8.8.1 lib/ohai/plugins/darwin/memory.rb
ohai-8.8.0 lib/ohai/plugins/darwin/memory.rb
ohai-8.7.0 lib/ohai/plugins/darwin/memory.rb
ohai-8.6.0 lib/ohai/plugins/darwin/memory.rb
ohai-8.6.0.alpha.1 lib/ohai/plugins/darwin/memory.rb
ohai-8.6.0.alpha.0 lib/ohai/plugins/darwin/memory.rb
ohai-8.5.0 lib/ohai/plugins/darwin/memory.rb
ohai-8.4.0 lib/ohai/plugins/darwin/memory.rb
ohai-8.3.0 lib/ohai/plugins/darwin/memory.rb
ohai-8.3.0.rc.0 lib/ohai/plugins/darwin/memory.rb
ohai-8.2.0 lib/ohai/plugins/darwin/memory.rb
ohai-8.1.1 lib/ohai/plugins/darwin/memory.rb
ohai-8.1.0 lib/ohai/plugins/darwin/memory.rb
ohai-7.4.1 lib/ohai/plugins/darwin/memory.rb
ohai-8.0.1 lib/ohai/plugins/darwin/memory.rb
ohai-7.6.0.rc.1 lib/ohai/plugins/darwin/memory.rb
ohai-7.6.0.rc.0 lib/ohai/plugins/darwin/memory.rb
ohai-7.4.0 lib/ohai/plugins/darwin/memory.rb
ohai-7.4.0.rc.0 lib/ohai/plugins/darwin/memory.rb
ohai-7.2.4 lib/ohai/plugins/darwin/memory.rb