linux_stat 0.2.1 → 0.3.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.
- checksums.yaml +4 -4
- data/README.md +118 -43
- data/bin/linuxstat.rb +2 -1
- data/ext/fs_stat/extconf.rb +4 -0
- data/ext/fs_stat/fs_stat.c +28 -0
- data/lib/linux_stat.rb +3 -4
- data/lib/linux_stat/filesystem.rb +97 -0
- data/lib/linux_stat/mounts.rb +64 -0
- data/lib/linux_stat/version.rb +1 -1
- metadata +9 -5
- data/Gemfile +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e2c68de7353a60dbdff528a65e721cc0f7632e95c5d8db2e0e1dc1b7242deb1
|
4
|
+
data.tar.gz: ada661cb896983bddf8f831b55822bfb006f6167c1ea2bbbd2319bf38028bd79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1399f3b227442243558ae76d4cdbc4679f21984b8d7e2aacd7ed25c471d5588e9901877c943d4a2220583f3eb4094b00e81016e6e573ef1038052574e8306fd0
|
7
|
+
data.tar.gz: 79c25491f4517ef2370b375d19177ca01b6589c6e0931e40dc0aa7c92269b605c97cab4cfe42cde51895ebe9b21d418fd301daf44dfdc496ff0b8bd24788dac6
|
data/README.md
CHANGED
@@ -1,10 +1,31 @@
|
|
1
1
|
# LinuxStat
|
2
2
|

