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