Class: DCell::InfoService

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeInfoService

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_archObject (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_countObject (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_speedObject (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_typeObject (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_vendorObject (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

#distributionObject (readonly)

Returns the value of attribute distribution.



6
7
8
# File 'lib/dcell/info_service.rb', line 6

def distribution
  @distribution
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



6
7
8
# File 'lib/dcell/info_service.rb', line 6

def hostname
  @hostname
end

#osObject (readonly)

Returns the value of attribute os.



6
7
8
# File 'lib/dcell/info_service.rb', line 6

def os
  @os
end

#os_versionObject (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

#platformObject (readonly)

Returns the value of attribute platform.



6
7
8
# File 'lib/dcell/info_service.rb', line 6

def platform
  @platform
end

#ruby_engineObject (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_platformObject (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_versionObject (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_distributionObject



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_hashObject



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