Sha256: c77c6ebb3c86f4710ad32695e21301af5a8d2bc814c5aabf7ba664912fd03f0e

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

require 'socket'
require_relative 'helper'

module Remon
  class Sysinfo
    extend Helper

    def self.init
      puts "loading sysinfo"
      nproc
      cores
      normalized_cores
      instance_type
      ips
    end

    def self.instance_type
     @instance_type ||= begin
        exitstatus, instance_type = safe_cmd 'curl -s -m2 169.254.169.254/latest/meta-data/instance-type', return_output: true
        exitstatus == 0 ? instance_type : "unknown"
      end
    end

    def self.nproc
      @nproc ||= `nproc`.chomp.strip.to_i
    end

    def self.cores
      @cores ||= cores_linux
    end

    def self.normalized_cores
      @normalized_cores ||= begin
        cores = self.cores
        nproc = self.nproc
        (cores == nproc) ? cores : (cores + (nproc - cores)* 30.0/100)
      end
    end

    def self.ips
      @ips ||= begin
        addr_infos = Socket.ip_address_list.select do |i|
          i.ip_address =~ /\A([0-9]{1,3}\.){3}[0-9]{1,3}\z/
        end
        addr_infos.reject! { |i| i.ip_address == "127.0.0.1" }
        ips = addr_infos.map { |i| i.ip_address }
      end
    end

    private

    def self.cores_linux
      i = 0;
      File.read("/proc/cpuinfo").split(/\n\n/).inject({}) do |cores, p|
        physical_id = p[/physical id\s+:\s+(\d+)/, 1]
        core_id = p[/core id\s+:\s+(\d+)/, 1]
        if physical_id and core_id
        cores["#{physical_id}:#{core_id}"] = true
        elsif physical_id
          cores["#{physical_id}:"] = true
        else
          cores[i += 1] = true;
        end
        cores
      end.size
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
remon-0.1.0 lib/remon/sysinfo.rb