Class: Procps::PS

Inherits:
Object
  • Object
show all
Defined in:
lib/procps/ps.rb,
lib/procps/ps/base_columns.rb,
lib/procps/ps/extra_columns.rb,
lib/procps/ps/command_builder.rb

Overview

By default it loads only base columns. To load extra columns require procps/ps/extra_columns after the gem is loaded.

You can also define custom columns with the Procps::PS.define_column method.

Defined Under Namespace

Classes: CommandBuilder

Constant Summary collapse

DEFAULT_BIN_PATH =
"/usr/bin/ps"
DEFAULT_COLUMNS =
%i(pid rss pcpu)
Address =
-> (base = 10, null: "-".freeze) { -> (v) { v.is_a?(String) ? v == null ? nil : v.to_i(base) : v } }
Address_10 =
Address[]
Address_16 =
Address[16]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bin_path = nil) ⇒ PS

Creates a Procps::PS object. Takes an argument with a bin path to ps command.



32
33
34
35
36
# File 'lib/procps/ps.rb', line 32

def initialize(bin_path = nil)
  @bin_path  = bin_path || DEFAULT_BIN_PATH
  @options   = { o: [] }
  @modifiers = Set.new
end

Instance Attribute Details

#bin_pathObject

Returns the value of attribute bin_path.



29
30
31
# File 'lib/procps/ps.rb', line 29

def bin_path
  @bin_path
end

#modifiersObject

Returns the value of attribute modifiers.



29
30
31
# File 'lib/procps/ps.rb', line 29

def modifiers
  @modifiers
end

#optionsObject

Returns the value of attribute options.



29
30
31
# File 'lib/procps/ps.rb', line 29

def options
  @options
end

Class Method Details

.alias_column(new_name, old_name) ⇒ Object

Creates an alias to a column



25
26
27
# File 'lib/procps/ps.rb', line 25

def self.alias_column(new_name, old_name)
  columns[new_name.to_sym] = old_name.to_sym
end

.columnsObject



13
14
15
# File 'lib/procps/ps.rb', line 13

def self.columns
  @@columns ||= {}
end

.define_column(name, header = nil, cast = nil, &cast_block) ⇒ Object

Define a column (see base columns in lib/procps/ps/base_columns.rb and extra columns in lib/procps/ps/extra_columns.rb)



19
20
21
22
# File 'lib/procps/ps.rb', line 19

def self.define_column(name, header = nil, cast = nil, &cast_block)
  header ||= name.to_s.upcase
  columns[name.downcase.to_sym] = Column.new(header, cast, &cast_block)
end

Instance Method Details

#columnsObject

List requested column objects with a typecast.



135
136
137
# File 'lib/procps/ps.rb', line 135

def columns
  @columns ||= @options[:o].map(&@@columns)
end

#limit(n) ⇒ Object

Limit processes list size



92
93
94
95
# File 'lib/procps/ps.rb', line 92

def limit(n)
  @limit = n
  self
end

#load(force = false) ⇒ Object Also known as: to_a

Executes a ps command & sets a result.



127
128
129
130
# File 'lib/procps/ps.rb', line 127

def load(force = false)
  reset if force
  @result ||= exec_command
end

#resetObject

Reset a result



121
122
123
124
# File 'lib/procps/ps.rb', line 121

def reset
  @result = nil
  self
end

#select(*columns) ⇒ Object

Select columns to list with ps command



39
40
41
42
43
44
45
46
47
48
# File 'lib/procps/ps.rb', line 39

def select(*columns)
  columns.each do |col|
    unless @@columns.include?(col)
      raise ArgumentError, "unknown column :#{col}, please add it manually to Procps::PS.columns."
    end

    @options[:o] << col
  end
  self
end

#sort(*orders) ⇒ Object

Set sorting option. Doesn’t supported by an original OSX ps (use with_args() method instead).

Example:

Procps::PS.new.select(:pid, :rss).sort("ppid", "-rss").to_a


115
116
117
118
# File 'lib/procps/ps.rb', line 115

def sort(*orders)
  (@options[:sort] ||= []).concat(orders)
  self
end

#sumObject

Sum a CPU time for parent proccesses



86
87
88
89
# File 'lib/procps/ps.rb', line 86

def sum
  @modifiers << "S"
  self
end

#take(n = 1) ⇒ Object

Limit processes list size & get result



98
99
100
# File 'lib/procps/ps.rb', line 98

def take(n = 1)
  limit(n).to_a
end

#where(command: nil, group: nil, user: nil, pid: nil, ppid: nil, sid: nil, tty: nil, real_group: nil, real_user: nil) ⇒ Object

Filter processes list with conditions

Available options:

  • :command

  • :group

  • :user

  • :pid

  • :ppid

  • :sid

  • :tty

  • :real_group

  • :real_user



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/procps/ps.rb', line 62

def where(
    command: nil,
    group: nil,
    user: nil,
    pid: nil,
    ppid: nil,
    sid: nil,
    tty: nil,
    real_group: nil,
    real_user: nil
  )
  @options[:C]    = Array(command)    if command
  @options[:g]    = Array(group)      if group
  @options[:u]    = Array(user)       if user
  @options[:p]    = Array(pid)        if pid
  @options[:ppid] = Array(ppid)       if ppid
  @options[:s]    = Array(sid)        if sid
  @options[:t]    = Array(tty)        if tty
  @options[:G]    = Array(real_group) if real_group
  @options[:U]    = Array(real_user)  if real_user
  self
end

#with_args(**args) ⇒ Object

Takes a hash of options to set custom ps arguments.

Example:

Procps::PS.new.select(:pid, :rss).with_args(m: true).to_a


106
107
108
109
# File 'lib/procps/ps.rb', line 106

def with_args(**args)
  @options.merge!(args)
  self
end