linux_stat 0.1.1 → 0.2.0

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.
@@ -1,6 +1,8 @@
1
1
  module LinuxStat
2
2
  module Process
3
3
  class << self
4
+ # Returns the list of processes from /proc/.
5
+ # The return type is an Array of Integers.
4
6
  def list
5
7
  Dir['/proc/*'].select! { |x|
6
8
  pid = File.split(x)[1]
@@ -8,10 +10,13 @@ module LinuxStat
8
10
  }.map! { |x| File.split(x)[-1].to_i }
9
11
  end
10
12
 
13
+ # Counts and returns the total number of process running on the system.
14
+ # The return type is Integer.
11
15
  def count
12
16
  list.count
13
17
  end
14
18
 
19
+ # Returns all the id of processes mapped with their names as a Hash.
15
20
  def names
16
21
  list.reduce({}) { |h, x|
17
22
  begin
@@ -22,6 +27,7 @@ module LinuxStat
22
27
  }
23
28
  end
24
29
 
30
+ # Returns all the id of processes mapped with their status as a Hash.
25
31
  def types
26
32
  list.reduce({}) { |h, x|
27
33
  begin
@@ -40,6 +46,8 @@ module LinuxStat
40
46
  }
41
47
  end
42
48
 
49
+ # Returns all the id of processes that are sleeping.
50
+ # The return type is an Array of Integers.
43
51
  def sleeping
44
52
  list.select { |x|
45
53
  begin
@@ -50,6 +58,8 @@ module LinuxStat
50
58
  }
51
59
  end
52
60
 
61
+ # Returns all the id of processes that are idle.
62
+ # The return type is an Array of Integers.
53
63
  def idle
54
64
  list.select { |x|
55
65
  begin
@@ -60,6 +70,8 @@ module LinuxStat
60
70
  }
61
71
  end
62
72
 
73
+ # Returns all the id of processes that are zombies.
74
+ # The return type is an Array of Integers.
63
75
  def zombie
64
76
  list.select { |x|
65
77
  begin
@@ -70,6 +82,8 @@ module LinuxStat
70
82
  }
71
83
  end
72
84
 
85
+ # Returns all the id of processes that are running.
86
+ # The return type is an Array of Integers.
73
87
  def running
74
88
  list.select { |x|
75
89
  begin
@@ -1,8 +1,11 @@
1
1
  module LinuxStat
2
2
  module Swap
3
3
  class << self
4
- # List all swap devices
4
+ # List all swap devices and returns a Hash.
5
+ # If the info isn't available, it will return an empty Hash.
5
6
  def list
7
+ return {} unless swaps_readable?
8
+
6
9
  file = IO.readlines('/proc/swaps').drop(1)
7
10
  file.reduce({}) do |h, x|
8
11
  name, *stats = x.strip.split
@@ -10,8 +13,18 @@ module LinuxStat
10
13
  end
11
14
  end
12
15
 
13
- # Show aggregated used and available swap
16
+ # Returns true if any swap device is available, else returns false.
17
+ # If the info isn't available, it will return an empty Hash.
18
+ def any?
19
+ !!IO.foreach('/proc/swaps').drop(1).first
20
+ end
21
+
22
+ # Show aggregated used and available swap.
23
+ # The values are in kilobytes.
24
+ # The return type is Hash.
25
+ # If the info isn't available, the return type is an empty Hash.
14
26
  def stat
27
+ return {} unless swaps_readable?
15
28
  values_t = read_usage
16
29
 
17
30
  total, used = values_t[0].sum, values_t[-1].sum
@@ -29,20 +42,35 @@ module LinuxStat
29
42
  }
30
43
  end
31
44
 
45
+ # Show total amount of swap.
46
+ # The value is in kilobytes.
47
+ # The return type is a Integer but if the info isn't available, it will return nil.
32
48
  def total
49
+ return nil unless swaps_readable?
33
50
  read_usage[0].sum
34
51
  end
35
52
 
53
+ # Show total amount of available swap.
54
+ # The value is in kilobytes.
55
+ # The return type is a Integer but if the info isn't available, it will return nil.
36
56
  def available
