Class: ServerMetrics::Cpu

Inherits:
Collector show all
Defined in:
lib/server_metrics/collectors/cpu.rb

Defined Under Namespace

Classes: CpuStats, ProcStatError

Instance Attribute Summary

Attributes inherited from Collector

#collector_id, #data, #error

Instance Method Summary collapse

Methods inherited from Collector

#convert_to_mb, #counter, from_hash, #initialize, #linux?, #memory, #normalize_key, #option, #osx?, #remember, #report, #run, #to_hash

Constructor Details

This class inherits a constructor from ServerMetrics::Collector

Instance Method Details

#build_reportObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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_processorsObject



38
39
40
# File 'lib/server_metrics/collectors/cpu.rb', line 38

def num_processors
  @num_processors ||= ServerMetrics::SystemInfo.num_processors  
end