hawatel_ps 0.1.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.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +4 -0
  5. data/.yardopts +1 -0
  6. data/CONTRIBUTING.md +74 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +184 -0
  10. data/Rakefile +12 -0
  11. data/hawatel_ps.gemspec +29 -0
  12. data/lib/hawatel_ps/linux/proc_control.rb +75 -0
  13. data/lib/hawatel_ps/linux/proc_fetch.rb +405 -0
  14. data/lib/hawatel_ps/linux/proc_info.rb +53 -0
  15. data/lib/hawatel_ps/linux/proc_table.rb +124 -0
  16. data/lib/hawatel_ps/linux.rb +4 -0
  17. data/lib/hawatel_ps/shared/hawatelps_exception.rb +21 -0
  18. data/lib/hawatel_ps/version.rb +3 -0
  19. data/lib/hawatel_ps/windows/proc_control.rb +67 -0
  20. data/lib/hawatel_ps/windows/proc_fetch.rb +217 -0
  21. data/lib/hawatel_ps/windows/proc_info.rb +51 -0
  22. data/lib/hawatel_ps/windows/proc_table.rb +138 -0
  23. data/lib/hawatel_ps/windows/wmi/wmi_cli.rb +65 -0
  24. data/lib/hawatel_ps/windows/wmi/wmi_exception.rb +23 -0
  25. data/lib/hawatel_ps/windows/wmi/wmi_instance.rb +56 -0
  26. data/lib/hawatel_ps/windows.rb +5 -0
  27. data/lib/hawatel_ps.rb +34 -0
  28. data/spec/hawatel_ps_spec.rb +11 -0
  29. data/spec/linux/bdd/proc_spec.rb +54 -0
  30. data/spec/linux/factories/etc/passwd +32 -0
  31. data/spec/linux/factories/proc/1761/cmdline +0 -0
  32. data/spec/linux/factories/proc/1761/environ +0 -0
  33. data/spec/linux/factories/proc/1761/io +7 -0
  34. data/spec/linux/factories/proc/1761/limits +17 -0
  35. data/spec/linux/factories/proc/1761/stat +1 -0
  36. data/spec/linux/factories/proc/1761/status +46 -0
  37. data/spec/linux/factories/proc/meminfo +45 -0
  38. data/spec/linux/factories/proc/net/tcp +7 -0
  39. data/spec/linux/factories/proc/net/udp +8 -0
  40. data/spec/linux/factories/proc/uptime +1 -0
  41. data/spec/linux/support/stub_dir.rb +33 -0
  42. data/spec/linux/support/stub_file.rb +107 -0
  43. data/spec/linux/tdd/proc_fetch_spec.rb +107 -0
  44. data/spec/linux/tdd/proc_table_spec.rb +85 -0
  45. data/spec/shared/hawatelps_exception_spec.rb +13 -0
  46. data/spec/spec_helper.rb +16 -0
  47. data/spec/windows/bdd/proc_spec.rb +119 -0
  48. data/spec/windows/factories/proc_fetch_factorie.rb +76 -0
  49. data/spec/windows/tdd/proc_control_spec.rb +36 -0
  50. data/spec/windows/tdd/proc_fetch_spec.rb +73 -0
  51. data/spec/windows/tdd/proc_table_spec.rb +71 -0
  52. data/spec/windows/tdd/wmi_spec.rb +59 -0
  53. metadata +181 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c1eba24ecceee6ba4df134bed7e92d276ac3cc41
