class ServerMetrics::Cpu

Public Instance Methods

build_report() click to toggle source
# File lib/server_metrics/collectors/cpu.rb, line 10
def build_report
  begin
    stats = CpuStats.fetch

    if previous = memory(:cpu_stats)
      previous_stats = CpuStats.from_hash(previous)

      report stats.diff(previous_stats)
    end

    remember(:cpu_stats => stats.to_h)
  rescue ProcStatError
    @error = "could not retrieve CPU stats from /proc/stat"
  end
  
  # This requires a system call, which is slow. `scout_realtime` doesn't display server load, so this
  # option allows `scout_realtime` to not collect load averages.
  if !@options[:skip_load]
    ENV['LANG'] = 'C' # forcing english for parsing
    uptime_output = `uptime`
    matches = uptime_output.match(/load averages?: ([\d.]+),? ([\d.]+),? ([\d.]+)\Z/)

    report(:last_minute => matches[1].to_f / num_processors,
           :last_five_minutes => matches[2].to_f / num_processors,
           :last_fifteen_minutes => matches[3].to_f / num_processors)
  end
end
num_processors() click to toggle source
# File lib/server_metrics/collectors/cpu.rb, line 38
def num_processors
  @num_processors ||= ServerMetrics::SystemInfo.num_processors  
end