Class: DCell::InfoService
- Inherits:
-
Object
- Object
- DCell::InfoService
- Includes:
- Celluloid
- Defined in:
- lib/dcell/info_service.rb
Constant Summary collapse
- UPTIME_REGEX =
/up ((\d+ days?,)?\s*(\d+:\d+|\d+ \w+)),.*(( \d.\d{2},?){3})/
Instance Attribute Summary collapse
-
#cpu_arch ⇒ Object
readonly
Returns the value of attribute cpu_arch.
-
#cpu_count ⇒ Object
readonly
Returns the value of attribute cpu_count.
-
#cpu_speed ⇒ Object
readonly
Returns the value of attribute cpu_speed.
-
#cpu_type ⇒ Object
readonly
Returns the value of attribute cpu_type.
-
#cpu_vendor ⇒ Object
readonly
Returns the value of attribute cpu_vendor.
-
#distribution ⇒ Object
readonly
Returns the value of attribute distribution.
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#os ⇒ Object
readonly
Returns the value of attribute os.
-
#os_version ⇒ Object
readonly
Returns the value of attribute os_version.
-
#platform ⇒ Object
readonly
Returns the value of attribute platform.
-
#ruby_engine ⇒ Object
readonly
Returns the value of attribute ruby_engine.
-
#ruby_platform ⇒ Object
readonly
Returns the value of attribute ruby_platform.
-
#ruby_version ⇒ Object
readonly
Returns the value of attribute ruby_version.
Instance Method Summary collapse
- #discover_distribution ⇒ Object
-
#initialize ⇒ InfoService
constructor
A new instance of InfoService.
- #load_averages(uptime_string = `uptime`) ⇒ Object (also: #load_average)
- #to_hash ⇒ Object
- #uptime(uptime_string = `uptime`) ⇒ Object
Constructor Details
#initialize ⇒ InfoService
Returns a new instance of InfoService.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/dcell/info_service.rb', line 12 def initialize @cpu_arch = RbConfig::CONFIG['host_cpu'] @os = RbConfig::CONFIG['host_os'][/^[A-Za-z]+/] uname = `uname -a`.match(/\w+ (\w[\w+\.\-]*) ([\w+\.\-]+)/) @hostname, @os_version = uname[1], uname[2] @platform = RUBY_PLATFORM @ruby_version = RUBY_VERSION @ruby_engine = RUBY_ENGINE case os when 'darwin' cpu_info = `sysctl -n machdep.cpu.brand_string`.match(/^((\w+).*) @ (\d+.\d+)GHz/) if cpu_info @cpu_type = cpu_info[1] @cpu_vendor = cpu_info[2].downcase.to_sym @cpu_speed = Float(cpu_info[3]) end @cpu_count = Integer(`sysctl hw.ncpu`[/\d+/]) os, release, build = `sw_vers`.scan(/:\t(.*)$/).flatten @distribution = "#{os} #{release} (#{build})" when 'linux' cpu_info = File.read("/proc/cpuinfo") @cpu_vendor = cpu_info[/vendor_id:\s+\s+(Genuine)?(\w+)/, 2] model_name = cpu_info.match(/model name\s+:\s+((\w+).*) @ (\d+.\d+)GHz/) if model_name @cpu_type = model_name[1].gsub(/\s+/, ' ') @cpu_vendor = model_name[2].downcase.to_sym @cpu_speed = Float(model_name[3]) end cores = cpu_info.scan(/core id\s+: \d+/).uniq.size @cpu_count = cores > 0 ? cores : 1 @distribution = discover_distribution else @cpu_type = @cpu_vendor = @cpu_speed = @cpu_count = nil end case RUBY_ENGINE when 'ruby' @ruby_platform = "ruby #{RUBY_VERSION}" when 'jruby' @ruby_platform = "jruby #{JRUBY_VERSION}" when 'rbx' @ruby_platform = "rbx #{Rubinius::VERSION}" else @ruby_platform = RUBY_ENGINE end end |
Instance Attribute Details
#cpu_arch ⇒ Object (readonly)
Returns the value of attribute cpu_arch.
7 8 9 |
# File 'lib/dcell/info_service.rb', line 7 def cpu_arch @cpu_arch end |
#cpu_count ⇒ Object (readonly)
Returns the value of attribute cpu_count.
7 8 9 |
# File 'lib/dcell/info_service.rb', line 7 def cpu_count @cpu_count end |
#cpu_speed ⇒ Object (readonly)
Returns the value of attribute cpu_speed.
7 8 9 |
# File 'lib/dcell/info_service.rb', line 7 def cpu_speed @cpu_speed end |
#cpu_type ⇒ Object (readonly)
Returns the value of attribute cpu_type.
7 8 9 |
# File 'lib/dcell/info_service.rb', line 7 def cpu_type @cpu_type end |
#cpu_vendor ⇒ Object (readonly)
Returns the value of attribute cpu_vendor.
7 8 9 |
# File 'lib/dcell/info_service.rb', line 7 def cpu_vendor @cpu_vendor end |
#distribution ⇒ Object (readonly)
Returns the value of attribute distribution.
6 7 8 |
# File 'lib/dcell/info_service.rb', line 6 def distribution @distribution end |
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
6 7 8 |
# File 'lib/dcell/info_service.rb', line 6 def hostname @hostname end |
#os ⇒ Object (readonly)
Returns the value of attribute os.
6 7 8 |
# File 'lib/dcell/info_service.rb', line 6 def os @os end |
#os_version ⇒ Object (readonly)
Returns the value of attribute os_version.
6 7 8 |
# File 'lib/dcell/info_service.rb', line 6 def os_version @os_version end |
#platform ⇒ Object (readonly)
Returns the value of attribute platform.
6 7 8 |
# File 'lib/dcell/info_service.rb', line 6 def platform @platform end |
#ruby_engine ⇒ Object (readonly)
Returns the value of attribute ruby_engine.
8 9 10 |
# File 'lib/dcell/info_service.rb', line 8 def ruby_engine @ruby_engine end |
#ruby_platform ⇒ Object (readonly)
Returns the value of attribute ruby_platform.
8 9 10 |
# File 'lib/dcell/info_service.rb', line 8 def ruby_platform @ruby_platform end |
#ruby_version ⇒ Object (readonly)
Returns the value of attribute ruby_version.
8 9 10 |
# File 'lib/dcell/info_service.rb', line 8 def ruby_version @ruby_version end |
Instance Method Details
#discover_distribution ⇒ Object
93 94 95 96 |
# File 'lib/dcell/info_service.rb', line 93 def discover_distribution `lsb_release -d`[/Description:\s+(.*)\s*$/, 1] rescue Errno::ENOENT end |
#load_averages(uptime_string = `uptime`) ⇒ Object Also known as: load_average
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/dcell/info_service.rb', line 69 def load_averages(uptime_string = `uptime`) matches = uptime_string.match(UPTIME_REGEX) unless matches Logger.warn "Couldn't parse uptime output: #{uptime_string}" return end averages = matches[4].strip averages.split(/,? /).map(&:to_f) end |
#to_hash ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/dcell/info_service.rb', line 98 def to_hash uptime_string = `uptime` { :os => os, :os_version => os_version, :hostname => hostname, :platform => platform, :distribution => distribution, :ruby_version => ruby_version, :ruby_engine => ruby_engine, :ruby_platform => ruby_platform, :load_averages => load_averages(uptime_string), :uptime => uptime(uptime_string), :cpu => { :arch => cpu_arch, :type => cpu_type, :vendor => cpu_vendor, :speed => cpu_speed, :count => cpu_count } } end |
#uptime(uptime_string = `uptime`) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/dcell/info_service.rb', line 81 def uptime(uptime_string = `uptime`) matches = uptime_string.match(UPTIME_REGEX) unless matches Logger.warn "Couldn't parse uptime output: #{uptime_string}" return end uptime = matches[1] days_string = uptime[/^(\d+) days/, 1] days_string ? Integer(days_string) : 0 end |