Class: Freyr::ProcessInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/freyr/process_info.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid) ⇒ ProcessInfo

Returns a new instance of ProcessInfo.



4
5
6
# File 'lib/freyr/process_info.rb', line 4

def initialize pid
  @pid = pid
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



3
4
5
# File 'lib/freyr/process_info.rb', line 3

def command
  @command
end

#pcpuObject (readonly)

Returns the value of attribute pcpu.



3
4
5
# File 'lib/freyr/process_info.rb', line 3

def pcpu
  @pcpu
end

#pidObject (readonly)

Returns the value of attribute pid.



3
4
5
# File 'lib/freyr/process_info.rb', line 3

def pid
  @pid
end

#pmemObject (readonly)

Returns the value of attribute pmem.



3
4
5
# File 'lib/freyr/process_info.rb', line 3

def pmem
  @pmem
end

#rssObject (readonly)

Returns the value of attribute rss.



3
4
5
# File 'lib/freyr/process_info.rb', line 3

def rss
  @rss
end

#ruserObject (readonly)

Returns the value of attribute ruser.



3
4
5
# File 'lib/freyr/process_info.rb', line 3

def ruser
  @ruser
end

#vszObject (readonly)

Returns the value of attribute vsz.



3
4
5
# File 'lib/freyr/process_info.rb', line 3

def vsz
  @vsz
end

Class Method Details

.[](*args) ⇒ Object



42
43
44
45
# File 'lib/freyr/process_info.rb', line 42

def [] *args
  n = new(*args)
  n.ps ? n : nil
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
# File 'lib/freyr/process_info.rb', line 8

def alive?
  Process.getpgid(pid)
  true
rescue Errno::ESRCH
  false
end

#mem_in_mbObject



37
38
39
# File 'lib/freyr/process_info.rb', line 37

def mem_in_mb
  @rss/1024.0
end

#portObject



31
32
33
34
35
# File 'lib/freyr/process_info.rb', line 31

def port
  `lsof -p #{pid} -P | egrep TCP.+LISTEN`.match(/\*:(\d+)/)[1].to_i
rescue
  nil
end

#psObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/freyr/process_info.rb', line 15

def ps
  return if !pid || pid.to_s.empty?
  @ps_info ||= begin
    info = `ps p #{pid} -o pid,rss,vsz,pmem,pcpu,ruser,command`
    match = info.match(/#{pid}\s+(\d+)\s+(\d+)\s+([\d\.]+)\s+([\d\.]+)\s+(\w+)\s+(.+)/)
    return unless match
    @rss = match[1].to_i
    @vsz = match[2].to_i
    @pmem = match[3].to_f
    @pcpu = match[4].to_f
    @ruser = match[5]
    @command = match[6]
    info
  end
end