Module: CpuMemoryStats

Defined in:
lib/modules/bsd.rb,
lib/modules/mac.rb,
lib/os_detection.rb,
lib/cpu-memory-stats.rb

Overview

CpuMemoryStats will automaticaly choose right system for getting stats author: Ondrej Bartas

Defined Under Namespace

Classes: Bsd, Mac, OSDetection

Class Method Summary collapse

Class Method Details

.getObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cpu-memory-stats.rb', line 7

def self.get
  unless self.constants.include?(OSDetection.detect)
    raise ArgumentError, "cpu-memory-stats can not work on this system, uninstall them or write new module for your system"
  else
    system = CpuMemoryStats.const_get(OSDetection.detect).new
    { 
      :cpu => system.cpu, 
      :memory => system.memory, 
      :load => system.load_avg, 
      :cpu_percent => percent(system.cpu),
      :memory_percent => percent(system.memory),
    }
  end
end

.percent(memory) ⇒ Object



22
23
24
25
# File 'lib/cpu-memory-stats.rb', line 22

def self.percent memory
  maximal = memory.inject(0){|o,i| o+i.last}
  memory.inject({}){|o,i| o[i.first] = (i.last.to_f / maximal.to_f * 100.0).round(2); o }
end