datacenter 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3fa2516a5f477daf4eeadc79df36156fbafec2a5
4
- data.tar.gz: 5c41eb5080de83a1d8e6dc0db5fd3cfc959d5de7
3
+ metadata.gz: 1e7f3298efa18d3e60beba75d49ed5b5c11fd916
4
+ data.tar.gz: b5a77c2d243c16dc4965df41af25d9968072b338
5
5
  SHA512:
6
- metadata.gz: 50b3a3429d16c823b19d5cc99e738b95fbafb1d32ee7aa14ba10f7adeb804213dc71c1033fa9137e5217e037c8e897c918a9e2bac0734ee6c4d217e0dc5a0756
7
- data.tar.gz: 5be18502eaaf0e95960cefa2ecca5d062920e377d0154f648563118d12197da4cb5a60f665531dedf33a8c2f3eb5ac7183fb07b6cc00319530ad6838a71f8547
6
+ metadata.gz: 4cedefb6e9ba69ee5ba27f84f89131a6743eec890ccc190da20ff0b678d16ed22aa601b3cdf335995c7ad44a6e0b5d1f058920124b1b2b8225aac23d624c825b
7
+ data.tar.gz: 087dffd0ab8b836d2249c6d8141dfb793abc833d5bd9e494d15c20eb97490f9d2ecec1d9c1b19e62c0b5b76d97558065865692cb5194ada63588f044688a2052
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ datacenter
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby 2.1
data/.travis.yml CHANGED
@@ -3,4 +3,8 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0
5
5
  - 2.1
6
- - jruby
6
+ - jruby
7
+ before_install:
8
+ - sudo apt-get -y install openssh-server
9
+ - ssh-keygen -q -t rsa -N "" -f ~/.ssh/id_rsa
10
+ - cp $HOME/.ssh/id_rsa.pub $HOME/.ssh/authorized_keys
data/datacenter.gemspec CHANGED
@@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency 'minitest', '~> 4.7'
26
26
  spec.add_development_dependency 'turn', '~> 0.9'
27
27
  spec.add_development_dependency 'simplecov'
28
- spec.add_development_dependency 'pry'
28
+ spec.add_development_dependency 'pry-nav'
29
29
  end
@@ -0,0 +1,37 @@
1
+ module Datacenter
2
+ class Cache
3
+
4
+ def initialize(expiration_time=nil)
5
+ @expiration_time = expiration_time
6
+ @data = {}
7
+ end
8
+
9
+ def fetch(key, &block)
10
+ set key, &block if !key?(key) || expired?(key)
11
+ get key
12
+ end
13
+
14
+ private
15
+
16
+ def get(key)
17
+ @data[key][:value]
18
+ end
19
+
20
+ def set(key, &block)
21
+ @data[key] = {
22
+ value: block.call,
23
+ fetched_at: Time.now
24
+ }
25
+ end
26
+
27
+ def key?(key)
28
+ @data.key? key
29
+ end
30
+
31
+ def expired?(key)
32
+ return false unless @expiration_time
33
+ Time.now >= @data[key][:fetched_at] + @expiration_time
34
+ end
35
+
36
+ end
37
+ end
@@ -4,7 +4,7 @@ module Datacenter
4
4
  attr_reader :shell
5
5
 
6
6
  def initialize(shell=nil)
7
- @shell = shell || Shell::Localhost.new
7
+ @shell = shell || Shell::Local.new
8
8
  end
9
9
 
10
10
  def ips
@@ -62,19 +62,19 @@ module Datacenter
62
62
  command = 'ps aux'
63
63
  start = 1
64
64
  else
65
- command = "ps aux | grep #{filter} | grep -v grep"
65
+ command = "ps aux | grep \"#{filter}\" | grep -v grep"
66
66
  start = 0
67
67
  end
68
68
  shell.run(command)
69
69
  .split("\n")[start..-1]
70
- .map { |l| Datacenter::Process.new l.split[1], self }
70
+ .map { |l| Process.new l.split[1].to_i, shell }
71
71
  end
72
72
 
