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
# File 'lib/cloudscopes/process.rb', line 8

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Raises:



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

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



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

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



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

def exe_name
  File.basename(exe)
end

#mem_usageObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cloudscopes/process.rb', line 56

def mem_usage
  # lots of magic follow, peruse at your own risk
  maps.strip.split("\n").collect do |l|
    l.split(/\s+/)
  end.select do |r|
    r[4].to_i == 0 && r[1] !~ /s/
  end.collect do |r|
    b,e = r[0].split(/-/,2).collect{ |a| a.to_i(16) }
    r[0] = e - b
    r
  end.inject(0) do |s,r|
    s += r[0]
  end
end

#procpath(field = nil) ⇒ Object



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

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

#uidObject



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

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

#userObject



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

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