Module: ProcessObserver::Linux

Defined in:
lib/process_observer/linux.rb

Overview

Module with methods to interact with Linux.

Constant Summary collapse

EXE =

Process list executable.

"ps"
OUTPUT_FORMAT =

Output format.

"-o \"pid,comm,stat,time,rss\""

Class Method Summary collapse

Class Method Details

.call(options = "-A") ⇒ Array<Process>

Call processes list executable with provided options string.

Parameters:

  • options (String) (defaults to: "-A")

    options string.

Returns:

  • (Array<Process>)

    array of processes.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/process_observer/linux.rb', line 21

def self.call(options = "-A")
  raise ArgumentError, "Provide options string", caller if options.to_s.empty?
  command = "#{EXE} #{options} #{OUTPUT_FORMAT}"
  Log.debug "Executing: #{command}"
  re = `#{command}`.chomp
  csv = CSV.parse(re.each_line.map { |line| line.strip.squeeze(" ") }.join("\n"), col_sep: " ")

  enum = csv.to_a.drop(1) # Skip header
  enum.map do |data|
    LinuxProcess.new(
      pid:  data[0],
      comm: data[1],
      stat: data[2],
      time: data[3],
      rss:  data[4]
    )
  end
end