Sha256: 18790b3acfaab05135457c348b0bb7f77adb981916ca5a36cb442d3c41228ace

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

class SystemInspector
  TYPES = [:gauges, :incrementors]
  attr_accessor *TYPES

  def self.memory
    @memory ||= Memory.new
  end

  def initialize
    @gauges = {}
    @incrementors = {}
    @platform =
      case RUBY_PLATFORM
      when /linux/
        require "instrumental_tools/system_inspector/linux"
        SystemInspector::Linux
      when /darwin/
        require "instrumental_tools/system_inspector/osx"
        SystemInspector::OSX
      when /(windows|win32|mingw)/
        require "instrumental_tools/system_inspector/win32"
        SystemInspector::Win32
      else
        raise "unsupported OS"
      end
  end

  def self.command_missing(command, section)
    puts "Command #{command} not found. Metrics for #{section} will not be collected."
  end

  def self.command_present?(command, section)
    `which #{command}`.length > 0 || command_missing(command, section)
  end

  def load_all
    self.class.memory.cycle

    load @platform.load_cpu
    load @platform.load_memory
    load @platform.load_disks
    load @platform.load_filesystem
  end

  def load(stats)
    @gauges.merge!(stats[:gauges] || {})
  end

  class Memory
    attr_reader :past_values, :current_values

    def initialize
      @past_values = {}
      @current_values = {}
    end

    def store(attribute, value)
      @current_values[attribute] = value
    end

    def retrieve(attribute)
      @past_values[attribute]
    end

    def cycle
      @past_values = @current_values
      @current_values = {}
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
instrumental_tools-1.1.4 lib/instrumental_tools/system_inspector.rb
instrumental_tools-1.1.3 lib/instrumental_tools/system_inspector.rb