Class: Procps::PS
- Inherits:
-
Object
- Object
- Procps::PS
- Defined in:
- lib/procps/ps.rb,
lib/procps/ps/columns.rb,
lib/procps/ps/command_builder.rb
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]
- @@columns =
{ :blocked => Column.new("BLOCKED"), :bsdstart => Column.new("START"), :bsdtime => Column.new("TIME"), :c => Column.new("C", &:to_f), :caught => Column.new("CAUGHT"), :cgroup => Column.new("CGROUP"), :cls => Column.new("CLS", SchedulingPolicy), :cmd => Column.new("CMD", Command), :comm => Column.new("COMMAND"), :command => Column.new("COMMAND", Command), :cp => Column.new("CP") { |v| v.to_f / 100_0 }, # tenths of a percent :egid => Column.new("EGID", &:to_i), :egroup => Column.new("EGROUP"), :eip => Column.new("EIP"), :esp => Column.new("ESP"), :etime => Column.new("ELAPSED"), :etimes => Column.new("ELAPSED", &:to_i), :f => Column.new("F", &:to_i), :fgid => Column.new("FGID", &:to_i), :fgroup => Column.new("FGROUP"), :fname => Column.new("FNAME"), :fuid => Column.new("FUID", &:to_i), :fuser => Column.new("FUSER"), :gid => Column.new("GID", &:to_i), :group => Column.new("GROUP"), :ignored => Column.new("IGNORED"), :ipcns => Column.new("IPCNS", Address_10), :label => Column.new("LABEL"), :lstart => Column.new("STARTED"), :lsession => Column.new("SESSION"), :lwp => Column.new("LWP", &:to_i), :machine => Column.new("MACHINE"), :maj_flt => Column.new("MAJFLT"), :min_flt => Column.new("MINFLT"), :mntns => Column.new("MNTNS", Address_10), :netns => Column.new("NETNS", Address_10), :ni => Column.new("NI", &:to_i), :nlwp => Column.new("NLWP", &:to_i), :nwchan => Column.new("WCHAN", Address_16), :ouid => Column.new("OWNER", &:to_i), :pending => Column.new("PENDING"), :pcpu => Column.new("%CPU", &:to_f), :pgid => Column.new("PGID", &:to_i), :pgrp => Column.new("PGRP", &:to_i), :pid => Column.new("PID", &:to_i), :pidns => Column.new("PIDNS", &:to_i), :pmem => Column.new("%MEM", &:to_f), :policy => Column.new("POL", SchedulingPolicy), :ppid => Column.new("PPID", &:to_i), :pri => Column.new("PRI", &:to_i), :psr => Column.new("PSR", &:to_i), :rgid => Column.new("RGID", &:to_i), :rgroup => Column.new("RGROUP"), :rss => Column.new("RSS", Memsize), :rtprio => Column.new("RTPRIO", Address_10), :ruid => Column.new("RUID", &:to_i), :ruser => Column.new("RUSER"), :s => Column.new("S"), :sched => Column.new("SCH") { |v| SchedulingPolicy.(v.to_i) }, :seat => Column.new("SEAT"), :sess => Column.new("SESS", &:to_i), :sgi_p => Column.new("SGI_P", Address[null: "*".freeze]), :sgid => Column.new("SGID", &:to_i), :sgroup => Column.new("SGROUP"), :sid => Column.new("SID", &:to_i), :size => Column.new("SIZE", &:to_i), :slice => Column.new("SLICE"), :spid => Column.new("SPID", &:to_i), :stackp => Column.new("STACKP", Address_16), :start => Column.new("STARTED"), :start_time => Column.new("START_TIME"), :stat => Column.new("STAT"), :suid => Column.new("SUID", &:to_i), :suser => Column.new("SUSER"), :supgid => Column.new("SUPGID", &:to_i), :supgrp => Column.new("SUPGRP"), :svgid => Column.new("SVGID", &:to_i), :svuid => Column.new("SVUID", &:to_i), :sz => Column.new("SZ", &:to_i), :tgid => Column.new("TGID", &:to_i), :thcount => Column.new("THCNT", &:to_i), :tid => Column.new("TID", &:to_i), :time => Column.new("TIME"), :tname => Column.new("TTY"), :tpgid => Column.new("TPGID", &:to_i), :tt => Column.new("TT"), :ucmd => Column.new("CMD"), :uid => Column.new("UID", &:to_i), :unit => Column.new("UNIT"), :user => Column.new("USER"), :userns => Column.new("USERNS", &:to_i), :utsns => Column.new("UTSNS", &:to_i), :uunit => Column.new("UUNIT"), :vsz => Column.new("VSZ", Memsize) }.tap do |cols| cols.update( :'%cpu' => cols.fetch(:pcpu), :'%mem' => cols.fetch(:pmem), :args => cols.fetch(:command), :class => cols.fetch(:cls), :cputime => cols.fetch(:time), :flag => cols.fetch(:f), :flags => cols.fetch(:f), :nice => cols.fetch(:ni), :rssize => cols.fetch(:rss), :rsz => cols.fetch(:rss), :sig => cols.fetch(:pending), :sig_block => cols.fetch(:blocked), :sigmask => cols.fetch(:blocked), :sig_catch => cols.fetch(:caught), :sigcatch => cols.fetch(:caught), :sig_ignore => cols.fetch(:ignored), :sigignore => cols.fetch(:ignored), :state => cols.fetch(:s), :tty => cols.fetch(:tt), :ucomm => cols.fetch(:comm), :uname => cols.fetch(:user), :vsize => cols.fetch(:vsz) ) end
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
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.
12 13 14 15 16 |
# File 'lib/procps/ps.rb', line 12 def initialize(bin_path = nil) @bin_path = bin_path || DEFAULT_BIN_PATH @options = { o: [] } @modifiers = Set.new end |
Instance Attribute Details
#bin_path ⇒ Object
Returns the value of attribute bin_path.
9 10 11 |
# File 'lib/procps/ps.rb', line 9 def bin_path @bin_path end |
#modifiers ⇒ Object
Returns the value of attribute modifiers.
9 10 11 |
# File 'lib/procps/ps.rb', line 9 def modifiers @modifiers end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/procps/ps.rb', line 9 def @options end |
Class Method Details
.columns ⇒ Object
134 135 136 |
# File 'lib/procps/ps/columns.rb', line 134 def self.columns @@columns end |
Instance Method Details
#columns ⇒ Object
List requested column objects with a typecast.
115 116 117 |
# File 'lib/procps/ps.rb', line 115 def columns @columns ||= @options[:o].map(&@@columns) end |
#limit(n) ⇒ Object
Limit processes list size
72 73 74 75 |
# File 'lib/procps/ps.rb', line 72 def limit(n) @limit = n self end |
#load(force = false) ⇒ Object Also known as: to_a
Executes a ps command & sets a result.
107 108 109 110 |
# File 'lib/procps/ps.rb', line 107 def load(force = false) reset if force @result ||= exec_command end |
#reset ⇒ Object
Reset a result
101 102 103 104 |
# File 'lib/procps/ps.rb', line 101 def reset @result = nil self end |
#select(*columns) ⇒ Object
Select columns to list with ps command
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/procps/ps.rb', line 19 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
95 96 97 98 |
# File 'lib/procps/ps.rb', line 95 def sort(*orders) (@options[:sort] ||= []).concat(orders) self end |
#sum ⇒ Object
Sum a CPU time for parent proccesses
66 67 68 69 |
# File 'lib/procps/ps.rb', line 66 def sum @modifiers << "S" self end |
#take(n = 1) ⇒ Object
Limit processes list size & get result
78 79 80 |
# File 'lib/procps/ps.rb', line 78 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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/procps/ps.rb', line 42 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 |