class Atatus::Metadata::SystemInfo::HWInfo

@api private

Constants

CPU_MHZ_REGEX
LINUX_BOOTID_PATH
LINUX_CPUINFO_PATH
LINUX_MEMINFO_PATH
MODEL_NAME_REGEX
PROCESSOR_COUNT_REGEX
TOTAL_MEMORY_REGEX

Attributes

cpuinfo_cores[RW]
cpuinfo_mhz[RW]
cpuinfo_model[RW]
host_bootid[RW]
meminfo_total[RW]

Public Class Methods

new() click to toggle source
# File lib/atatus/metadata/system_info/hw_info.rb, line 13
def initialize
  @os = RbConfig::CONFIG['target_os']
end
read!() click to toggle source
# File lib/atatus/metadata/system_info/hw_info.rb, line 29
def self.read!
  new.read!
end

Public Instance Methods

cpuinfo() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/atatus/metadata/system_info/hw_info.rb, line 34
def cpuinfo
  @cpuinfo ||=
    begin
      cpuinfo = {
        cores: @cpuinfo_cores,
        model: @cpuinfo_model
      }
      cpuinfo[:mhz] = @cpuinfo_mhz unless @cpuinfo_mhz.nil?

      cpuinfo
    end
end
hostid() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/atatus/metadata/system_info/hw_info.rb, line 53
def hostid
  @hostid ||=
    begin
      return Socket.gethostname if @host_bootid.nil?
      @host_bootid
    end
end
meminfo() click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/atatus/metadata/system_info/hw_info.rb, line 48
def meminfo
  @meminfo ||= @meminfo_total
end
read!() click to toggle source
# File lib/atatus/metadata/system_info/hw_info.rb, line 17
def read!
  if @os =~ /(linux)/i
    read_from_cpuinfo!
    read_from_meminfo!
    read_from_bootid!
  elsif @os =~ /(darwin)/i
    read_cpuinfo_from_sysctl!
    read_meminfo_from_sysctl!
  end
  self
end

Private Instance Methods

get_sysctl_value(key) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/atatus/metadata/system_info/hw_info.rb, line 64
def get_sysctl_value(key)
  `sysctl -n #{key} 2>/dev/null`
end
read_cpuinfo_from_sysctl!() click to toggle source
# File lib/atatus/metadata/system_info/hw_info.rb, line 106
def read_cpuinfo_from_sysctl!
  self.cpuinfo_cores = get_sysctl_value('hw.logicalcpu_max').to_i
  self.cpuinfo_model = get_sysctl_value('machdep.cpu.brand_string').strip
end
read_from_bootid!() click to toggle source

rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity rubocop:enable Metrics/CyclomaticComplexity, Metrics/AbcSize

# File lib/atatus/metadata/system_info/hw_info.rb, line 100
def read_from_bootid!
  return unless File.exist?(LINUX_BOOTID_PATH)
  self.host_bootid = File.read(LINUX_BOOTID_PATH)
  self.host_bootid.strip!
end
read_from_cpuinfo!() click to toggle source

rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize

# File lib/atatus/metadata/system_info/hw_info.rb, line 80
def read_from_cpuinfo!
  return unless File.exist?(LINUX_CPUINFO_PATH)
  cpuinfo = File.read(LINUX_CPUINFO_PATH)
  self.cpuinfo_cores = cpuinfo.scan(PROCESSOR_COUNT_REGEX).size
  self.cpuinfo_model = cpuinfo.scan(MODEL_NAME_REGEX).flatten.first.strip
  self.cpuinfo_mhz = cpuinfo.scan(CPU_MHZ_REGEX).flatten.first
end
read_from_meminfo!() click to toggle source

rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize

# File lib/atatus/metadata/system_info/hw_info.rb, line 92
def read_from_meminfo!
  return unless File.exist?(LINUX_MEMINFO_PATH)
  meminfo = File.read(LINUX_MEMINFO_PATH)
  self.meminfo_total = (meminfo.scan(TOTAL_MEMORY_REGEX).flatten.first.to_i) * 1024 # to bytes
end
read_meminfo_from_sysctl!() click to toggle source
# File lib/atatus/metadata/system_info/hw_info.rb, line 111
def read_meminfo_from_sysctl!
  self.meminfo_total = get_sysctl_value('hw.memsize').to_i
  self.meminfo_total
end