73
- def top(order,n=10)
74
- mappings = { memory: 'rss', pid: 'pid', cpu: '%cpu' }
73
+ def top(order, n=10)
74
+ mappings = {memory: 'rss', pid: 'pid', cpu: '%cpu'}
75
75
  shell.run("ps aux --sort -#{mappings[order]} | head -n #{n+1}")
76
76
  .split("\n")[1..-1]
77
- .map { |l| Datacenter::Process.new l.split[1], self }
77
+ .map { |l| Process.new l.split[1], shell }
78
78
  end
79
79
 
80
80
  private
@@ -101,7 +101,7 @@ module Datacenter
101
101
  end
102
102
 
103
103
  def cpuinfo
104
- Hash[shell.run('cat /proc/cpuinfo').split("\n").select {|e| e.length>0}.map { |e| e.split(':').map(&:strip) }]
104
+ Hash[shell.run('cat /proc/cpuinfo').split("\n").select { |e| e.length > 0 }.map { |e| e.split(':').map(&:strip) }]
105
105
  end
106
106
 
107
107
  class DiskPartition
@@ -13,18 +13,14 @@ module Datacenter
13
13
  :mem_usage
14
14
  ]
15
15
 
16
- TIME_CACHE = 2
16
+ EXPIRATION_TIME = 2
17
17
 
18
- attr_reader :pid, :machine, :cache
18
+ attr_reader :pid
19
19
 
20
- def initialize(pid, machine=nil)
20
+ def initialize(pid, shell=nil)
21
21
  @pid = pid
22
- @machine = machine
23
- @cache = {:fetched=>0, :content=>[]}
24
- end
25
-
26
- def alive?
27
- !(machine.shell.run 'ls /proc').scan("\n#{pid}\n").empty?
22
+ @shell = shell || Shell::Local.new
23
+ @cache = Cache.new EXPIRATION_TIME
28
24
  end
29
25
 
30
26
  ATTRIBUTES.each do |attribute|
@@ -33,11 +29,34 @@ module Datacenter
33
29
  end
34
30
  end
35
31
 
32
+ def alive?
33
+ send_signal 0
34
+ true
35
+ rescue Errno::ESRCH
36
+ false
37
+ end
38
+
39
+ def send_signal(signal)
40
+ out = shell.run("kill -s #{signal} #{pid}")
41
+ raise Errno::ESRCH, pid.to_s if out.match 'No such process'
42
+ end
43
+
44
+ def stop
45
+ send_signal :TERM if alive?
46
+ end
47
+
48
+ def kill
49
+ send_signal :KILL if alive?
50
+ while alive?; end
51
+ end
52
+
36
53
  private
37
54
 
55
+ attr_reader :shell
56
+
38
57
  def info
