Class: Datacenter::Machine
- Inherits:
-
Object
- Object
- Datacenter::Machine
- Defined in:
- lib/datacenter/machine.rb
Defined Under Namespace
Classes: DiskPartition
Instance Attribute Summary collapse
-
#shell ⇒ Object
readonly
Returns the value of attribute shell.
Instance Method Summary collapse
- #cores ⇒ Object
- #cpu ⇒ Object
- #disk_partitions ⇒ Object
-
#initialize(shell = nil) ⇒ Machine
constructor
A new instance of Machine.
- #ips ⇒ Object
- #memory ⇒ Object
- #memory_free ⇒ Object
- #memory_used ⇒ Object
- #name ⇒ Object
- #os ⇒ Object
- #processes(filter = '') ⇒ Object
- #swap ⇒ Object
- #swap_free ⇒ Object
- #swap_used ⇒ Object
- #top(order, n = 10) ⇒ Object
Constructor Details
#initialize(shell = nil) ⇒ Machine
Returns a new instance of Machine.
6 7 8 |
# File 'lib/datacenter/machine.rb', line 6 def initialize(shell=nil) @shell = shell || Shell::Local.new end |
Instance Attribute Details
#shell ⇒ Object (readonly)
Returns the value of attribute shell.
4 5 6 |
# File 'lib/datacenter/machine.rb', line 4 def shell @shell end |
Instance Method Details
#cores ⇒ Object
29 30 31 |
# File 'lib/datacenter/machine.rb', line 29 def cores shell.run('nproc').to_i end |
#cpu ⇒ Object
25 26 27 |
# File 'lib/datacenter/machine.rb', line 25 def cpu cpuinfo['model name'] end |
#disk_partitions ⇒ Object
57 58 59 |
# File 'lib/datacenter/machine.rb', line 57 def disk_partitions partitions.map { |p| DiskPartition.new p } end |
#ips ⇒ Object
10 11 12 13 14 15 |
# 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 ⇒ Object
33 34 35 |
# File 'lib/datacenter/machine.rb', line 33 def memory meminfo['MemTotal'].to_i / 1024.0 end |
#memory_free ⇒ Object
37 38 39 |
# File 'lib/datacenter/machine.rb', line 37 def memory_free meminfo['MemFree'].to_i / 1024.0 end |
#memory_used ⇒ Object
41 42 43 |
# File 'lib/datacenter/machine.rb', line 41 def memory_used memory - memory_free end |
#name ⇒ Object
17 18 19 |
# File 'lib/datacenter/machine.rb', line 17 def name shell.run('hostname').strip end |
#os ⇒ Object
21 22 23 |
# File 'lib/datacenter/machine.rb', line 21 def os @os ||= OS.new shell end |
#processes(filter = '') ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# 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 ⇒ Object
45 46 47 |
# File 'lib/datacenter/machine.rb', line 45 def swap meminfo['SwapTotal'].to_i / 1024.0 end |
#swap_free ⇒ Object
49 50 51 |
# File 'lib/datacenter/machine.rb', line 49 def swap_free meminfo['SwapFree'].to_i / 1024.0 end |
#swap_used ⇒ Object
53 54 55 |
# File 'lib/datacenter/machine.rb', line 53 def swap_used swap - swap_free end |
#top(order, n = 10) ⇒ Object
74 75 76 77 78 79 |
# 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 |