Class: Cloudscopes::Process::SystemProcess

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

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ SystemProcess

Returns a new instance of SystemProcess.



8
9
10
11
12
# File 'lib/cloudscopes/process.rb', line 8

def initialize(id)
  @id = id.to_i
  @@maxpid ||= File.read('/proc/sys/kernel/pid_max').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)


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

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



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

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



41
42
43
# File 'lib/cloudscopes/process.rb', line 41

def exe_name
  File.basename(exe)
end

#mem_usage_rssObject



57
58
59
# File 'lib/cloudscopes/process.rb', line 57

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



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

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

#procpath(field = nil) ⇒ Object



14
15
16
# File 'lib/cloudscopes/process.rb', line 14

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

#uidObject



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

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

#userObject



53
54
55
# File 'lib/cloudscopes/process.rb', line 53

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