sys-proctable 0.7.4-mswin32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,78 @@
1
+ = Description
2
+ sys-proctable
3
+
4
+ A Ruby version of the 'ps' command. This is a C extension, not parsed
5
+ output. For HP-UX, proc structs are grabbed via the pstat_getproc() call.
6
+
7
+ = Synopsis
8
+ require 'sys/proctable'
9
+ include Sys
10
+
11
+ # Everything
12
+ ProcTable.ps{ |p|
13
+ puts p.pid.to_s
14
+ puts p.comm
15
+ ...
16
+ }
17
+
18
+ or
19
+
20
+ # Return the results as an array of ProcTableStructs
21
+ a = ProcTable.ps()
22
+ a.each do |p|
23
+ puts a.pid
24
+ ...
25
+ end
26
+
27
+ = Constants
28
+ VERSION
29
+ Returns the current version number for this package (as a string).
30
+
31
+ = Class Methods
32
+ ProcTable.fields
33
+ Returns a list of fields available on the current OS. May also take
34
+ a block.
35
+
36
+ ProcTable.ps(pid=nil)
37
+ ProcTable.ps{ |s| ... }
38
+ Returns a struct of type ProcTableStruct for every process in the proc
39
+ table in block form. Otherwise it returns an array of ProcTableStruct's.
40
+
41
+ If a pid is provided then a single ProcTable struct is returned, or nil
42
+ if the pid is not found.
43
+
44
+ = Supported fields
45
+ You can view the supported fields with the "fields()" class method.
46
+
47
+ = Future Plans
48
+ Have the flags field return a meaningful value.
49
+
50
+ = Notes
51
+ The "comm" field isn't really part of the psinfo struct. It is just a copy
52
+ (i.e. is identical to) the "fname" field. I added it to provide a degree
53
+ of consistency between all of the platforms. I will also make a point
54
+ of adding it to any future platform releases.
55
+
56
+ = Known Bugs
57
+ None that I'm aware of. Please log any bugs on the project page at
58
+ http://www.rubyforge.org/projects/sysutils
59
+
60
+ = License
61
+ Ruby's
62
+
63
+ = Copyright
64
+ (C) 2003-2006 Daniel J. Berger
65
+ All Rights Reserved.
66
+
67
+ = Warranty
68
+ This package is provided "as is" and without any express or
69
+ implied warranties, including, without limitation, the implied
70
+ warranties of merchantability and fitness for a particular purpose.
71
+
72
+ = Author
73
+ Daniel J. Berger
74
+ djberg96 at nospam at gmail dot com
75
+ imperator on IRC (Freenode)
76
+
77
+ = See Also
78
+ ps, proc
@@ -0,0 +1,86 @@
1
+ = Description
2
+ sys-proctable
3
+
4
+ A Ruby version of the 'ps' command. This is a C extension, not parsed
5
+ output. For Linux, process information is read out of /proc.
6
+
7
+ = Synopsis
8
+ require 'sys/proctable'
9
+ include Sys
10
+
11
+ # Everything
12
+ ProcTable.ps{ |p|
13
+ puts p.pid.to_s
14
+ puts p.comm
15
+ ...
16
+ }
17
+
18
+ or
19
+
20
+ # Just one process
21
+ p = ProcTable.ps(2123)
22
+ puts p.pid.to_s
23
+ puts p.comm
24
+ ...
25
+
26
+ or
27
+
28
+ # Return the results as an array of ProcTableStructs
29
+ a = ProcTable.ps()
30
+ a.each do |p|
31
+ puts a.pid
32
+ ...
33
+ end
34
+
35
+ = Constants
36
+ VERSION
37
+ Returns the current version number for this package (as a string).
38
+
39
+ = Class Methods
40
+ ProcTable.fields
41
+ Returns a list of fields available on the current OS. May also take a
42
+ block.
43
+
44
+ ProcTable.ps(pid=nil)
45
+ ProcTable.ps{ |s| ... }
46
+ If no pid's or processes are included as arguments, in block form it
47
+ returns a struct of type ProcTableStruct for every process in the proc
48
+ table. Otherwise it returns an array of ProcTableStruct's.
49
+
50
+ If a process id is provided, a single ProcTable struct is returned, or
51
+ nil if the pid is not found.
52
+
53
+ = Exception Classes
54
+ ProcTableError < StandardError
55
+ Raised if the /proc field is unreadable and/or unmounted.
56
+
57
+ = Supported fields
58
+ You can view the supported fields with the "fields()" class method.
59
+
60
+ = Future Plans
61
+ Create a pure Ruby version as an alternative.
62
+ Change the ttynum field to ttydev and return a string instead of an int.
63
+
64
+ = Known Bugs
65
+ None known. Please log any bugs on the project page at
66
+ http://www.rubyforge.org/projects/sysutils
67
+
68
+ = License
69
+ Ruby's
70
+
71
+ = Copyright
72
+ (C) 2003-2006 Daniel J. Berger
73
+ All Rights Reserved
74
+
75
+ = Warranty
76
+ This package is provided "as is" and without any express or
77
+ implied warranties, including, without limitation, the implied
78
+ warranties of merchantability and fitness for a particular purpose.
79
+
80
+ = Author
81
+ Daniel J. Berger
82
+ djberg96 at nospam at gmail dot com
83
+ rubyhacker1 on IRC (Freenode)
84
+
85
+ = See Also
86
+ ps, proc(5)
@@ -0,0 +1,100 @@
1
+ = Description
2
+ sys-proctable
3
+
4
+ A Ruby version of the 'ps' command. This is a C extension, not parsed
5
+ output. For Solaris, data is read out of /proc via the psinfo struct.
6
+
7
+ = Synopsis
8
+ require 'sys/proctable'
9
+ include Sys
10
+
11
+ # Everything
12
+ ProcTable.ps{ |p|
13
+ puts p.pid.to_s
14
+ puts p.comm
15
+ ...
16
+ }
17
+
18
+ or
19
+
20
+ # Just one process
21
+ s = ProcTable.ps(2123)
22
+ puts s.pid.to_s
23
+ puts s.comm
24
+ ...
25
+
26
+ # Return the results as an array of ProcTableStructs
27
+ a = ProcTable.ps
28
+ a.each do |p|
29
+ puts a.pid
30
+ ...
31
+ end
32
+
33
+ = Constants
34
+ VERSION
35
+ Returns the current version number for this package (as a string).
36
+
37
+ = Class Methods
38
+ ProcTable.fields
39
+ Returns a list of fields available on the current OS. May also take
40
+ a block.
41
+
42
+ ProcTable.ps(pid=nil)
43
+ ProcTable.ps{ |s| ... }
44
+ If no pid is included as an argument, in block form it
45
+ returns a struct of type ProcTableStruct for every process in the proc
46
+ table. Otherwise it returns an array of ProcTable struct's. If a pid
47
+ is provided it will return a single ProcTable struct for that pid, or
48
+ nil if it is not found.
49
+
50
+ = Exception Classes
51
+ ProcTableError < StandardError
52
+ Raised if the /proc directory is unreadable and/or unmounted.
53
+
54
+ = Supported fields
55
+ You can view the supported fields with the "fields()" class method.
56
+
57
+ = Future Plans
58
+ Return a more meaningful result for the wchan member (2.6+).
59
+ Add env info where possible.
60
+
61
+ = Notes
62
+ The "comm" field isn't really part of the psinfo struct. It is just a copy
63
+ (i.e. is identical to) the "fname" field. I added it to provide a degree
64
+ of consistency between all of the platforms. I will also make a point
65
+ of adding it to any future platform releases.
66
+
67
+ The cmdline string is limited to 80 characters, except for those processes
68
+ which you (or your program) own.
69
+
70
+ The ttydev field is, for now, an integer. If there is no associated tty with
71
+ the process, a -1 is returned. In the future, this field will be changed to
72
+ a string (i.e. the actual device name).
73
+
74
+ I suppose I *could* add a -lkvm version for Solaris (this is what Dan Urist
75
+ is now using in Proc::ProcessTable) but I haven't found a compelling reason
76
+ to do so. If you think you have one, please let me know.
77
+
78
+ = Known Bugs
79
+ None that I am aware of. Please log any bugs on the RubyForge project page at
80
+ http://www.rubyforge.org/projects/sysutils
81
+
82
+ = License
83
+ Ruby's
84
+
85
+ = Copyright
86
+ (C) 2003-2006 Daniel J. Berger
87
+ All Rights Reserved.
88
+
89
+ = Warranty
90
+ This package is provided "as is" and without any express or
91
+ implied warranties, including, without limitation, the implied
92
+ warranties of merchantability and fitness for a particular purpose.
93
+
94
+ = Author
95
+ Daniel J. Berger
96
+ djberg96 at nospam at gmail dot com
97
+ imperator on IRC (Freenode)
98
+
99
+ = See Also
100
+ ps, proc
@@ -0,0 +1,53 @@
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
+ include Sys
11
+
12
+ Top.top(5).each{ |ps|
13
+ p ps
14
+ }
15
+
16
+ = Constants
17
+ VERSION
18
+ Returns the version number of this package as a String.
19
+
20
+ = Class Methods
21
+ Top.top(number=10, field="pctcpu")
22
+ Returns an array of ProcTableStruct's. The size of the array (i.e. the
23
+ number of processes) that it returns is based on +number+, and sorted by
24
+ +pctcpu+. By default, the size and field values are 10 and "pctcpu",
25
+ respectively.
26
+
27
+ = Notes
28
+ Not all fields are available on all platforms. Please check your
29
+ platform specific documentation for which fields are available.
30
+
31
+ I used sort() instead of sort_by() internally to maintain backward
32
+ compatability with Ruby 1.6.8, which I happen to need.
33
+
34
+ = Bugs
35
+ None that I'm aware of. Please log bug reports on the project page at
36
+ http://www.rubyforge.org/projects/sysutils
37
+
38
+ = License
39
+ Ruby's
40
+
41
+ = Copyright
42
+ (C) 2004-2006 Daniel J. Berger
43
+ All Rights Reserved.
44
+
45
+ = Warranty
46
+ This package is provided "as is" and without any express or
47
+ implied warranties, including, without limitation, the implied
48
+ warranties of merchantability and fitness for a particular purpose.
49
+
50
+ = Author
51
+ Daniel J. Berger
52
+ djberg96 at nospam at gmail dot com
53
+ imperator on IRC (Freenode)
@@ -0,0 +1,122 @@
1
+ = Description
2
+ A Ruby interface for gathering process information. For MS Windows,
3
+ the process information is gathered via OLE + WMI, using pure Ruby.
4
+
5
+ = Synopsis
6
+ require 'sys/proctable'
7
+ include Sys
8
+
9
+ # Everything
10
+ ProcTable.ps{ |p|
11
+ puts p.pid.to_s
12
+ puts p.comm
13
+ ...
14
+ }
15
+
16
+ or
17
+
18
+ # A single pid
19
+ p = ProcTable.ps(1234)
20
+ puts p.pid.to_s
21
+ puts p.comm
22
+ ...
23
+
24
+ or
25
+
26
+ # Return the results as an array of ProcTableStructs
27
+ a = ProcTable.ps
28
+ a.each do |p|
29
+ puts a.pid
30
+ ...
31
+ end
32
+
33
+ = Constants
34
+ VERSION
35
+ Returns the current version number for this package (as a string).
36
+
37
+ = Class Methods
38
+ ProcTable.fields
39
+ Returns an Array of fields available on the current OS in the
40
+ ProcTableStruct.
41
+
42
+ ProcTable.ps(pid=nil, host='localhost')
43
+ ProcTable.ps{ |s| ... }
44
+ Returns a struct of type ProcTableStruct for every process in the proc
45
+ table in block form. Otherwise it returns an array of ProcTableStruct's.
46
+
47
+ If 'pid' is provided, then only a struct for that pid is returned, or
48
+ nil if it is not found.
49
+
50
+ If 'host' is provided, then processes from that host are gathered. By
51
+ default, process information is gathered on the local host.
52
+
53
+ = Supported fields
54
+ The currently supported fields for MS Windows (i.e. your ProcTable struct
55
+ members) are:
56
+
57
+ caption, cmdline, comm, creation_class_name, creation_date,
58
+ cs_creation_class_name, cs_name, description, executable_path,
59
+ execution_state, handle, handle_count, install_date, kernel_mode_time,
60
+ maximum_working_set_size, minimum_working_set_size, name,
61
+ os_creation_class_name, os_name, other_operation_count,
62
+ other_transfer_count, page_faults, page_file_usage, peak_virtual_size,
63
+ ppid, peak_working_set_size, priority, private_page_count, pid,
64
+ quota_non_paged_pool_usage, quota_paged_pool_usage,
65
+ quota_peak_non_paged_pool_usage, quota_non_paged_pool_usage,
66
+ read_operation_count, read_transfer_count, session_id,
67
+ termination_date, thread_count, user_mode_time, virtual_size,
68
+ windows_version, working_set_size, write_operation_count,
69
+ write_transfer_count
70
+
71
+ You can also view them with the fields() class method.
72
+
73
+ = Notes
74
+ For the sake of attempting to provide a somewhat common API, the 'comm'
75
+ and 'cmdline' fields have been included as part of the structure. The
76
+ 'comm' member corresponds to the Name attribute of Win32_Process. The
77
+ 'cmdline' attribute corresponds to either the Executable_Path attribute
78
+ (on Windows 2000 or earlier) or the CommandLine attribute (on Windows XP
79
+ and later).
80
+
81
+ Also note that the ProcessId and ParentProcessId attributes have been
82
+ abbreviated as 'pid' and 'ppid' in the struct members, again to keep the
83
+ members more consistent between platforms.
84
+
85
+ The "Mem Usage" and "VM Size" that you may be used to seeing in your Task
86
+ Manager window (probably) correspond to the 'working_set_size' and
87
+ 'page_file_usage' struct members, respectively, keeping in mind that
88
+ those values are in bytes, not kilobytes. I say 'probably' because
89
+ comments that I've read online indicate that it may not always line up
90
+ with what you see in the Task Manager, based on the current version (or
91
+ even Service Pack) of Windows that you are using.
92
+
93
+ = Future Plans
94
+ Possibly use the Win32_PerfFormattedData_PerfProc_Process class to get
95
+ additional process information.
96
+
97
+ = Known Bugs
98
+ Versions of Ruby earlier than 1.8.2 resulted in segfaults when trying to
99
+ run this code. You will likely encounter the same behavior.
100
+
101
+ Please log any additional bug reports on the project page at
102
+ http://www.rubyforge.org/projects/sysutils
103
+
104
+ = License
105
+ Ruby's
106
+
107
+ = Copyright
108
+ (C) 2003-2006 Daniel J. Berger
109
+ All Rights Reserved
110
+
111
+ = Warranty
112
+ This package is provided "as is" and without any express or
113
+ implied warranties, including, without limitation, the implied
114
+ warranties of merchantability and fitness for a particular purpose.
115
+
116
+ = Author
117
+ Daniel J. Berger
118
+ djberg96 at nospam at gmail dot com
119
+ imperator on IRC (freenode)
120
+
121
+ = See Also
122
+ OLE + WMI
@@ -0,0 +1,197 @@
1
+ require 'win32ole'
2
+ require 'socket'
3
+ require 'parsedate'
4
+
5
+ module Sys
6
+ class ProcTableError < StandardError; end
7
+ class ProcTable
8
+ VERSION = '0.7.4'
9
+
10
+ ########################################################################
11
+ # The comm field corresponds to the 'name' field. The 'cmdline' field
12
+ # is the CommandLine attribute on Windows XP or later, or the
13
+ # 'executable_path' field on Windows 2000 or earlier.
14
+ ########################################################################
15
+ @fields = %w/
16
+ caption
17
+ cmdline
18
+ comm
19
+ creation_class_name
20
+ creation_date
21
+ cs_creation_class_name
22
+ cs_name
23
+ description
24
+ executable_path
25
+ execution_state
26
+ handle
27
+ handle_count
28
+ install_date
29
+ kernel_mode_time
30
+ maximum_working_set_size
31
+ minimum_working_set_size
32
+ name
33
+ os_creation_class_name
34
+ os_name
35
+ other_operation_count
36
+ other_transfer_count
37
+ page_faults
38
+ page_file_usage
39
+ peak_virtual_size
40
+ ppid
41
+ peak_working_set_size
42
+ priority
43
+ private_page_count
44
+ pid
45
+ quota_non_paged_pool_usage
46
+ quota_paged_pool_usage
47
+ quota_peak_non_paged_pool_usage
48
+ quota_non_paged_pool_usage
49
+ read_operation_count
50
+ read_transfer_count
51
+ session_id
52
+ termination_date
53
+ thread_count
54
+ user_mode_time
55
+ virtual_size
56
+ windows_version
57
+ working_set_size
58
+ write_operation_count
59
+ write_transfer_count
60
+ /
61
+
62
+ ProcTableStruct = Struct.new("ProcTableStruct", *@fields)
63
+
64
+ # call-seq:
65
+ # ProcTable.fields
66
+ #
67
+ # Returns an array of fields that each ProcTableStruct will contain. This
68
+ # may be useful if you want to know in advance what fields are available
69
+ # without having to perform at least one read of the /proc table.
70
+ #
71
+ def self.fields
72
+ @fields
73
+ end
74
+
75
+ # call-seq:
76
+ # ProcTable.ps(pid=nil)
77
+ # ProcTable.ps(pid=nil){ |ps| ... }
78
+ #
79
+ # In block form, yields a ProcTableStruct for each process entry that you
80
+ # have rights to. This method returns an array of ProcTableStruct's in
81
+ # non-block form.
82
+ #
83
+ # If a +pid+ is provided, then only a single ProcTableStruct is yielded or
84
+ # returned, or nil if no process information is found for that +pid+.
85
+ #
86
+ def self.ps(pid=nil, host=Socket.gethostname)
87
+ if pid
88
+ raise TypeError unless pid.kind_of?(Fixnum)
89
+ end
90
+
91
+ begin
92
+ wmi = WIN32OLE.connect("winmgmts://#{host}/root/cimv2")
93
+ rescue WIN32OLERuntimeError => e
94
+ raise ProcTableError, e
95
+ else
96
+ a = [] # Array used to hold structs if no block given
97
+ wmi.InstancesOf("Win32_Process").each{ |proc|
98
+ if pid
99
+ next unless proc.ProcessId == pid
100
+ end
101
+
102
+ # Some fields are added later, and so are nil initially
103
+ s = ProcTableStruct.new(
104
+ proc.Caption,
105
+ nil, # Added later, based on OS version
106
+ proc.Name,
107
+ proc.CreationClassName,
108
+ self.parse_ms_date(proc.CreationDate),
109
+ proc.CSCreationClassName,
110
+ proc.CSName,
111
+ proc.Description,
112
+ proc.ExecutablePath,
113
+ proc.ExecutionState,
114
+ proc.Handle,
115
+ proc.HandleCount,
116
+ self.parse_ms_date(proc.InstallDate),
117
+ self.convert(proc.KernelModeTime),
118
+ proc.MaximumWorkingSetSize,
119
+ proc.MinimumWorkingSetSize,
120
+ proc.Name,
121
+ proc.OSCreationClassName,
122
+ proc.OSName,
123
+ self.convert(proc.OtherOperationCount),
124
+ self.convert(proc.OtherTransferCount),
125
+ proc.PageFaults,
126
+ proc.PageFileUsage,
127
+ self.convert(proc.PeakVirtualSize),
128
+ proc.ParentProcessId,
129
+ self.convert(proc.PeakWorkingSetSize),
130
+ proc.Priority,
131
+ self.convert(proc.PrivatePageCount),
132
+ proc.ProcessId,
133
+ proc.QuotaNonPagedPoolUsage,
134
+ proc.QuotaPagedPoolUsage,
135
+ proc.QuotaPeakNonPagedPoolUsage,
136
+ proc.QuotaPeakPagedPoolUsage,
137
+ self.convert(proc.ReadOperationCount),
138
+ self.convert(proc.ReadTransferCount),
139
+ proc.SessionId,
140
+ self.parse_ms_date(proc.TerminationDate),
141
+ proc.ThreadCount,
142
+ self.convert(proc.UserModeTime),
143
+ self.convert(proc.VirtualSize),
144
+ proc.WindowsVersion,
145
+ self.convert(proc.WorkingSetSize),
146
+ self.convert(proc.WriteOperationCount),
147
+ self.convert(proc.WriteTransferCount)
148
+ )
149
+
150
+ ###############################################################
151
+ # On Windows XP or later, set the cmdline to the CommandLine
152
+ # attribute. Otherwise, set it to the ExecutablePath
153
+ # attribute.
154
+ ###############################################################
155
+ if proc.WindowsVersion.to_f < 5.1
156
+ s.cmdline = proc.ExecutablePath
157
+ else
158
+ s.cmdline = proc.CommandLine
159
+ end
160
+
161
+ if block_given?
162
+ yield s
163
+ else
164
+ a.push(s)
165
+ end
166
+ }
167
+ end
168
+ unless block_given?
169
+ return nil if a.empty?
170
+ return a.first if pid
171
+ return a
172
+ end
173
+ end
174
+
175
+ private
176
+
177
+ #######################################################################
178
+ # Converts a string in the format '20040703074625.015625-360' into a
179
+ # Ruby Time object.
180
+ #######################################################################
181
+ def self.parse_ms_date(str)
182
+ return if str.nil?
183
+ parsed = ParseDate.parsedate(str)
184
+ return Time.mktime(*parsed)
185
+ end
186
+
187
+ #####################################################################
188
+ # There is a bug in win32ole where uint64 types are returned as a
189
+ # String instead of a Fixnum. This method deals with that for now.
190
+ #####################################################################
191
+ def self.convert(str)
192
+ return nil if str.nil? # Return nil, not 0
193
+ return str.to_i
194
+ end
195
+
196
+ end
197
+ end