class OneApm::Collector::ProcPoller

Constants

OA_UPTIME_PATH

Public Class Methods

enable?() click to toggle source
# File lib/one_apm/collector/support/proc_poller.rb, line 10
def self.enable?
  ::RUBY_PLATFORM.downcase =~ /linux/
end

Public Instance Methods

cpu_utilization() click to toggle source
# File lib/one_apm/collector/support/proc_poller.rb, line 19
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
poll_memory() click to toggle source
# File lib/one_apm/collector/support/proc_poller.rb, line 14
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
to_s() click to toggle source
# File lib/one_apm/collector/support/proc_poller.rb, line 31
def to_s
  "ProcPoller from: #{proc_status_file}"
end

Private Instance Methods

proc_status_file() click to toggle source
# File lib/one_apm/collector/support/proc_poller.rb, line 41
def proc_status_file
  "/proc/#{pid}/stat"
end
stats() click to toggle source
# File lib/one_apm/collector/support/proc_poller.rb, line 45
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
uptime() click to toggle source

in seconds

# File lib/one_apm/collector/support/proc_poller.rb, line 37
def uptime
  File.read(OA_UPTIME_PATH).split[0].to_f
end