57
+ return nil unless swaps_readable?
37
58
  values_t = read_usage
38
59
  values_t[0].sum - values_t[1].sum
39
60
  end
40
61
 
62
+ # Show total amount of used swap.
63
+ # The value is in kilobytes.
64
+ # The return type is a Integer but if the info isn't available, it will return nil.
41
65
  def used
66
+ return nil unless swaps_readable?
42
67
  read_usage[-1].sum
43
68
  end
44
69
 
70
+ # Show percentage of swap used.
71
+ # The return type is a Float but if the info isn't available, it will return nil.
45
72
  def percent_used
73
+ return nil unless swaps_readable?
46
74
  values_t = read_usage
47
75
 
48
76
  total = values_t[0].sum
@@ -51,21 +79,26 @@ module LinuxStat
51
79
  values_t[-1].sum.*(100).fdiv(total).round(2)
52
80
  end
53
81
 
82
+ # Show percentage of swap available.
83
+ # The return type is a Float but if the info isn't available, it will return nil.
54
84
  def percent_available
85
+ return nil unless swaps_readable?
55
86
  values_t = read_usage
56
87
 
57
88
  total = values_t[0].sum
58
89
  return 0.0 if total == 0
59
90
 
60
91
  total.-(values_t[-1].sum).*(100).fdiv(total).round(2)
61
-
62
92
  end
63
93
 
64
94
  private
65
95
  def read_usage
66
96
  return [[], []] unless swaps_readable?
67
97
 
