Class: God::System::SlashProcPoller

Inherits:
PortablePoller show all
Defined in:
lib/god/system/slash_proc_poller.rb

Constant Summary collapse

MEMINFO_PATH =
'/proc/meminfo'
UPTIME_PATH =
'/proc/uptime'
REQUIRED_PATHS =
[MEMINFO_PATH, UPTIME_PATH].freeze
@@kb_per_page =

TODO: Need to make this portable

4
@@hertz =
100
@@total_mem =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid) ⇒ SlashProcPoller

Returns a new instance of SlashProcPoller.



24
25
26
27
28
29
30
31
32
# File 'lib/god/system/slash_proc_poller.rb', line 24

def initialize(pid)
  super(pid)
  return if @@total_mem

  # in K
  File.open(MEMINFO_PATH) do |f|
    @@total_mem = f.gets.split[1]
  end
end

Class Method Details

.readable?(path) ⇒ Boolean

Some systems (CentOS?) have a /proc, but they can hang when trying to read from them. Try to use this sparingly as it is expensive.

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/god/system/slash_proc_poller.rb', line 62

def self.readable?(path)
  Timeout.timeout(1) { File.read(path) }
rescue Timeout::Error
  false
end

.usable?Boolean

FreeBSD has /proc by default, but nothing mounted there! So we should check for the actual required paths! Returns true if REQUIRED_PATHS are readable.

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/god/system/slash_proc_poller.rb', line 18

def self.usable?
  REQUIRED_PATHS.all? do |path|
    test('r', path) && readable?(path)
  end
end

Instance Method Details

#memoryObject



34
35
36
37
38
# File 'lib/god/system/slash_proc_poller.rb', line 34

def memory
  stat[:rss].to_i * @@kb_per_page
rescue # This shouldn't fail is there's an error (or proc doesn't exist)
  0
end

#percent_cpuObject

TODO: Change this to calculate the wma instead



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/god/system/slash_proc_poller.rb', line 47

def percent_cpu
  stats = stat
  total_time = stats[:utime].to_i + stats[:stime].to_i # in jiffies
  seconds = uptime - (stats[:starttime].to_i / @@hertz)
  if seconds == 0
    0
  else
    ((total_time * 1000 / @@hertz) / seconds) / 10
  end
rescue # This shouldn't fail is there's an error (or proc doesn't exist)
  0
end

#percent_memoryObject



40
41
42
43
44
# File 'lib/god/system/slash_proc_poller.rb', line 40

def percent_memory
  (memory / @@total_mem.to_f) * 100
rescue # This shouldn't fail is there's an error (or proc doesn't exist)
  0
end