procps-rb 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +17 -0
- data/README.md +63 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/procps/column.rb +42 -0
- data/lib/procps/column_types/command.rb +24 -0
- data/lib/procps/column_types/memsize.rb +31 -0
- data/lib/procps/column_types/scheduling_policy.rb +20 -0
- data/lib/procps/ps/columns.rb +139 -0
- data/lib/procps/ps/command_builder.rb +44 -0
- data/lib/procps/ps.rb +116 -0
- data/lib/procps/version.rb +3 -0
- data/lib/procps-rb.rb +1 -0
- data/lib/procps.rb +5 -0
- data/procps-rb.gemspec +24 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c55efb3a9bcec1af52ee2a38bc166d5e99b6a876
|
4
|
+
data.tar.gz: 4c5b83ba9f7661a5b9f6d669dc3fda97d94f3af7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b1b7ac964ac7a9d139e27746c86975c4c7924d0be766472aa1dce420035c214f33ef78aa4ed6dc41a7ce3ac4130191f852930eb6c8b09115a3e5210b97f96d23
|
7
|
+
data.tar.gz: af7fc2691e0775aec77ce954f1438b1191360f76f0e5cc3c6a629baec33465b37ee304276d14a85739a2c264b86c3d4753f6dd6fb9f64f312da692d3b0089088
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in ps-rb.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
# To use a debugger
|
7
|
+
gem 'byebug', group: [:development, :test]
|
8
|
+
|
9
|
+
group :development, :test do
|
10
|
+
# You need these
|
11
|
+
gem "rspec"
|
12
|
+
gem "benchmark-ips"
|
13
|
+
gem "sourcify"
|
14
|
+
gem "awesome_print"
|
15
|
+
gem "pry"
|
16
|
+
gem "pry-doc"
|
17
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Procps
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/procps`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'procps-rb'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install procps-rb
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
|
27
|
+
Procps::PS.new("/bin/ps").select(:pid, :ppid, :rss, :command).where(user: 'root').sort("+rss").to_a
|
28
|
+
|
29
|
+
Procps::PS.new("/bin/ps").select(:pid, :ppid, :rss, :command).where(user: 'root').with_args(m: true).take(3)
|
30
|
+
|
31
|
+
# => [
|
32
|
+
# {
|
33
|
+
# :pid => 269,
|
34
|
+
# :ppid => 1,
|
35
|
+
# :rss => 127.42M,
|
36
|
+
# :command => /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mds_stores
|
37
|
+
# },
|
38
|
+
# {
|
39
|
+
# :pid => 83,
|
40
|
+
# :ppid => 1,
|
41
|
+
# :rss => 109.88M,
|
42
|
+
# :command => /System/Library/CoreServices/launchservicesd
|
43
|
+
# },
|
44
|
+
# {
|
45
|
+
# :pid => 175,
|
46
|
+
# :ppid => 1,
|
47
|
+
# :rss => 105.12M,
|
48
|
+
# :command => /System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/Resources/WindowServer -daemon
|
49
|
+
# }
|
50
|
+
# ]
|
51
|
+
|
52
|
+
```
|
53
|
+
|
54
|
+
## Development
|
55
|
+
|
56
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
57
|
+
|
58
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/estum/procps-rb.
|
63
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "procps"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
12
|
+
|
13
|
+
# require "irb"
|
14
|
+
# IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module Procps
|
2
|
+
class Column
|
3
|
+
attr_reader :header
|
4
|
+
alias :to_s :header
|
5
|
+
|
6
|
+
def initialize(header, cast = nil, &cast_block)
|
7
|
+
@cast = block_given? ? cast_block : cast
|
8
|
+
raise ArgumentError, "a value of the :cast option must respond to a #call method or be nil" unless @cast.nil? || @cast.respond_to?(:call)
|
9
|
+
@header = header.to_s.freeze
|
10
|
+
freeze
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(value)
|
14
|
+
@cast.nil? ? value : @cast.call(value)
|
15
|
+
rescue => e
|
16
|
+
warn e
|
17
|
+
value
|
18
|
+
end
|
19
|
+
|
20
|
+
class Type
|
21
|
+
attr_reader :original
|
22
|
+
alias :to_s :original
|
23
|
+
|
24
|
+
def initialize(value)
|
25
|
+
@original = value
|
26
|
+
normalize if self.class.method_defined?(:normalize)
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_h
|
30
|
+
Hash[instance_variables.map { |name| [name[1..-1].to_sym, instance_variable_get(name)] }]
|
31
|
+
end
|
32
|
+
|
33
|
+
def inspect
|
34
|
+
"<#{self.class}: #{to_h.inspect}>"
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.call(value)
|
38
|
+
new(value)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'procps/column'
|
2
|
+
|
3
|
+
module Procps
|
4
|
+
class Command < Column::Type
|
5
|
+
attr_reader :name, :arguments, :title
|
6
|
+
alias :to_s :name
|
7
|
+
alias :args :arguments
|
8
|
+
|
9
|
+
def inspect
|
10
|
+
original
|
11
|
+
end
|
12
|
+
|
13
|
+
protected def normalize
|
14
|
+
if @original =~ /(?<=^\[)(?<name>.+?)(?=\]$)/
|
15
|
+
@name = name
|
16
|
+
elsif @original =~ /^(?<name>[A-Za-z0-9_\-]+): (?<title>.+)$/
|
17
|
+
@name = name
|
18
|
+
@title = title
|
19
|
+
else
|
20
|
+
@name, *@arguments = Shellwords.split(@original)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'procps/column'
|
2
|
+
|
3
|
+
module Procps
|
4
|
+
class Memsize < DelegateClass(Integer)
|
5
|
+
def self.call(value)
|
6
|
+
new(value)
|
7
|
+
end
|
8
|
+
|
9
|
+
SUFFIXES = %w(B K M G T)
|
10
|
+
|
11
|
+
def initialize(value)
|
12
|
+
super(value.to_i * 1024)
|
13
|
+
end
|
14
|
+
|
15
|
+
def human
|
16
|
+
step = 0
|
17
|
+
output = __getobj__.to_f
|
18
|
+
|
19
|
+
while output > 1024
|
20
|
+
step += 1
|
21
|
+
output = output / 1024
|
22
|
+
break if step == SUFFIXES.size
|
23
|
+
end
|
24
|
+
|
25
|
+
fmt = output == output.to_i ? "%d%s" : "%.2f%s"
|
26
|
+
format(fmt, output, SUFFIXES[step])
|
27
|
+
end
|
28
|
+
|
29
|
+
alias :inspect :human
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'procps/column'
|
2
|
+
|
3
|
+
module Procps
|
4
|
+
class SchedulingPolicy < Column::Type
|
5
|
+
attr_reader :value
|
6
|
+
alias :to_sym :value
|
7
|
+
|
8
|
+
POLICIES = %i(SCHED_OTHER SCHED_FIFO SCHED_RR SCHED_BATCH SCHED_ISO SCHED_IDLE).freeze
|
9
|
+
NORMALIZED_MAP = {"TS" => POLICIES[0], "FF" => POLICIES[1], "RR" => POLICIES[2],
|
10
|
+
"B" => POLICIES[3], "ISO" => POLICIES[4], "IDL" => POLICIES[5],
|
11
|
+
"?" => :unknown_value, "-" => :not_reported }.freeze
|
12
|
+
|
13
|
+
private_constant :POLICIES
|
14
|
+
private_constant :NORMALIZED_MAP
|
15
|
+
|
16
|
+
protected def normalize
|
17
|
+
@value = @original.is_a?(Integer) ? POLICIES[@original] : NORMALIZED_MAP[@original]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
require 'procps/column'
|
2
|
+
require 'procps/column_types/command'
|
3
|
+
require 'procps/column_types/memsize'
|
4
|
+
require 'procps/column_types/scheduling_policy'
|
5
|
+
|
6
|
+
module Procps
|
7
|
+
class PS
|
8
|
+
Address = -> (base = 10, null: "-".freeze) { -> (v) { v.is_a?(String) ? v == null ? nil : v.to_i(base) : v } }
|
9
|
+
Address_10 = Address[]
|
10
|
+
Address_16 = Address[16]
|
11
|
+
# KiB = -> (v) { v.to_i * 1024 }
|
12
|
+
|
13
|
+
@@columns = {
|
14
|
+
:blocked => Column.new("BLOCKED"),
|
15
|
+
:bsdstart => Column.new("START"),
|
16
|
+
:bsdtime => Column.new("TIME"),
|
17
|
+
:c => Column.new("C", &:to_f),
|
18
|
+
:caught => Column.new("CAUGHT"),
|
19
|
+
:cgroup => Column.new("CGROUP"),
|
20
|
+
:cls => Column.new("CLS", SchedulingPolicy),
|
21
|
+
:cmd => Column.new("CMD", Command),
|
22
|
+
:comm => Column.new("COMMAND"),
|
23
|
+
:command => Column.new("COMMAND", Command),
|
24
|
+
:cp => Column.new("CP") { |v| v.to_f / 100_0 }, # tenths of a percent
|
25
|
+
:egid => Column.new("EGID", &:to_i),
|
26
|
+
:egroup => Column.new("EGROUP"),
|
27
|
+
:eip => Column.new("EIP"),
|
28
|
+
:esp => Column.new("ESP"),
|
29
|
+
:etime => Column.new("ELAPSED"),
|
30
|
+
:etimes => Column.new("ELAPSED", &:to_i),
|
31
|
+
:f => Column.new("F", &:to_i),
|
32
|
+
:fgid => Column.new("FGID", &:to_i),
|
33
|
+
:fgroup => Column.new("FGROUP"),
|
34
|
+
:fname => Column.new("FNAME"),
|
35
|
+
:fuid => Column.new("FUID", &:to_i),
|
36
|
+
:fuser => Column.new("FUSER"),
|
37
|
+
:gid => Column.new("GID", &:to_i),
|
38
|
+
:group => Column.new("GROUP"),
|
39
|
+
:ignored => Column.new("IGNORED"),
|
40
|
+
:ipcns => Column.new("IPCNS", Address_10),
|
41
|
+
:label => Column.new("LABEL"),
|
42
|
+
:lstart => Column.new("STARTED"),
|
43
|
+
:lsession => Column.new("SESSION"),
|
44
|
+
:lwp => Column.new("LWP", &:to_i),
|
45
|
+
:machine => Column.new("MACHINE"),
|
46
|
+
:maj_flt => Column.new("MAJFLT"),
|
47
|
+
:min_flt => Column.new("MINFLT"),
|
48
|
+
:mntns => Column.new("MNTNS", Address_10),
|
49
|
+
:netns => Column.new("NETNS", Address_10),
|
50
|
+
:ni => Column.new("NI", &:to_i),
|
51
|
+
:nlwp => Column.new("NLWP", &:to_i),
|
52
|
+
:nwchan => Column.new("WCHAN", Address_16),
|
53
|
+
:ouid => Column.new("OWNER", &:to_i),
|
54
|
+
:pending => Column.new("PENDING"),
|
55
|
+
:pcpu => Column.new("%CPU", &:to_f),
|
56
|
+
:pgid => Column.new("PGID", &:to_i),
|
57
|
+
:pgrp => Column.new("PGRP", &:to_i),
|
58
|
+
:pid => Column.new("PID", &:to_i),
|
59
|
+
:pidns => Column.new("PIDNS", &:to_i),
|
60
|
+
:pmem => Column.new("%MEM", &:to_f),
|
61
|
+
:policy => Column.new("POL", SchedulingPolicy),
|
62
|
+
:ppid => Column.new("PPID", &:to_i),
|
63
|
+
:pri => Column.new("PRI", &:to_i),
|
64
|
+
:psr => Column.new("PSR", &:to_i),
|
65
|
+
:rgid => Column.new("RGID", &:to_i),
|
66
|
+
:rgroup => Column.new("RGROUP"),
|
67
|
+
:rss => Column.new("RSS", Memsize),
|
68
|
+
:rtprio => Column.new("RTPRIO", Address_10),
|
69
|
+
:ruid => Column.new("RUID", &:to_i),
|
70
|
+
:ruser => Column.new("RUSER"),
|
71
|
+
:s => Column.new("S"),
|
72
|
+
:sched => Column.new("SCH") { |v| SchedulingPolicy.(v.to_i) },
|
73
|
+
:seat => Column.new("SEAT"),
|
74
|
+
:sess => Column.new("SESS", &:to_i),
|
75
|
+
:sgi_p => Column.new("SGI_P", Address[null: "*".freeze]),
|
76
|
+
:sgid => Column.new("SGID", &:to_i),
|
77
|
+
:sgroup => Column.new("SGROUP"),
|
78
|
+
:sid => Column.new("SID", &:to_i),
|
79
|
+
:size => Column.new("SIZE", &:to_i),
|
80
|
+
:slice => Column.new("SLICE"),
|
81
|
+
:spid => Column.new("SPID", &:to_i),
|
82
|
+
:stackp => Column.new("STACKP", Address_16),
|
83
|
+
:start => Column.new("STARTED"),
|
84
|
+
:start_time => Column.new("START_TIME"),
|
85
|
+
:stat => Column.new("STAT"),
|
86
|
+
:suid => Column.new("SUID", &:to_i),
|
87
|
+
:suser => Column.new("SUSER"),
|
88
|
+
:supgid => Column.new("SUPGID", &:to_i),
|
89
|
+
:supgrp => Column.new("SUPGRP"),
|
90
|
+
:svgid => Column.new("SVGID", &:to_i),
|
91
|
+
:svuid => Column.new("SVUID", &:to_i),
|
92
|
+
:sz => Column.new("SZ", &:to_i),
|
93
|
+
:tgid => Column.new("TGID", &:to_i),
|
94
|
+
:thcount => Column.new("THCNT", &:to_i),
|
95
|
+
:tid => Column.new("TID", &:to_i),
|
96
|
+
:time => Column.new("TIME"),
|
97
|
+
:tname => Column.new("TTY"),
|
98
|
+
:tpgid => Column.new("TPGID", &:to_i),
|
99
|
+
:tt => Column.new("TT"),
|
100
|
+
:ucmd => Column.new("CMD"),
|
101
|
+
:uid => Column.new("UID", &:to_i),
|
102
|
+
:unit => Column.new("UNIT"),
|
103
|
+
:user => Column.new("USER"),
|
104
|
+
:userns => Column.new("USERNS", &:to_i),
|
105
|
+
:utsns => Column.new("UTSNS", &:to_i),
|
106
|
+
:uunit => Column.new("UUNIT"),
|
107
|
+
:vsz => Column.new("VSZ", Memsize)
|
108
|
+
}.tap do |cols|
|
109
|
+
cols.update(
|
110
|
+
:'%cpu' => cols.fetch(:pcpu),
|
111
|
+
:'%mem' => cols.fetch(:pmem),
|
112
|
+
:args => cols.fetch(:command),
|
113
|
+
:class => cols.fetch(:cls),
|
114
|
+
:cputime => cols.fetch(:time),
|
115
|
+
:flag => cols.fetch(:f),
|
116
|
+
:flags => cols.fetch(:f),
|
117
|
+
:nice => cols.fetch(:ni),
|
118
|
+
:rssize => cols.fetch(:rss),
|
119
|
+
:rsz => cols.fetch(:rss),
|
120
|
+
:sig => cols.fetch(:pending),
|
121
|
+
:sig_block => cols.fetch(:blocked),
|
122
|
+
:sigmask => cols.fetch(:blocked),
|
123
|
+
:sig_catch => cols.fetch(:caught),
|
124
|
+
:sigcatch => cols.fetch(:caught),
|
125
|
+
:sig_ignore => cols.fetch(:ignored),
|
126
|
+
:sigignore => cols.fetch(:ignored),
|
127
|
+
:state => cols.fetch(:s),
|
128
|
+
:tty => cols.fetch(:tt),
|
129
|
+
:ucomm => cols.fetch(:comm),
|
130
|
+
:uname => cols.fetch(:user),
|
131
|
+
:vsize => cols.fetch(:vsz)
|
132
|
+
)
|
133
|
+
end
|
134
|
+
|
135
|
+
def self.columns
|
136
|
+
@@columns
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
|
3
|
+
module Procps
|
4
|
+
class PS
|
5
|
+
class CommandBuilder
|
6
|
+
def initialize(ps)
|
7
|
+
@ps = ps
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
Shellwords.join([@ps.bin_path, *modifiers, *options].compact)
|
12
|
+
end
|
13
|
+
|
14
|
+
def modifiers
|
15
|
+
@modifiers ||= @ps.modifiers.size > 0 ? @ps.modifiers.to_a * "" : nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def options
|
19
|
+
@options ||= begin
|
20
|
+
@ps.options.flat_map do |opt, value|
|
21
|
+
case value
|
22
|
+
when false then nil
|
23
|
+
when true then normalize_option_key(opt)
|
24
|
+
else [normalize_option_key(opt), normalize_option_value(value)]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def normalize_option_key(opt)
|
33
|
+
opt.size == 1 ? "-#{opt}" : "--#{opt}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def normalize_option_value(value)
|
37
|
+
case value
|
38
|
+
when Array then value.join(",")
|
39
|
+
else value.to_s
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/procps/ps.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'procps/ps/columns'
|
2
|
+
require 'procps/ps/command_builder'
|
3
|
+
|
4
|
+
module Procps
|
5
|
+
class PS
|
6
|
+
DEFAULT_BIN_PATH = "/usr/bin/ps"
|
7
|
+
DEFAULT_COLUMNS = %i(pid rss pcpu)
|
8
|
+
|
9
|
+
attr_accessor :bin_path, :options, :modifiers
|
10
|
+
|
11
|
+
def initialize(bin_path = nil)
|
12
|
+
@bin_path = bin_path || DEFAULT_BIN_PATH
|
13
|
+
@options = { o: [] }
|
14
|
+
@modifiers = Set.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def sum
|
18
|
+
@modifiers << "S"
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def select(*columns)
|
23
|
+
@options[:o].concat(columns)
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def where(
|
28
|
+
command: nil,
|
29
|
+
group: nil,
|
30
|
+
user: nil,
|
31
|
+
pid: nil,
|
32
|
+
ppid: nil,
|
33
|
+
sid: nil,
|
34
|
+
tty: nil,
|
35
|
+
real_group: nil,
|
36
|
+
real_user: nil
|
37
|
+
)
|
38
|
+
@options[:C] = Array(command) if command
|
39
|
+
@options[:g] = Array(group) if group
|
40
|
+
@options[:u] = Array(user) if user
|
41
|
+
@options[:p] = Array(pid) if pid
|
42
|
+
@options[:ppid] = Array(ppid) if ppid
|
43
|
+
@options[:s] = Array(sid) if sid
|
44
|
+
@options[:t] = Array(tty) if tty
|
45
|
+
@options[:G] = Array(real_group) if real_group
|
46
|
+
@options[:U] = Array(real_user) if real_user
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
def limit(n)
|
51
|
+
@limit = n
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def take(n = 1)
|
56
|
+
limit(n).to_a
|
57
|
+
end
|
58
|
+
|
59
|
+
def with_args(**args)
|
60
|
+
@options.merge!(args)
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
def sort(*orders)
|
65
|
+
(@options[:sort] ||= []).concat(orders)
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
def reset
|
70
|
+
@result = nil
|
71
|
+
self
|
72
|
+
end
|
73
|
+
|
74
|
+
def load
|
75
|
+
@result ||= exec_command
|
76
|
+
end
|
77
|
+
|
78
|
+
alias :to_a :load
|
79
|
+
|
80
|
+
def columns
|
81
|
+
@columns ||= @options[:o].map(&@@columns)
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def to_command
|
87
|
+
@command ||= begin
|
88
|
+
@options[:o].concat(DEFAULT_COLUMNS) unless @options[:o].size > 0
|
89
|
+
CommandBuilder.new(self).call
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def exec_command
|
94
|
+
p to_command
|
95
|
+
|
96
|
+
headers, *rows = IO.popen(to_command, "r") do |io|
|
97
|
+
io.readlines
|
98
|
+
end
|
99
|
+
|
100
|
+
rows.replace(rows.take(@limit)) if @limit
|
101
|
+
|
102
|
+
# headers = headers.split(/\s+/, columns.size)
|
103
|
+
rows.map! do |row|
|
104
|
+
parse_result_row(row)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def parse_result_row(row)
|
109
|
+
process = {}
|
110
|
+
row.strip.split(/\s+/, columns.size).each_with_index do |col, i|
|
111
|
+
process[@options[:o][i]] = columns[i].(col)
|
112
|
+
end
|
113
|
+
process
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
data/lib/procps-rb.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "procps"
|
data/lib/procps.rb
ADDED
data/procps-rb.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'procps/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "procps-rb"
|
8
|
+
spec.version = Procps::VERSION
|
9
|
+
spec.authors = ["Anton Semenov"]
|
10
|
+
spec.email = ["anton.estum@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby procps wrapper.}
|
13
|
+
spec.description = %q{Ruby procps wrapper.}
|
14
|
+
spec.homepage = "https://github.com/estum/procps-rb"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: procps-rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anton Semenov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Ruby procps wrapper.
|
42
|
+
email:
|
43
|
+
- anton.estum@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- ".travis.yml"
|
51
|
+
- Gemfile
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/console
|
55
|
+
- bin/setup
|
56
|
+
- lib/procps-rb.rb
|
57
|
+
- lib/procps.rb
|
58
|
+
- lib/procps/column.rb
|
59
|
+
- lib/procps/column_types/command.rb
|
60
|
+
- lib/procps/column_types/memsize.rb
|
61
|
+
- lib/procps/column_types/scheduling_policy.rb
|
62
|
+
- lib/procps/ps.rb
|
63
|
+
- lib/procps/ps/columns.rb
|
64
|
+
- lib/procps/ps/command_builder.rb
|
65
|
+
- lib/procps/version.rb
|
66
|
+
- procps-rb.gemspec
|
67
|
+
homepage: https://github.com/estum/procps-rb
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 2.5.1
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: Ruby procps wrapper.
|
91
|
+
test_files: []
|
92
|
+
has_rdoc:
|