Sha256: 9b63ef4a99709aa26e3cca9123f9a9713096ca9f8aa635f1d6784bf3a21654f0

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

require "vmstat"
require_relative "../base"

module AppPerfAgent
  module Plugin
    module System
      class Cpu < AppPerfAgent::Plugin::Base
        attr_accessor :last

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

        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
      end
    end
  end
end

AppPerfAgent.logger.info "Loading CPU monitoring."

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
app_perf_agent-0.0.9 lib/app_perf_agent/plugin/system/cpu.rb
app_perf_agent-0.0.8 lib/app_perf_agent/plugin/system/cpu.rb
app_perf_agent-0.0.7 lib/app_perf_agent/plugin/system/cpu.rb
app_perf_agent-0.0.6 lib/app_perf_agent/plugin/system/cpu.rb
app_perf_agent-0.0.5 lib/app_perf_agent/plugin/system/cpu.rb