|
3
3
|
|
4
|
+
For reading the HTML version, visit [linux_stat](https://souravgoswami.github.io/linux_stat/).
|
5
|
+
|
4
6
|
LinuxStat lets you read status 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
7
|
|
6
8
|
It only works for Linux, and detecting the OS is upto the user of this gem.
|
7
9
|
|
10
|
+
## Dependencies:
|
11
|
+
+ You need to have the C compile to be able to compile the C extension.
|
12
|
+
On Arch Linux:
|
13
|
+
```
|
14
|
+
# pacman -S gcc
|
15
|
+
```
|
16
|
+
|
17
|
+
On Debian based systems:
|
18
|
+
```
|
19
|
+
# apt install gcc
|
20
|
+
```
|
21
|
+
|
22
|
+
+ You might also require ruby-dev in Debian based systems which provides support for ruby.h header file:
|
23
|
+
```
|
24
|
+
# apt install ruby-dev
|
25
|
+
```
|
26
|
+
|
27
|
+
+ Once your are done, and you can compile the C file, you can follow the installation!
|
28
|
+
|
8
29
|
## Installation
|
9
30
|
|
10
31
|
Add this line to your application's Gemfile:
|
@@ -23,6 +44,7 @@ Or install it yourself as:
|
|
23
44
|
|
24
45
|
## Usage
|
25
46
|
### LinuxStat::BIOS
|
47
|
+
### LinuxStat::BIOS
|
26
48
|
```
|
27
49
|
LinuxStat::BIOS.date
|
28
50
|
=> "04/10/2017"
|
@@ -78,7 +100,7 @@ LinuxStat::CPU.count
|
|
78
100
|
=> 4
|
79
101
|
|
80
102
|
LinuxStat::CPU.cur_freq
|
81
|
-
=> [
|
103
|
+
=> [1998722, 1998401, 1974377, 1875264]
|
82
104
|
|
83
105
|
LinuxStat::CPU.max_freq
|
84
106
|
=> [2000000, 2000000, 2000000, 2000000]
|
@@ -87,16 +109,38 @@ LinuxStat::CPU.model
|
|
87
109
|
=> "Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz"
|
88
110
|
|
89
111
|
LinuxStat::CPU.stat
|
90
|
-
=> {0=>
|
112
|
+
=> {0=>6.45, 1=>0.0, 2=>0.0, 3=>12.5, 4=>0.0}
|
91
113
|
|
92
114
|
LinuxStat::CPU.total_usage
|
93
|
-
=>
|
115
|
+
=> 3.45
|
94
116
|
|
95
117
|
LinuxStat::CPU.usage
|
96
|
-
=>
|
118
|
+
=> 6.45
|
97
119
|
|
98
120
|
LinuxStat::CPU.usages
|
99
|
-
=> {0=>
|
121
|
+
=> {0=>9.68, 1=>11.11, 2=>12.5, 3=>14.29, 4=>12.5}
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
### LinuxStat::Filesystem
|
126
|
+
```
|
127
|
+
LinuxStat::Filesystem.available
|
128
|
+
=> 43155402752
|
129
|
+
|
130
|
+
LinuxStat::Filesystem.free
|
131
|
+
=> 43155402752
|
132
|
+
|
133
|
+
LinuxStat::Filesystem.stat
|
134
|
+
=> {:total=>119981191168, :free=>43155402752, :used=>76825788416}
|
135
|
+
|
136
|
+
LinuxStat::Filesystem.stat_raw
|
137
|
+
=> {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>10535987, :block_avail_unpriv=>10535987, :inodes=>58612160, :free_inodes=>56718515, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
|
138
|
+
|
139
|
+
LinuxStat::Filesystem.total
|
140
|
+
=> 119981191168
|
141
|
+
|
142
|
+
LinuxStat::Filesystem.used
|
143
|
+
=> 76825788416
|
100
144
|
|
101
145
|
```
|
102
146
|
|
@@ -128,22 +172,41 @@ LinuxStat::Kernel.version
|
|
128
172
|
### LinuxStat::Memory
|
129
173
|
```
|
130
174
|
LinuxStat::Memory.available
|
131
|
-
=>
|
175
|
+
=> 403724
|
132
176
|
|
133
177
|
LinuxStat::Memory.percent_available
|
134
|
-
=> 10.
|
178
|
+
=> 10.52
|
135
179
|
|
136
180
|
LinuxStat::Memory.percent_used
|
137
|
-
=> 89.
|
181
|
+
=> 89.48
|
138
182
|
|
139
183
|
LinuxStat::Memory.stat
|
140
|
-
=> {:total=>3836264, :used=>
|
184
|
+
=> {:total=>3836264, :used=>3432540, :available=>403724, :percent_used=>89.48, :percent_available=>10.52}
|
141
185
|
|
142
186
|
LinuxStat::Memory.total
|
143
187
|
=> 3836264
|
144
188
|
|
145
189
|
LinuxStat::Memory.used
|
146
|
-
=>
|
190
|
+
=> 3432540
|
191
|
+
|
192
|
+
```
|
193
|
+
|
194
|
+
### LinuxStat::Mounts
|
195
|
+
```
|
196
|
+
LinuxStat::Mounts.list
|
197
|
+
=> ["proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0", "sys /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0", "dev /dev devtmpfs rw,nosuid,relatime,size=1891796k,nr_inodes=472949,mode=755 0 0", "run /run tmpfs rw,nosuid,nodev,relatime,mode=755 0 0", "...
|
198
|
+
|
199
|
+
LinuxStat::Mounts.root
|
200
|
+
=> "/dev/sda2"
|
201
|
+
|
202
|
+
LinuxStat::Mounts.root_fs
|
203
|
+
=> "xfs"
|
204
|
+
|
205
|
+
LinuxStat::Mounts.root_mount_options
|
206
|
+
=> "rw,noatime,attr2,inode64,logbufs=8,logbsize=32k,noquota"
|
207
|
+
|
208
|
+
LinuxStat::Mounts.tmpfs
|
209
|
+
=> {"/dev/shm"=>"tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0", "/sys/fs/cgroup"=>"tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,size=4096k,nr_inodes=1024,mode=755 0 0", "/ramdisk"=>"tmpfs /ramdisk tmpfs rw,nosuid,nodev,relatime,size=6291456k 0 0", "/tmp...
|
147
210
|
|
148
211
|
```
|
149
212
|
|
@@ -172,29 +235,29 @@ LinuxStat::OS.os_release
|
|
172
235
|
=> {: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/"...
|
173
236
|
|
174
237
|
LinuxStat::OS.uptime
|
175
|
-
=> {:hour=>
|
238
|
+
=> {:hour=>10, :minute=>54, :second=>22.45}
|
176
239
|
|
177
240
|
```
|
178
241
|
|
179
242
|
### LinuxStat::Process
|
180
243
|
```
|
181
244
|
LinuxStat::Process.count
|
182
|
-
=>
|
245
|
+
=> 210
|
183
246
|
|
184
247
|
LinuxStat::Process.idle
|
185
|
-
=> [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118,
|
248
|
+
=> [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118, 119, 122, 131, 134, 140, 152, 153, 179, 181, 183, 184, 191, 192, 193, 194, 195, 196, 198, 236, 286, 314, 324, 346, 385, 3540, 3541, 3542, 3543, 3544, 3545, 30463, 3...
|
186
249
|
|
187
250
|
LinuxStat::Process.list
|
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,
|
251
|
+
=> [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, 122, 131, 134,...
|
189
252
|
|
190
253
|
LinuxStat::Process.names
|
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
|
254
|
+
=> {1=>"systemd", 2=>"kthreadd", 3=>"rcu_gp", 4=>"rcu_par_gp", 6=>"kworker/0:0H-events_highpri", 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...
|
192
255
|
|
193
256
|
LinuxStat::Process.running
|
194
|
-
=> [
|
257
|
+
=> [33736]
|
195
258
|
|
196
259
|
LinuxStat::Process.sleeping
|
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,
|
260
|
+
=> [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, 178, 180, 197, 225, 239, 309, 317, 329, 332, 333, 334, 338, 367, 368, 369, 370, 378, 381, 383, 384, 402,...
|
198
261
|
|
199
262
|
LinuxStat::Process.types
|
200
263
|
=> {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...
|
@@ -210,39 +273,59 @@ LinuxStat::Swap.any?
|
|
210
273
|
=> true
|
211
274
|
|
212
275
|
LinuxStat::Swap.available
|
213
|
-
=>
|
276
|
+
=> 1913936
|
214
277
|
|
215
278
|
LinuxStat::Swap.list
|
216
|
-
=> {"/dev/zram0"=>[:partition, 4194300,
|
279
|
+
=> {"/dev/zram0"=>[:partition, 4194300, 2280364, -2]}
|
217
280
|
|
218
281
|
LinuxStat::Swap.percent_available
|
219
|
-
=>
|
282
|
+
=> 45.63
|
220
283
|
|
221
284
|
LinuxStat::Swap.percent_used
|
222
|
-
=>
|
285
|
+
=> 54.37
|
223
286
|
|
224
287
|
LinuxStat::Swap.stat
|
225
|
-
=> {:total=>4194300, :used=>
|
288
|
+
=> {:total=>4194300, :used=>2280364, :available=>1913936, :percent_used=>54.37, :percent_available=>45.63}
|
226
289
|
|
227
290
|
LinuxStat::Swap.total
|
228
291
|
=> 4194300
|
229
292
|
|
230
293
|
LinuxStat::Swap.used
|
231
|
-
=>
|
294
|
+
=> 2280364
|
232
295
|
|
233
296
|
```
|
234
297
|
|
235
|
-
|
236
|
-
In general, if a method returns either a Float or a Integer or a Time, it will return
|
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.
|
298
|
+
## Return Types
|
299
|
+
+ In general, if a method returns either a Float or a Integer or a Time, it will return a Float or Integer or Time in all cases. But if the status isn't available, it will return nil.
|
239
300
|
|
240
|
-
|
241
|
-
|
242
|
-
|
301
|
+
+ If the method returns a Hash / Array, it will return return Hash / Array in all cases. If the status isn't available, it will return an empty Hash / Array.
|
302
|
+
|
303
|
+
+ If the method returns a String, it will return return String in all cases. If the status isn't available, it will return an empty *frozen* String.
|
304
|
+
|
305
|
+
+ It doesn't have implementation of any Error that gets raised in runtime for the ease of use.
|
306
|
+
|
307
|
+
+ 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. 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
308
|
|
244
309
|
If some error is *raised* it should be reported as a bug.
|
245
310
|
|
311
|
+
## Ruby on Rails
|
312
|
+
|
313
|
+
1. Just add `gem linux_stat`:
|
314
|
+
|
315
|
+
```
|
316
|
+
bundle add linux_stat
|
317
|
+
```
|
318
|
+
|
319
|
+
You can use LinuxStat directly in rails.
|
320
|
+
|
321
|
+

|
322
|
+
|
323
|
+
Don't need to worry about versions if you are using anything > 0.2.1.
|
324
|
+
|
325
|
+
Newer versions will have bug fixes, performance improvements and new features but the return types of old methods will always be the same across upgrades > 0.2.1.
|
326
|
+
|
327
|
+
Even if we remove something, there will be always a deprecation warning for some months or for even a year before we actually remove it.
|
328
|
+
|
246
329
|
## Development
|
247
330
|
|
248
331
|
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.
|
@@ -254,34 +337,26 @@ Like other gems, this doesn't have a test like RSpec. We suggest using the bin/l
|
|
254
337
|
If you need to test a specific module, say the CPU, just run it like this:
|
255
338
|
|
256
339
|
```
|
257
|
-
ruby bin/linuxstat.rb CPU
|
340
|
+
$ ruby bin/linuxstat.rb CPU
|
258
341
|
```
|
259
342
|
|
260
343
|
Or:
|
261
344
|
```
|
262
|
-
ruby bin/linuxstat.rb cpu
|
345
|
+
$ ruby bin/linuxstat.rb cpu
|
263
346
|
```
|
264
347
|
|
265
348
|
That is, the argument passed is not case-sensitive.
|
266
|
-
But if the
|
349
|
+
But if the argument passed isn't available and outright wrong, it will run all the module methods. For example, you can't do:
|
267
350
|
|
268
351
|
```
|
269
|
-
ruby bin/linuxstat.rb upc
|
352
|
+
$ ruby bin/linuxstat.rb upc
|
270
353
|
```
|
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
354
|
This is not a valid module and can't be run.
|
276
355
|
|
277
356
|
## Contributing
|
278
357
|
|
279
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/Souravgoswami/linux_stat.
|
358
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Souravgoswami/linux_stat.
|
280
359
|
|
281
360
|
## License
|
282
361
|
|
283
362
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
284
|
-
|
285
|
-
## Code of Conduct
|
286
|
-
|
287
|
-
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/Souravgoswami/linux_stat/blob/master/CODE_OF_CONDUCT.md).
|
data/bin/linuxstat.rb
CHANGED
@@ -41,7 +41,8 @@ execute.sort.each do |c|
|
|
41
41
|
meths.each do |meth|
|
42
42
|
time = Time.now
|
43
43
|
v = e.send(meth)
|
44
|
-
|
44
|
+
time2 = Time.now
|
45
|
+
time = time2.-(time).*(1_000_000).round(3)
|
45
46
|
|
46
47
|
v = v.inspect
|
47
48
|
dis = v.length > 253 ? v[0..250].strip + '...'.freeze : v
|
@@ -0,0 +1,28 @@
|
|
1
|
+
#include <sys/statvfs.h>
|
2
|
+
#include "ruby.h"
|
3
|
+
|
4
|
+
static VALUE statfs(VALUE obj, VALUE dir) {
|
5
|
+
struct statvfs buf ;
|
6
|
+
char *d = StringValuePtr(dir) ;
|
7
|
+
VALUE hash = rb_hash_new() ;
|
8
|
+
|
9
|
+
if(statvfs(d, &buf) < 0) return hash ;
|
10
|
+
|
11
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("block_size")), INT2NUM(buf.f_bsize)) ;
|
12
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("fragment_size")), INT2NUM(buf.f_frsize)) ;
|
13
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("blocks")), INT2NUM(buf.f_blocks)) ;
|
14
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("block_free")), INT2NUM(buf.f_bfree)) ;
|
15
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("block_avail_unpriv")), INT2NUM(buf.f_bavail)) ;
|
16
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("inodes")), INT2NUM(buf.f_files)) ;
|
17
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("free_inodes")), INT2NUM(buf.f_ffree)) ;
|
18
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("filesystem_id")), INT2NUM(buf.f_fsid)) ;
|
19
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("mount_flags")), INT2NUM(buf.f_flag)) ;
|
20
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("max_filename_length")), INT2NUM(buf.f_namemax)) ;
|
21
|
+
|
22
|
+
return hash ;
|
23
|
+
}
|
24
|
+
|
25
|
+
void Init_fs_stat() {
|
26
|
+
VALUE fs = rb_define_module("FS") ;
|
27
|
+
rb_define_module_function(fs, "stat", statfs, 1) ;
|
28
|
+
}
|
data/lib/linux_stat.rb
CHANGED
@@ -1,8 +1,4 @@
|
|
1
1
|
require "linux_stat/version"
|
2
|
-
|
3
|
-
module LinuxStat
|
4
|
-
end
|
5
|
-
|
6
2
|
require "linux_stat/battery"
|
7
3
|
require "linux_stat/bios"
|
8
4
|
require "linux_stat/cpu"
|
@@ -12,3 +8,6 @@ require "linux_stat/net"
|
|
12
8
|
require "linux_stat/os"
|
13
9
|
require "linux_stat/process"
|
14
10
|
require "linux_stat/swap"
|
11
|
+
require "linux_stat/mounts"
|
12
|
+
require "linux_stat/fs_stat"
|
13
|
+
require "linux_stat/filesystem"
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# require 'linux_stat/fs_stat'
|
2
|
+
|
3
|
+
module LinuxStat
|
4
|
+
module Filesystem
|
5
|
+
extend FS
|
6
|
+
|
7
|
+
class << self
|
8
|
+
# stat(fs = '/')
|
9
|
+
# Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
|
10
|
+
#
|
11
|
+
# It returns a Hash with the following info:
|
12
|
+
# 1. total size of the device (in bytes)
|
13
|
+
# 2. free space (in kilobytes)
|
14
|
+
# 3. used space (in kilobytes)
|
15
|
+
#
|
16
|
+
# In a hash format:
|
17
|
+
# {:total=>119981191168, :free=>43155574784, :used=>76825616384, :available=>43155574784}
|
18
|
+
#
|
19
|
+
# If the stat can't be acquired, this method will return an empty Hash.
|
20
|
+
|
21
|
+
def stat(fs = ?/.freeze)
|
22
|
+
s = stat_raw(fs)
|
23
|
+
return {} if s.empty?
|
24
|
+
s.default = 0
|
25
|
+
|
26
|
+
{
|
27
|
+
total: s[:block_size] * s[:blocks],
|
28
|
+
free: s[:block_size] * s[:block_free],
|
29
|
+
used: s[:blocks].-(s[:block_free]) * s[:block_size],
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
# stat(fs = '/')
|
34
|
+
# Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
|
35
|
+
# It returns the total size of a given disk in bytes.
|
36
|
+
#
|
37
|
+
# If the stat can't be acquired, this method will return nil.
|
38
|
+
def total(fs = ?/.freeze)
|
39
|
+
s = stat_raw(fs)
|
40
|
+
return nil if s.empty?
|
41
|
+
s.default = 0
|
42
|
+
s[:block_size] * s[:blocks]
|
43
|
+
end
|
44
|
+
|
45
|
+
# stat(fs = '/')
|
46
|
+
# Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
|
47
|
+
# It returns the total free space in a disk in bytes.
|
48
|
+
# It is to be noted that free is not same as available.
|
49
|
+
# Free returns the size of free blocks.
|
50
|
+
#
|
51
|
+
# If the stat can't be acquired, this method will return an empty Hash.
|
52
|
+
def free(fs = ?/.freeze)
|
53
|
+
s = stat_raw(fs)
|
54
|
+
return nil if s.empty?
|
55
|
+
s.default = 0
|
56
|
+
s[:block_size] * s[:block_free]
|
57
|
+
end
|
58
|
+
|
59
|
+
# stat(fs = '/')
|
60
|
+
# Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
|
61
|
+
# It returns the used space of a given disk in bytes.
|
62
|
+
#
|
63
|
+
# If the stat can't be acquired, this method will return nil.
|
64
|
+
def used(fs = ?/.freeze)
|
65
|
+
s = stat_raw(fs)
|
66
|
+
return nil if s.empty?
|
67
|
+
s.default = 0
|
68
|
+
s[:blocks].-(s[:block_free]) * s[:block_size]
|
69
|
+
end
|
70
|
+
|
71
|
+
# stat(fs = '/')
|
72
|
+
# Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
|
73
|
+
# It returns the total free space in a disk in bytes.
|
74
|
+
# It is to be noted that free is not same as available.
|
75
|
+
# Available returns the size of free blocks for unpriviledged users.
|
76
|
+
#
|
77
|
+
# If the stat can't be acquired, this method will return an empty Hash.
|
78
|
+
def available(fs = ?/.freeze)
|
79
|
+
s = stat_raw(fs)
|
80
|
+
return nil if s.empty?
|
81
|
+
s.default = 0
|
82
|
+
s[:block_size] * s[:block_avail_unpriv]
|
83
|
+
end
|
84
|
+
|
85
|
+
# stat(fs = '/')
|
86
|
+
# Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
|
87
|
+
#
|
88
|
+
# It returns a Hash with the following data (for example):
|
89
|
+
# {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>10535967, :block_avail_unpriv=>10535967, :inodes=>58612160, :free_inodes=>56718550, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
|
90
|
+
#
|
91
|
+
# If the stat can't be acquired, this method will return an empty Hash.
|
92
|
+
def stat_raw(fs = '/'.freeze)
|
93
|
+
FS.stat(fs)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module LinuxStat
|
2
|
+
module Mounts
|
3
|
+
class << self
|
4
|
+
# Reads /proc/mounts and returns list of devices.
|
5
|
+
#
|
6
|
+
# It returns an Array.
|
7
|
+
# If the info isn't available or /proc/mounts is not readable, it will return an empty Array.
|
8
|
+
def list
|
9
|
+
mounts
|
10
|
+
end
|
11
|
+
|
12
|
+
# Reads /proc/mounts and returns partition name of the device mounted at /.
|
13
|
+
#
|
14
|
+
# It returns a String.
|
15
|
+
# But if the info isn't available or /proc/mounts is not readable, it will return an empty frozen String.
|
16
|
+
def root
|
17
|
+
find_root[0].to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
# Reads /proc/mounts and returns the file system of the device mounted at /.
|
21
|
+
#
|
22
|
+
# It returns a String.
|
23
|
+
# But if the info isn't available or /proc/mounts is not readable, it will return an empty frozen String.
|
24
|
+
def root_fs
|
25
|
+
find_root[2].to_s
|
26
|
+
end
|
27
|
+
|
28
|
+
# Reads /proc/mounts and returns the options used for mounting /.
|
29
|
+
#
|
30
|
+
# It returns a String.
|
31
|
+
# But if the info isn't available or /proc/mounts is not readable, it will return an empty frozen string.
|
32
|
+
def root_mount_options
|
33
|
+
find_root[3].to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
# Reads /proc/mounts and finds all tmpfs.
|
37
|
+
#
|
38
|
+
# It returns a Hash
|
39
|
+
# But if the info isn't available or /proc/mounts is not readable, it will return an empty Hash.
|
40
|
+
def tmpfs
|
41
|
+
ret = {}
|
42
|
+
mounts.each { |x|
|
43
|
+
ret.merge!({x.split[1] => x}) if x.start_with?('tmpfs '.freeze)
|
44
|
+
}
|
45
|
+
ret
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
def mount_readable?
|
50
|
+
@@mount_readable ||= File.readable?('/proc/mounts')
|
51
|
+
end
|
52
|
+
|
53
|
+
def mounts
|
54
|
+
return [] unless mount_readable?
|
55
|
+
IO.readlines('/proc/mounts').each(&:strip!)
|
56
|
+
end
|
57
|
+
|
58
|
+
def find_root
|
59
|
+
return [] unless mount_readable?
|
60
|
+
@@root ||= IO.foreach('/proc/mounts').find { |x| x.split[1] == '/'.freeze }.split
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/lib/linux_stat/version.rb
CHANGED
metadata
CHANGED
@@ -1,34 +1,38 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linux_stat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.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-30 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
|
-
extensions:
|
17
|
+
extensions:
|
18
|
+
- ext/fs_stat/extconf.rb
|
18
19
|
extra_rdoc_files:
|
19
20
|
- README.md
|
20
21
|
files:
|
21
|
-
- Gemfile
|
22
22
|
- README.md
|
23
23
|
- bin/console
|
24
24
|
- bin/linuxstat.rb
|
25
25
|
- bin/setup
|
26
|
+
- ext/fs_stat/extconf.rb
|
27
|
+
- ext/fs_stat/fs_stat.c
|
26
28
|
- lib/linux_stat.rb
|
27
29
|
- lib/linux_stat/battery.rb
|
28
30
|
- lib/linux_stat/bios.rb
|
29
31
|
- lib/linux_stat/cpu.rb
|
32
|
+
- lib/linux_stat/filesystem.rb
|
30
33
|
- lib/linux_stat/kernel.rb
|
31
34
|
- lib/linux_stat/memory.rb
|
35
|
+
- lib/linux_stat/mounts.rb
|
32
36
|
- lib/linux_stat/net.rb
|
33
37
|
- lib/linux_stat/os.rb
|
34
38
|
- lib/linux_stat/process.rb
|
@@ -56,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
60
|
- !ruby/object:Gem::Version
|
57
61
|
version: '0'
|
58
62
|
requirements: []
|
59
|
-
rubygems_version: 3.1.
|
63
|
+
rubygems_version: 3.1.4
|
60
64
|
signing_key:
|
61
65
|
specification_version: 4
|
62
66
|
summary: Efficient linux system reporting gem
|