Sha256: f3f7dfcdd686e1b0f9efee7477f24085d68ae659813c34db6b1c9ec26a4a6cd9

Contents?: true

Size: 1.94 KB

Versions: 6

Compression:

Stored size: 1.94 KB

Contents

# encoding: utf-8
require 'one_apm/collector/support/shell_poller'

module OneApm
  module Collector
    class ProcPoller < OneApm::Collector::ShellPoller

      UPTIME_PATH = '/proc/uptime'.freeze

      def self.enable?
        ::RUBY_PLATFORM.downcase =~ /linux/
      end

      def poll_memory
        return 0.0 unless File.exist?(proc_status_file)
        stats[:rss].to_f * OneApm::Collector::ShellPoller.kb_page_size / 1024.0
      end
      
      def cpu_utilization
        @pid = Process.pid
        stat = stats 
        total_time = stat[:utime].to_f + stat[:stime].to_f # in jiffies
        seconds = uptime - stat[:starttime].to_f / 100
        return 0.0 if seconds <= 0.0
        ((total_time * 1000.0 / 100.0) / seconds) / 10.0 / 100.0
      rescue => e
        OneApm::Manager.logger.warn "Fetch cpu usage error: #{e.message}"
        0.0
      end

      def to_s
        "ProcPoller from: #{proc_status_file}"
      end

      private
      # in seconds
      def uptime
        File.read(UPTIME_PATH).split[0].to_f
      end

      def proc_status_file
        "/proc/#{pid}/stat"
      end

      def stats
        stat = {}
        stat[:pid], stat[:comm], stat[:state], stat[:ppid], stat[:pgrp],
        stat[:session], stat[:tty_nr], stat[:tpgid], stat[:flags],
        stat[:minflt], stat[:cminflt], stat[:majflt], stat[:cmajflt],
        stat[:utime], stat[:stime], stat[:cutime], stat[:cstime],
        stat[:priority], stat[:nice], _, stat[:itrealvalue],
        stat[:starttime], stat[:vsize], stat[:rss], stat[:rlim],
        stat[:startcode], stat[:endcode], stat[:startstack], stat[:kstkesp],
        stat[:kstkeip], stat[:signal], stat[:blocked], stat[:sigignore],
        stat[:sigcatch], stat[:wchan], stat[:nswap], stat[:cnswap],
        stat[:exit_signal], stat[:processor], stat[:rt_priority],
        stat[:policy] = File.open(proc_status_file, "r") {|f| f.read_nonblock(4096).strip }.scan(/\(.*?\)|\w+/)
        stat
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
oneapm_rpm-1.3.6 lib/one_apm/collector/support/proc_poller.rb
oneapm_rpm-1.3.6.rc1 lib/one_apm/collector/support/proc_poller.rb
oneapm_rpm-1.3.5 lib/one_apm/collector/support/proc_poller.rb
oneapm_rpm-1.3.5.rc1 lib/one_apm/collector/support/proc_poller.rb
oneapm_rpm-1.3.4 lib/one_apm/collector/support/proc_poller.rb
oneapm_rpm-1.3.4.rc1 lib/one_apm/collector/support/proc_poller.rb