host 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/.rvmrc +55 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +32 -0
- data/Guardfile +7 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +59 -0
- data/lib/host.rb +44 -0
- data/lib/host/hashifier.rb +47 -0
- data/lib/host/linux.rb +13 -0
- data/lib/host/linux/load_average.rb +59 -0
- data/lib/host/linux/memory.rb +66 -0
- data/lib/host/linux/processor.rb +78 -0
- data/lib/host/load_average.rb +60 -0
- data/lib/host/memory.rb +47 -0
- data/lib/host/processor.rb +48 -0
- data/lib/host/version.rb +7 -0
- data/spec/host/linux/load_average_spec.rb +32 -0
- data/spec/host/linux/memory_spec.rb +24 -0
- data/spec/host/linux/processor_spec.rb +24 -0
- data/spec/host/load_average_spec.rb +5 -0
- data/spec/host/memory_spec.rb +5 -0
- data/spec/host/processor_spec.rb +5 -0
- data/spec/spec_helper.rb +12 -0
- metadata +126 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rvmrc
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
|
7
|
+
environment_id="ruby-1.9.3-p0@host"
|
8
|
+
|
9
|
+
#
|
10
|
+
# Uncomment following line if you want options to be set only for given project.
|
11
|
+
#
|
12
|
+
# PROJECT_JRUBY_OPTS=( --1.9 )
|
13
|
+
|
14
|
+
#
|
15
|
+
# First we attempt to load the desired environment directly from the environment
|
16
|
+
# file. This is very fast and efficient compared to running through the entire
|
17
|
+
# CLI and selector. If you want feedback on which environment was used then
|
18
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
19
|
+
#
|
20
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
|
21
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
22
|
+
then
|
23
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
24
|
+
|
25
|
+
if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
|
26
|
+
then
|
27
|
+
. "${rvm_path:-$HOME/.rvm}/hooks/after_use"
|
28
|
+
fi
|
29
|
+
else
|
30
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
31
|
+
if ! rvm --create "$environment_id"
|
32
|
+
then
|
33
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
34
|
+
exit 1
|
35
|
+
fi
|
36
|
+
fi
|
37
|
+
|
38
|
+
#
|
39
|
+
# If you use an RVM gemset file to install a list of gems (*.gems), you can have
|
40
|
+
# it be automatically loaded. Uncomment the following and adjust the filename if
|
41
|
+
# necessary.
|
42
|
+
#
|
43
|
+
# filename=".gems"
|
44
|
+
# if [[ -s "$filename" ]]
|
45
|
+
# then
|
46
|
+
# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
|
47
|
+
# fi
|
48
|
+
|
49
|
+
# If you use bundler, this might be useful to you:
|
50
|
+
# if command -v bundle && [[ -s Gemfile ]]
|
51
|
+
# then
|
52
|
+
# bundle install
|
53
|
+
# fi
|
54
|
+
|
55
|
+
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
git (1.2.5)
|
6
|
+
guard (0.8.4)
|
7
|
+
thor (~> 0.14.6)
|
8
|
+
guard-rspec (0.5.0)
|
9
|
+
guard (>= 0.8.4)
|
10
|
+
jeweler (1.6.4)
|
11
|
+
bundler (~> 1.0)
|
12
|
+
git (>= 1.2.5)
|
13
|
+
rake
|
14
|
+
rake (0.9.2)
|
15
|
+
rspec (2.6.0)
|
16
|
+
rspec-core (~> 2.6.0)
|
17
|
+
rspec-expectations (~> 2.6.0)
|
18
|
+
rspec-mocks (~> 2.6.0)
|
19
|
+
rspec-core (2.6.4)
|
20
|
+
rspec-expectations (2.6.0)
|
21
|
+
diff-lcs (~> 1.1.2)
|
22
|
+
rspec-mocks (2.6.0)
|
23
|
+
thor (0.14.6)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler
|
30
|
+
guard-rspec
|
31
|
+
jeweler
|
32
|
+
rspec
|
data/Guardfile
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
guard 'rspec', :cli => '--color --format doc' do
|
2
|
+
watch(%r{^spec/.+_spec\.rb$})
|
3
|
+
|
4
|
+
watch(%r{^lib/host/(.+)\.rb$}) { |m| "spec/host/#{m[1]}_spec.rb" }
|
5
|
+
watch(%r{^lib/host/(.+)/(.+)\.rb$}) { |m| "spec/host/#{m[1]}/#{m[2]}_spec.rb" }
|
6
|
+
watch("spec/spec_helper.rb") { "spec" }
|
7
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Renato Mascarenhas
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= host
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to host
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Renato Mascarenhas. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.expand_path('lib/host/version')
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'bundler'
|
7
|
+
begin
|
8
|
+
Bundler.setup(:default, :development)
|
9
|
+
rescue Bundler::BundlerError => e
|
10
|
+
$stderr.puts e.message
|
11
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
12
|
+
exit e.status_code
|
13
|
+
end
|
14
|
+
require 'rake'
|
15
|
+
|
16
|
+
require 'jeweler'
|
17
|
+
Jeweler::Tasks.new do |gem|
|
18
|
+
gem.name = "host"
|
19
|
+
gem.version = Host::VERSION
|
20
|
+
gem.homepage = "http://github.com/rmascarenhas/host"
|
21
|
+
gem.license = "MIT"
|
22
|
+
|
23
|
+
gem.summary = %Q{Host provides a simple way to get useful information about
|
24
|
+
your system}
|
25
|
+
|
26
|
+
gem.description = <<-END
|
27
|
+
Host is a symple library for accessing your host system information in runtime.
|
28
|
+
It allows you to get CPU and memory usage, the load average and more.
|
29
|
+
Different data is provided for different operating systems. Checkout the
|
30
|
+
documentation on the Github page and help us support a wider range of
|
31
|
+
operating systems.
|
32
|
+
END
|
33
|
+
|
34
|
+
gem.email = "haze-gem@googlegroups.com"
|
35
|
+
gem.authors = ["Fabio de Lima Pereira", "Rafael Regis do Prado", "Renato Mascarenhas"]
|
36
|
+
end
|
37
|
+
Jeweler::RubygemsDotOrgTasks.new
|
38
|
+
|
39
|
+
require 'rspec/core'
|
40
|
+
require 'rspec/core/rake_task'
|
41
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
42
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
43
|
+
end
|
44
|
+
|
45
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
46
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
47
|
+
end
|
48
|
+
|
49
|
+
task :default => :spec
|
50
|
+
|
51
|
+
require 'rake/rdoctask'
|
52
|
+
Rake::RDocTask.new do |rdoc|
|
53
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
54
|
+
|
55
|
+
rdoc.rdoc_dir = 'rdoc'
|
56
|
+
rdoc.title = "host #{version}"
|
57
|
+
rdoc.rdoc_files.include('README*')
|
58
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
59
|
+
end
|
data/lib/host.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# lib/host.rb
|
2
|
+
|
3
|
+
module Host
|
4
|
+
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def linux?
|
8
|
+
check_platform('linux')
|
9
|
+
end
|
10
|
+
|
11
|
+
def macosx?
|
12
|
+
check_platform('darwin')
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.os
|
16
|
+
return :linux if linux?
|
17
|
+
return :macosx if macosx?
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# Takes a string and returns a method name for that,
|
23
|
+
# using snake_case (instead of camelCase).
|
24
|
+
def methodize(name)
|
25
|
+
name.split(/([[:lower:]][[:upper:]]*)/).delete_if(&:empty?).each(&:downcase!).join('_')
|
26
|
+
end
|
27
|
+
|
28
|
+
# Checks if +RUBY_PLATFORM+ matches the given name
|
29
|
+
def check_platform(name)
|
30
|
+
!(RUBY_PLATFORM =~ Regexp.new(name)).nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
require_relative 'host/hashifier'
|
36
|
+
|
37
|
+
case Host.os
|
38
|
+
when :linux then require_relative 'host/linux'
|
39
|
+
end
|
40
|
+
|
41
|
+
require_relative 'host/processor'
|
42
|
+
require_relative 'host/memory'
|
43
|
+
require_relative 'host/load_average'
|
44
|
+
require_relative 'host/version'
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# lib/host/hashifier.rb
|
2
|
+
|
3
|
+
module Host
|
4
|
+
|
5
|
+
# Module for parse a string containing data into a hash of values.
|
6
|
+
# Given a string and regular expression that parses and saves key and
|
7
|
+
# value, this module returns the built hash.
|
8
|
+
#
|
9
|
+
# Used mainly to parse +/proc/cpuinfo+ and +/proc/meminfo+ files
|
10
|
+
# (and similars)
|
11
|
+
module Hashifier
|
12
|
+
|
13
|
+
# Builds an array of hashes with the data passed, since duplicate keys
|
14
|
+
# cause another entry to be stored in the array returned (as it was a
|
15
|
+
# representation of many resources of the same type).
|
16
|
+
#
|
17
|
+
# Note: the way it is implemented, you can pass not only a Ruby Array,
|
18
|
+
# but any object that responds to +<<+
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
# cpu = File.read('/proc/cpuinfo')
|
22
|
+
# hashify(cpu, :regexp => /your regexp here/, :into => @cpu_info)
|
23
|
+
def hashify(data, options)
|
24
|
+
regexp = options[:regexp]
|
25
|
+
dest = options[:into]
|
26
|
+
|
27
|
+
info = {}
|
28
|
+
|
29
|
+
data.each_line { |line|
|
30
|
+
matches = line.match(regexp)
|
31
|
+
next if matches.nil?
|
32
|
+
|
33
|
+
if info.has_key?(matches[1].strip.to_sym)
|
34
|
+
dest << info
|
35
|
+
info = {}
|
36
|
+
else
|
37
|
+
info[matches[1].strip.to_sym] = matches[2]
|
38
|
+
end
|
39
|
+
}
|
40
|
+
|
41
|
+
dest << info
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/lib/host/linux.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# lib/hot/load_average.rb
|
2
|
+
|
3
|
+
module Host
|
4
|
+
|
5
|
+
module Linux
|
6
|
+
|
7
|
+
# Implements the recovering of load average information on Linux systems
|
8
|
+
module LoadAverage
|
9
|
+
|
10
|
+
LAST_MINUTE, LAST_5_MINUTES, LAST_15_MINUTES = 0, 1, 2
|
11
|
+
RUNNING_PROCESSES = 3
|
12
|
+
TOTAL_PROCESSES = 4
|
13
|
+
|
14
|
+
# Returns the load average in the last +minutes+. If the parameter
|
15
|
+
# passed is neither 1, 5 or 15, then the value returned is the load
|
16
|
+
# average on the nearest time.
|
17
|
+
#
|
18
|
+
# For instance, Host::LoadAverage.on_last(4) will return the same as
|
19
|
+
# Host::LoadAverage.on_last(5).
|
20
|
+
def on_last(minutes)
|
21
|
+
@info[nearest(minutes)]
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns the number of currently running processes on the system.
|
25
|
+
def running_processes
|
26
|
+
@info[RUNNING_PROCESSES]
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns total number of processes on the system.
|
30
|
+
def total_processes
|
31
|
+
@info[TOTAL_PROCESSES]
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
# Retrieves information about load average on a Linux system,
|
38
|
+
# gathering the indices for the last minute, last 5 minutes
|
39
|
+
# and last 15 minutes.
|
40
|
+
def get_loadinfo
|
41
|
+
loadavg = File.read('/proc/loadavg')
|
42
|
+
averages = loadavg.gsub(/\d+\.\d+/)
|
43
|
+
|
44
|
+
load_info
|
45
|
+
|
46
|
+
@info[LAST_MINUTE] = averages.next.to_f
|
47
|
+
@info[LAST_5_MINUTES] = averages.next.to_f
|
48
|
+
@info[LAST_15_MINUTES] = averages.next.to_f
|
49
|
+
|
50
|
+
processes = loadavg.gsub(/\d+\/\d+/).first.split('/')
|
51
|
+
|
52
|
+
@info[RUNNING_PROCESSES] = processes[0].to_i
|
53
|
+
@info[TOTAL_PROCESSES] = processes[1].to_i
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# lib/host/linux/memory.rb
|
2
|
+
|
3
|
+
module Host
|
4
|
+
|
5
|
+
module Linux
|
6
|
+
|
7
|
+
# Implements the retrieving of memory information on Linux systems.
|
8
|
+
module Memory
|
9
|
+
|
10
|
+
include Host::Hashifier
|
11
|
+
|
12
|
+
# Just sugar. Equivalent to Host::Memory.get('MemTotal') with
|
13
|
+
# implicit transformation to integer (in kB)
|
14
|
+
def total
|
15
|
+
as_integer @info[:MemTotal]
|
16
|
+
end
|
17
|
+
|
18
|
+
# Sugar. Equivalent to Host::Memory.get('MemFree') with implicit
|
19
|
+
# transformation to integer (in kB)
|
20
|
+
def free
|
21
|
+
as_integer @info[:MemFree]
|
22
|
+
end
|
23
|
+
|
24
|
+
# Sugar. Equivalent to Host::Memory.get('Buffers') with implicit
|
25
|
+
# transformation to integer (in kB)
|
26
|
+
def buffers
|
27
|
+
as_integer @info[:Buffers]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Retrieves specific information for the given key (as some exotic
|
31
|
+
# information in +/proc/meminfo+, for instance).
|
32
|
+
def get(key)
|
33
|
+
@info[key.to_sym]
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
# Returns the integer representation of the string, removing blank speces
|
40
|
+
# and kB unit
|
41
|
+
def as_integer(string)
|
42
|
+
string.gsub(/\s*|kB/, '').to_i
|
43
|
+
end
|
44
|
+
|
45
|
+
# Acts differently dependending on the operating systems.
|
46
|
+
# Raises +NotSupportedOperatingSystem+ if we cannot recognize the OS.
|
47
|
+
def parse_meminfo
|
48
|
+
case Host.os
|
49
|
+
when :linux then parse_meminfo_linux
|
50
|
+
# other os...
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Opens +/proc/meminfo+ and parses it, storing the information in a
|
55
|
+
# key-value manner.
|
56
|
+
def get_meminfo
|
57
|
+
matcher = /(.*):\s*(.*)$/
|
58
|
+
mem_info = File.read('/proc/meminfo')
|
59
|
+
|
60
|
+
hashify mem_info, :regexp => matcher, :into => memory_info
|
61
|
+
@info = @info.first # we do not have multiple keys for memory information
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# lib/host/linux/processor.rb
|
2
|
+
|
3
|
+
module Host
|
4
|
+
|
5
|
+
module Linux
|
6
|
+
|
7
|
+
# Implements the gathering of processor information on a Linux system.
|
8
|
+
module Processor
|
9
|
+
include Host::Hashifier
|
10
|
+
|
11
|
+
# Returns the number of cores identified on the system
|
12
|
+
def cores
|
13
|
+
@info.size
|
14
|
+
end
|
15
|
+
|
16
|
+
# returns an object containing the information of the core
|
17
|
+
# with the given +id+ (as in +/proc/cpuinfo+ for example). You can then
|
18
|
+
# get specific data on the returned object.
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
# p = Host::Processor.core(1)
|
22
|
+
# p.model_name #=> "GenuineIntel"
|
23
|
+
def core(id)
|
24
|
+
@current_core = @info[id]
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
# Lets you iterate over the processor cores and get specific information.
|
29
|
+
# Similar to iterate on +cores+, using Host::Processor.core
|
30
|
+
def each_core
|
31
|
+
@info.each { |processor|
|
32
|
+
@current_core = processor
|
33
|
+
yield self
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
def model_name
|
38
|
+
@current_core && @current_core[:model_name]
|
39
|
+
end
|
40
|
+
|
41
|
+
# Returns an array of flags for the processor
|
42
|
+
def flags
|
43
|
+
if @current_core && @current_core.has_key?(:flags)
|
44
|
+
@current_core[:flags].split
|
45
|
+
else
|
46
|
+
[]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Retrieves a specific data stored on +/proc/cpuinfo+ for the
|
51
|
+
# given key.
|
52
|
+
def get(key)
|
53
|
+
@current_core && @current_core[key.to_sym]
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
# Opens +/proc/cpuinfo+ and parses it, filling the info hash
|
59
|
+
# with all the information in that file
|
60
|
+
def get_cpuinfo
|
61
|
+
matcher = /(.*):\s*(.*)$/
|
62
|
+
cpu_info = File.read '/proc/cpuinfo'
|
63
|
+
|
64
|
+
hashify cpu_info, :regexp => matcher, :into => processor_info
|
65
|
+
|
66
|
+
@info.each do |processor|
|
67
|
+
processor.keys.each { |key|
|
68
|
+
processor[key.to_s.gsub(' ', '_').to_sym] = processor.delete(key)
|
69
|
+
}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# lib/host/load_average.rb
|
2
|
+
|
3
|
+
module Host
|
4
|
+
|
5
|
+
# Can provide the information on load average in the last 1 minute, 5 minutes
|
6
|
+
# and 15 minutes, as in +/proc/loadavg+ on Linux systems.
|
7
|
+
#
|
8
|
+
# Also, different information can be provided, depending on your operating system.
|
9
|
+
# On Linux, this class can also retrieve the number of running process (and the
|
10
|
+
# total number of processes).
|
11
|
+
#
|
12
|
+
# Help us support your operating system as well!
|
13
|
+
#
|
14
|
+
# Usage:
|
15
|
+
#
|
16
|
+
# # Supposing we are in a Linux box:
|
17
|
+
# Host::LoadAverage.on_last(1) #=> 0.2
|
18
|
+
# Host::LoadAverage.on_last(5) #=> 0.1
|
19
|
+
# Host::LoadAverage.on_last(15) #=> 0.5
|
20
|
+
#
|
21
|
+
# Host::LoadAverage.running_processes #=> 4
|
22
|
+
# Host::LoadAverage.total_processes #=> 312
|
23
|
+
class LoadAverage
|
24
|
+
|
25
|
+
class << self
|
26
|
+
|
27
|
+
def load_info
|
28
|
+
@info ||= []
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
# Returns the most recent known load average for the given number of
|
34
|
+
# minutes.
|
35
|
+
def nearest(minutes)
|
36
|
+
diff_1 = (minutes - 1).abs
|
37
|
+
diff_5 = (minutes - 5).abs
|
38
|
+
diff_15 = (minutes - 15).abs
|
39
|
+
|
40
|
+
diffs = [diff_1, diff_5, diff_15]
|
41
|
+
min = diffs.min
|
42
|
+
|
43
|
+
diffs.index(min)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Takes a different action depending on the OS
|
47
|
+
def extend_loadavg
|
48
|
+
case Host.os
|
49
|
+
when :linux then extend Linux::LoadAverage
|
50
|
+
# other os...
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
extend_loadavg
|
56
|
+
get_loadinfo
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
data/lib/host/memory.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# lib/host/memory.rb
|
2
|
+
|
3
|
+
module Host
|
4
|
+
|
5
|
+
# Class representing memory in a system. As Host::Processor, this class
|
6
|
+
# is also operating system dependent, which means that different information
|
7
|
+
# is provided depending on your OS.
|
8
|
+
#
|
9
|
+
# The minimum information provided for the supported operating systems is:
|
10
|
+
# * total memory
|
11
|
+
# * free memory
|
12
|
+
#
|
13
|
+
# Other information can be retrieved via the Host::Processor.get method which
|
14
|
+
# returns the information associated with the given key (depending on your OS).
|
15
|
+
#
|
16
|
+
# Example usage:
|
17
|
+
#
|
18
|
+
# Host::Memory.total #=> "4023256 kB"
|
19
|
+
# Host::Memory.free #=> "2115874 kB"
|
20
|
+
# # For specific information (as in +/proc/meminfo+ for instance),
|
21
|
+
# # you can use the +get+ method
|
22
|
+
# Host::Memory.get('KernelStack') #=> "1080 kB"
|
23
|
+
class Memory
|
24
|
+
|
25
|
+
class << self
|
26
|
+
|
27
|
+
def memory_info
|
28
|
+
@info ||= []
|
29
|
+
end
|
30
|
+
|
31
|
+
# Acts differently dependending on the operating systems.
|
32
|
+
# Raises +NotSupportedOperatingSystem+ if we cannot recognize the OS.
|
33
|
+
def extend_memory
|
34
|
+
case Host.os
|
35
|
+
when :linux then extend Linux::Memory
|
36
|
+
# other os...
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
extend_memory
|
43
|
+
get_meminfo
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# lib/host/processor.rb
|
2
|
+
|
3
|
+
|
4
|
+
module Host
|
5
|
+
|
6
|
+
# Class used to hold information about the system processor.
|
7
|
+
# Gathers information acording to the operating system and provides
|
8
|
+
# an easy way to access them.
|
9
|
+
# Important: the information that this class can retrieve depends on
|
10
|
+
# the host operating system. Linux is the better choice if you want
|
11
|
+
# a wide amount of information (as in /proc/cpuinfo). Other systems
|
12
|
+
# are also supported, such as OSX and FreBSD, but not as many
|
13
|
+
# informaiton cane be provided. Help us to enhance our support for
|
14
|
+
# many different operating systems!
|
15
|
+
#
|
16
|
+
# The minimum functionality provided is:
|
17
|
+
# * get the number of cores
|
18
|
+
# * get information about a specific core
|
19
|
+
# * iterate over cores
|
20
|
+
# * get the model name
|
21
|
+
class Processor
|
22
|
+
class << self
|
23
|
+
|
24
|
+
def processor_info
|
25
|
+
@info ||= []
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Depending on the operating system, a different action is taken here,
|
31
|
+
# since there is no "one size fits them all" way of getting such
|
32
|
+
# information.
|
33
|
+
def extend_processor
|
34
|
+
puts Host.os
|
35
|
+
case Host.os
|
36
|
+
when :linux then extend Linux::Processor
|
37
|
+
# other OS..
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
extend_processor
|
44
|
+
get_cpuinfo
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
data/lib/host/version.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe Host::LoadAverage do
|
4
|
+
|
5
|
+
if Host.linux?
|
6
|
+
it 'should retrieve the load average on the last minute' do
|
7
|
+
Host::LoadAverage.on_last(1).should be_kind_of Float
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should get the load average on the last 5 minutes' do
|
11
|
+
Host::LoadAverage.on_last(5).should be_kind_of Float
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should get the load average on the last 15 minutes' do
|
15
|
+
Host::LoadAverage.on_last(15).should be_kind_of Float
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should retrieve load average on the most recent known time' do
|
19
|
+
Host::LoadAverage.on_last(2).should == Host::LoadAverage.on_last(1)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should get the number of currently running processes' do
|
23
|
+
Host::LoadAverage.running_processes.should > 0
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should get the total number of processes on the system' do
|
27
|
+
Host::LoadAverage.total_processes.should > 0
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe Host::Memory do
|
4
|
+
|
5
|
+
if Host.linux?
|
6
|
+
it 'should get the total memory' do
|
7
|
+
Host::Memory.total.should > 0
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should get the available free memory' do
|
11
|
+
Host::Memory.free.should > 0
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should return nil when trying to get invalid information' do
|
15
|
+
Host::Memory.get('ZOMG').should be_nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should retrieve specific information, such as KernelStack' do
|
19
|
+
Host::Memory.get('KernelStack').should be_kind_of String
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
|
3
|
+
describe Host::Linux::Processor do
|
4
|
+
|
5
|
+
if Host.linux?
|
6
|
+
it 'should get the number of cores' do
|
7
|
+
Host::Processor.cores.should > 0
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should get the model name via a method' do
|
11
|
+
Host::Processor.core(0).model_name.should be_kind_of String
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should iterate over the cores' do
|
15
|
+
Host::Processor.each_core { |core|
|
16
|
+
core.get(:core_id).should be_kind_of String
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should retrieve the processor flags' do
|
21
|
+
Host::Processor.core(0).flags.should be_kind_of Array
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'host'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: host
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Fabio de Lima Pereira
|
9
|
+
- Rafael Regis do Prado
|
10
|
+
- Renato Mascarenhas
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2011-11-10 00:00:00.000000000 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
requirement: &3194300 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ! '>='
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '0'
|
24
|
+
type: :development
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *3194300
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: guard-rspec
|
29
|
+
requirement: &3192320 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *3192320
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: bundler
|
40
|
+
requirement: &3190380 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *3190380
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: jeweler
|
51
|
+
requirement: &3188920 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *3188920
|
60
|
+
description: ! " Host is a symple library for accessing your host system information
|
61
|
+
in runtime.\n It allows you to get CPU and memory usage, the load average and
|
62
|
+
more.\n Different data is provided for different operating systems. Checkout
|
63
|
+
the \n documentation on the Github page and help us support a wider range of\n
|
64
|
+
\ operating systems. \n"
|
65
|
+
email: haze-gem@googlegroups.com
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files:
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.rdoc
|
71
|
+
files:
|
72
|
+
- .document
|
73
|
+
- .rspec
|
74
|
+
- .rvmrc
|
75
|
+
- Gemfile
|
76
|
+
- Gemfile.lock
|
77
|
+
- Guardfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.rdoc
|
80
|
+
- Rakefile
|
81
|
+
- lib/host.rb
|
82
|
+
- lib/host/hashifier.rb
|
83
|
+
- lib/host/linux.rb
|
84
|
+
- lib/host/linux/load_average.rb
|
85
|
+
- lib/host/linux/memory.rb
|
86
|
+
- lib/host/linux/processor.rb
|
87
|
+
- lib/host/load_average.rb
|
88
|
+
- lib/host/memory.rb
|
89
|
+
- lib/host/processor.rb
|
90
|
+
- lib/host/version.rb
|
91
|
+
- spec/host/linux/load_average_spec.rb
|
92
|
+
- spec/host/linux/memory_spec.rb
|
93
|
+
- spec/host/linux/processor_spec.rb
|
94
|
+
- spec/host/load_average_spec.rb
|
95
|
+
- spec/host/memory_spec.rb
|
96
|
+
- spec/host/processor_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
homepage: http://github.com/rmascarenhas/host
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
hash: 3682824794771593770
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.8.10
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: Host provides a simple way to get useful information about your system
|
126
|
+
test_files: []
|