class ServerMetrics::Cpu::CpuStats

Helper class

Attributes

idle[RW]
interrupts[RW]
iowait[RW]
procs_blocked[RW]
procs_running[RW]
steal[RW]
system[RW]
time[RW]
user[RW]

Public Class Methods

fetch() click to toggle source
# File lib/server_metrics/collectors/cpu.rb, line 46
def self.fetch
  output = nil
  begin
    output = File.read("#{ServerMetrics::SystemInfo.proc_dir}/stat")
  rescue Errno::ENOENT
    # No such file or directory - /proc/stat
    # /proc/stat doesn't exist on this system.
    raise ProcStatError
  end

  data = output.lines.collect { |line| line.split }

  cpu_stats = CpuStats.new

  if cpu = data.detect { |line| line[0] == 'cpu' }
    cpu_stats.user, nice, cpu_stats.system, cpu_stats.idle, cpu_stats.iowait,
        hardirq, softirq, cpu_stats.steal = *cpu[1..-1].collect { |c| c.to_i }
    cpu_stats.user += nice
    cpu_stats.system += hardirq + softirq
  end

  if interrupts = data.detect { |line| line[0] == 'intr' }
    cpu_stats.interrupts, _ = *interrupts[1..-1].collect { |c| c.to_i }
  end

  if procs_running = data.detect { |line| line[0] == 'procs_running' }
    cpu_stats.procs_running, _ = *procs_running[1..-1].collect { |c| c.to_i }
  end

  if procs_blocked = data.detect { |line| line[0] == 'procs_blocked' }
    cpu_stats.procs_blocked, _ = *procs_blocked[1..-1].collect { |c| c.to_i }
  end

  cpu_stats
end
from_hash(h) click to toggle source
# File lib/server_metrics/collectors/cpu.rb, line 82
def self.from_hash(h)
  cpu_stats= CpuStats.new
  hash = {}
  h.each { |k, v| hash[k.to_sym] = v }

  if time = hash.delete(:time)
    cpu_stats.time = Time.parse(time) rescue time
  end

  hash.each do |k, v|
    cpu_stats.send("#{k}=", v) if cpu_stats.respond_to?("#{k}=")
  end
  cpu_stats
end
new() click to toggle source
# File lib/server_metrics/collectors/cpu.rb, line 97
def initialize
  self.time = Time.now
end

Public Instance Methods

diff(other) click to toggle source
# File lib/server_metrics/collectors/cpu.rb, line 101
def diff(other)
  diff_user = user - other.user
  diff_system = system - other.system
  diff_idle = idle - other.idle
  diff_iowait = iowait - other.iowait

  div = diff_user + diff_system + diff_idle + diff_iowait

  if steal && other.steal && steal > 0
    diff_steal = steal - other.steal
    div += diff_steal
  end

  divo2 = div / 2

  results = {
      :user => (100.0 * diff_user + divo2) / div,
      :system => (100.0 * diff_system + divo2) / div,
      :idle => (100.0 * diff_idle + divo2) / div,
      :io_wait => (100.0 * diff_iowait + divo2) / div,
      :procs_running => self.procs_running,
      :procs_blocked => self.procs_blocked
  }

  if diff_steal && steal > 0
    results[:steal] = (100.0 * diff_steal + divo2) / div
  end

  if self.time && other.time
    diff_in_seconds = self.time.to_f - other.time.to_f
    diff_interrupts = (self.interrupts.to_f - other.interrupts.to_f) / diff_in_seconds
    # If interrupts counter has rolled over, start at 0
    results[:interrupts] = diff_interrupts >= 0 ? diff_interrupts : 0
  end

  results
end
to_h() click to toggle source
# File lib/server_metrics/collectors/cpu.rb, line 139
def to_h
  {
      :user => user, :system => system, :idle => idle, :iowait => iowait,
      :interrupts => interrupts, :procs_running => procs_running,
      :procs_blocked => procs_blocked, :time => Time.now.to_s,
      :steal => steal
  }
end