68
- IO.readlines('/proc/swaps').drop(1).map { |x|
98
+ val = IO.readlines('/proc/swaps').drop(1)
99
+ return [[], []] if val.empty?
100
+
101
+ val.map { |x|
69
102
  x.strip.split.values_at(2, 3).map!(&:to_i)
70
103
  }.transpose
71
104
  end
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,32 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_stat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-22 00:00:00.000000000 Z
11
+ date: 2020-11-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Efficient linux system reporting gem. Linux Only | Efficient | Reliable
14
14
  email:
15
15
  - souravgoswami@protonmail.com
16
16
  executables: []
17
17
  extensions: []
18
- extra_rdoc_files: []
18
+ extra_rdoc_files:
19
+ - README.md
19
20
  files:
20
- - ".gitignore"
21
- - ".rspec"
22
- - ".travis.yml"
23
- - CODE_OF_CONDUCT.md
24
21
  - Gemfile
25
- - Gemfile.lock
26
- - LICENSE.txt
27
22
  - README.md
28
- - Rakefile
29
23
  - bin/console
24
+ - bin/linuxstat.rb
30
25
  - bin/setup
31
26
  - lib/linux_stat.rb
32
27
  - lib/linux_stat/battery.rb
@@ -39,12 +34,13 @@ files:
39
34
  - lib/linux_stat/process.rb
40
35
  - lib/linux_stat/swap.rb
41
36
  - lib/linux_stat/version.rb
42
- - linux_stat.gemspec
43
- - run_all_methods.rb
44
- homepage:
37
+ homepage: https://github.com/Souravgoswami/linux_stat/
45
38
  licenses:
46
39
  - MIT
47
- metadata: {}
40
+ metadata:
41
+ homepage_uri: https://github.com/Souravgoswami/linux_stat/
42
+ source_code_uri: https://github.com/Souravgoswami/linux_stat
43
+ changelog_uri: https://github.com/Souravgoswami/linux_stat/commits/master
48
44
  post_install_message:
49
45
  rdoc_options: []
50
46
  require_paths:
@@ -53,7 +49,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
49
  requirements:
54
50
  - - ">="
55
51
  - !ruby/object:Gem::Version
56
- version: 2.4.0
52
+ version: 2.5.0
57
53
  required_rubygems_version: !ruby/object:Gem::Requirement
58
54
  requirements:
59
55
  - - ">="
data/.gitignore DELETED
@@ -1,11 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
-
10
- # rspec failure tracking
11
- .rspec_status
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
@@ -1,6 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.7.1
6
- before_install: gem install bundler -v 2.1.4
@@ -1,74 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
28
- * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- * Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at souravgoswami@protonmail.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [https://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: https://contributor-covenant.org
74
- [version]: https://contributor-covenant.org/version/1/4/
@@ -1,34 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- linux_stat (0.1.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- diff-lcs (1.4.4)
10
- rake (12.3.3)
11
- rspec (3.10.0)
12
- rspec-core (~> 3.10.0)
13
- rspec-expectations (~> 3.10.0)
14
- rspec-mocks (~> 3.10.0)
15
- rspec-core (3.10.0)
16
- rspec-support (~> 3.10.0)
17
- rspec-expectations (3.10.0)
18
- diff-lcs (>= 1.2.0, < 2.0)
19
- rspec-support (~> 3.10.0)
20
- rspec-mocks (3.10.0)
21
- diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.10.0)
23
- rspec-support (3.10.0)
24
-
25
- PLATFORMS
26
- ruby
27
-
28
- DEPENDENCIES
29
- linux_stat!
30
- rake (~> 12.0)
31
- rspec (~> 3.0)
32
-
33
- BUNDLED WITH
34
- 2.1.4
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2020 Sourav Goswami
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/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
@@ -1,29 +0,0 @@
1
- require_relative 'lib/linux_stat/version'
2
-
3
- Gem::Specification.new do |spec|
4
- spec.name = "linux_stat"
5
- spec.version = LinuxStat::VERSION
6
- spec.authors = ["Sourav Goswami"]
7
- spec.email = ["souravgoswami@protonmail.com"]
8
-
9
- spec.summary = %q{Efficient linux system reporting gem}
10
- spec.description = %q{Efficient linux system reporting gem. Linux Only | Efficient | Reliable}
11
- # spec.homepage = "TODO: Put your gem's website or public repo URL here."
12
- spec.license = "MIT"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
14
-
15
- # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
-
17
- # spec.metadata["homepage_uri"] = spec.homepage
18
- # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
19
- # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
-
21
- # Specify which files should be added to the gem when it is released.
22
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
- end
26
- spec.bindir = "exe"
27
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
- spec.require_paths = ["lib"]
29
- end
@@ -1,51 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'bundler/setup'
3
- require 'linux_stat'
4
- require 'io/console'
5
-
6
- # Print time each method takes unless --no-time or -nt option is passed
7
- PRINT_TIME = !ARGV.any? { |x| x[/^\-\-no-time$/] || x[/^\-nt$/] }
8
-
9
- $-v = true
10
-
11
- tt = Time.now
12
- wait = 3
13
- anim_delay = wait.fdiv(STDOUT.winsize[1])
14
- anim = %w(| / - \\)
15
-
16
- STDOUT.winsize[1].-(20).times do |x|
17
- t = wait - Time.now.-(tt).to_i
18
- print "\e[2K#{anim.rotate![0]} Starting test in #{t.clamp(0, wait)} #{?. * x}\r"
19
- sleep anim_delay
20
- end
21
-
22
- print "\e[2K"
23
-
24
- LinuxStat.constants.sort.each do |c|
25
- e = eval("LinuxStat::#{c}")
26
-
27
- next if e.class != Module && e.class != Class
28
-
29
- meths = e.methods(false)
30
-
31
- meths.each do |meth|
32
- time = Time.now
33
- v = e.send(meth).to_s
34
- time = Time.now.-(time).*(1000).round(3)
35
-
36
- dis = v.length > 253 ? v[0..250].strip + '...'.freeze : v
37
- puts "\e[1;38;2;80;80;255m#{e}.#{meth}\e[0m", "=> #{dis}"
38
-
39
- puts( "(" +
40
- if time > 10
41
- "\e[1;38;2;255;80;80m"
42
- elsif time > 5
43
- "\e[1;38;2;255;170;0m"
44
- else
45
- "\e[1;38;2;0;170;0m"
46
- end + "Time taken: #{time}ms\e[0m)"
47
- ) if PRINT_TIME
48
-
49
- puts
50
- end
51
- end