4
+ data.tar.gz: ba617239b79e504d611fab5bb0fb890f70c57afb
5
+ SHA512:
6
+ metadata.gz: 1a5fee76376c92ea87024107aa7b9576ef805aa1d4dfe4b5de2e9f02f9876daeac06d61f16b496fb9b2663847f31236a94483d1ceb75bdf09c5465bf8b9979ef
7
+ data.tar.gz: 28b7794a040fa2b84a23733fa1e46063cd0274bbfa1c45b5b8bfe23f0c277e4d65f84c789ab9214e6ae2699ad12de17c8029bd44873e1fa4311d0aa39084a389
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea
11
+ /coverage
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --profile
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.6
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --private
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,74 @@
1
+ # Contributing
2
+
3
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hawatel/hawatel_ps. This project is intended to be a safe, welcoming space for collaboration, and contributors.
4
+
5
+ 1. [Fork](https://help.github.com/articles/fork-a-repo/) the project, clone your fork,
6
+ and configure the remotes:
7
+
8
+ ```sh
9
+ # Clone your fork of the repo into the current directory
10
+ git clone https://github.com/<your-username>/hawatel_ps
11
+ # Navigate to the newly cloned directory
12
+ cd hawatel_ps
13
+ # Assign the original repo to a remote called "upstream"
14
+ git remote add upstream https://github.com/hawatel/hawatel_ps
15
+ ```
16
+
17
+ 2. If you cloned a while ago, get the latest changes from upstream:
18
+
19
+ ```bash
20
+ git checkout master
21
+ git pull upstream master
22
+ ```
23
+
24
+ 3. Create a new topic branch (off of `master`) to contain your feature, change,
25
+ or fix.
26
+
27
+ **IMPORTANT**: Making changes in `master` is discouraged. You should always
28
+ keep your local `master` in sync with upstream `master` and make your
29
+ changes in topic branches.
30
+
31
+ ```sh
32
+ git checkout -b <topic-branch-name>
33
+ ```
34
+
35
+ 4. Commit your changes in logical chunks. Keep your commit messages organized,
36
+ with a short description in the first line and more detailed information on
37
+ the following lines. Feel free to use Git's
38
+ [interactive rebase](https://help.github.com/articles/about-git-rebase/)
39
+ feature to tidy up your commits before making them public.
40
+
41
+ 5. Make sure all the tests are still passing.
42
+
43
+ ```sh
44
+ rspec spec/windows
45
+ ```
46
+ or
47
+ ```sh
48
+ rspec spec/linux
49
+ ```
50
+
51
+ 6. Push your topic branch up to your fork:
52
+
53
+ ```sh
54
+ git push origin <topic-branch-name>
55
+ ```
56
+
57
+ 7. [Open a Pull Request](https://help.github.com/articles/using-pull-requests/)
58
+ with a clear title and description.
59
+
60
+ 8. If you haven't updated your pull request for a while, you should consider
61
+ rebasing on master and resolving any conflicts.
62
+
63
+ **IMPORTANT**: _Never ever_ merge upstream `master` into your branches. You
64
+ should always `git rebase` on `master` to bring your changes up to date when
65
+ necessary.
66
+
67
+ ```sh
68
+ git checkout master
69
+ git pull upstream master
70
+ git checkout <your-topic-branch>
71
+ git rebase master
72
+ ```
73
+
74
+ Thank you for your contributions!
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hawatel_ps.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Daniel Iwaniuk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,184 @@
1
+ # HawatelPS
2
+
3
+ ## Summary
4
+ **HawatelPS** (hawatel_ps) is a Ruby gem for retrieving information about **running processes**. It is easy to use, and you can get useful information about a process. You can `terminate`, `suspend`, `resume` and check the `status` of the process on Linux platform. On Windows platform, you can `terminate` and check the current `status` of the process.
5
+
6
+ * On Linux platform, the HawatelPS collects all information from pseudo-file system /proc, and it is **free from any gem dependencies**.
7
+ * On Windows platform the HawatelPS collects all information from WMI (Windows Management Instrumentation), and it is **free from any gem dependencies**.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'hawatel_ps'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install hawatel_ps
24
+
25
+ ## Example usage
26
+ ###### Find process with specified PID
27
+ ```ruby
28
+ process = HawatelPS.search_by_pid(123)
29
+ p process.name if !process.nil?
30
+ ```
31
+ ###### Get list all running processes
32
+ ```ruby
33
+ processes = HawatelPS.proc_table
34
+ ```
35
+ ###### Find processes which matching a certain name
36
+ ``` ruby
37
+ processes = HawatelPS.search_by_name('ruby')
38
+ processes.each do |process|
39
+ p "PID: #{process.pid}"
40
+ end
41
+ ```
42
+ ###### Find processes where vmsize attribute is equal or greater than 2000 kb
43
+ ```ruby
44
+ processes = HawatelPS.search_by_condition(attr => 'vmsize', oper => '>=', value => '2000')
45
+ processes.each do |process|
46
+ p process.pid
47
+ end
48
+ ```
49
+ ###### Suspend processes which name matching with regular expression
50
+ ``` ruby
51
+ processes = HawatelPS.search_by_name('/^ruby/')
52
+ processes.each do |process|
53
+ p process.suspend
54
+ end
55
+ ```
56
+ ###### Resume stopped processes
57
+ ``` ruby
58
+ processes = HawatelPS.search_by_condition(attr => 'state', oper => '==', value => 'stopped')
59
+ processes.each do |process|
60
+ p process.resume
61
+ end
62
+ ```
63
+ ###### Terminate processes which using more that 60% CPU
64
+ ``` ruby
65
+ processes = HawatelPS.search_by_condition(attr => 'cpu_percent', oper => '>', value => '60')
66
+ processes.each do |process|
67
+ p process.terminate
68
+ end
69
+ ```
70
+
71
+ ## Process attributes
72
+ #### Linux
73
+ **NOTE:** In case lack of permission to retrieving specified information, the attribute should return a string with the message "Permission denied."
74
+
75
+ | Attribute name | Description |
76
+ | ------------- |-------------|
77
+ | cancelled_write_bytes | Cancelled write bytes. |
78
+ | childs |Childs of the process.|
79
+ | cmdline | Command line arguments. |
80
+ | cpu_percent | Average percent cpu usage since start. |
81
+ | cpu_time | Total cpu time. |
82
+ | ctime | Create process time. |
83
+ | cwd | Current working directory. |
84
+ | egid | Effective GID. |
85
+ | environ | Environment of the process. |
86
+ | euid | Effective UID. |
87
+ | exe | Filename of the executable. |
88
+ | fsgidl | File system GID. |
89
+ | fsuid | File system UID. |
90
+ | limits | Limits |
91
+ | listen_ports | List ports on which process listening. |
92
+ | memory_percent | Percent total memory usage by process. |
93
+ | open_files | List open files by process (exclude /proc & /dev). |
94
+ | pid | Process id. |
95
+ | ppid | Process id of the parent process. |
96
+ | rchar | Chars read. |
97
+ | read_bytes | Bytes read. |
98
+ | rgid | Real GID. |
99
+ | ruid | Real UID. |
100
+ | sgid | Saved set GID. |
101
+ | state | State of process (running, stopped, ….). |
102
+ | stime | Kernel mode jiffies. |
103
+ | suid | Saved set UID. |
104
+ | syscr | Read syscalls (numer of read I/O operations). |
105
+ | syscw | Write syscalls (numer of write I/O operations). |
106
+ | threads | Numer of threads. |
107
+ | tty | Controlling tty (terminal). |
108
+ | username | The user name of the owner of this process. |
109
+ | utime | User mode jiffies. |
110
+ | vmdata | Size of private data segments. |
111
+ | vmlib | Size of shared library code. |
112
+ | vmrss | Size of memory portions. |
113
+ | vmsize | Total program size. |
114
+ | vmswap | Amount of swap used by process. |
115
+ | wchar | Chars written. |
116
+ | write_bytes | Bytes written. |
117
+
118
+ #### Windows
119
+
120
+ | Attribute name | Description
121
+ |-------------------------------|-------------
122
+ availablevirtualsize |The free virtual address space available to this process.
123
+ caption |Short description of an process one-line string.
124
+ childs |Childs of the process.
125
+ commandline |Command line used to start a specific process, if applicable.
126
+ cpupercent |Average percent cpu usage since process start.
127
+ creationclassname |It is always Win32_Process
128
+ creationdate |Date the process begins executing.
129
+ cscreationclassname |It is always Win32_ComputerSystem.
130
+ csname |Name of the scoping computer system.
131
+ description |Description of a process.
132
+ domain |The domain name under which this process is running.
133
+ executablepath |Path to the executable file of the process.
134
+ executionstate |It is always nil.
135
+ handle |Process identifier.
136
+ handlecount |Total number of open handles owned by the process. HandleCount is the sum of the handles currently open by each thread in this process.
137
+ installdate |Date an object is installed. The object may be installed without a value being written to this property.
138
+ kernelmodetime |Time in kernel mode, in 100 nanosecond units. If this information is not available, use a value of 0 (zero)
139
+ maximumworkingsetsize |Maximum working set size of the process. The working set of a process is the set of memory pages visible to the process in physical RAM.
140
+ memorypercent |Average percent memory usage by the process (with shared memory, equivalent to WorkingSet in TaskManager)
141
+ minimumworkingsetsize |Minimum working set size of the process. The working set of a process is the set of memory pages visible to the process in physical RAM.
142
+ name |Name of the executable file responsible for the process, equivalent to the Image Name property in Task Manager.
143
+ oscreationclassname |It is always Win32_OperatingSystem.
144
+ osname |Name of the scoping operating system.
145
+ otheroperationcount |Number of I/O operations performed that are not read or write operations.
146
+ othertransfercount |Amount of data transferred during operations that are not read or write operations.
147
+ pagefaults |Number of page faults that a process generates.
148
+ pagefileusage |Amount of page file space that a process is using currently. This value is consistent with the VMSize value in TaskMgr.exe.
149
+ parentprocessid |Unique identifier of the process that creates a process.
150
+ peakpagefileusage |Maximum amount of page file space used during the life of a process.
151
+ peakvirtualsize |Maximum virtual address space a process uses at any one time. Using virtual address space does not necessarily imply corresponding use of either disk or main memory pages.
152
+ peakworkingsetsize |Peak working set size of a process.
153
+ priority |Scheduling priority of a process within an operating system.
154
+ privatepagecount |Current number of pages allocated that are only accessible to the process represented by this Win32_Process instance.
155
+ processid |Numeric identifier used to distinguish one process from another.
156
+ quotanonpagedpoolusage |Quota amount of nonpaged pool usage for a process.
157
+ quotapagedpoolusage |Quota amount of paged pool usage for a process.
158
+ quotapeaknonpagedpoolusage |Peak quota amount of nonpaged pool usage for a process.
159
+ quotapeakpagedpoolusage |Peak quota amount of paged pool usage for a process.
160
+ readoperationcount |Number of read operations performed.
161
+ readtransfercount |Amount of data read.
162
+ sessionid |Unique identifier that an operating system generates when a session is created. A session spans a period of time from logon until logoff from a specific system.
163
+ sid |The security identifier descriptor for the owner of this process.
164
+ status |State of a process ('running' or 'not running')
165
+ terminationdate |Process was stopped or terminated. To get the termination time, a handle to the process must be held open. Otherwise, this property returns NULL.
166
+ threadcount |Number of active threads in a process.
167
+ user |The user name of the owner of this process.
168
+ usermodetime |Time in user mode, in 100 nanosecond units. If this information is not available, use a value of 0 (zero).
169
+ virtualsize |Current size of the virtual address space that a process is using, not the physical or virtual memory actually used by the process.
170
+ windowsversion |Version of Windows in which the process is running.
171
+ wmi_object |Native WMI Object represented by the process.
172
+ workingsetsize |Amount of memory in bytes that a process needs to execute efficiently—for an operating system that uses page-based memory management.
173
+ writeoperationcount |Number of write operations performed.
174
+ writetransfercount |Amount of data written.
175
+
176
+
177
+ ## Contributing
178
+
179
+ See [CONTRIBUTING](CONTRIBUTING.md)
180
+
181
+
182
+ ## License
183
+
184
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ platform = 'windows' if RUBY_PLATFORM =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/
5
+ platform = 'linux' if RUBY_PLATFORM =~ /linux/
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = FileList["spec/#{platform}/*/*_spec.rb"]
9
+ spec.rspec_opts = ['--color', '--format d']
10
+ end
11
+
12
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hawatel_ps/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hawatel_ps"
8
+ spec.version = HawatelPs::VERSION
9
+ spec.authors = ['Przemyslaw Mantaj','Daniel Iwaniuk']
10
+ spec.email = ['przemyslaw.mantaj@hawatel.com', 'daniel.iwaniuk@hawatel.com']
11
+
12
+ spec.summary = "Ruby gem for retrieving information about running processes"
13
+ spec.description = %q{HawatelPS (hawatel_ps) is a Ruby gem for retrieving information about running processes. It is easy to use and you can get useful information about a process.
14
+ You can terminate, suspend, resume and check status of the process on Linxu platform. On Windows platform you can terminate and check state of the process.}
15
+ spec.homepage = "http://github.com/hawatel/hawatel_ps"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files`.split($/)
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.required_ruby_version = '>= 1.9'
24
+
25
+ spec.add_development_dependency "factory_girl"
26
+ spec.add_development_dependency "bundler", "~> 1.10"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec"
29
+ end
@@ -0,0 +1,75 @@
1
+ module HawatelPS
2
+ module Linux
3
+ class ProcControl
4
+
5
+ # Check current process status
6
+ # @example
7
+ # p = HawatelPS.search_by_name('top')
8
+ # p.status
9
+ #
10
+ # @return [String] - current process status (running, stopped ..)
11
+ def status
12
+ sleep(0.2)
13
+ status_file = "/proc/#{@proc_attrs[:pid]}/status"
14
+ if File.readable?(status_file)
15
+ File.foreach(status_file).each do |attr|
16
+ return attr.split(' ')[2].chop[1..-1] if attr =~ /State:/
17
+ end
18
+ end
19
+ 'terminated'
20
+ end
21
+
22
+ # Suspend process without loss data (equal to Kill -SIGSTOP <pid> command)
23
+ #
24
+ # @example
25
+ # p = HawatelPS.search_by_name('top')
26
+ # p.suspend
27
+ #
28
+ # @return [String] - current process status (running, stopped ..)
29
+ def suspend
30
+ process_status = status
31
+ if process_status != 'terminated' && process_status != 'stopped'
32
+ return status if Process.kill('STOP', @proc_attrs[:pid].to_i)
33
+ end
34
+ process_status
35
+ rescue Errno::EPERM
36
+ return 'non-privilaged operation'
37
+ end
38
+
39
+ # Resume suspended process (equal to Kill -SIGCONT <pid> command)
40
+ #
41
+ # @example
42
+ # p = HawatelPS.search_by_name('top')
43
+ # p.resume
44
+ #
45
+ # @return [String] - current process status (running, stopped ..)
46
+ def resume
47
+ process_status = status
48
+ if process_status == 'stopped'
49
+ return status if Process.kill('CONT', @proc_attrs[:pid].to_i)
50
+ end
51
+ process_status
52
+ rescue Errno::EPERM
53
+ return 'non-privilaged operation'
54
+ end
55
+
56
+ # Kill process (equal to Kill -SIGKILL <pid> command)
57
+ #
58
+ # @example
59
+ # p = HawatelPS.search_by_name('top')
60
+ # p.kill
61
+ #
62
+ # @return [String] - current process status (running, stopped ..)
63
+ def terminate
64
+ process_status = status
65
+ if process_status != 'terminated'
66
+ return status if Process.kill('KILL', @proc_attrs[:pid])
67
+ end
68
+ process_status
69
+ rescue Errno::EPERM
70
+ return 'non-privilaged operation'
71
+ end
72
+
73
+ end
74
+ end
75
+ end