linux_stat 0.1.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -3
- data/README.md +65 -24
- data/bin/console +2 -12
- data/{run_all_methods.rb → bin/linuxstat.rb} +25 -9
- data/lib/linux_stat.rb +0 -2
- data/lib/linux_stat/battery.rb +24 -1
- data/lib/linux_stat/bios.rb +12 -2
- data/lib/linux_stat/cpu.rb +23 -11
- data/lib/linux_stat/kernel.rb +97 -14
- data/lib/linux_stat/memory.rb +23 -5
- data/lib/linux_stat/net.rb +3 -1
- data/lib/linux_stat/os.rb +33 -2
- data/lib/linux_stat/process.rb +14 -0
- data/lib/linux_stat/swap.rb +37 -4
- data/lib/linux_stat/version.rb +1 -1
- metadata +5 -12
- data/.gitignore +0 -11
- data/.rspec +0 -3
- data/.travis.yml +0 -6
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile.lock +0 -34
- data/LICENSE.txt +0 -21
- data/Rakefile +0 -6
- data/linux_stat.gemspec +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd93c4105b08257a85c176b7898d5ae282b1d74bb33d99d2b11536fae6402eda
|
4
|
+
data.tar.gz: a4a3a2032a1573c7f60ca09567fc9cb03ebea90cdc330f4652d7c7711d26adf6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24d055e84863433fa67cf3c5a59ddea66ad03346aaf469eb033dfa3dae362ee62117f4b07bcb251f26e73517a13dfde460132f3773e214ab34812c7838e7ccf9
|
7
|
+
data.tar.gz: 0ac774300aab62af85094f9be2162449cbfa5be8bfba0f04fcf1801e11b9bbb730db8e51a765bbd53d9d0b887b769037ddc19fa5012018e1b5d8dfc3137adddb
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# LinuxStat
|
2
|
-
![logo](https://raw.githubusercontent.com/Souravgoswami/linux_stat/master/logo.jpg)
|
2
|
+
![logo](https://raw.githubusercontent.com/Souravgoswami/linux_stat/master/logo/logo.jpg)
|
3
3
|
|
4
4
|
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.
|
5
5
|
|
@@ -78,7 +78,7 @@ LinuxStat::CPU.count
|
|
78
78
|
=> 4
|
79
79
|
|
80
80
|
LinuxStat::CPU.cur_freq
|
81
|
-
=> [
|
81
|
+
=> [2000001, 1999996, 1999998, 1999997]
|
82
82
|
|
83
83
|
LinuxStat::CPU.max_freq
|
84
84
|
=> [2000000, 2000000, 2000000, 2000000]
|
@@ -87,16 +87,16 @@ LinuxStat::CPU.model
|
|
87
87
|
=> "Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz"
|
88
88
|
|
89
89
|
LinuxStat::CPU.stat
|
90
|
-
=> {0=>
|
90
|
+
=> {0=>30.0, 1=>50.0, 2=>28.57, 3=>16.67, 4=>28.57}
|
91
91
|
|
92
92
|
LinuxStat::CPU.total_usage
|
93
|
-
=>
|
93
|
+
=> 26.67
|
94
94
|
|
95
95
|
LinuxStat::CPU.usage
|
96
|
-
=>
|
96
|
+
=> 25.0
|
97
97
|
|
98
98
|
LinuxStat::CPU.usages
|
99
|
-
=> {0=>
|
99
|
+
=> {0=>33.33, 1=>50.0, 2=>28.57, 3=>28.57, 4=>16.67}
|
100
100
|
|
101
101
|
```
|
102
102
|
|
@@ -105,6 +105,9 @@ LinuxStat::CPU.usages
|
|
105
105
|
LinuxStat::Kernel.build_date
|
106
106
|
=> 2020-10-21 01:11:20 +0000
|
107
107
|
|
108
|
+
LinuxStat::Kernel.build_date_string
|
109
|
+
=> "21 Oct 2020 01:11:20 +0000"
|
110
|
+
|
108
111
|
LinuxStat::Kernel.build_user
|
109
112
|
=> "souravgoswami@archlinux"
|
110
113
|
|
@@ -125,22 +128,22 @@ LinuxStat::Kernel.version
|
|
125
128
|
### LinuxStat::Memory
|
126
129
|
```
|
127
130
|
LinuxStat::Memory.available
|
128
|
-
=>
|
131
|
+
=> 405020
|
129
132
|
|
130
133
|
LinuxStat::Memory.percent_available
|
131
|
-
=>
|
134
|
+
=> 10.56
|
132
135
|
|
133
136
|
LinuxStat::Memory.percent_used
|
134
|
-
=>
|
137
|
+
=> 89.44
|
135
138
|
|
136
139
|
LinuxStat::Memory.stat
|
137
|
-
=> {:total=>3836264, :used=>
|
140
|
+
=> {:total=>3836264, :used=>3431244, :available=>405020, :percent_used=>89.44, :percent_available=>10.56}
|
138
141
|
|
139
142
|
LinuxStat::Memory.total
|
140
143
|
=> 3836264
|
141
144
|
|
142
145
|
LinuxStat::Memory.used
|
143
|
-
=>
|
146
|
+
=> 3431244
|
144
147
|
|
145
148
|
```
|
146
149
|
|
@@ -169,29 +172,29 @@ LinuxStat::OS.os_release
|
|
169
172
|
=> {: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/"...
|
170
173
|
|
171
174
|
LinuxStat::OS.uptime
|
172
|
-
=> {:hour=>
|
175
|
+
=> {:hour=>40, :minute=>46, :second=>19.75}
|
173
176
|
|
174
177
|
```
|
175
178
|
|
176
179
|
### LinuxStat::Process
|
177
180
|
```
|
178
181
|
LinuxStat::Process.count
|
179
|
-
=>
|
182
|
+
=> 223
|
180
183
|
|
181
184
|
LinuxStat::Process.idle
|
182
|
-
=> [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118,
|
185
|
+
=> [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118, 120, 122, 131, 134, 140, 152, 153, 166, 168, 170, 171, 174, 180, 181, 182, 183, 184, 185, 187, 224, 272, 320, 327, 328, 2238, 2239, 2240, 2241, 2242, 2243, 89437, 9...
|
183
186
|
|
184
187
|
LinuxStat::Process.list
|
185
|
-
=> [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, 111, 114, 115, 117, 118,
|
188
|
+
=> [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, 111, 114, 115, 117, 118, 120, 122, 131, 134,...
|
186
189
|
|
187
190
|
LinuxStat::Process.names
|
188
191
|
=> {1=>"systemd", 2=>"kthreadd", 3=>"rcu_gp", 4=>"rcu_par_gp", 6=>"kworker/0:0H", 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_inject/...
|
189
192
|
|
190
193
|
LinuxStat::Process.running
|
191
|
-
=> [
|
194
|
+
=> [525, 91940, 91971, 91993, 92020, 95119]
|
192
195
|
|
193
196
|
LinuxStat::Process.sleeping
|
194
|
-
=> [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, 111, 114, 115,
|
197
|
+
=> [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, 111, 114, 115, 165, 167, 186, 214, 227, 302, 313, 319, 321, 333, 334, 335, 352, 353, 354, 355, 362, 366, 367, 369, 382,...
|
195
198
|
|
196
199
|
LinuxStat::Process.types
|
197
200
|
=> {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...
|
@@ -203,36 +206,74 @@ LinuxStat::Process.zombie
|
|
203
206
|
|
204
207
|
### LinuxStat::Swap
|
205
208
|
```
|
209
|
+
LinuxStat::Swap.any?
|
210
|
+
=> true
|
211
|
+
|
206
212
|
LinuxStat::Swap.available
|
207
|
-
=>
|
213
|
+
=> 2292252
|
208
214
|
|
209
215
|
LinuxStat::Swap.list
|
210
|
-
=> {"/dev/zram0"=>[:partition, 4194300,
|
216
|
+
=> {"/dev/zram0"=>[:partition, 4194300, 1902048, -2]}
|
211
217
|
|
212
218
|
LinuxStat::Swap.percent_available
|
213
|
-
=>
|
219
|
+
=> 54.65
|
214
220
|
|
215
221
|
LinuxStat::Swap.percent_used
|
216
|
-
=>
|
222
|
+
=> 45.35
|
217
223
|
|
218
224
|
LinuxStat::Swap.stat
|
219
|
-
=> {:total=>4194300, :used=>
|
225
|
+
=> {:total=>4194300, :used=>1902048, :available=>2292252, :percent_used=>45.35, :percent_available=>54.65}
|
220
226
|
|
221
227
|
LinuxStat::Swap.total
|
222
228
|
=> 4194300
|
223
229
|
|
224
230
|
LinuxStat::Swap.used
|
225
|
-
=>
|
231
|
+
=> 1902048
|
226
232
|
|
227
233
|
```
|
228
234
|
|
235
|
+
### Return Types:
|
236
|
+
In general, if a method returns either a Float or a Integer or a Time, it will return float. But if the status isn't available, it will return nil.
|
237
|
+
If the method returns a Hash / Array, it will return return Hash. If the status isn't available, it will return an empty Hash / Array.
|
238
|
+
If the method returns a String, it will return return Hash. If the status isn't available, it will return an empty *frozen* String.
|
239
|
+
|
240
|
+
It doesn't have implementation of any Error that gets raised in runtime for the ease of use.
|
241
|
+
If you need to check some stat that returns an integer or float, and you get nil, you know it's not available, so you can work accordingly.
|
242
|
+
But if you need the integer or float value in 0 to whatever format, you can use the .to_i or .to_f method on the object, nil will get converted to number then.
|
243
|
+
|
244
|
+
If some error is *raised* it should be reported as a bug.
|
229
245
|
|
230
246
|
## Development
|
231
247
|
|
232
|
-
After checking out the repo, run `bin/setup` to install dependencies.
|
248
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
233
249
|
|
234
250
|
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).
|
235
251
|
|
252
|
+
## Testing
|
253
|
+
Like other gems, this doesn't have a test like RSpec. We suggest using the bin/linuxstat.rb file on various systems.
|
254
|
+
If you need to test a specific module, say the CPU, just run it like this:
|
255
|
+
|
256
|
+
```
|
257
|
+
ruby bin/linuxstat.rb CPU
|
258
|
+
```
|
259
|
+
|
260
|
+
Or:
|
261
|
+
```
|
262
|
+
ruby bin/linuxstat.rb cpu
|
263
|
+
```
|
264
|
+
|
265
|
+
That is, the argument passed is not case-sensitive.
|
266
|
+
But if the method passed isn't available and outright wrong, it will run all the module methods. For example, you can't do:
|
267
|
+
|
268
|
+
```
|
269
|
+
ruby bin/linuxstat.rb upc
|
270
|
+
```
|
271
|
+
|
272
|
+
You can run `rake` to test all the modules.
|
273
|
+
You can also use `rake cpu` to test the CPU module or `rake memory` to test the memory module. But this is case-sensitive.
|
274
|
+
|
275
|
+
This is not a valid module and can't be run.
|
276
|
+
|
236
277
|
## Contributing
|
237
278
|
|
238
279
|
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).
|
data/bin/console
CHANGED
@@ -1,14 +1,4 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
|
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"
|
2
|
+
$-v = true
|
3
|
+
%w(bundler/setup linux_stat irb).each(&method(:require))
|
14
4
|
IRB.start(__FILE__)
|
@@ -1,6 +1,10 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'linux_stat'
|
2
|
+
begin
|
3
|
+
require 'linux_stat'
|
4
|
+
rescue LoadError
|
5
|
+
require 'bundler/setup'
|
6
|
+
require 'linux_stat'
|
7
|
+
end
|
4
8
|
|
5
9
|
$-v = true
|
6
10
|
|
@@ -8,7 +12,18 @@ $-v = true
|
|
8
12
|
MARKDOWN = ARGV.any? { |x| x[/^\-\-markdown$/] || x[/^\-md$/] }
|
9
13
|
PRINT_TIME = MARKDOWN ? false : !ARGV.any? { |x| x[/^\-\-no-time$/] || x[/^\-nt$/] }
|
10
14
|
|
11
|
-
|
15
|
+
%w(--markdown -md --no-time -nt).each(&ARGV.method(:delete))
|
16
|
+
|
17
|
+
# Run only desired classes / modules
|
18
|
+
constants = LinuxStat.constants
|
19
|
+
|
20
|
+
execute = constants.map(&:downcase).map.with_index { |x, i|
|
21
|
+
constants[i] if ARGV.find { |y| y.downcase.to_sym == x }
|
22
|
+
}.compact
|
23
|
+
|
24
|
+
execute.replace(constants) if execute.empty?
|
25
|
+
|
26
|
+
execute.sort.each do |c|
|
12
27
|
e = eval("LinuxStat::#{c}")
|
13
28
|
|
14
29
|
next if e.class != Module && e.class != Class
|
@@ -25,9 +40,10 @@ LinuxStat.constants.sort.each do |c|
|
|
25
40
|
|
26
41
|
meths.each do |meth|
|
27
42
|
time = Time.now
|
28
|
-
v = e.send(meth)
|
29
|
-
time = Time.now.-(time).*(
|
43
|
+
v = e.send(meth)
|
44
|
+
time = Time.now.-(time).*(1_000_000).round(3)
|
30
45
|
|
46
|
+
v = v.inspect
|
31
47
|
dis = v.length > 253 ? v[0..250].strip + '...'.freeze : v
|
32
48
|
|
33
49
|
if MARKDOWN
|
@@ -37,13 +53,13 @@ LinuxStat.constants.sort.each do |c|
|
|
37
53
|
end
|
38
54
|
|
39
55
|
puts( "(" +
|
40
|
-
if time >
|
41
|
-
"\e[1;38;2;255;
|
42
|
-
elsif time >
|
56
|
+
if time > 10_000
|
57
|
+
"\e[1;38;2;255;50;50m"
|
58
|
+
elsif time > 5_000
|
43
59
|
"\e[1;38;2;255;170;0m"
|
44
60
|
else
|
45
61
|
"\e[1;38;2;0;170;0m"
|
46
|
-
end + "Time taken: #{time}
|
62
|
+
end + "Time taken: #{time}\u03BCs\e[0m)"
|
47
63
|
) if PRINT_TIME
|
48
64
|
|
49
65
|
puts
|
data/lib/linux_stat.rb
CHANGED
data/lib/linux_stat/battery.rb
CHANGED
@@ -3,10 +3,13 @@ module LinuxStat
|
|
3
3
|
PATH = "/sys/class/power_supply/BAT0"
|
4
4
|
|
5
5
|
class << self
|
6
|
+
# Returns true or false based on the presence of the battery.
|
6
7
|
def present?
|
7
8
|
@@present ||= Dir.exist?(PATH)
|
8
9
|
end
|
9
10
|
|
11
|
+
# Returns the details of the battery.
|
12
|
+
#If the battery is not present it will return an empty Hash.
|
10
13
|
def stat
|
11
14
|
st = status.downcase
|
12
15
|
return {} unless present?
|
@@ -23,40 +26,60 @@ module LinuxStat
|
|
23
26
|
}
|
24
27
|
end
|
25
28
|
|
29
|
+
# Returns the model of the battery.
|
30
|
+
#If the battery is not present or the information isn't available it will return an empty String.
|
26
31
|
def model
|
27
32
|
return ''.freeze unless model_readable?
|
28
33
|
IO.read(File.join(PATH, 'model_name')).tap(&:strip!)
|
29
34
|
end
|
30
35
|
|
36
|
+
# Returns the manufacturer of the battery.
|
37
|
+
# If the battery is not present or the information is not available, it will return an empty String.
|
31
38
|
def manufacturer
|
32
39
|
return ''.freeze unless manufacturer_readable?
|
33
40
|
IO.read(File.join(PATH, 'manufacturer')).tap(&:strip!)
|
34
41
|
end
|
35
42
|
|
43
|
+
# Returns the technology of the battery.
|
44
|
+
# If the battery is not present or the information is not available, it will return an empty String.
|
36
45
|
def technology
|
37
46
|
return ''.freeze unless tech_readable?
|
38
47
|
IO.read(File.join(PATH, 'technology')).tap(&:strip!)
|
39
48
|
end
|
40
49
|
|
50
|
+
# Returns the status of the battery.
|
51
|
+
# If the battery is not present or the information is not available, it will return an empty String.
|
52
|
+
# The status generally includes either of the full, charging, discharging and unknown states in most cases.
|
41
53
|
def status
|
42
54
|
return ''.freeze unless status_readable?
|
43
55
|
IO.read(File.join(PATH, 'status')).tap(&:strip!)
|
44
56
|
end
|
45
57
|
|
58
|
+
# Returns true if the battery is charging, false if the battery is not charging.
|
59
|
+
# If the battery is not present or the information is not available, it will return nil.
|
46
60
|
def charging?
|
61
|
+
return nil if status.empty?
|
47
62
|
%w(full charging unknown).each(&:freeze).include?(status.downcase)
|
48
63
|
end
|
49
64
|
|
65
|
+
# Returns true if the battery is discharging, false if the battery is not discharging.
|
66
|
+
# If the battery is not present or the information is not available, it will return nil.
|
50
67
|
def discharging?
|
68
|
+
return nil if status.empty?
|
51
69
|
status.downcase == 'discharging'
|
52
70
|
end
|
53
71
|
|
72
|
+
# Returns true if the battery status if full, false if the battery status is not full.
|
73
|
+
# If the battery is not present or the information is not available, it will return nil.
|
54
74
|
def full?
|
75
|
+
return nil if status.empty?
|
55
76
|
status.downcase == 'full'
|
56
77
|
end
|
57
78
|
|
79
|
+
# Returns the charge of the battery.
|
80
|
+
# If the battery is not present or the information is not available, it will return nil.
|
58
81
|
def charge
|
59
|
-
return
|
82
|
+
return nil unless charge_now_readable?
|
60
83
|
charge_now = IO.read(File.join(PATH, 'charge_now')).to_i
|
61
84
|
charge_full = IO.read(File.join(PATH, 'charge_full')).to_i
|
62
85
|
|
data/lib/linux_stat/bios.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
module LinuxStat
|
2
2
|
module BIOS
|
3
3
|
class << self
|
4
|
+
# Returns the model of the BIOS.
|
5
|
+
# If the information is not available it will return a frozen empty string.
|
6
|
+
# The output is also cached ; as changing the value in runtime is unexpected.
|
4
7
|
def model
|
5
8
|
# Cached ; as changing the value in runtime is unexpected
|
6
9
|
@@model ||= if File.readable?('/sys/devices/virtual/dmi/id/product_name')
|
@@ -12,6 +15,9 @@ module LinuxStat
|
|
12
15
|
end
|
13
16
|
end
|
14
17
|
|
18
|
+
# Returns the vendor of the BIOS.
|
19
|
+
# If the information is not available it will return a frozen empty string.
|
20
|
+
# The output is also cached ; as changing the value in runtime is unexpected.
|
15
21
|
def vendor
|
16
22
|
# Cached ; as changing the value in runtime is unexpected
|
17
23
|
@@vendor ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_vendor')
|
@@ -21,8 +27,10 @@ module LinuxStat
|
|
21
27
|
end
|
22
28
|
end
|
23
29
|
|
30
|
+
# Returns the version of the BIOS.
|
31
|
+
# If the information is not available it will return a frozen empty string.
|
32
|
+
# The output is also cached ; as changing the value in runtime is unexpected.
|
24
33
|
def version
|
25
|
-
# Cached ; as changing the value in runtime is unexpected
|
26
34
|
@@version ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_version')
|
27
35
|
IO.read('/sys/devices/virtual/dmi/id/bios_version').tap(&:strip!)
|
28
36
|
else
|
@@ -30,8 +38,10 @@ module LinuxStat
|
|
30
38
|
end
|
31
39
|
end
|
32
40
|
|
41
|
+
# Returns the date of the BIOS.
|
42
|
+
# If the information is not available it will return a frozen empty string.
|
43
|
+
# The output is also cached ; as changing the value in runtime is unexpected.
|
33
44
|
def date
|
34
|
-
# Cached ; as changing the value in runtime is unexpected
|
35
45
|
@@date ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_date')
|
36
46
|
IO.read('/sys/devices/virtual/dmi/id/bios_date').tap(&:strip!)
|
37
47
|
else
|
data/lib/linux_stat/cpu.rb
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
module LinuxStat
|
2
2
|
module CPU
|
3
3
|
class << self
|
4
|
+
# Returns the cpu usage of all threads.
|
5
|
+
#
|
6
|
+
# The first one is aggregated CPU usage reported by the Linux kernel.
|
7
|
+
# And the consecutive ones are the real core usages.
|
8
|
+
#
|
9
|
+
# On a system with 4 threads, the output will be like::
|
10
|
+
# {0=>84.38, 1=>100.0, 2=>50.0, 3=>87.5, 4=>87.5}
|
11
|
+
#
|
12
|
+
# If the information is not available, it will return an empty Hash
|
4
13
|
def stat(sleep = 0.075)
|
5
14
|
return {} unless stat?
|
6
15
|
|
@@ -21,8 +30,12 @@ module LinuxStat
|
|
21
30
|
end
|
22
31
|
end
|
23
32
|
|
33
|
+
# Returns the total cpu usage.
|
34
|
+
# It's like running LinuxStat::CPU.stat[0] but it's much more efficient and calculates just the aggregated usage which is available at the top of the /proc/stat file.
|
35
|
+
#
|
36
|
+
# If the information is not available, it will return nil.
|
24
37
|
def total_usage(sleep = 0.075)
|
25
|
-
return
|
38
|
+
return nil unless stat?
|
26
39
|
|
27
40
|
data = IO.foreach('/proc/stat').first.split.tap(&:shift).map!(&:to_f)
|
28
41
|
sleep(sleep)
|
@@ -36,31 +49,30 @@ module LinuxStat
|
|
36
49
|
totald.-(idle_now - idle_then).fdiv(totald).*(100).round(2).abs
|
37
50
|
end
|
38
51
|
|
52
|
+
# Returns the total number of CPU threads.
|
53
|
+
# If the information isn't available, it will return 0.
|
39
54
|
def count
|
40
55
|
# CPU count can change during the program runtime
|
41
56
|
cpuinfo.count { |x| x.start_with?('processor') }
|
42
57
|
end
|
43
58
|
|
59
|
+
# Returns the model of processor.
|
60
|
+
# If the information isn't available, it will return en empty string.
|
61
|
+
# The output is also cached ; as changing the value in runtime is unexpected.
|
44
62
|
def model
|
45
|
-
# Cached ; as changing the value in runtime is unexpected ; nobody is going
|
46
|
-
# to add/remove CPUs during program runtime
|
47
63
|
@@name ||= cpuinfo.find { |x| x.start_with?('model name') }.to_s.split(?:)[-1].to_s.strip
|
48
64
|
end
|
49
65
|
|
50
|
-
# Returns an array with current core frequencies corresponding to the usages
|
66
|
+
# Returns an array with current core frequencies corresponding to the usages.
|
67
|
+
# If the information isn't available, it will return an empty array.
|
51
68
|
def cur_freq
|
52
|
-
# Cached ; as changing the value in runtime is unexpected ; nobody is going
|
53
|
-
# to add/remove CPUs during program runtime
|
54
|
-
|
55
69
|
@@cpu_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_cur_freq"]
|
56
70
|
@@cpu_freqs.map { |x| IO.read(x).to_i }
|
57
71
|
end
|
58
72
|
|
59
|
-
# Returns an array with max core frequencies corresponding to the usages
|
73
|
+
# Returns an array with max core frequencies corresponding to the usages.
|
74
|
+
# If the information isn't available, it will return an empty array.
|
60
75
|
def max_freq
|
61
|
-
# Cached ; as changing the value in runtime is unexpected ; nobody is going
|
62
|
-
# to add/remove CPUs during program runtime
|
63
|
-
|
64
76
|
@@max_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_max_freq"]
|
65
77
|
@@max_freqs.map { |x| IO.read(x).to_i }
|
66
78
|
end
|
data/lib/linux_stat/kernel.rb
CHANGED
@@ -1,24 +1,33 @@
|
|
1
1
|
module LinuxStat
|
2
2
|
module Kernel
|
3
3
|
class << self
|
4
|
+
# Returns the Linux Kernel version.
|
5
|
+
# If the information isn't available, it will return a frozen empty string.
|
6
|
+
# The output is also cached ; as changing the value in runtime is unexpected.
|
4
7
|
def version
|
5
8
|
return ''.freeze if string.empty?
|
6
9
|
@@version ||= splitted[2]
|
7
10
|
end
|
8
11
|
|
12
|
+
# Returns the name of the user who built the kernel using KBUILD_FLAGS.
|
13
|
+
# If the information isn't available, it will return a frozen empty string.
|
14
|
+
# The output is also cached ; as changing the value in runtime is unexpected.
|
9
15
|
def build_user
|
10
16
|
@@build_user ||= string.split(/(\(.+\))/).each(&:strip!)
|
11
17
|
.reject(&:empty?).find { |x| x[/^\(.+\)$/] }.to_s
|
12
|
-
.split[0].to_s[1..-2].to_s
|
18
|
+
.split[0].to_s[1..-2].to_s.freeze
|
13
19
|
end
|
14
20
|
|
21
|
+
# Returns the compiler used to compile the Linux Kernel.
|
22
|
+
# If the information isn't available, it will return a frozen empty string.
|
23
|
+
# The output is also cached ; as changing the value in runtime is unexpected.
|
15
24
|
def compiler
|
16
25
|
return ''.freeze if string.empty?
|
17
26
|
|
18
27
|
@@compiler ||= string.split(/(\(.+\))/).each(&:strip!)
|
19
28
|
.reject(&:empty?)
|
20
29
|
.find { |x| x[/^\(.+\)$/] }.to_s
|
21
|
-
.split.find { |x| !x[/^(.+@.+)$/] }.to_s[/\w+/].to_s
|
30
|
+
.split.find { |x| !x[/^(.+@.+)$/] }.to_s[/\w+/].to_s.freeze
|
22
31
|
|
23
32
|
@@compiler_val ||= case @@compiler
|
24
33
|
when /gcc/i then [:gcc ]
|
@@ -28,32 +37,106 @@ module LinuxStat
|
|
28
37
|
end << compiler_version
|
29
38
|
end
|
30
39
|
|
40
|
+
# Returns the compiler version used to compile the Linux Kernel.
|
41
|
+
# If the information isn't available, it will return a frozen empty string.
|
42
|
+
# The output is also cached ; as changing the value in runtime is unexpected.
|
31
43
|
def compiler_version
|
32
44
|
@@compiler_version ||= string.split(/(\(.+?\))/).each(&:strip!)
|
33
45
|
.reject(&:empty?)[2..4].to_a
|
34
|
-
.find { |x| x[/[\d.]+/] }.to_s[/[\d.]+/].to_s
|
46
|
+
.find { |x| x[/[\d.]+/] }.to_s[/[\d.]+/].to_s.freeze
|
35
47
|
end
|
36
48
|
|
49
|
+
# Returns the time when the kernel was compiled.
|
50
|
+
# The return value is a Time object.
|
51
|
+
# If the information isn't available, it will return nil
|
52
|
+
#
|
53
|
+
# The time will be searched in specific order.
|
54
|
+
# It will match any date matching any of these formats:
|
55
|
+
# 1. %b %d %H:%M:%S %z %Y
|
56
|
+
# 2. %d %b %Y %H:%M:%S %z
|
57
|
+
# 3. %Y-%m-%d
|
58
|
+
# Most kernels have date in them in this format.
|
59
|
+
#
|
60
|
+
# Do note that Ruby sometimes fails to work with timezones like BST for example.
|
61
|
+
# In such case, the timezone is unrealiable and often returns the local timezone.
|
62
|
+
# You have to use regexp yourself to get the proper zone.
|
63
|
+
# Use LinuxStat::Kernel.build_date_string to get the original string if you need that.
|
64
|
+
#
|
65
|
+
# The output is also cached ; as changing the value in runtime is unexpected.
|
37
66
|
def build_date
|
38
|
-
return
|
67
|
+
return nil if splitted.empty?
|
39
68
|
|
40
69
|
@@time ||= begin
|
41
|
-
require 'time'
|
70
|
+
require 'time' unless Time.respond_to?(:strptime)
|
42
71
|
|
43
|
-
|
44
|
-
x.each(&:strip!)
|
45
|
-
Time.strptime(x.join(?\s.freeze), '%d %b %Y %H:%M:%S %z'.freeze) rescue nil
|
46
|
-
end
|
72
|
+
splitted.each_cons(5).map do |x|
|
73
|
+
joined = x.each(&:strip!).join(?\s.freeze)
|
47
74
|
|
48
|
-
|
75
|
+
# Match 21 Oct 2020 01:11:20 +0000
|
76
|
+
if joined[/^\d{2}\s\w{3}\s\d{4}\s\d{2}:\d{2}:\d{2}\s\+\d*$/]
|
77
|
+
Time.strptime(joined, '%d %b %Y %H:%M:%S %Z') rescue nil
|
78
|
+
|
79
|
+
# Match Aug 25 17:23:54 UTC 2020
|
80
|
+
elsif joined[/^\w{3}\s\d{2}\s\d{2}:\d{2}:\d{2}\s\w+\s\d*$/]
|
81
|
+
Time.strptime(joined, '%b %d %H:%M:%S %z %Y') rescue nil
|
82
|
+
|
83
|
+
# Match 2017-09-19
|
84
|
+
elsif joined[/\d{4}-\d{2}-\d{2}/]
|
85
|
+
Time.strptime(joined[/\d{4}-\d{2}-\d{2}/] + " +00:00", '%Y-%m-%d %z') rescue nil
|
86
|
+
|
87
|
+
else
|
88
|
+
nil
|
89
|
+
end
|
90
|
+
end.tap(&:compact!)[0]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Returns the time when the kernel was compiled.
|
95
|
+
# The return value is a String.
|
96
|
+
# If the information isn't available, it will return nil
|
97
|
+
#
|
98
|
+
# The time will be searched in specific order.
|
99
|
+
# It will match any date matching any of these formats:
|
100
|
+
# 1. %b %d %H:%M:%S %z %Y
|
101
|
+
# 2. %d %b %Y %H:%M:%S %z
|
102
|
+
# 3. %Y-%m-%d
|
103
|
+
# Most kernels have date in them in this format.
|
104
|
+
#
|
105
|
+
# Do note that Ruby sometimes fails to work with timezones like BST for example.
|
106
|
+
# In such case, the timezone is unrealiable and often returns the local timezone.
|
107
|
+
# You have to use regexp yourself to get the proper zone.
|
108
|
+
# Use LinuxStat::Kernel.build_date_string to get the original string if you need that.
|
109
|
+
#
|
110
|
+
# The output is also cached ; as changing the value in runtime is unexpected.
|
111
|
+
def build_date_string
|
112
|
+
return nil if splitted.empty?
|
113
|
+
|
114
|
+
@@time2 ||= begin
|
115
|
+
require 'time' unless Time.respond_to?(:strptime)
|
116
|
+
|
117
|
+
splitted.each_cons(5).map do |x|
|
118
|
+
joined = x.each(&:strip!).join(?\s.freeze)
|
119
|
+
|
120
|
+
# Match 21 Oct 2020 01:11:20 +0000
|
121
|
+
if (joined[/^\d{2}\s\w{3}\s\d{4}\s\d{2}:\d{2}:\d{2}\s\+\d*$/] && (Time.strptime(joined, '%d %b %Y %H:%M:%S %Z') rescue nil)) ||
|
122
|
+
|
123
|
+
# Match Aug 25 17:23:54 UTC 2020
|
124
|
+
(joined[/^\w{3}\s\d{2}\s\d{2}:\d{2}:\d{2}\s\w+\s\d*$/] && (Time.strptime(joined, '%b %d %H:%M:%S %z %Y') rescue nil)) ||
|
125
|
+
|
126
|
+
# Match 2017-09-19
|
127
|
+
(joined[/\d{4}-\d{2}-\d{2}/] && (Time.strptime(joined[/\d{4}-\d{2}-\d{2}/] + " +00:00", '%Y-%m-%d %z') rescue nil))
|
128
|
+
joined
|
129
|
+
else
|
130
|
+
nil
|
131
|
+
end
|
132
|
+
end.tap(&:compact!)[0]
|
49
133
|
end
|
50
134
|
end
|
51
135
|
|
136
|
+
# Reads maximum 1024 bytes from /proc/version and returns the string.
|
137
|
+
# The output is also cached ; as changing the value in runtime is unexpected.
|
52
138
|
def string
|
53
|
-
|
54
|
-
# Hotfix update can be problem, but it's rare and might not
|
55
|
-
# affect the version string during program runtime.
|
56
|
-
@@string ||= File.readable?('/proc/version') ? IO.read('/proc/version', 1000).tap(&:strip!) : ''
|
139
|
+
@@string ||= File.readable?('/proc/version') ? IO.read('/proc/version', 1024).tap(&:strip!) : ''
|
57
140
|
end
|
58
141
|
|
59
142
|
private
|
data/lib/linux_stat/memory.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
module LinuxStat
|
2
2
|
module Memory
|
3
3
|
class << self
|
4
|
+
# Returns the memory details reported by /proc/meminfo. In this format:
|
5
|
+
# {:total=>3836264, :used=>3097952, :available=>738312, :percent_used=>80.75, :percent_available=>19.25}
|
6
|
+
#
|
7
|
+
# The value is in Kilobyte.
|
8
|
+
# If the statistics is not available, it will return an empty Hash.
|
4
9
|
def stat
|
5
10
|
return {} unless meminfo?
|
6
11
|
|
@@ -24,31 +29,44 @@ module LinuxStat
|
|
24
29
|
}
|
25
30
|
end
|
26
31
|
|
32
|
+
# Returns the total memory details reported by /proc/meminfo.
|
33
|
+
# The value is in Kilobyte.
|
34
|
+
# It retuns an Integer but if the info is not available, it will return nil.
|
27
35
|
def total
|
28
|
-
return
|
36
|
+
return nil unless meminfo?
|
29
37
|
IO.foreach('/proc/meminfo').first.split[1].to_i
|
30
38
|
end
|
31
39
|
|
40
|
+
# Returns the total memory details reported by /proc/meminfo.
|
41
|
+
# The value is in Kilobyte.
|
42
|
+
# It retuns an Integer but if the info is not available, it will return nil
|
32
43
|
def available
|
33
|
-
return
|
44
|
+
return nil unless meminfo?
|
34
45
|
IO.foreach('/proc/meminfo').first(3)[-1].split[1].to_i
|
35
46
|
end
|
36
47
|
|
48
|
+
# Returns the amount of memory used reported by /proc/meminfo.
|
49
|
+
# The value is in Kilobyte.
|
50
|
+
# It retuns an Integer but if the info is not available, it will return nil.
|
37
51
|
def used
|
38
|
-
return
|
52
|
+
return nil unless meminfo?
|
39
53
|
memory = IO.foreach('/proc/meminfo').first(3)
|
40
54
|
memory[0].split[1].to_i - memory[2].split[1].to_i
|
41
55
|
end
|
42
56
|
|
57
|
+
# Returns the percentage of memory used reported by /proc/meminfo.
|
58
|
+
# It retuns an Integer but if the info is not available, it will return nil
|
43
59
|
def percent_used
|
44
|
-
return
|
60
|
+
return nil unless meminfo?
|
45
61
|
memory = IO.foreach('/proc/meminfo').first(3)
|
46
62
|
total = memory[0].split[1].to_i
|
47
63
|
total.-(memory[2].split[1].to_i).*(100).fdiv(total).round(2)
|
48
64
|
end
|
49
65
|
|
66
|
+
# Returns the percentage of memory used reported by /proc/meminfo.
|
67
|
+
# It retuns an Integer but if the info is not available, it will return nil
|
50
68
|
def percent_available
|
51
|
-
return
|
69
|
+
return nil unless meminfo?
|
52
70
|
memory = IO.foreach('/proc/meminfo').first(3)
|
53
71
|
memory[2].split[1].to_i.*(100).fdiv(memory[0].split[1].to_i).round(2)
|
54
72
|
end
|
data/lib/linux_stat/net.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
module LinuxStat
|
2
2
|
module Net
|
3
3
|
class << self
|
4
|
+
# Returns the local IP address of the system as a String.
|
5
|
+
# If the information isn't available, it will a frozen empty string.
|
4
6
|
def ipv4_private
|
5
7
|
require 'socket'
|
6
8
|
ip = Socket.ip_address_list.find(&:ipv4_private?)
|
7
|
-
ip ? ip.ip? ? ip.ip_unpack[0].freeze : '' : ''
|
9
|
+
ip ? ip.ip? ? ip.ip_unpack[0].freeze : ''.freeze : ''.freeze
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
data/lib/linux_stat/os.rb
CHANGED
@@ -1,16 +1,38 @@
|
|
1
1
|
module LinuxStat
|
2
2
|
module OS
|
3
3
|
class << self
|
4
|
+
# Reads /etc/os-release and returns a Hash. For example:
|
5
|
+
# {: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/", :BUG_REPORT_URL=>"https://bugs.archlinux.org/", :LOGO=>"archlinux"}
|
6
|
+
#
|
7
|
+
# If the info isn't available, it will return an empty Hash.
|
8
|
+
#
|
9
|
+
# The amount of data read is 4096 bytes. Any more than that will result in truncated output.
|
10
|
+
#
|
11
|
+
# The information is also cached, and once loaded, won't change in runtime. Because changing the /etc/lsb-release
|
12
|
+
# isn't expected in runtime.
|
4
13
|
def os_release
|
5
14
|
# Cached ; as changing the value in runtime is unexpected
|
6
15
|
@@os_release ||= File.readable?('/etc/os-release') ? release('/etc/os-release') : {}
|
7
16
|
end
|
8
17
|
|
18
|
+
# Reads /etc/lsb-release and returns a Hash. For example:
|
19
|
+
# {:LSB_VERSION=>"1.4", :DISTRIB_ID=>"Arch", :DISTRIB_RELEASE=>"rolling", :DISTRIB_DESCRIPTION=>"Arch Linux"}
|
20
|
+
#
|
21
|
+
# If the info isn't available, it will return an empty Hash.
|
22
|
+
#
|
23
|
+
# The amount of data read is 4096 bytes. Any more than that will result in truncated output.
|
24
|
+
#
|
25
|
+
# The information is also cached, and once loaded, won't change in runtime. Because changing the /etc/lsb-release
|
26
|
+
# isn't expected in runtime.
|
9
27
|
def lsb_release
|
10
28
|
# Cached ; as changing the value in runtime is unexpected
|
11
29
|
@@lsb_release ||= File.readable?('/etc/lsb-release') ? release('/etc/lsb-release') : {}
|
12
30
|
end
|
13
31
|
|
32
|
+
# Reads /etc/lsb-release or /etc/os-release tries to get information about the distribution.
|
33
|
+
# If the information isn't available, it will read and return /etc/issue.
|
34
|
+
# The return type is String.
|
35
|
+
# If none of the info is available, it will return an empty frozen String.
|
14
36
|
def distribution
|
15
37
|
@@distribution ||= if os_release.key?(:NAME)
|
16
38
|
os_release[:NAME]
|
@@ -29,6 +51,9 @@ module LinuxStat
|
|
29
51
|
end
|
30
52
|
end
|
31
53
|
|
54
|
+
# Reads /etc/hostname and returns the hostname.
|
55
|
+
# The return type is String.
|
56
|
+
# If the info info isn't available, it will return 'localhost'.
|
32
57
|
def hostname
|
33
58
|
@@hostname ||= if File.exist?('/etc/hostname')
|
34
59
|
IO.read('/etc/hostname').strip
|
@@ -37,6 +62,8 @@ module LinuxStat
|
|
37
62
|
end
|
38
63
|
end
|
39
64
|
|
65
|
+
# Reads ruby configuration and tries to guess if the system is 32 bit or 64 bit.
|
66
|
+
# The return type is Integer.
|
40
67
|
def bits
|
41
68
|
@@bits ||= if RbConfig::CONFIG['host_cpu'].end_with?('64') || RUBY_PLATFORM[/x86_64/]
|
42
69
|
64
|
@@ -45,8 +72,12 @@ module LinuxStat
|
|
45
72
|
end
|
46
73
|
end
|
47
74
|
|
75
|
+
# Reads /proc/uptime and returns the system uptime:
|
76
|
+
# {:hour=>10, :minute=>34, :second=>12.59}
|
77
|
+
#
|
78
|
+
# If the stat isn't available, an empty hash is returned.
|
48
79
|
def uptime
|
49
|
-
|
80
|
+
return {} unless uptime_readable?
|
50
81
|
|
51
82
|
uptime = IO.read('/proc/uptime').to_f
|
52
83
|
uptime_i = uptime.to_i
|
@@ -64,7 +95,7 @@ module LinuxStat
|
|
64
95
|
|
65
96
|
private
|
66
97
|
def release(file)
|
67
|
-
IO.readlines(file,
|
98
|
+
IO.readlines(file, 4096).reduce({}) { |h, x|
|
68
99
|
x.strip!
|
69
100
|
next h if x.empty?
|
70
101
|
|
data/lib/linux_stat/process.rb
CHANGED
@@ -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
|
data/lib/linux_stat/swap.rb
CHANGED
@@ -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
|
-
#
|
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)
|
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
|
data/lib/linux_stat/version.rb
CHANGED
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.
|
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-
|
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,8 +34,6 @@ 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
37
|
homepage: https://github.com/Souravgoswami/linux_stat/
|
45
38
|
licenses:
|
46
39
|
- MIT
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.travis.yml
DELETED
data/CODE_OF_CONDUCT.md
DELETED
@@ -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/
|
data/Gemfile.lock
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
linux_stat (0.1.4)
|
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
|
data/LICENSE.txt
DELETED
@@ -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
data/linux_stat.gemspec
DELETED
@@ -1,28 +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 = "https://github.com/Souravgoswami/linux_stat/"
|
12
|
-
spec.license = "MIT"
|
13
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
|
14
|
-
|
15
|
-
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
-
spec.metadata["source_code_uri"] = "https://github.com/Souravgoswami/linux_stat"
|
17
|
-
spec.metadata["changelog_uri"] = "https://github.com/Souravgoswami/linux_stat/commits/master"
|
18
|
-
|
19
|
-
# Specify which files should be added to the gem when it is released.
|
20
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
-
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|logo)/}) }
|
23
|
-
end
|
24
|
-
|
25
|
-
spec.bindir = "exe"
|
26
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
-
spec.require_paths = ["lib"]
|
28
|
-
end
|