class Datacenter::Machine
Attributes
shell[R]
Public Class Methods
new(shell=nil)
click to toggle source
# File lib/datacenter/machine.rb, line 6 def initialize(shell=nil) @shell = shell || Shell::Local.new end
Public Instance Methods
cores()
click to toggle source
# File lib/datacenter/machine.rb, line 29 def cores shell.run('nproc').to_i end
cpu()
click to toggle source
# File lib/datacenter/machine.rb, line 25 def cpu cpuinfo['model name'] end
disk_partitions()
click to toggle source
# File lib/datacenter/machine.rb, line 57 def disk_partitions partitions.map { |p| DiskPartition.new p } end
ips()
click to toggle source
# File lib/datacenter/machine.rb, line 10 def ips shell.run('ifconfig') .split("\n") .select { |l| l.strip.start_with? 'inet ' } .map { |l| l.match(/(([0-9]+\.){3}[0-9]+)/).to_s } end
memory()
click to toggle source
# File lib/datacenter/machine.rb, line 33 def memory meminfo['MemTotal'].to_i / 1024.0 end
memory_free()
click to toggle source
# File lib/datacenter/machine.rb, line 37 def memory_free meminfo['MemFree'].to_i / 1024.0 end
memory_used()
click to toggle source
# File lib/datacenter/machine.rb, line 41 def memory_used memory - memory_free end
name()
click to toggle source
# File lib/datacenter/machine.rb, line 17 def name shell.run('hostname').strip end
os()
click to toggle source
# File lib/datacenter/machine.rb, line 21 def os @os ||= OS.new shell end
processes(filter='')
click to toggle source
# File lib/datacenter/machine.rb, line 61 def processes(filter='') if filter.empty? command = 'ps aux' start = 1 else command = "ps aux | grep \"#{filter}\" | grep -v grep" start = 0 end shell.run(command) .split("\n")[start..-1] .map { |l| Process.new l.split[1].to_i, shell } end
swap()
click to toggle source
# File lib/datacenter/machine.rb, line 45 def swap meminfo['SwapTotal'].to_i / 1024.0 end
swap_free()
click to toggle source
# File lib/datacenter/machine.rb, line 49 def swap_free meminfo['SwapFree'].to_i / 1024.0 end
swap_used()
click to toggle source
# File lib/datacenter/machine.rb, line 53 def swap_used swap - swap_free end
top(order, n=10)
click to toggle source
# File lib/datacenter/machine.rb, line 74 def top(order, n=10) mappings = {memory: 'rss', pid: 'pid', cpu: '%cpu'} shell.run("ps aux --sort -#{mappings[order]} | head -n #{n+1}") .split("\n")[1..-1] .map { |l| Process.new l.split[1], shell } end
Private Instance Methods
cpuinfo()
click to toggle source
# File lib/datacenter/machine.rb, line 104 def cpuinfo Hash[shell.run('cat /proc/cpuinfo').split("\n").select { |e| e.length > 0 }.map { |e| e.split(':').map(&:strip) }] end
meminfo()
click to toggle source
# File lib/datacenter/machine.rb, line 100 def meminfo Hash[shell.run('cat /proc/meminfo').split("\n").map { |e| e.split(':').map(&:strip) }] end
partitions()
click to toggle source
# File lib/datacenter/machine.rb, line 83 def partitions shell.run('df -lT') .scan(/^\/dev.*/) .map do |p| line = p.split { filesystem: line[0], type: line[1], size: line[2].to_f / 1024, used: line[3].to_f / 1024, available: line[4].to_f / 1024, used_percentage: line[5].to_f, mounted: line[6] } end end