linux_stat 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a458ed15e6c60deec8faa2f1633a7025b2f21af35a72228414746767ce6752c2
4
+ data.tar.gz: 7c5f8b808747d244310374315879cbdd92fb2f39df260ffa23ee9f275ae5badb
5
+ SHA512:
6
+ metadata.gz: 11ef7195e16c5df7144272a241f147e4bb6f66e6ea0da67025df313b2ccb3b78e454766b701a9946d8467ba305b2e4345d8e9eb9df704a84d51aa25be69450e2
7
+ data.tar.gz: 21ac8e0f7cb2174f49ef1cf702114522ac766c178344746352d58b9a6744ef847898f66ee8da903ba224560a9925a0eb3361d2f1a2bd080941228b13fee540b6
@@ -0,0 +1,11 @@
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 ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
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/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in linux_stat.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,34 @@
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
@@ -0,0 +1,21 @@
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.
@@ -0,0 +1,188 @@
1
+ # LinuxStat
2
+
3
+ LinuxStat lets you read statistics of a Linux system. It can show you cpu stats and usages, memory stats and usages, swap stats and usages, battery usage, bios info, kernel details, local ip, os details and parse os-release + lsb-release, etc.
4
+
5
+ It only works for Linux, and detecting the OS is upto the user of this gem.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'linux_stat'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install linux_stat
22
+
23
+ ## Usage
24
+
25
+ ```
26
+ LinuxStat::BIOS.date
27
+ => 04/10/2017
28
+
29
+ LinuxStat::BIOS.version
30
+ => 1.1.2
31
+
32
+ LinuxStat::BIOS.model
33
+ => Inspiron 5567
34
+
35
+ LinuxStat::BIOS.vendor
36
+ => Dell Inc.
37
+
38
+ LinuxStat::Battery.present?
39
+ => true
40
+
41
+ LinuxStat::Battery.status
42
+ => Full
43
+
44
+ LinuxStat::Battery.model
45
+ => DELL CYMGM77
46
+
47
+ LinuxStat::Battery.manufacturer
48
+ => Samsung SDI
49
+
50
+ LinuxStat::Battery.technology
51
+ => Li-ion
52
+
53
+ LinuxStat::Battery.charge
54
+ => 100.0
55
+
56
+ LinuxStat::Battery.charging?
57
+ => true
58
+
59
+ LinuxStat::Battery.discharging?
60
+ => false
61
+
62
+ LinuxStat::Battery.full?
63
+ => true
64
+
65
+ LinuxStat::Battery.stat
66
+ => {:model=>"DELL CYMGM77", :manufacturer=>"Samsung SDI", :technology=>"Li-ion", :status=>"Full", :charge=>100.0, :charging=>true, :discharging=>false, :full=>true}
67
+
68
+ LinuxStat::CPU.count
69
+ => 4
70
+
71
+ LinuxStat::CPU.model
72
+ => Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz
73
+
74
+ LinuxStat::CPU.stat
75
+ => {0=>0.0, 1=>0.0, 2=>0.0, 3=>0.0, 4=>0.0}
76
+
77
+ LinuxStat::CPU.cur_freq
78
+ => [2000094, 2000072, 2000045, 2000119]
79
+
80
+ LinuxStat::CPU.max_freq
81
+ => [2000000, 2000000, 2000000, 2000000]
82
+
83
+ LinuxStat::Kernel.version
84
+ => 5.9.1-xanmod1-1
85
+
86
+ LinuxStat::Kernel.build_date
87
+ => 2020-10-21 01:11:20 +0000
88
+
89
+ LinuxStat::Kernel.string
90
+ => Linux version 5.9.1-xanmod1-1 (souravgoswami@archlinux) (gcc (GCC) 10.2.0, GNU ld (GNU Binutils) 2.35.1) #1 SMP PREEMPT Wed, 21 Oct 2020 01:11:20 +0000
91
+
92
+ LinuxStat::Kernel.compiler
93
+ => [:gcc, "10.2.0"]
94
+
95
+ LinuxStat::Memory.total
96
+ => 3836256
97
+
98
+ LinuxStat::Memory.stat
99
+ => {:total=>3836256, :used=>3331632, :available=>504624, :percent_used=>86.85, :percent_available=>13.15}
100
+
101
+ LinuxStat::Memory.available
102
+ => 504624
103
+
104
+ LinuxStat::Memory.used
105
+ => 3331632
106
+
107
+ LinuxStat::Memory.percent_used
108
+ => 86.85
109
+
110
+ LinuxStat::Memory.percent_available
111
+ => 13.15
112
+
113
+ LinuxStat::Net.ipv4_private
114
+ => 192.168.0.106
115
+
116
+ LinuxStat::OS.os_release
117
+ => {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling", :ANSI_COLOR=>"38;2;23;147;209", :HOME_URL=>"https://www.archlinux.org/", :DOCUMENTATION_URL=>"https://wiki.archlinux.org/", :SUPPORT_URL=>"https://bbs.archlinux.org/"...
118
+
119
+ LinuxStat::OS.lsb_release
120
+ => {:LSB_VERSION=>"1.4", :DISTRIB_ID=>"Arch", :DISTRIB_RELEASE=>"rolling", :DISTRIB_DESCRIPTION=>"Arch Linux"}
121
+
122
+ LinuxStat::OS.uptime
123
+ => {:hour=>6, :minute=>57, :second=>37.16}
124
+
125
+ LinuxStat::Process.count
126
+ => 209
127
+
128
+ LinuxStat::Process.sleeping
129
+ => [1, 2, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 38, 40, 41, 42, 46, 47, 48, 50, 51, 52, 112, 114, 115, 167, 169, 187, 214, 229, 314, 316, 317, 318, 319, 324, 325, 327, 340, 361, 362, 363, 364, 372, 375, 378,...
130
+
131
+ LinuxStat::Process.zombie
132
+ => []
133
+
134
+ LinuxStat::Process.running
135
+ => [21279]
136
+
137
+ LinuxStat::Process.idle
138
+ => [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118, 119, 121, 131, 134, 140, 152, 153, 168, 170, 172, 173, 177, 181, 182, 183, 184, 185, 186, 220, 225, 271, 322, 326, 329, 333, 2914, 2915, 2916, 2917, 2918, 2919, 148...
139
+
140
+ LinuxStat::Process.names
141
+ => {1=>"systemd", 2=>"kthreadd", 3=>"rcu_gp", 4=>"rcu_par_gp", 6=>"kworker/0:0H-kblockd", 9=>"mm_percpu_wq", 10=>"ksoftirqd/0", 11=>"rcuc/0", 12=>"rcu_preempt", 13=>"rcub/0", 14=>"migration/0", 15=>"idle_inject/0", 16=>"cpuhp/0", 17=>"cpuhp/1", 18=>"idle...
142
+
143
+ LinuxStat::Process.list
144
+ => [1, 2, 3, 4, 6, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40, 41, 42, 46, 47, 48, 49, 50, 51, 52, 102, 103, 104, 106, 107, 108, 109, 110, 112, 114, 115, 117, 118, 119, 121, 131, 134,...
145
+
146
+ LinuxStat::Process.types
147
+ => {1=>:sleeping, 2=>:sleeping, 3=>:idle, 4=>:idle, 6=>:idle, 9=>:idle, 10=>:sleeping, 11=>:sleeping, 12=>:idle, 13=>:sleeping, 14=>:sleeping, 15=>:sleeping, 16=>:sleeping, 17=>:sleeping, 18=>:sleeping, 19=>:sleeping, 20=>:sleeping, 21=>:sleeping, 23=>:i...
148
+
149
+ LinuxStat::Swap.total
150
+ => 4194300
151
+
152
+ LinuxStat::Swap.stat
153
+ => {:total=>4194300, :used=>1236572, :available=>2957728, :percent_used=>29.48, :percent_available=>70.52}
154
+
155
+ LinuxStat::Swap.available
156
+ => 2957728
157
+
158
+ LinuxStat::Swap.used
159
+ => 1236572
160
+
161
+ LinuxStat::Swap.percent_used
162
+ => 29.48
163
+
164
+ LinuxStat::Swap.list
165
+ => {"/dev/zram0"=>[:partition, 4194300, 1236572, -2]}
166
+
167
+ LinuxStat::Swap.percent_available
168
+ => 70.52
169
+ ```
170
+
171
+
172
+ ## Development
173
+
174
+ 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.
175
+
176
+ 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).
177
+
178
+ ## Contributing
179
+
180
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/linux_stat. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[Souravgoswami]/linux_stat/blob/master/CODE_OF_CONDUCT.md).
181
+
182
+ ## License
183
+
184
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
185
+
186
+ ## Code of Conduct
187
+
188
+ Everyone interacting in the LinuxStat project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/linux_stat/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "linux_stat"
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(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
1
+ require "linux_stat/version"
2
+
3
+ module LinuxStat
4
+ class StatUnavailable < StandardError
5
+ end
6
+ end
7
+
8
+ require "linux_stat/battery"
9
+ require "linux_stat/bios"
10
+ require "linux_stat/cpu"
11
+ require "linux_stat/kernel"
12
+ require "linux_stat/memory"
13
+ require "linux_stat/net"
14
+ require "linux_stat/os"
15
+ require "linux_stat/process"
16
+ require "linux_stat/swap"
@@ -0,0 +1,90 @@
1
+ module LinuxStat
2
+ module Battery
3
+ PATH = "/sys/class/power_supply/BAT0"
4
+
5
+ class << self
6
+ def present?
7
+ @@present ||= Dir.exist?(PATH)
8
+ end
9
+
10
+ def stat
11
+ st = status.downcase
12
+ return {} unless present?
13
+
14
+ {
15
+ model: model,
16
+ manufacturer: manufacturer,
17
+ technology: technology,
18
+ status: status,
19
+ charge: charge,
20
+ charging: %w(full charging unknown).each(&:freeze).include?(st),
21
+ discharging: st == 'discharging'.freeze,
22
+ full: st == 'full'.freeze,
23
+ }
24
+ end
25
+
26
+ def model
27
+ return ''.freeze unless model_readable?
28
+ IO.read(File.join(PATH, 'model_name')).tap(&:strip!)
29
+ end
30
+
31
+ def manufacturer
32
+ return ''.freeze unless manufacturer_readable?
33
+ IO.read(File.join(PATH, 'manufacturer')).tap(&:strip!)
34
+ end
35
+
36
+ def technology
37
+ return ''.freeze unless tech_readable?
38
+ IO.read(File.join(PATH, 'technology')).tap(&:strip!)
39
+ end
40
+
41
+ def status
42
+ return ''.freeze unless status_readable?
43
+ IO.read(File.join(PATH, 'status')).tap(&:strip!)
44
+ end
45
+
46
+ def charging?
47
+ %w(full charging unknown).each(&:freeze).include?(status.downcase)
48
+ end
49
+
50
+ def discharging?
51
+ status.downcase == 'discharging'
52
+ end
53
+
54
+ def full?
55
+ status.downcase == 'full'
56
+ end
57
+
58
+ def charge
59
+ return 0.0 unless charge_now_readable?
60
+ charge_now = IO.read(File.join(PATH, 'charge_now')).to_i
61
+ charge_full = IO.read(File.join(PATH, 'charge_full')).to_i
62
+
63
+ percentage = charge_now.*(100).fdiv(charge_full)
64
+ percentage = percentage > 100 ? 100.0 : percentage < 0 ? 0.0 : percentage
65
+ end
66
+
67
+ private
68
+ def model_readable?
69
+ @@model_readable ||= File.readable?(File.join(PATH, 'model_name'))
70
+ end
71
+
72
+ def manufacturer_readable?
73
+ @@manufacturer_readable ||= File.readable?(File.join(PATH, 'manufacturer'))
74
+ end
75
+
76
+ def tech_readable?
77
+ @@tech_readable ||= File.readable?(File.join(PATH, 'technology'))
78
+ end
79
+
80
+ def status_readable?
81
+ @@status_readable ||= File.readable?(File.join(PATH, 'status'))
82
+ end
83
+
84
+ def charge_now_readable?
85
+ @@charge_now_readable ||= File.readable?(File.join(PATH, 'charge_now')) &&
86
+ File.readable?(File.join(PATH, 'charge_full'))
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,43 @@
1
+ module LinuxStat
2
+ module BIOS
3
+ class << self
4
+ def model
5
+ # Cached ; as changing the value in runtime is unexpected
6
+ @@model ||= if File.readable?('/sys/devices/virtual/dmi/id/product_name')
7
+ IO.read('/sys/devices/virtual/dmi/id/product_name').tap(&:strip!)
8
+ elsif File.readable?('/sys/firmware/devicetree/base/model')
9
+ IO.read('/sys/firmware/devicetree/base/model').tap(&:strip!)
10
+ else
11
+ ''.freeze
12
+ end
13
+ end
14
+
15
+ def vendor
16
+ # Cached ; as changing the value in runtime is unexpected
17
+ @@vendor ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_vendor')
18
+ IO.read('/sys/devices/virtual/dmi/id/bios_vendor').tap(&:strip!)
19
+ else
20
+ ''.freeze
21
+ end
22
+ end
23
+
24
+ def version
25
+ # Cached ; as changing the value in runtime is unexpected
26
+ @@version ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_version')
27
+ IO.read('/sys/devices/virtual/dmi/id/bios_version').tap(&:strip!)
28
+ else
29
+ ''.freeze
30
+ end
31
+ end
32
+
33
+ def date
34
+ # Cached ; as changing the value in runtime is unexpected
35
+ @@date ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_date')
36
+ IO.read('/sys/devices/virtual/dmi/id/bios_date').tap(&:strip!)
37
+ else
38
+ ''.freeze
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,63 @@
1
+ module LinuxStat
2
+ module CPU
3
+ class << self
4
+ def stat(sleep = 0.075)
5
+ return {} unless stat?
6
+
7
+ data = IO.readlines('/proc/stat').select! { |x| x[/^cpu\d*/] }.map! { |x| x.split.map!(&:to_f) }
8
+ sleep(sleep)
9
+ data2 = IO.readlines('/proc/stat').select! { |x| x[/^cpu\d*/] }.map! { |x| x.split.map!(&:to_f) }
10
+
11
+ data.size.times.reduce({}) do |h, x|
12
+ user, nice, sys, idle, iowait, irq, softirq, steal = *data[x].drop(1)
13
+ user2, nice2, sys2, idle2, iowait2, irq2, softirq2, steal2 = *data2[x].drop(1)
14
+
15
+ idle_then, idle_now = idle + iowait, idle2 + iowait2
16
+ totald = idle_now.+(user2 + nice2 + sys2 + irq2 + softirq2 + steal2) - idle_then.+(user + nice + sys + irq + softirq + steal)
17
+
18
+ h.merge!(
19
+ x => totald.-(idle_now - idle_then).fdiv(totald).*(100).round(2).abs
20
+ )
21
+ end
22
+ end
23
+
24
+ def count
25
+ # CPU count can change during the program runtime
26
+ cpuinfo.count { |x| x.start_with?('processor') }
27
+ end
28
+
29
+ def model
30
+ # Cached ; as changing the value in runtime is unexpected ; nobody is going
31
+ # to add/remove CPUs during program runtime
32
+ @@name ||= cpuinfo.find { |x| x.start_with?('model name') }.to_s.split(?:)[-1].to_s.strip
33
+ end
34
+
35
+ # Returns an array with current core frequencies corresponding to the usages
36
+ def cur_freq
37
+ # Cached ; as changing the value in runtime is unexpected ; nobody is going
38
+ # to add/remove CPUs during program runtime
39
+
40
+ @@cpu_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_cur_freq"]
41
+ @@cpu_freqs.map { |x| IO.read(x).to_i }
42
+ end
43
+
44
+ # Returns an array with max core frequencies corresponding to the usages
45
+ def max_freq
46
+ # Cached ; as changing the value in runtime is unexpected ; nobody is going
47
+ # to add/remove CPUs during program runtime
48
+
49
+ @@max_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_max_freq"]
50
+ @@max_freqs.map { |x| IO.read(x).to_i }
51
+ end
52
+
53
+ private
54
+ def cpuinfo
55
+ File.readable?('/proc/cpuinfo') ? IO.readlines('/proc/cpuinfo') : []
56
+ end
57
+
58
+ def stat?
59
+ @@stat_readable ||= File.readable?('/proc/stat')
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,38 @@
1
+ module LinuxStat
2
+ module Kernel
3
+ class << self
4
+ def version
5
+ return @@version ||= ''.freeze if string.empty?
6
+ @@version ||= string.split[2]
7
+ end
8
+
9
+ def compiler
10
+ return @@compiler ||= ''.freeze if string.empty?
11
+
12
+ @@compiler ||= case string.split[4].to_s
13
+ when /gcc/i then [:gcc ]
14
+ when /clang/i then [:clang]
15
+ when /icc/i then [:icc]
16
+ end << string[/\(.*\)/].split.drop(1).find { |x| x[/^\d.*\d/] }[/^\d.*\d/]
17
+ end
18
+
19
+ def build_date
20
+ return @@time ||= Time.new(0) if string.empty?
21
+
22
+ @@time ||= begin
23
+ require 'time'
24
+ Time.strptime(string.split[16..-1].join(' '), "%d %b %Y %H:%M:%S %z")
25
+ rescue StandardError
26
+ Time.new(0)
27
+ end
28
+ end
29
+
30
+ def string
31
+ # Cached ; as changing the value in runtime is unexpected
32
+ # Hotfix update can be problem, but it's rare and might not
33
+ # affect the version string during program runtime.
34
+ @@string ||= File.readable?('/proc/version') ? IO.read('/proc/version').tap(&:strip!) : ''
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,62 @@
1
+ module LinuxStat
2
+ module Memory
3
+ class << self
4
+ def stat
5
+ return {} unless meminfo?
6
+
7
+ memory = IO.foreach('/proc/meminfo').first(3)
8
+
9
+ total = memory[0].split[1].to_i
10
+ available = memory[2].split[1].to_i
11
+ used = total - available
12
+
13
+ percent_used = total == 0 ? 0.0 : used.*(100).fdiv(total).round(2)
14
+ percent_available = total == 0 ? 0.0 : available.*(100).fdiv(total).round(2)
15
+
16
+ # We have all the methods, but each methods reads the same file
17
+ # So better to use the above calculation
18
+ {
19
+ total: total,
20
+ used: used,
21
+ available: available,
22
+ percent_used: percent_used,
23
+ percent_available: percent_available
24
+ }
25
+ end
26
+
27
+ def total
28
+ return 0 unless meminfo?
29
+ IO.foreach('/proc/meminfo').first.split[1].to_i
30
+ end
31
+
32
+ def available
33
+ return 0 unless meminfo?
34
+ IO.foreach('/proc/meminfo').first(3)[-1].split[1].to_i
35
+ end
36
+
37
+ def used
38
+ return 0 unless meminfo?
39
+ memory = IO.foreach('/proc/meminfo').first(3)
40
+ memory[0].split[1].to_i - memory[2].split[1].to_i
41
+ end
42
+
43
+ def percent_used
44
+ return 0.0 unless meminfo?
45
+ memory = IO.foreach('/proc/meminfo').first(3)
46
+ total = memory[0].split[1].to_i
47
+ total.-(memory[2].split[1].to_i).*(100).fdiv(total).round(2)
48
+ end
49
+
50
+ def percent_available
51
+ return 0.0 unless meminfo?
52
+ memory = IO.foreach('/proc/meminfo').first(3)
53
+ memory[2].split[1].to_i.*(100).fdiv(memory[0].split[1].to_i).round(2)
54
+ end
55
+
56
+ private
57
+ def meminfo?
58
+ @@readable ||= File.readable?('/proc/meminfo')
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,11 @@
1
+ module LinuxStat
2
+ module Net
3
+ class << self
4
+ def ipv4_private
5
+ require 'socket'
6
+ ip = Socket.ip_address_list.find(&:ipv4_private?)
7
+ ip ? ip.ip? ? ip.ip_unpack[0].freeze : '' : ''
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,52 @@
1
+ module LinuxStat
2
+ module OS
3
+ class << self
4
+ def os_release
5
+ # Cached ; as changing the value in runtime is unexpected
6
+ @@os_release ||= File.readable?('/etc/os-release') ? release('/etc/os-release') : {}
7
+ end
8
+
9
+ def lsb_release
10
+ # Cached ; as changing the value in runtime is unexpected
11
+ @@lsb_release ||= File.readable?('/etc/lsb-release') ? release('/etc/lsb-release') : {}
12
+ end
13
+
14
+ def uptime
15
+ raise StatUnavailable, 'Cannot read /proc/uptime' unless uptime_readable?
16
+
17
+ uptime = IO.read('/proc/uptime').to_f
18
+ uptime_i = uptime.to_i
19
+
20
+ h = uptime_i / 3600
21
+ m = uptime_i % 3600 / 60
22
+ s = uptime.%(3600).%(60).round(2)
23
+
24
+ {
25
+ hour: h,
26
+ minute: m,
27
+ second: s
28
+ }
29
+ end
30
+
31
+ private
32
+ def release(file)
33
+ IO.readlines(file).reduce({}) { |h, x|
34
+ x.strip!
35
+ next h if x.empty?
36
+
37
+ splitted = x.split(?=)
38
+
39
+ key = splitted[0].to_s.strip
40
+ value = splitted[1..-1].join(?=).to_s.strip
41
+
42
+ dumped = value[0] == ?" && value[-1] == ?"
43
+ h.merge!( key.to_sym => dumped ? value.undump : value )
44
+ }
45
+ end
46
+
47
+ def uptime_readable?
48
+ @@uptime_readable = File.readable?('/proc/uptime')
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,84 @@
1
+ module LinuxStat
2
+ module Process
3
+ class << self
4
+ def list
5
+ Dir['/proc/*'].select! { |x|
6
+ pid = File.split(x)[1]
7
+ pid.to_i.to_s == pid
8
+ }.map! { |x| File.split(x)[-1].to_i }
9
+ end
10
+
11
+ def count
12
+ list.count
13
+ end
14
+
15
+ def names
16
+ list.reduce({}) { |h, x|
17
+ begin
18
+ h.merge!( x => IO.foreach(File.join('/proc', x.to_s, 'status')).first.split[1] )
19
+ rescue Exception
20
+ h
21
+ end
22
+ }
23
+ end
24
+
25
+ def types
26
+ list.reduce({}) { |h, x|
27
+ begin
28
+ h.merge!(x =>
29
+ case IO.read(File.join('/proc', x.to_s, 'stat')).split[2]
30
+ when ?S.freeze then :sleeping
31
+ when ?I.freeze then :idle
32
+ when ?Z.freeze then :zombie
33
+ when ?R.freeze then :running
34
+ else :unknown
35
+ end
36
+ )
37
+ rescue Exception
38
+ h
39
+ end
40
+ }
41
+ end
42
+
43
+ def sleeping
44
+ list.select { |x|
45
+ begin
46
+ IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?S
47
+ rescue Exception
48
+ nil
49
+ end
50
+ }
51
+ end
52
+
53
+ def idle
54
+ list.select { |x|
55
+ begin
56
+ IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?I
57
+ rescue Exception
58
+ nil
59
+ end
60
+ }
61
+ end
62
+
63
+ def zombie
64
+ list.select { |x|
65
+ begin
66
+ IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?Z
67
+ rescue Exception
68
+ nil
69
+ end
70
+ }
71
+ end
72
+
73
+ def running
74
+ list.select { |x|
75
+ begin
76
+ IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?R
77
+ rescue Exception
78
+ nil
79
+ end
80
+ }
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,78 @@
1
+ module LinuxStat
2
+ module Swap
3
+ class << self
4
+ # List all swap devices
5
+ def list
6
+ file = IO.readlines('/proc/swaps').drop(1)
7
+ file.reduce({}) do |h, x|
8
+ name, *stats = x.strip.split
9
+ h.merge!(name => stats.map! { |v| v.to_i.to_s == v ? v.to_i : v.to_sym })
10
+ end
11
+ end
12
+
13
+ # Show aggregated used and available swap
14
+ def stat
15
+ values_t = read_usage
16
+
17
+ total, used = values_t[0].sum, values_t[-1].sum
18
+ available = total - used
19
+ percent_used = total == 0 ? 0.0 : used.*(100).fdiv(total).round(2)
20
+ percent_available = total == 0.0 ? 0 : available.*(100).fdiv(total).round(2)
21
+
22
+ # We have all the methods, but each methods reads the same file
23
+ {
24
+ total: total,
25
+ used: used,
26
+ available: available,
27
+ percent_used: percent_used,
28
+ percent_available: percent_available
29
+ }
30
+ end
31
+
32
+ def total
33
+ read_usage[0].sum
34
+ end
35
+
36
+ def available
37
+ values_t = read_usage
38
+ values_t[0].sum - values_t[1].sum
39
+ end
40
+
41
+ def used
42
+ read_usage[-1].sum
43
+ end
44
+
45
+ def percent_used
46
+ values_t = read_usage
47
+
48
+ total = values_t[0].sum
49
+ return 0.0 if total == 0
50
+
51
+ values_t[-1].sum.*(100).fdiv(total).round(2)
52
+ end
53
+
54
+ def percent_available
55
+ values_t = read_usage
56
+
57
+ total = values_t[0].sum
58
+ return 0.0 if total == 0
59
+
60
+ total.-(values_t[-1].sum).*(100).fdiv(total).round(2)
61
+
62
+ end
63
+
64
+ private
65
+ def read_usage
66
+ return [[], []] unless swaps_readable?
67
+
68
+ IO.readlines('/proc/swaps').drop(1).map { |x|
69
+ x.strip.split.values_at(2, 3).map!(&:to_i)
70
+ }.transpose
71
+ end
72
+
73
+ def swaps_readable?
74
+ @@swaps_readable ||= File.readable?('/proc/swaps')
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,3 @@
1
+ module LinuxStat
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,29 @@
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
@@ -0,0 +1,51 @@
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
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linux_stat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sourav Goswami
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-11-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Efficient linux system reporting gem. Linux Only | Efficient | Reliable
14
+ email:
15
+ - souravgoswami@protonmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - lib/linux_stat.rb
32
+ - lib/linux_stat/battery.rb
33
+ - lib/linux_stat/bios.rb
34
+ - lib/linux_stat/cpu.rb
35
+ - lib/linux_stat/kernel.rb
36
+ - lib/linux_stat/memory.rb
37
+ - lib/linux_stat/net.rb
38
+ - lib/linux_stat/os.rb
39
+ - lib/linux_stat/process.rb
40
+ - lib/linux_stat/swap.rb
41
+ - lib/linux_stat/version.rb
42
+ - linux_stat.gemspec
43
+ - run_all_methods.rb
44
+ homepage:
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.4.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.1.2
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Efficient linux system reporting gem
67
+ test_files: []