39
- if cache[:content].empty? || (Time.now - cache[:fetched] > TIME_CACHE)
40
- ps = machine.shell.run('ps aux').scan(/.*#{pid}.*/)[0].split
58
+ @cache.fetch(:info) do
59
+ ps = shell.run('ps aux').scan(/.*#{pid}.*/)[0].split
41
60
  Hash.new.tap do |info|
42
61
  status = Hash[proc_file(:status).split("\n").map{ |s| s.split(':').map(&:strip) }]
43
62
  info[:name] = status['Name']
@@ -48,20 +67,15 @@ module Datacenter
48
67
  info[:virtual_memory] = ps[4].to_i / 1024.0
49
68
  info[:memory] = ps[5].to_i / 1024.0
50
69
  info[:status] = ps[7]
51
- info[:command] = ps[10..-1].reduce {|acum,e| "#{acum} #{e}"}
52
- @cache = {:fetched => Time.now, :content=>info}
70
+ info[:command] = ps[10..-1].reduce { |acum,e| "#{acum} #{e}" }
53
71
  end
54
- else
55
- cache[:content]
56
- end
72
+ end
57
73
  end
58
74
 
59
- def proc_dir
60
- "/proc/#{pid}"
75
+ def proc_file(name)
76
+ filename = File.join '/proc', pid.to_s, name.to_s
77
+ shell.run "cat #{filename}"
61
78
  end
62
79
 
63
- def proc_file(file)
64
- machine.shell.run "cat #{File.join(proc_dir, file.to_s)}"
65
- end
66
80
  end
67
81
  end
@@ -1,29 +1,56 @@
1
1
  module Datacenter
2
2
  module Shell
3
3
 
4
- class Localhost
4
+ class Local
5
5
  def run(command)
6
- `#{command}`.strip
6
+ if RUBY_ENGINE == 'jruby'
7
+ run_system command
8
+ else
9
+ run_open3 command
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def run_open3(command)
16
+ i,o,e,t = Open3.popen3 command
17
+ (o.readlines.join + e.readlines.join).strip
18
+ end
19
+
20
+ def run_system(command)
21
+ $stdout = StringIO.new
22
+ $stderr = StringIO.new
23
+
24
+ system command
25
+
26
+ [$stdout, $stderr].map do |io|
27
+ io.rewind
28
+ io.readlines.join.force_encoding('UTF-8')
29
+ end.join.strip
30
+
31
+ ensure
32
+ $stdout = STDOUT
33
+ $stderr = STDERR
7
34
  end
8
35
  end
9
36
 
10
- class Ssh
11
- attr_reader :ssh_args
37
+ class Remote
38
+ attr_reader :options
12
39
 
13
40
  def initialize(*args)
14
- @ssh_args = args
41
+ @options = args
15
42
  end
16
43
 
17
44
  def run(command)
18
45
  if @session
19
46
  @session.exec!(command).strip
20
47
  else
21
- Net::SSH.start(*@ssh_args) { |ssh| ssh.exec! command }.strip
48
+ Net::SSH.start(*options) { |ssh| ssh.exec! command }.to_s.strip
22
49
  end
23
50
  end
24
51
 
25
52
  def open
26
- @session = Net::SSH.start *@ssh_args unless @session
53
+ @session = Net::SSH.start *options unless @session
27
54
  end
28
55
 
29
56
  def close
@@ -1,3 +1,3 @@
1
1
  module Datacenter
2
- VERSION = "0.0.1"
2
+ VERSION = '0.1.0'
3
3
  end
data/lib/datacenter.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'net/ssh'
2
+ require 'open3'
2
3
 
3
4
  Dir.glob(File.expand_path('datacenter/*.rb', File.dirname(__FILE__))).sort.each { |f| require f }
@@ -0,0 +1,29 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Datacenter::Cache do
4
+
5
+ it 'Without expiration' do
6
+ cache = Datacenter::Cache.new
7
+ i = 0
8
+ 3.times do
9
+ cache.fetch(:key_1) { i += 1 }.must_equal 1
10
+ end
11
+ end
12
+
13
+ it 'Cached key' do
14
+ cache = Datacenter::Cache.new 1000
15
+ i = 0
16
+ 3.times do
17
+ cache.fetch(:key_1) { i += 1 }.must_equal 1
18
+ end
19
+ end
20
+
21
+ it 'Expired key' do
22
+ cache = Datacenter::Cache.new 0
23
+ i = 0
24
+ 1.upto(3) do |n|
25
+ cache.fetch(:key_1) { i += 1 }.must_equal n
26
+ end
27
+ end
28
+
29
+ end
data/spec/commands.yml CHANGED
@@ -1,4 +1,9 @@
1
- ---
1
+ "kill -s 0 22803": ""
2
+
3
+ "kill -s 0 -22803": "bash: kill: (-22803) - No such process"
4
+
5
+ "kill -s TERM 22803": ""
6
+
2
7
  "uname -i": x86_64
3
8
 
4
9
  "uname -r": 3.5.0-49-generic
@@ -120,7 +125,7 @@ ifconfig: |
120
125
  root 8 0.0 0.0 0 0 ? S May15 0:00 [migration/1]
121
126
  matias 22803 11.9 0.8 513268 33792 ? Sl 09:43 52:47 gnome-system-monitor
122
127
 
123
- ps aux | grep gnome-system-monitor | grep -v grep: |
128
+ ps aux | grep "gnome-system-monitor" | grep -v grep: |
124
129
  matias 22803 7.2 0.8 597980 34948 ? Sl May21 136:34 gnome-system-monitor
125
130
 
126
131
  "cat /proc/22803/status": |
@@ -146,7 +151,4 @@ ps aux | grep gnome-system-monitor | grep -v grep: |
146
151
  VmLib: 27732 kB
147
152
  VmPTE: 556 kB
148
153
  VmSwap: 0 kB
149
- Threads: 4
150
-
151
- "ls /proc": "18\n1813\n22803\n22903\n23\n2350\n23542\n2370\n246\n247\n25129\ntimer_list\nvmstat\nzoneinfo"
152
-
154
+ Threads: 4
@@ -1,2 +1,8 @@
1
1
  require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
2
8
  SimpleCov.start
@@ -0,0 +1,59 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Datacenter::Machine do
4
+
5
+ let(:machine) { Datacenter::Machine.new mock_shell }
6
+
7
+ it ('IPs') { machine.ips.must_equal %w(192.168.50.127 127.0.0.1) }
8
+
9
+ it ('Name') { machine.name.must_equal "matias" }
10
+
11
+ it 'OS' do
12
+ machine.os.name.must_equal 'GNU/Linux'
13
+ machine.os.distribution.must_equal 'Ubuntu'
14
+ machine.os.version.must_equal '12.04'
15
+ end
16
+
17
+ it ('CPU') { machine.cpu.must_equal 'Intel(R) Core(TM)2 Duo CPU E7500 @ 2.93GHz'}
18
+
19
+ it ('Cores') { machine.cores.must_equal 2 }
20
+
21
+ it ('Total Memory') { machine.memory.must_equal 3944.953125 }
22
+
23
+ it ('Free Memory') { machine.memory_free.must_equal 1262.95703125 }
24
+
25
+ it ('Used Memory') { machine.memory_used.must_equal 2681.99609375 }
26
+
27
+ it ('Total Swap') { machine.swap.must_equal 4084.99609375 }
28
+
29
+ it ('Free Swap') { machine.swap_free.must_equal 4084.99609375 }
30
+
31
+ it ('Used Swap') { machine.swap_used.must_equal 0.0}
32
+
33
+ it ('List of Proccess') { machine.processes.find {|p| p.pid == 22803}.command.must_equal 'gnome-system-monitor' }
34
+
35
+ it ('List of Proccess With Filter') { machine.processes('gnome-system-monitor').find {|p| p.pid == 22803}.name.must_equal 'gnome-system-mo' }
36
+
37
+ it ('Top by Memory') { machine.top(:memory)[7].command.must_equal 'gnome-system-monitor' }
38
+
39
+ it ('Top by CPU') { machine.top(:cpu)[0].command.must_equal 'gnome-system-monitor' }
40
+
41
+
42
+ describe 'Disk Paritions' do
43
+
44
+ it ('Size') { machine.disk_partitions.map(&:size).must_equal [296432.3671875, 286580.5556640625] }
45
+
46
+ it ('Available') { machine.disk_partitions.map(&:available).must_equal [276762.1953125, 203569.6171875] }
47
+
48
+ it ('Used') { machine.disk_partitions.map(&:used).must_equal [4612.2734375, 3864.20703125] }
49
+
50
+ it ('% Use') { machine.disk_partitions.map(&:used_percentage).must_equal [2, 15] }
51
+
52
+ it ('Filesystem') { machine.disk_partitions.map(&:filesystem).must_equal ["/dev/sda1", "/dev/sdb1"] }
53
+
54
+ it ('Type') { machine.disk_partitions.map(&:type).must_equal ["ext4", "ext4"] }
55
+
56
+ it ('Mounted on') { machine.disk_partitions.map(&:mounted).must_equal ["/", "/sys"] }
57
+ end
58
+
59
+ end
@@ -3,6 +3,7 @@ require 'minitest/autorun'
3
3
  require 'turn'
4
4
  require 'yaml'
5
5
  require 'datacenter'
6
+ require 'pry-nav'
6
7
 
7
8
  Turn.config do |c|
8
9
  c.format = :pretty
@@ -10,6 +11,10 @@ Turn.config do |c|
10
11
  c.ansi = true
11
12
  end
12
13
 
14
+ class Module
15
+ include Minitest::Spec::DSL
16
+ end
17
+
13
18
  module Datacenter
14
19
  module Shell
15
20
  class Mock
@@ -32,5 +37,26 @@ module Datacenter
32
37
  attr_reader :commands
33
38
 
34
39
  end
40
+
41
+ class Kill
42
+ def initialize(pid)
43
+ @pid = pid
44
+ end
45
+
46
+ def run(command)
47
+ if command == "kill -s 0 #{@pid}"
48
+ ''
49
+ elsif command == "kill -s KILL #{@pid}"
50
+ @pid = nil
51
+ ''
52
+ else
53
+ 'kill: No such process'
54
+ end
55
+ end
56
+ end
35
57
  end
58
+ end
59
+
60
+ class Minitest::Spec
61
+ let(:mock_shell) { Datacenter::Shell::Mock.new File.expand_path('../commands.yml', __FILE__) }
36
62
  end
data/spec/os_spec.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Datacenter::OS do
4
+
5
+ let(:os) { Datacenter::OS.new mock_shell }
6
+
7
+ it ('Name') { os.name.must_equal 'GNU/Linux' }
8
+
9
+ it ('Distribution') { os.distribution.must_equal 'Ubuntu' }
10
+
11
+ it ('Version') { os.version.must_equal '12.04' }
12
+
13
+ it ('Kernel') { os.kernel.must_equal '3.5.0-49-generic' }
14
+
15
+ it ('Platform') { os.platform.must_equal 'x86_64' }
16
+
17
+ end
@@ -0,0 +1,34 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Datacenter::Process do
4
+
5
+ let(:pid) { 22803 }
6
+ let(:process) { Datacenter::Process.new pid, mock_shell }
7
+
8
+ it ('Pid') { process.pid.must_equal pid }
9
+
10
+ it ('Alive') { process.must_be :alive? }
11
+
12
+ it ('Dead') { Datacenter::Process.new(-pid, mock_shell).wont_be :alive? }
13
+
14
+ it ('Stop') { process.stop }
15
+
16
+ it ('Kill') { Datacenter::Process.new(pid, Datacenter::Shell::Kill.new(pid)).kill }
17
+
18
+ it ('Name') { process.name.must_equal 'gnome-system-mo' }
19
+
20
+ it ('Command') { process.command.must_equal 'gnome-system-monitor' }
21
+
22
+ it ('Memory') { process.memory.must_equal 33.0 }
23
+
24
+ it ('Virtual Memory') { process.virtual_memory.must_equal 501.23828125 }
25
+
26
+ it ('% Memory') { process.mem_usage.must_equal 0.8 }
27
+
28
+ it ('% CPU') { process.cpu_usage.must_equal 11.9 }
29
+
30
+ it ('Status') { process.status.must_equal 'Sl' }
31
+
32
+ it ('User') { process.user.must_equal 'matias' }
33
+
34
+ end
@@ -0,0 +1,41 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Datacenter::Shell do
4
+
5
+ module SharedExpamples
6
+ it 'Success' do
7
+ filename = File.join '/tmp', Time.now.to_i.to_s
8
+ shell.run "echo \"test file\" > #{filename}"
9
+ shell.run("cat #{filename}").must_equal 'test file'
10
+ end
11
+
12
+ it 'Error' do
13
+ filename = '/invalid_dir/invalid_file'
14
+ shell.run("cat #{filename}").must_equal "cat: #{filename}: No such file or directory"
15
+ end
16
+ end
17
+
18
+ describe 'Local' do
19
+
20
+ let(:shell) { Datacenter::Shell::Local.new }
21
+
22
+ include SharedExpamples
23
+
24
+ end
25
+
26
+ describe 'Remote' do
27
+
28
+ let(:connection_args) { ['localhost', `whoami`.strip] }
29
+ let(:shell) { Datacenter::Shell::Remote.new *connection_args }
30
+
31
+ include SharedExpamples
32
+
33
+ it 'Block' do
34
+ Datacenter::Shell::Remote.open(*connection_args) do |shell|
35
+ shell.run('ls /').must_equal `ls /`.strip
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datacenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-27 00:00:00.000000000 Z
11
+ date: 2014-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: pry
98
+ name: pry-nav
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -117,6 +117,8 @@ extra_rdoc_files: []
117
117
  files:
118
118
  - ".coveralls.yml"
119
119
  - ".gitignore"
120
+ - ".ruby-gemset"
121
+ - ".ruby-version"
120
122
  - ".travis.yml"
121
123
  - Gemfile
122
124
  - LICENSE.txt
@@ -124,16 +126,20 @@ files:
124
126
  - Rakefile
125
127
  - datacenter.gemspec
126
128
  - lib/datacenter.rb
129
+ - lib/datacenter/cache.rb
127
130
  - lib/datacenter/machine.rb
128
131
  - lib/datacenter/os.rb
129
132
  - lib/datacenter/process.rb
130
133
  - lib/datacenter/shell.rb
131
134
  - lib/datacenter/version.rb
132
- - monitor.rb
135
+ - spec/cache_spec.rb
133
136
  - spec/commands.yml
134
137
  - spec/coverage_helper.rb
135
- - spec/datacenter_spec.rb
138
+ - spec/machine_spec.rb
136
139
  - spec/minitest_helper.rb
140
+ - spec/os_spec.rb
141
+ - spec/process_spec.rb
142
+ - spec/shell_spec.rb
137
143
  homepage: https://github.com/gabynaiman/datacenter
138
144
  licenses:
139
145
  - MIT
@@ -159,7 +165,11 @@ signing_key:
159
165
  specification_version: 4
160
166
  summary: Manage and monitor servers and processes
161
167
  test_files:
168
+ - spec/cache_spec.rb
162
169
  - spec/commands.yml
163
170
  - spec/coverage_helper.rb
164
- - spec/datacenter_spec.rb
171
+ - spec/machine_spec.rb
165
172
  - spec/minitest_helper.rb
173
+ - spec/os_spec.rb
174
+ - spec/process_spec.rb
175
+ - spec/shell_spec.rb
data/monitor.rb DELETED
@@ -1,106 +0,0 @@
1
- require_relative 'lib/datacenter.rb'
2
- require 'hirb'
3
- require 'benchmark'
4
- extend Hirb::Console
5
-
6
- ## Set up
7
- server = ARGV[0]
8
- user = ARGV[1]
9
-
10
- shell = Datacenter::Shell::Ssh.new server,user
11
- shell.open
12
- machine = Datacenter::Machine.new shell
13
-
14
- ## General Information
15
- puts "General Information \n\n"
16
-
17
- # Server name
18
- server = [{:value=>"Server : #{server} \n", :level=>0}]
19
- # Operating System
20
- os = [{:value=>"Operating System", :level=>0}]
21
- os << {:value=>"#{machine.os.name} #{machine.os.distribution}" "#{machine.os.version}", :level=>3}
22
- os << {:value=>"Kernel #{machine.os.kernel} #{machine.os.platform}", :level=>3}
23
- # Hardware
24
- hw = [{:value=>"Hardware", :level=>0}]
25
- hw << {:value=>"Memory: #{machine.memory.to_i} MB", :level=>3}
26
- hw << {:value=>"Processor: #{machine.cpu} x #{machine.cores}", :level=>3}
27
- # Status
28
- st = [{:value=>"Status", :level=>0}]
29
- st += machine.disk_partition.map { |p| Hash[:value => "Partition #{p.filesystem}: #{p.available.to_i} MB available", :level=>3] }
30
- st << {:value=>"Memory Free: #{machine.memory_free.to_i} MB", :level=>3}
31
- st << {:value=>"Swap Free: #{machine.swap_free.to_i} MB", :level=>3}
32
-
33
- info_machine = server + os + hw + st
34
- puts "#{Hirb::Helpers::Tree.render(info_machine)} \n"
35
-
36
- ## Detailed Information
37
- puts "\n\nDetailed Information \n\n"
38
-
39
- filter_size = Proc.new{|e| "#{e.to_i} MB"}
40
- filter_perc = Proc.new{|e| "#{e} %"}
41
-
42
- puts 'Operating System'
43
- os_fields = [:name, :distribution, :platform, :kernel, :version]
44
- table machine.os, :fields=>os_fields
45
-
46
- puts 'Hardware'
47
-
48
- hw_fields = [:cpu, :cores, :memory]
49
- table machine, :fields=>hw_fields, :filters=>{:memory=>filter_size}
50
-
51
- puts 'Processes With Filter'
52
- pr_fields = [:name, :mem_usage, :cpu_usage, :command, :status, :user]
53
- puts "Time: #{Benchmark.measure { table machine.processes('ruby'), :fields=>pr_fields,
54
- :filters=>{:mem_usage=>filter_perc,
55
- :cpu_usage=>filter_perc}}.real }"
56
-
57
- puts 'Top Processes by Memory Usage'
58
- pr_fields = [:name, :mem_usage, :cpu_usage, :command, :status, :user]
59
- puts "Time: #{Benchmark.measure { table machine.top(:memory), :fields=>pr_fields,
60
- :filters=>{:mem_usage=>filter_perc,
61
- :cpu_usage=>filter_perc}}.real }"
62
-
63
- puts 'Top Processes by CPU Usage'
64
- puts "Time: #{Benchmark.measure { table machine.top(:cpu), :fields=>pr_fields,
65
- :filters=>{:mem_usage=>filter_perc,
66
- :cpu_usage=>filter_perc} }.real}"
67
-
68
- puts 'Filesystems'
69
- bench = Benchmark.measure do
70
- hdd_fields = [:filesystem, :type, :size, :used, :available, :p_use, :mounted]
71
- table machine.disk_partition, :fields=>hdd_fields, :filters=>{:size=>filter_size,
72
- :used=>filter_size,
73
- :available=>filter_size,
74
- :p_use=>filter_perc}
75
- end
76
- puts "Time: #{bench.real}"
77
-
78
- puts 'Memory'
79
- bench = Benchmark.measure do
80
- mem_fields = [:memory, :memory_free, :memory_used, :swap, :swap_used, :swap_free]
81
- table machine, :fields=>mem_fields, :filters=> Hash.new.tap { |f| mem_fields.map { |e| f[e]=filter_size } }
82
- end
83
- puts "Time #{bench.real}"
84
-
85
- ## Menu: TODO: Hay que definir como usarlo, es para verlo
86
- class InfoMachineType
87
- attr_reader :description
88
-
89
- def initialize(description, detail)
90
- @description = description
91
- @detail = detail
92
- end
93
-
94
- def detail
95
- Hirb::Helpers::Tree.render(@detail)
96
- end
97
- end
98
-
99
- type_gen = InfoMachineType.new 'Informacion General', info_machine
100
- type_det = InfoMachineType.new 'Info Detallada', info_machine
101
-
102
- puts menu [type_gen, type_det], :prompt=> "Elegir opción: ",
103
- :fields => [:description],
104
- :default_field=>
105
- :detail,
106
- :two_d=>true
@@ -1,132 +0,0 @@
1
- require 'minitest_helper'
2
-
3
- describe Datacenter do
4
-
5
- COMMANDS_FILE = File.join(File.dirname(__FILE__), 'commands.yml')
6
-
7
- describe Datacenter::OS do
8
-
9
- let(:shell) { Datacenter::Shell::Mock.new COMMANDS_FILE }
10
- let(:os) { Datacenter::OS.new shell }
11
-
12
- it ('Name') { os.name.must_equal 'GNU/Linux' }
13
-
14
- it ('Distribution') { os.distribution.must_equal 'Ubuntu' }
15
-
16
- it ('Version') { os.version.must_equal '12.04' }
17
-
18
- it ('Kernel') { os.kernel.must_equal '3.5.0-49-generic' }
19
-
20
- it ('Platform') { os.platform.must_equal 'x86_64' }
21
-
22
- end
23
-
24
- describe Datacenter::Machine do
25
-
26
- describe 'Local' do
27
-
28
- let(:shell) { Datacenter::Shell::Mock.new COMMANDS_FILE }
29
- let(:machine) { Datacenter::Machine.new shell}
30
-
31
- it ('IPs') { machine.ips.must_equal %w(192.168.50.127 127.0.0.1) }
32
-
33
- it ('Name') { machine.name.must_equal "matias" }
34
-
35
- it 'OS' do
36
- machine.os.name.must_equal 'GNU/Linux'
37
- machine.os.distribution.must_equal 'Ubuntu'
38
- machine.os.version.must_equal '12.04'
39
- end
40
-
41
- it ('CPU') { machine.cpu.must_equal 'Intel(R) Core(TM)2 Duo CPU E7500 @ 2.93GHz'}
42
-
43
- it ('Cores') { machine.cores.must_equal 2 }
44
-
45
- it ('Total Memory') { machine.memory.must_equal 3944.953125 }
46
-
47
- it ('Free Memory') { machine.memory_free.must_equal 1262.95703125 }
48
-
49
- it ('Used Memory') { machine.memory_used.must_equal 2681.99609375 }
50
-
51
- it ('Total Swap') { machine.swap.must_equal 4084.99609375 }
52
-
53
- it ('Free Swap') { machine.swap_free.must_equal 4084.99609375 }
54
-
55
- it ('Used Swap') { machine.swap_used.must_equal 0.0}
56
-
57
- it ('List of Proccess') { machine.processes.find {|p| p.pid=="22803"}.command.must_equal 'gnome-system-monitor' }
58
-
59
- it ('List of Proccess With Filter') { machine.processes('gnome-system-monitor').find {|p| p.pid=="22803"}.name.must_equal 'gnome-system-mo' }
60
-
61
- it ('Top by Memory') { machine.top(:memory)[7].command.must_equal 'gnome-system-monitor' }
62
-
63
- it ('Top by CPU') { machine.top(:cpu)[0].command.must_equal 'gnome-system-monitor' }
64
-
65
- describe 'Disk Paritions' do
66
-
67
- it ('Size') { machine.disk_partitions.map(&:size).must_equal [296432.3671875, 286580.5556640625] }
68
-
69
- it ('Available') { machine.disk_partitions.map(&:available).must_equal [276762.1953125, 203569.6171875] }
70
-
71
- it ('Used') { machine.disk_partitions.map(&:used).must_equal [4612.2734375, 3864.20703125] }
72
-
73
- it ('% Use') { machine.disk_partitions.map(&:used_percentage).must_equal [2, 15] }
74
-
75
- it ('Filesystem') { machine.disk_partitions.map(&:filesystem).must_equal ["/dev/sda1", "/dev/sdb1"] }
76
-
77
- it ('Type') { machine.disk_partitions.map(&:type).must_equal ["ext4", "ext4"] }
78
-
79
- it ('Mounted on') { machine.disk_partitions.map(&:mounted).must_equal ["/", "/sys"] }
80
- end
81
-
82
- end
83
-
84
- describe Datacenter::Process do
85
-
86
- let(:pid) { 22803 }
87
- let(:shell) { Datacenter::Shell::Mock.new COMMANDS_FILE }
88
- let(:machine) { Datacenter::Machine.new shell}
89
- let(:process) { Datacenter::Process.new pid,machine }
90
-
91
- it ('Pid') { process.pid.must_equal pid }
92
-
93
- it ('Alive') { process.alive?.must_equal true }
94
-
95
- it 'Dead' do
96
- process = Datacenter::Process.new -1,machine
97
- process.alive?.must_equal false
98
- end
99
-
100
- it ('Name') { process.name.must_equal 'gnome-system-mo' }
101
-
102
- it ('Command') { process.command.must_equal 'gnome-system-monitor' }
103
-
104
- it ('Memory') { process.memory.must_equal 33.0 }
105
-
106
- it ('Virtual Memory') { process.virtual_memory.must_equal 501.23828125 }
107
-
108
- it ('% Memory') { process.mem_usage.must_equal 0.8}
109
-
110
- it ('% CPU') { process.cpu_usage.must_equal 11.9 }
111
-
112
- it ('Status') { process.status.must_equal 'Sl' }
113
-
114
- it ('User') { process.user.must_equal 'matias' }
115
-
116
- end
117
-
118
- end
119
-
120
- describe Datacenter::Shell::Ssh do
121
-
122
- let(:shell) { Datacenter::Shell::Ssh.new 'localhost', `whoami`.strip }
123
-
124
- before { shell.open }
125
- after { shell.close }
126
-
127
- it 'Run' do
128
- shell.run('ls /').must_equal `ls /`.strip
129
- end
130
- end
131
-
132
- end