Class: ProcessQueryLanguage::Backend::ProcessStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/process-query-language/backend/process-status.rb

Overview

This backend uses the ‘ps` utility and parses its output. Because `ps` uses whitespace as the delimiter (and truncates too wide fields) except the last, we need to sort the fields so that :command is the last.

Constant Summary collapse

FIELDS =
%w{ pid rss command }.map(&:to_sym)

Instance Method Summary collapse

Instance Method Details

#scan(fields) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/process-query-language/backend/process-status.rb', line 14

def scan(fields)
  result = []
  %x[ps -Aco #{FIELDS.join(',')}].lines.to_a[1..-1].each do |line|
    values = line.split(' ')
    process = {}
    FIELDS.each_with_index do |value, i|
      next unless fields.include?(FIELDS[i])
      process[FIELDS[i]] = convert(FIELDS[i], values[i])
    end
    result << process
  end

  return result
end