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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/server_metrics/collectors/cpu.rb', line 9

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

  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,
         "Last five minutes" => matches[2].to_f,
         "Last fifteen minutes" => matches[3].to_f)
end