Class: AppPerfAgent::Plugin::System::Cpu

Inherits:
Base
  • Object
show all
Defined in:
lib/app_perf_agent/plugin/system/cpu.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

descendants

Constructor Details

#initializeCpu

Returns a new instance of Cpu.



10
11
12
13
# File 'lib/app_perf_agent/plugin/system/cpu.rb', line 10

def initialize
  self.last = Vmstat.snapshot.cpus
  super
end

Instance Attribute Details

#lastObject

Returns the value of attribute last.



8
9
10
# File 'lib/app_perf_agent/plugin/system/cpu.rb', line 8

def last
  @last
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/app_perf_agent/plugin/system/cpu.rb', line 15

def call
  cpus = Vmstat.snapshot.cpus
  metrics = cpus.each_with_index.flat_map {|cpu, index|
    total = (cpu.idle + cpu.nice + cpu.system + cpu.user) - (last[index].idle + last[index].nice + last[index].system + last[index].user)
    [
      ["system.cpu.idle",   (cpu.idle - last[index].idle).to_f / total.to_f * 100.to_f,   { "num" => cpu.num }],
      ["system.cpu.nice",   (cpu.nice - last[index].nice).to_f / total.to_f * 100.to_f,   { "num" => cpu.num }],
      ["system.cpu.system", (cpu.system - last[index].system).to_f / total.to_f * 100.to_f, { "num" => cpu.num }],
      ["system.cpu.user",   (cpu.user - last[index].user).to_f / total.to_f * 100.to_f,   { "num" => cpu.num }]
    ]
  }
  self.last = cpus
  metrics
end