Class: Procps::PS
- Inherits:
-
Object
- Object
- Procps::PS
- 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
-
#bin_path ⇒ Object
Returns the value of attribute bin_path.
-
#modifiers ⇒ Object
Returns the value of attribute modifiers.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
-
.alias_column(new_name, old_name) ⇒ Object
Creates an alias to a column.
- .columns ⇒ Object
-
.define_column(name, header = nil, cast = nil, &cast_block) ⇒ Object
Define a column (see base columns in
lib/procps/ps/base_columns.rband extra columns inlib/procps/ps/extra_columns.rb).
Instance Method Summary collapse
-
#columns ⇒ Object
List requested column objects with a typecast.
-
#initialize(bin_path = nil) ⇒ PS
constructor
Creates a Procps::PS object.
-
#limit(n) ⇒ Object
Limit processes list size.
-
#load(force = false) ⇒ Object
(also: #to_a)
Executes a ps command & sets a result.
-
#reset ⇒ Object
Reset a result.
-
#select(*columns) ⇒ Object
Select columns to list with ps command.
-
#sort(*orders) ⇒ Object
Set sorting option.
-
#sum ⇒ Object
Sum a CPU time for parent proccesses.
-
#take(n = 1) ⇒ Object
Limit processes list size & get result.
-
#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.
-
#with_args(**args) ⇒ Object
Takes a hash of options to set custom ps arguments.
Constructor Details
#initialize(bin_path = nil) ⇒ PS
Creates a Procps::PS object. Takes an argument with a bin path to ps command.
31 32 33 34 35 |
# File 'lib/procps/ps.rb', line 31 def initialize(bin_path = nil) @bin_path = bin_path || DEFAULT_BIN_PATH = { o: [] } @modifiers = Set.new end |
Instance Attribute Details
#bin_path ⇒ Object
Returns the value of attribute bin_path.
28 29 30 |
# File 'lib/procps/ps.rb', line 28 def bin_path @bin_path end |
#modifiers ⇒ Object
Returns the value of attribute modifiers.
28 29 30 |
# File 'lib/procps/ps.rb', line 28 def modifiers @modifiers end |
#options ⇒ Object
Returns the value of attribute options.
28 29 30 |
# File 'lib/procps/ps.rb', line 28 def end |
Class Method Details
.alias_column(new_name, old_name) ⇒ Object
Creates an alias to a column
24 25 26 |
# File 'lib/procps/ps.rb', line 24 def self.alias_column(new_name, old_name) columns[new_name.to_sym] = old_name.to_sym end |
.columns ⇒ Object
12 13 14 |
# File 'lib/procps/ps.rb', line 12 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)
18 19 20 21 |
# File 'lib/procps/ps.rb', line 18 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
#columns ⇒ Object
List requested column objects with a typecast.
134 135 136 |
# File 'lib/procps/ps.rb', line 134 def columns @columns ||= [:o].map(&@@columns) end |
#limit(n) ⇒ Object
Limit processes list size
91 92 93 94 |
# File 'lib/procps/ps.rb', line 91 def limit(n) @limit = n self end |
#load(force = false) ⇒ Object Also known as: to_a
Executes a ps command & sets a result.
126 127 128 129 |
# File 'lib/procps/ps.rb', line 126 def load(force = false) reset if force @result ||= exec_command end |
#reset ⇒ Object
Reset a result
120 121 122 123 |
# File 'lib/procps/ps.rb', line 120 def reset @result = nil self end |
#select(*columns) ⇒ Object
Select columns to list with ps command
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/procps/ps.rb', line 38 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 [: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
114 115 116 117 |
# File 'lib/procps/ps.rb', line 114 def sort(*orders) ([:sort] ||= []).concat(orders) self end |
#sum ⇒ Object
Sum a CPU time for parent proccesses
85 86 87 88 |
# File 'lib/procps/ps.rb', line 85 def sum @modifiers << "S" self end |
#take(n = 1) ⇒ Object
Limit processes list size & get result
97 98 99 |
# File 'lib/procps/ps.rb', line 97 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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/procps/ps.rb', line 61 def where( command: nil, group: nil, user: nil, pid: nil, ppid: nil, sid: nil, tty: nil, real_group: nil, real_user: nil ) [:C] = Array(command) if command [:g] = Array(group) if group [:u] = Array(user) if user [:p] = Array(pid) if pid [:ppid] = Array(ppid) if ppid [:s] = Array(sid) if sid [:t] = Array(tty) if tty [:G] = Array(real_group) if real_group [: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
105 106 107 108 |
# File 'lib/procps/ps.rb', line 105 def with_args(**args) .merge!(args) self end |