Class: Cloudscopes::Process::SystemProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudscopes/process.rb

Constant Summary collapse

@@maxpid =
File.read('/proc/sys/kernel/pid_max').to_i

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ SystemProcess

Returns a new instance of SystemProcess.



11
12
13
14
# File 'lib/cloudscopes/process.rb', line 11

def initialize(id)
  @id = id.to_i
  raise "Invalid system process id #{id}" unless @id > 0 && @id < @@maxpid
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cloudscopes/process.rb', line 20

def method_missing(name, *args)
  raise ArgumentError.new("wrong number of arguments (#{args.length} for 0)") unless args.length == 0
  begin
    File.read(procpath(name.to_s))
  rescue Errno::ENOENT => e # ignore kernel threads
    ''
  rescue SystemCallError => e # report and ignore
    $stderr.puts "Error accessing process #{@id}: #{e.class}:#{e.message}"
    ''
  end
end

Instance Method Details

#exeObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/cloudscopes/process.rb', line 32

def exe
  begin
    File.readlink(procpath('exe'))
  rescue Errno::ENOENT => e # ignore kernel threads
    ''
  rescue SystemCallError => e # report and ignore
    $stderr.puts "Error accessing process #{@id}: #{e.class}:#{e.message}"
    ''
  end
end

#exe_nameObject



43
44
45
# File 'lib/cloudscopes/process.rb', line 43

def exe_name
  File.basename(exe)
end

#mem_usage_rssObject



59
60
61
# File 'lib/cloudscopes/process.rb', line 59

def mem_usage_rss
  statm.strip.split(/\s+/)[1].to_i * Etc.sysconf(Etc::SC_PAGESIZE)
end

#mem_usage_virtObject Also known as: mem_usage



62
63
64
# File 'lib/cloudscopes/process.rb', line 62

def mem_usage_virt
  statm.strip.split(/\s+/)[0].to_i * Etc.sysconf(Etc::SC_PAGESIZE)
end

#procpath(field = nil) ⇒ Object



16
17
18
# File 'lib/cloudscopes/process.rb', line 16

def procpath(field = nil)
  "/proc/#{@id}/#{field}"
end

#uidObject



47
48
49
50
51
52
53
# File 'lib/cloudscopes/process.rb', line 47

def uid
  begin
    File.stat(procpath('mem')).uid
  rescue Errno::ENOENT => e
    nil
  end
end

#userObject



55
56
57
# File 'lib/cloudscopes/process.rb', line 55

def user
  Etc.getpwuid(uid || 0).name
end