sys-proctable 1.0.0-universal-mingw32 → 1.1.0-universal-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +2 -3
- data/CHANGES +367 -357
- data/MANIFEST +33 -34
- data/README +110 -118
- data/Rakefile +215 -218
- data/benchmarks/bench_ps.rb +21 -21
- data/doc/top.txt +46 -46
- data/examples/example_ps.rb +20 -20
- data/lib/sys-proctable.rb +1 -1
- data/lib/sys/proctable/version.rb +6 -6
- data/lib/sys/top.rb +31 -31
- data/lib/windows/sys/proctable.rb +207 -207
- data/sys-proctable.gemspec +40 -40
- data/test/test_sys_proctable_all.rb +98 -87
- data/test/test_sys_proctable_windows.rb +324 -324
- data/test/test_sys_top.rb +65 -46
- metadata +4 -4
- metadata.gz.sig +0 -0
data/benchmarks/bench_ps.rb
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
########################################################################
|
2
|
-
# bench_ps.rb
|
3
|
-
#
|
4
|
-
# Benchmark program to show overall speed and compare the block form
|
5
|
-
# versus the non-block form. You should run this benchmark via the
|
6
|
-
# 'rake bench' Rake task.
|
7
|
-
########################################################################
|
8
|
-
require 'benchmark'
|
9
|
-
require 'sys/proctable'
|
10
|
-
|
11
|
-
MAX = 10
|
12
|
-
|
13
|
-
Benchmark.bm do |bench|
|
14
|
-
bench.report("Block form"){
|
15
|
-
MAX.times{ Sys::ProcTable.ps{} }
|
16
|
-
}
|
17
|
-
|
18
|
-
bench.report("Non-block form"){
|
19
|
-
MAX.times{ Sys::ProcTable.ps }
|
20
|
-
}
|
21
|
-
end
|
1
|
+
########################################################################
|
2
|
+
# bench_ps.rb
|
3
|
+
#
|
4
|
+
# Benchmark program to show overall speed and compare the block form
|
5
|
+
# versus the non-block form. You should run this benchmark via the
|
6
|
+
# 'rake bench' Rake task.
|
7
|
+
########################################################################
|
8
|
+
require 'benchmark'
|
9
|
+
require 'sys/proctable'
|
10
|
+
|
11
|
+
MAX = 10
|
12
|
+
|
13
|
+
Benchmark.bm do |bench|
|
14
|
+
bench.report("Block form"){
|
15
|
+
MAX.times{ Sys::ProcTable.ps{} }
|
16
|
+
}
|
17
|
+
|
18
|
+
bench.report("Non-block form"){
|
19
|
+
MAX.times{ Sys::ProcTable.ps }
|
20
|
+
}
|
21
|
+
end
|
data/doc/top.txt
CHANGED
@@ -1,47 +1,47 @@
|
|
1
|
-
= Description
|
2
|
-
A simple 'top' interface for Ruby
|
3
|
-
|
4
|
-
= Prerequisites
|
5
|
-
Requires the "sys/proctable" package (which should be installed along
|
6
|
-
with this package).
|
7
|
-
|
8
|
-
= Synopsis
|
9
|
-
require "sys/top"
|
10
|
-
|
11
|
-
Sys::Top.top(5).each{ |ps|
|
12
|
-
p ps
|
13
|
-
}
|
14
|
-
|
15
|
-
= Constants
|
16
|
-
VERSION
|
17
|
-
Returns the version number of this package as a String.
|
18
|
-
|
19
|
-
= Class Methods
|
20
|
-
Sys::Top.top(number=10, field="pctcpu")
|
21
|
-
Returns an array of ProcTableStruct's. The size of the array (i.e. the
|
22
|
-
number of processes) that it returns is based on +number+, and sorted by
|
23
|
-
+pctcpu+. By default, the size and field values are 10 and "pctcpu",
|
24
|
-
respectively.
|
25
|
-
|
26
|
-
= Notes
|
27
|
-
Not all fields are available on all platforms. Please check your
|
28
|
-
platform specific documentation for which fields are available.
|
29
|
-
|
30
|
-
= Bugs
|
31
|
-
None that I'm aware of. Please log bug reports on the project page at
|
32
|
-
http://www.rubyforge.org/projects/sysutils
|
33
|
-
|
34
|
-
= License
|
35
|
-
Artistic 2.0
|
36
|
-
|
37
|
-
= Copyright
|
38
|
-
(C) 2004-2009 Daniel J. Berger
|
39
|
-
All Rights Reserved.
|
40
|
-
|
41
|
-
= Warranty
|
42
|
-
This package is provided "as is" and without any express or
|
43
|
-
implied warranties, including, without limitation, the implied
|
44
|
-
warranties of merchantability and fitness for a particular purpose.
|
45
|
-
|
46
|
-
= Author
|
1
|
+
= Description
|
2
|
+
A simple 'top' interface for Ruby
|
3
|
+
|
4
|
+
= Prerequisites
|
5
|
+
Requires the "sys/proctable" package (which should be installed along
|
6
|
+
with this package).
|
7
|
+
|
8
|
+
= Synopsis
|
9
|
+
require "sys/top"
|
10
|
+
|
11
|
+
Sys::Top.top(5).each{ |ps|
|
12
|
+
p ps
|
13
|
+
}
|
14
|
+
|
15
|
+
= Constants
|
16
|
+
VERSION
|
17
|
+
Returns the version number of this package as a String.
|
18
|
+
|
19
|
+
= Class Methods
|
20
|
+
Sys::Top.top(number=10, field="pctcpu")
|
21
|
+
Returns an array of ProcTableStruct's. The size of the array (i.e. the
|
22
|
+
number of processes) that it returns is based on +number+, and sorted by
|
23
|
+
+pctcpu+. By default, the size and field values are 10 and "pctcpu",
|
24
|
+
respectively.
|
25
|
+
|
26
|
+
= Notes
|
27
|
+
Not all fields are available on all platforms. Please check your
|
28
|
+
platform specific documentation for which fields are available.
|
29
|
+
|
30
|
+
= Bugs
|
31
|
+
None that I'm aware of. Please log bug reports on the project page at
|
32
|
+
http://www.rubyforge.org/projects/sysutils
|
33
|
+
|
34
|
+
= License
|
35
|
+
Artistic 2.0
|
36
|
+
|
37
|
+
= Copyright
|
38
|
+
(C) 2004-2009 Daniel J. Berger
|
39
|
+
All Rights Reserved.
|
40
|
+
|
41
|
+
= Warranty
|
42
|
+
This package is provided "as is" and without any express or
|
43
|
+
implied warranties, including, without limitation, the implied
|
44
|
+
warranties of merchantability and fitness for a particular purpose.
|
45
|
+
|
46
|
+
= Author
|
47
47
|
Daniel J. Berger
|
data/examples/example_ps.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
#######################################################################
|
2
|
-
# example_ps.rb
|
3
|
-
#
|
4
|
-
# Generic test program that demonstrates the use of ProcTable.ps. You
|
5
|
-
# can run this via the 'rake example' task.
|
6
|
-
#
|
7
|
-
# Modify as you see fit
|
8
|
-
#######################################################################
|
9
|
-
require 'sys/proctable'
|
10
|
-
include Sys
|
11
|
-
|
12
|
-
puts "VERSION: " + ProcTable::VERSION
|
13
|
-
sleep 2
|
14
|
-
|
15
|
-
ProcTable.ps{ |s|
|
16
|
-
ProcTable.fields.each{ |field|
|
17
|
-
puts "#{field}: " + s.send(field).to_s
|
18
|
-
}
|
19
|
-
puts '=' * 30
|
20
|
-
}
|
1
|
+
#######################################################################
|
2
|
+
# example_ps.rb
|
3
|
+
#
|
4
|
+
# Generic test program that demonstrates the use of ProcTable.ps. You
|
5
|
+
# can run this via the 'rake example' task.
|
6
|
+
#
|
7
|
+
# Modify as you see fit
|
8
|
+
#######################################################################
|
9
|
+
require 'sys/proctable'
|
10
|
+
include Sys
|
11
|
+
|
12
|
+
puts "VERSION: " + ProcTable::VERSION
|
13
|
+
sleep 2
|
14
|
+
|
15
|
+
ProcTable.ps{ |s|
|
16
|
+
ProcTable.fields.each{ |field|
|
17
|
+
puts "#{field}: " + s.send(field).to_s
|
18
|
+
}
|
19
|
+
puts '=' * 30
|
20
|
+
}
|
data/lib/sys-proctable.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require 'sys/proctable'
|
1
|
+
require 'sys/proctable'
|
@@ -1,6 +1,6 @@
|
|
1
|
-
module Sys
|
2
|
-
class ProcTable
|
3
|
-
# The version of the sys-proctable library
|
4
|
-
VERSION = '1.
|
5
|
-
end
|
6
|
-
end
|
1
|
+
module Sys
|
2
|
+
class ProcTable
|
3
|
+
# The version of the sys-proctable library
|
4
|
+
VERSION = '1.1.0'.freeze
|
5
|
+
end
|
6
|
+
end
|
data/lib/sys/top.rb
CHANGED
@@ -1,31 +1,31 @@
|
|
1
|
-
require 'sys/proctable'
|
2
|
-
|
3
|
-
# The Sys module serves as a namespace only
|
4
|
-
module Sys
|
5
|
-
|
6
|
-
# The Top class serves as a toplevel name for the 'top' method.
|
7
|
-
class Top
|
8
|
-
|
9
|
-
# The version of the sys-top library
|
10
|
-
VERSION = '1.0.4'
|
11
|
-
|
12
|
-
# Returns an array of Struct::ProcTableStruct elements containing up
|
13
|
-
# to +num+ elements, sorted by +field+. The default number of elements
|
14
|
-
# is 10, while the default field is 'pctcpu'.
|
15
|
-
#
|
16
|
-
# Exception: the default sort field is 'pid' on AIX and Windows.
|
17
|
-
#
|
18
|
-
def self.top(num=10, field='pctcpu')
|
19
|
-
field = field.to_s if field.is_a?(Symbol)
|
20
|
-
|
21
|
-
aix = RbConfig::CONFIG['host_os'] =~ /aix/i
|
22
|
-
|
23
|
-
# Sort by pid on Windows and AIX by default
|
24
|
-
if (File::ALT_SEPARATOR || aix) && field == 'pctcpu'
|
25
|
-
field = 'pid'
|
26
|
-
end
|
27
|
-
|
28
|
-
Sys::ProcTable.ps.sort_by{ |obj| obj.send(field) || '' }[0..num-1]
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
1
|
+
require 'sys/proctable'
|
2
|
+
|
3
|
+
# The Sys module serves as a namespace only
|
4
|
+
module Sys
|
5
|
+
|
6
|
+
# The Top class serves as a toplevel name for the 'top' method.
|
7
|
+
class Top
|
8
|
+
|
9
|
+
# The version of the sys-top library
|
10
|
+
VERSION = '1.0.4'
|
11
|
+
|
12
|
+
# Returns an array of Struct::ProcTableStruct elements containing up
|
13
|
+
# to +num+ elements, sorted by +field+. The default number of elements
|
14
|
+
# is 10, while the default field is 'pctcpu'.
|
15
|
+
#
|
16
|
+
# Exception: the default sort field is 'pid' on AIX and Windows.
|
17
|
+
#
|
18
|
+
def self.top(num=10, field='pctcpu')
|
19
|
+
field = field.to_s if field.is_a?(Symbol)
|
20
|
+
|
21
|
+
aix = RbConfig::CONFIG['host_os'] =~ /aix/i
|
22
|
+
|
23
|
+
# Sort by pid on Windows and AIX by default
|
24
|
+
if (File::ALT_SEPARATOR || aix) && field == 'pctcpu'
|
25
|
+
field = 'pid'
|
26
|
+
end
|
27
|
+
|
28
|
+
Sys::ProcTable.ps.sort_by{ |obj| obj.send(field) || '' }[0..num-1]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -1,207 +1,207 @@
|
|
1
|
-
require 'win32ole'
|
2
|
-
require 'socket'
|
3
|
-
require 'date'
|
4
|
-
require 'sys/proctable/version'
|
5
|
-
|
6
|
-
# The Sys module serves as a namespace only
|
7
|
-
module Sys
|
8
|
-
|
9
|
-
# The ProcTable class encapsulates process table information
|
10
|
-
class ProcTable
|
11
|
-
|
12
|
-
# There is no constructor
|
13
|
-
private_class_method :new
|
14
|
-
|
15
|
-
# Error typically raised if one of the Sys::ProcTable methods fails
|
16
|
-
class Error < StandardError; end
|
17
|
-
|
18
|
-
# The comm field corresponds to the 'name' field. The 'cmdline' field
|
19
|
-
# is the CommandLine attribute on Windows XP or later, or the
|
20
|
-
# 'executable_path' field on Windows 2000 or earlier.
|
21
|
-
#
|
22
|
-
@fields = %w[
|
23
|
-
caption
|
24
|
-
cmdline
|
25
|
-
comm
|
26
|
-
creation_class_name
|
27
|
-
creation_date
|
28
|
-
cs_creation_class_name
|
29
|
-
cs_name
|
30
|
-
description
|
31
|
-
executable_path
|
32
|
-
execution_state
|
33
|
-
handle
|
34
|
-
handle_count
|
35
|
-
install_date
|
36
|
-
kernel_mode_time
|
37
|
-
maximum_working_set_size
|
38
|
-
minimum_working_set_size
|
39
|
-
name
|
40
|
-
os_creation_class_name
|
41
|
-
os_name
|
42
|
-
other_operation_count
|
43
|
-
other_transfer_count
|
44
|
-
page_faults
|
45
|
-
page_file_usage
|
46
|
-
ppid
|
47
|
-
peak_page_file_usage
|
48
|
-
peak_virtual_size
|
49
|
-
peak_working_set_size
|
50
|
-
priority
|
51
|
-
private_page_count
|
52
|
-
pid
|
53
|
-
quota_non_paged_pool_usage
|
54
|
-
quota_paged_pool_usage
|
55
|
-
quota_peak_non_paged_pool_usage
|
56
|
-
quota_peak_paged_pool_usage
|
57
|
-
read_operation_count
|
58
|
-
read_transfer_count
|
59
|
-
session_id
|
60
|
-
status
|
61
|
-
termination_date
|
62
|
-
thread_count
|
63
|
-
user_mode_time
|
64
|
-
virtual_size
|
65
|
-
windows_version
|
66
|
-
working_set_size
|
67
|
-
write_operation_count
|
68
|
-
write_transfer_count
|
69
|
-
]
|
70
|
-
|
71
|
-
ProcTableStruct = Struct.new("ProcTableStruct", *@fields)
|
72
|
-
|
73
|
-
# call-seq:
|
74
|
-
# ProcTable.fields
|
75
|
-
#
|
76
|
-
# Returns an array of fields that each ProcTableStruct will contain. This
|
77
|
-
# may be useful if you want to know in advance what fields are available
|
78
|
-
# without having to perform at least one read of the /proc table.
|
79
|
-
#
|
80
|
-
def self.fields
|
81
|
-
@fields
|
82
|
-
end
|
83
|
-
|
84
|
-
# call-seq:
|
85
|
-
# ProcTable.ps(pid=nil)
|
86
|
-
# ProcTable.ps(pid=nil){ |ps| ... }
|
87
|
-
#
|
88
|
-
# In block form, yields a ProcTableStruct for each process entry that you
|
89
|
-
# have rights to. This method returns an array of ProcTableStruct's in
|
90
|
-
# non-block form.
|
91
|
-
#
|
92
|
-
# If a +pid+ is provided, then only a single ProcTableStruct is yielded or
|
93
|
-
# returned, or nil if no process information is found for that +pid+.
|
94
|
-
#
|
95
|
-
def self.ps(pid=nil, host=Socket.gethostname)
|
96
|
-
if pid
|
97
|
-
raise TypeError unless pid.kind_of?(Fixnum)
|
98
|
-
end
|
99
|
-
|
100
|
-
array = block_given? ? nil : []
|
101
|
-
struct = nil
|
102
|
-
|
103
|
-
begin
|
104
|
-
wmi = WIN32OLE.connect("winmgmts://#{host}/root/cimv2")
|
105
|
-
rescue WIN32OLERuntimeError => e
|
106
|
-
raise Error, e # Re-raise as ProcTable::Error
|
107
|
-
else
|
108
|
-
wmi.InstancesOf("Win32_Process").each{ |wproc|
|
109
|
-
if pid
|
110
|
-
next unless wproc.ProcessId == pid
|
111
|
-
end
|
112
|
-
|
113
|
-
# Some fields are added later, and so are nil initially
|
114
|
-
struct = ProcTableStruct.new(
|
115
|
-
wproc.Caption,
|
116
|
-
nil, # Added later, based on OS version
|
117
|
-
wproc.Name,
|
118
|
-
wproc.CreationClassName,
|
119
|
-
self.parse_ms_date(wproc.CreationDate),
|
120
|
-
wproc.CSCreationClassName,
|
121
|
-
wproc.CSName,
|
122
|
-
wproc.Description,
|
123
|
-
wproc.ExecutablePath,
|
124
|
-
wproc.ExecutionState,
|
125
|
-
wproc.Handle,
|
126
|
-
wproc.HandleCount,
|
127
|
-
self.parse_ms_date(wproc.InstallDate),
|
128
|
-
self.convert(wproc.KernelModeTime),
|
129
|
-
wproc.MaximumWorkingSetSize,
|
130
|
-
wproc.MinimumWorkingSetSize,
|
131
|
-
wproc.Name,
|
132
|
-
wproc.OSCreationClassName,
|
133
|
-
wproc.OSName,
|
134
|
-
self.convert(wproc.OtherOperationCount),
|
135
|
-
self.convert(wproc.OtherTransferCount),
|
136
|
-
wproc.PageFaults,
|
137
|
-
wproc.PageFileUsage,
|
138
|
-
wproc.ParentProcessId,
|
139
|
-
self.convert(wproc.PeakPageFileUsage),
|
140
|
-
self.convert(wproc.PeakVirtualSize),
|
141
|
-
self.convert(wproc.PeakWorkingSetSize),
|
142
|
-
wproc.Priority,
|
143
|
-
self.convert(wproc.PrivatePageCount),
|
144
|
-
wproc.ProcessId,
|
145
|
-
wproc.QuotaNonPagedPoolUsage,
|
146
|
-
wproc.QuotaPagedPoolUsage,
|
147
|
-
wproc.QuotaPeakNonPagedPoolUsage,
|
148
|
-
wproc.QuotaPeakPagedPoolUsage,
|
149
|
-
self.convert(wproc.ReadOperationCount),
|
150
|
-
self.convert(wproc.ReadTransferCount),
|
151
|
-
wproc.SessionId,
|
152
|
-
wproc.Status,
|
153
|
-
self.parse_ms_date(wproc.TerminationDate),
|
154
|
-
wproc.ThreadCount,
|
155
|
-
self.convert(wproc.UserModeTime),
|
156
|
-
self.convert(wproc.VirtualSize),
|
157
|
-
wproc.WindowsVersion,
|
158
|
-
self.convert(wproc.WorkingSetSize),
|
159
|
-
self.convert(wproc.WriteOperationCount),
|
160
|
-
self.convert(wproc.WriteTransferCount)
|
161
|
-
)
|
162
|
-
|
163
|
-
###############################################################
|
164
|
-
# On Windows XP or later, set the cmdline to the CommandLine
|
165
|
-
# attribute. Otherwise, set it to the ExecutablePath
|
166
|
-
# attribute.
|
167
|
-
###############################################################
|
168
|
-
if wproc.WindowsVersion.to_f < 5.1
|
169
|
-
struct.cmdline = wproc.ExecutablePath
|
170
|
-
else
|
171
|
-
struct.cmdline = wproc.CommandLine
|
172
|
-
end
|
173
|
-
|
174
|
-
struct.freeze # This is read-only data
|
175
|
-
|
176
|
-
if block_given?
|
177
|
-
yield struct
|
178
|
-
else
|
179
|
-
array << struct
|
180
|
-
end
|
181
|
-
}
|
182
|
-
end
|
183
|
-
|
184
|
-
pid ? struct : array
|
185
|
-
end
|
186
|
-
|
187
|
-
private
|
188
|
-
|
189
|
-
#######################################################################
|
190
|
-
# Converts a string in the format '20040703074625.015625-360' into a
|
191
|
-
# Ruby Time object.
|
192
|
-
#######################################################################
|
193
|
-
def self.parse_ms_date(str)
|
194
|
-
return if str.nil?
|
195
|
-
return DateTime.parse(str)
|
196
|
-
end
|
197
|
-
|
198
|
-
#####################################################################
|
199
|
-
# There is a bug in win32ole where uint64 types are returned as a
|
200
|
-
# String instead of a Fixnum. This method deals with that for now.
|
201
|
-
#####################################################################
|
202
|
-
def self.convert(str)
|
203
|
-
return nil if str.nil? # Return nil, not 0
|
204
|
-
return str.to_i
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
1
|
+
require 'win32ole'
|
2
|
+
require 'socket'
|
3
|
+
require 'date'
|
4
|
+
require 'sys/proctable/version'
|
5
|
+
|
6
|
+
# The Sys module serves as a namespace only
|
7
|
+
module Sys
|
8
|
+
|
9
|
+
# The ProcTable class encapsulates process table information
|
10
|
+
class ProcTable
|
11
|
+
|
12
|
+
# There is no constructor
|
13
|
+
private_class_method :new
|
14
|
+
|
15
|
+
# Error typically raised if one of the Sys::ProcTable methods fails
|
16
|
+
class Error < StandardError; end
|
17
|
+
|
18
|
+
# The comm field corresponds to the 'name' field. The 'cmdline' field
|
19
|
+
# is the CommandLine attribute on Windows XP or later, or the
|
20
|
+
# 'executable_path' field on Windows 2000 or earlier.
|
21
|
+
#
|
22
|
+
@fields = %w[
|
23
|
+
caption
|
24
|
+
cmdline
|
25
|
+
comm
|
26
|
+
creation_class_name
|
27
|
+
creation_date
|
28
|
+
cs_creation_class_name
|
29
|
+
cs_name
|
30
|
+
description
|
31
|
+
executable_path
|
32
|
+
execution_state
|
33
|
+
handle
|
34
|
+
handle_count
|
35
|
+
install_date
|
36
|
+
kernel_mode_time
|
37
|
+
maximum_working_set_size
|
38
|
+
minimum_working_set_size
|
39
|
+
name
|
40
|
+
os_creation_class_name
|
41
|
+
os_name
|
42
|
+
other_operation_count
|
43
|
+
other_transfer_count
|
44
|
+
page_faults
|
45
|
+
page_file_usage
|
46
|
+
ppid
|
47
|
+
peak_page_file_usage
|
48
|
+
peak_virtual_size
|
49
|
+
peak_working_set_size
|
50
|
+
priority
|
51
|
+
private_page_count
|
52
|
+
pid
|
53
|
+
quota_non_paged_pool_usage
|
54
|
+
quota_paged_pool_usage
|
55
|
+
quota_peak_non_paged_pool_usage
|
56
|
+
quota_peak_paged_pool_usage
|
57
|
+
read_operation_count
|
58
|
+
read_transfer_count
|
59
|
+
session_id
|
60
|
+
status
|
61
|
+
termination_date
|
62
|
+
thread_count
|
63
|
+
user_mode_time
|
64
|
+
virtual_size
|
65
|
+
windows_version
|
66
|
+
working_set_size
|
67
|
+
write_operation_count
|
68
|
+
write_transfer_count
|
69
|
+
]
|
70
|
+
|
71
|
+
ProcTableStruct = Struct.new("ProcTableStruct", *@fields)
|
72
|
+
|
73
|
+
# call-seq:
|
74
|
+
# ProcTable.fields
|
75
|
+
#
|
76
|
+
# Returns an array of fields that each ProcTableStruct will contain. This
|
77
|
+
# may be useful if you want to know in advance what fields are available
|
78
|
+
# without having to perform at least one read of the /proc table.
|
79
|
+
#
|
80
|
+
def self.fields
|
81
|
+
@fields
|
82
|
+
end
|
83
|
+
|
84
|
+
# call-seq:
|
85
|
+
# ProcTable.ps(pid=nil)
|
86
|
+
# ProcTable.ps(pid=nil){ |ps| ... }
|
87
|
+
#
|
88
|
+
# In block form, yields a ProcTableStruct for each process entry that you
|
89
|
+
# have rights to. This method returns an array of ProcTableStruct's in
|
90
|
+
# non-block form.
|
91
|
+
#
|
92
|
+
# If a +pid+ is provided, then only a single ProcTableStruct is yielded or
|
93
|
+
# returned, or nil if no process information is found for that +pid+.
|
94
|
+
#
|
95
|
+
def self.ps(pid=nil, host=Socket.gethostname)
|
96
|
+
if pid
|
97
|
+
raise TypeError unless pid.kind_of?(Fixnum)
|
98
|
+
end
|
99
|
+
|
100
|
+
array = block_given? ? nil : []
|
101
|
+
struct = nil
|
102
|
+
|
103
|
+
begin
|
104
|
+
wmi = WIN32OLE.connect("winmgmts://#{host}/root/cimv2")
|
105
|
+
rescue WIN32OLERuntimeError => e
|
106
|
+
raise Error, e # Re-raise as ProcTable::Error
|
107
|
+
else
|
108
|
+
wmi.InstancesOf("Win32_Process").each{ |wproc|
|
109
|
+
if pid
|
110
|
+
next unless wproc.ProcessId == pid
|
111
|
+
end
|
112
|
+
|
113
|
+
# Some fields are added later, and so are nil initially
|
114
|
+
struct = ProcTableStruct.new(
|
115
|
+
wproc.Caption,
|
116
|
+
nil, # Added later, based on OS version
|
117
|
+
wproc.Name,
|
118
|
+
wproc.CreationClassName,
|
119
|
+
self.parse_ms_date(wproc.CreationDate),
|
120
|
+
wproc.CSCreationClassName,
|
121
|
+
wproc.CSName,
|
122
|
+
wproc.Description,
|
123
|
+
wproc.ExecutablePath,
|
124
|
+
wproc.ExecutionState,
|
125
|
+
wproc.Handle,
|
126
|
+
wproc.HandleCount,
|
127
|
+
self.parse_ms_date(wproc.InstallDate),
|
128
|
+
self.convert(wproc.KernelModeTime),
|
129
|
+
wproc.MaximumWorkingSetSize,
|
130
|
+
wproc.MinimumWorkingSetSize,
|
131
|
+
wproc.Name,
|
132
|
+
wproc.OSCreationClassName,
|
133
|
+
wproc.OSName,
|
134
|
+
self.convert(wproc.OtherOperationCount),
|
135
|
+
self.convert(wproc.OtherTransferCount),
|
136
|
+
wproc.PageFaults,
|
137
|
+
wproc.PageFileUsage,
|
138
|
+
wproc.ParentProcessId,
|
139
|
+
self.convert(wproc.PeakPageFileUsage),
|
140
|
+
self.convert(wproc.PeakVirtualSize),
|
141
|
+
self.convert(wproc.PeakWorkingSetSize),
|
142
|
+
wproc.Priority,
|
143
|
+
self.convert(wproc.PrivatePageCount),
|
144
|
+
wproc.ProcessId,
|
145
|
+
wproc.QuotaNonPagedPoolUsage,
|
146
|
+
wproc.QuotaPagedPoolUsage,
|
147
|
+
wproc.QuotaPeakNonPagedPoolUsage,
|
148
|
+
wproc.QuotaPeakPagedPoolUsage,
|
149
|
+
self.convert(wproc.ReadOperationCount),
|
150
|
+
self.convert(wproc.ReadTransferCount),
|
151
|
+
wproc.SessionId,
|
152
|
+
wproc.Status,
|
153
|
+
self.parse_ms_date(wproc.TerminationDate),
|
154
|
+
wproc.ThreadCount,
|
155
|
+
self.convert(wproc.UserModeTime),
|
156
|
+
self.convert(wproc.VirtualSize),
|
157
|
+
wproc.WindowsVersion,
|
158
|
+
self.convert(wproc.WorkingSetSize),
|
159
|
+
self.convert(wproc.WriteOperationCount),
|
160
|
+
self.convert(wproc.WriteTransferCount)
|
161
|
+
)
|
162
|
+
|
163
|
+
###############################################################
|
164
|
+
# On Windows XP or later, set the cmdline to the CommandLine
|
165
|
+
# attribute. Otherwise, set it to the ExecutablePath
|
166
|
+
# attribute.
|
167
|
+
###############################################################
|
168
|
+
if wproc.WindowsVersion.to_f < 5.1
|
169
|
+
struct.cmdline = wproc.ExecutablePath
|
170
|
+
else
|
171
|
+
struct.cmdline = wproc.CommandLine
|
172
|
+
end
|
173
|
+
|
174
|
+
struct.freeze # This is read-only data
|
175
|
+
|
176
|
+
if block_given?
|
177
|
+
yield struct
|
178
|
+
else
|
179
|
+
array << struct
|
180
|
+
end
|
181
|
+
}
|
182
|
+
end
|
183
|
+
|
184
|
+
pid ? struct : array
|
185
|
+
end
|
186
|
+
|
187
|
+
private
|
188
|
+
|
189
|
+
#######################################################################
|
190
|
+
# Converts a string in the format '20040703074625.015625-360' into a
|
191
|
+
# Ruby Time object.
|
192
|
+
#######################################################################
|
193
|
+
def self.parse_ms_date(str)
|
194
|
+
return if str.nil?
|
195
|
+
return DateTime.parse(str)
|
196
|
+
end
|
197
|
+
|
198
|
+
#####################################################################
|
199
|
+
# There is a bug in win32ole where uint64 types are returned as a
|
200
|
+
# String instead of a Fixnum. This method deals with that for now.
|
201
|
+
#####################################################################
|
202
|
+
def self.convert(str)
|
203
|
+
return nil if str.nil? # Return nil, not 0
|
204
|
+
return str.to_i
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|