class AppPerfAgent::Plugin::System::Cpu

Attributes

last[RW]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/app_perf_agent/plugin/system/cpu.rb, line 10
def initialize
  self.last = Vmstat.snapshot.cpus
  super
end

Public Instance Methods

call() click to toggle source
# 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