linux_stat 0.2.0 → 0.3.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd93c4105b08257a85c176b7898d5ae282b1d74bb33d99d2b11536fae6402eda
4
- data.tar.gz: a4a3a2032a1573c7f60ca09567fc9cb03ebea90cdc330f4652d7c7711d26adf6
3
+ metadata.gz: a392f3c04781b05bb96636a882b6e69143bef15befb094f980c7b542923e0649
4
+ data.tar.gz: 4ef67fa10ab4210090790b368852cac7deee99965560b201f136f0cd093e9a87
5
5
  SHA512:
6
- metadata.gz: 24d055e84863433fa67cf3c5a59ddea66ad03346aaf469eb033dfa3dae362ee62117f4b07bcb251f26e73517a13dfde460132f3773e214ab34812c7838e7ccf9
7
- data.tar.gz: 0ac774300aab62af85094f9be2162449cbfa5be8bfba0f04fcf1801e11b9bbb730db8e51a765bbd53d9d0b887b769037ddc19fa5012018e1b5d8dfc3137adddb
6
+ metadata.gz: 816e423f9a22797022d87a347dbcde20277d0b1d6fa8cc90001367cbdf2012e625400c2a659bbbd308fcf58363c19f2af88ec39d5bcc1fa04ccd4f61d77371b7
7
+ data.tar.gz: dfae97ea5bd4ef7c0daa4c784d7d951b915121753d8b61363624aaaa12e9559968e623638004accb8438a77b820913e726856e166bd437f1073047b936a83ba1
data/README.md CHANGED
@@ -1,10 +1,31 @@
1
1
  # LinuxStat
2
- ![logo](https://raw.githubusercontent.com/Souravgoswami/linux_stat/master/logo/logo.jpg)
2
+ ![logo](https://raw.githubusercontent.com/Souravgoswami/linux_stat/master/images/logo.jpg)
3
3
 
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.
4
+ For reading the HTML version, visit [linux_stat](https://souravgoswami.github.io/linux_stat/).
5
+
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:
@@ -78,7 +99,7 @@ LinuxStat::CPU.count
78
99
  => 4
79
100
 
80
101
  LinuxStat::CPU.cur_freq
81
- => [2000001, 1999996, 1999998, 1999997]
102
+ => [1998722, 1998401, 1974377, 1875264]
82
103
 
83
104
  LinuxStat::CPU.max_freq
84
105
  => [2000000, 2000000, 2000000, 2000000]
@@ -87,16 +108,38 @@ LinuxStat::CPU.model
87
108
  => "Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz"
88
109
 
89
110
  LinuxStat::CPU.stat
90
- => {0=>30.0, 1=>50.0, 2=>28.57, 3=>16.67, 4=>28.57}
111
+ => {0=>6.45, 1=>0.0, 2=>0.0, 3=>12.5, 4=>0.0}
91
112
 
92
113
  LinuxStat::CPU.total_usage
93
- => 26.67
114
+ => 3.45
94
115
 
95
116
  LinuxStat::CPU.usage
96
- => 25.0
117
+ => 6.45
97
118
 
98
119
  LinuxStat::CPU.usages
99
- => {0=>33.33, 1=>50.0, 2=>28.57, 3=>28.57, 4=>16.67}
120
+ => {0=>9.68, 1=>11.11, 2=>12.5, 3=>14.29, 4=>12.5}
121
+
122
+ ```
123
+
124
+ ### LinuxStat::Filesystem
125
+ ```
126
+ LinuxStat::Filesystem.available
127
+ => 43155402752
128
+
129
+ LinuxStat::Filesystem.free
130
+ => 43155402752
131
+
132
+ LinuxStat::Filesystem.stat
133
+ => {:total=>119981191168, :free=>43155402752, :used=>76825788416}
134
+
135
+ LinuxStat::Filesystem.stat_raw
136
+ => {: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}
137
+
138
+ LinuxStat::Filesystem.total
139
+ => 119981191168
140
+
141
+ LinuxStat::Filesystem.used
142
+ => 76825788416
100
143
 
101
144
  ```
102
145
 
@@ -128,22 +171,41 @@ LinuxStat::Kernel.version
128
171
  ### LinuxStat::Memory
129
172
  ```
130
173
  LinuxStat::Memory.available
131
- => 405020
174
+ => 403724
132
175
 
133
176
  LinuxStat::Memory.percent_available
134
- => 10.56
177
+ => 10.52
135
178
 
136
179
  LinuxStat::Memory.percent_used
137
- => 89.44
180
+ => 89.48
138
181
 
139
182
  LinuxStat::Memory.stat
140
- => {:total=>3836264, :used=>3431244, :available=>405020, :percent_used=>89.44, :percent_available=>10.56}
183
+ => {:total=>3836264, :used=>3432540, :available=>403724, :percent_used=>89.48, :percent_available=>10.52}
141
184
 
142
185
  LinuxStat::Memory.total
143
186
  => 3836264
144
187
 
145
188
  LinuxStat::Memory.used
146
- => 3431244
189
+ => 3432540
190
+
191
+ ```
192
+
193
+ ### LinuxStat::Mounts
194
+ ```
195
+ LinuxStat::Mounts.list
196
+ => ["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", "...
197
+
198
+ LinuxStat::Mounts.root
199
+ => "/dev/sda2"
200
+
201
+ LinuxStat::Mounts.root_fs
202
+ => "xfs"
203
+
204
+ LinuxStat::Mounts.root_mount_options
205
+ => "rw,noatime,attr2,inode64,logbufs=8,logbsize=32k,noquota"
206
+
207
+ LinuxStat::Mounts.tmpfs
208
+ => {"/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
209
 
148
210
  ```
149
211
 
@@ -172,29 +234,29 @@ LinuxStat::OS.os_release
172
234
  => {: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
235
 
174
236
  LinuxStat::OS.uptime
175
- => {:hour=>40, :minute=>46, :second=>19.75}
237
+ => {:hour=>10, :minute=>54, :second=>22.45}
176
238
 
177
239
  ```
178
240
 
179
241
  ### LinuxStat::Process
180
242
  ```
181
243
  LinuxStat::Process.count
182
- => 223
244
+ => 210
183
245
 
184
246
  LinuxStat::Process.idle
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...
247
+ => [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
248
 
187
249
  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, 111, 114, 115, 117, 118, 120, 122, 131, 134,...
250
+ => [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
251
 
190
252
  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=>"idle_inject/...
253
+ => {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
254
 
193
255
  LinuxStat::Process.running
194
- => [525, 91940, 91971, 91993, 92020, 95119]
256
+ => [33736]
195
257
 
196
258
  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, 111, 114, 115, 165, 167, 186, 214, 227, 302, 313, 319, 321, 333, 334, 335, 352, 353, 354, 355, 362, 366, 367, 369, 382,...
259
+ => [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
260
 
199
261
  LinuxStat::Process.types
200
262
  => {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 +272,64 @@ LinuxStat::Swap.any?
210
272
  => true
211
273
 
212
274
  LinuxStat::Swap.available
213
- => 2292252
275
+ => 1913936
214
276
 
215
277
  LinuxStat::Swap.list
216
- => {"/dev/zram0"=>[:partition, 4194300, 1902048, -2]}
278
+ => {"/dev/zram0"=>[:partition, 4194300, 2280364, -2]}
217
279
 
218
280
  LinuxStat::Swap.percent_available
219
- => 54.65
281
+ => 45.63
220
282
 
221
283
  LinuxStat::Swap.percent_used
222
- => 45.35
284
+ => 54.37
223
285
 
224
286
  LinuxStat::Swap.stat
225
- => {:total=>4194300, :used=>1902048, :available=>2292252, :percent_used=>45.35, :percent_available=>54.65}
287
+ => {:total=>4194300, :used=>2280364, :available=>1913936, :percent_used=>54.37, :percent_available=>45.63}
226
288
 
227
289
  LinuxStat::Swap.total
228
290
  => 4194300
229
291
 
230
292
  LinuxStat::Swap.used
231
- => 1902048
293
+ => 2280364
232
294
 
233
295
  ```
234
296
 
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.
297
+ ## Return Types
298
+ + 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.
299
+
300
+ + 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.
301
+
302
+ + 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.
239
303
 
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.
304
+ + It doesn't have implementation of any Error that gets raised in runtime for the ease of use.
305
+
306
+ + 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
307
 
244
308
  If some error is *raised* it should be reported as a bug.
245
309
 
310
+ ## Ruby on Rails
311
+
312
+ 1. Just add `gem linux_stat`:
313
+
314
+ ```
315
+ $ bundle add linux_stat
316
+ ```
317
+
318
+ You can use LinuxStat directly in rails.
319
+
320
+ ![RailsApp](https://raw.githubusercontent.com/Souravgoswami/linux_stat/master/images/rails.gif)
321
+
322
+
323
+ ## Android
324
+
325
+ LinuxStat does support Android OS. But it's not rigorously tested on all device like android apps.
326
+
327
+ But in Termux you can just run LinuxStat without facing issues.
328
+
329
+ ![termux](https://raw.githubusercontent.com/Souravgoswami/linux_stat/master/images/termux.webp)
330
+
331
+ Issues regarding running LinuxStat on termux are also welcomed.
332
+
246
333
  ## Development
247
334
 
248
335
  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 +341,26 @@ Like other gems, this doesn't have a test like RSpec. We suggest using the bin/l
254
341
  If you need to test a specific module, say the CPU, just run it like this:
255
342
 
256
343
  ```
257
- ruby bin/linuxstat.rb CPU
344
+ $ ruby bin/linuxstat.rb CPU
258
345
  ```
259
346
 
260
347
  Or:
261
348
  ```
262
- ruby bin/linuxstat.rb cpu
349
+ $ ruby bin/linuxstat.rb cpu
263
350
  ```
264
351
 
265
352
  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:
353
+ 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
354
 
268
355
  ```
269
- ruby bin/linuxstat.rb upc
356
+ $ ruby bin/linuxstat.rb upc
270
357
  ```
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
358
  This is not a valid module and can't be run.
276
359
 
277
360
  ## Contributing
278
361
 
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).
362
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Souravgoswami/linux_stat.
280
363
 
281
364
  ## License
282
365
 
283
366
  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/[USERNAME]/linux_stat/blob/master/CODE_OF_CONDUCT.md).
@@ -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
- time = Time.now.-(time).*(1_000_000).round(3)
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,7 @@
1
+ require 'mkmf'
2
+
3
+ unless (have_header('sys/statvfs.h') && have_header('ruby.h'))
4
+ abort('Missing header')
5
+ end
6
+
7
+ create_makefile 'linux_stat/fs_stat'
@@ -0,0 +1,30 @@
1
+ #include <sys/statvfs.h>
2
+ #include "ruby.h"
3
+ #pragma GCC optimize ("O3")
4
+ #pragma clang optimize on
5
+
6
+ static VALUE statfs(VALUE obj, VALUE dir) {
7
+ struct statvfs buf ;
8
+ char *d = StringValuePtr(dir) ;
9
+ VALUE hash = rb_hash_new() ;
10
+
11
+ if(statvfs(d, &buf) < 0) return hash ;
12
+
13
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_size")), INT2FIX(buf.f_bsize)) ;
14
+ rb_hash_aset(hash, ID2SYM(rb_intern("fragment_size")), INT2FIX(buf.f_frsize)) ;
15
+ rb_hash_aset(hash, ID2SYM(rb_intern("blocks")), INT2FIX(buf.f_blocks)) ;
16
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_free")), INT2FIX(buf.f_bfree)) ;
17
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_avail_unpriv")), INT2FIX(buf.f_bavail)) ;
18
+ rb_hash_aset(hash, ID2SYM(rb_intern("inodes")), INT2FIX(buf.f_files)) ;
19
+ rb_hash_aset(hash, ID2SYM(rb_intern("free_inodes")), INT2FIX(buf.f_ffree)) ;
20
+ rb_hash_aset(hash, ID2SYM(rb_intern("filesystem_id")), INT2FIX(buf.f_fsid)) ;
21
+ rb_hash_aset(hash, ID2SYM(rb_intern("mount_flags")), INT2FIX(buf.f_flag)) ;
22
+ rb_hash_aset(hash, ID2SYM(rb_intern("max_filename_length")), INT2FIX(buf.f_namemax)) ;
23
+
24
+ return hash ;
25
+ }
26
+
27
+ void Init_fs_stat() {
28
+ VALUE fs = rb_define_module("FS") ;
29
+ rb_define_module_function(fs, "stat", statfs, 1) ;
30
+ }
@@ -0,0 +1,7 @@
1
+ require 'mkmf'
2
+
3
+ unless (have_header('sys/utsname.h') && have_header('ruby.h'))
4
+ abort('Missing header')
5
+ end
6
+
7
+ create_makefile 'linux_stat/utsname'
@@ -0,0 +1,29 @@
1
+ #include <sys/utsname.h>
2
+ #include "ruby.h"
3
+ #pragma GCC optimize ("O3")
4
+ #pragma clang optimize on
5
+
6
+ static struct utsname buf ;
7
+ static short status ;
8
+
9
+ void init_buf() {
10
+ status = uname(&buf) ;
11
+ }
12
+
13
+ static VALUE getMachine(VALUE obj) {
14
+ VALUE machine = status < 0 ? rb_str_new_cstr("") : rb_str_new_cstr(buf.machine) ;
15
+ return machine ;
16
+ }
17
+
18
+ static VALUE getNodename(VALUE obj) {
19
+ VALUE nodename = status < 0 ? rb_str_new_cstr("") : rb_str_new_cstr(buf.nodename) ;
20
+ return nodename ;
21
+ }
22
+
23
+ void Init_utsname() {
24
+ init_buf() ;
25
+
26
+ VALUE _uname = rb_define_module("Uname") ;
27
+ rb_define_module_function(_uname, "machine", getMachine, 0) ;
28
+ rb_define_module_function(_uname, "nodename", getNodename, 0) ;
29
+ }
@@ -1,14 +1,17 @@
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"
9
5
  require "linux_stat/kernel"
10
6
  require "linux_stat/memory"
11
7
  require "linux_stat/net"
8
+
9
+ require 'linux_stat/utsname'
12
10
  require "linux_stat/os"
11
+
13
12
  require "linux_stat/process"
14
13
  require "linux_stat/swap"
14
+ require "linux_stat/mounts"
15
+
16
+ require "linux_stat/fs_stat"
17
+ require "linux_stat/filesystem"
@@ -1,7 +1,9 @@
1
1
  module LinuxStat
2
2
  module CPU
3
3
  class << self
4
- # Returns the cpu usage of all threads.
4
+ # stat(sleep = 0.075)
5
+ # Where sleep is the delay to gather the data.
6
+ # This method returns the cpu usage of all threads.
5
7
  #
6
8
  # The first one is aggregated CPU usage reported by the Linux kernel.
7
9
  # And the consecutive ones are the real core usages.
@@ -30,7 +32,10 @@ module LinuxStat
30
32
  end
31
33
  end
32
34
 
33
- # Returns the total cpu usage.
35
+ # total_usage(sleep = 0.075)
36
+ # Where sleep is the delay to gather the data.
37
+ # This method returns the cpu usage of all threads.
38
+ #
34
39
  # 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
40
  #
36
41
  # If the information is not available, it will return nil.
@@ -67,14 +72,26 @@ module LinuxStat
67
72
  # If the information isn't available, it will return an empty array.
68
73
  def cur_freq
69
74
  @@cpu_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_cur_freq"]
70
- @@cpu_freqs.map { |x| IO.read(x).to_i }
75
+ @@cur_freqs_readable ||= @@cpu_freqs.all?(&File.method(:readable?))
76
+
77
+ if @@cur_freqs_readable
78
+ @@cpu_freqs.map { |x| IO.read(x).to_i }
79
+ else
80
+ []
81
+ end
71
82
  end
72
83
 
73
84
  # Returns an array with max core frequencies corresponding to the usages.
74
85
  # If the information isn't available, it will return an empty array.
75
86
  def max_freq
76
87
  @@max_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_max_freq"]
77
- @@max_freqs.map { |x| IO.read(x).to_i }
88
+ @@max_freqs_readable ||= @@max_freqs.all?(&File.method(:readable?))
89
+
90
+ if @@max_freqs_readable
91
+ @@max_freqs.map { |x| IO.read(x).to_i }
92
+ else
93
+ []
94
+ end
78
95
  end
79
96
 
80
97
  alias usages stat
@@ -0,0 +1,94 @@
1
+ module LinuxStat
2
+ module Filesystem
3
+ prepend FS
4
+
5
+ class << self
6
+ # stat(fs = '/')
7
+ # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
8
+ #
9
+ # It returns a Hash with the following info:
10
+ # 1. total size of the device (in bytes)
11
+ # 2. free space (in kilobytes)
12
+ # 3. used space (in kilobytes)
13
+ #
14
+ # In a hash format:
15
+ # {:total=>119981191168, :free=>43155574784, :used=>76825616384, :available=>43155574784}
16
+ #
17
+ # If the stat can't be acquired, this method will return an empty Hash.
18
+ def stat(fs = ?/.freeze)
19
+ s = stat_raw(fs)
20
+ return {} if s.empty?
21
+ s.default = 0
22
+
23
+ {
24
+ total: s[:block_size] * s[:blocks],
25
+ free: s[:block_size] * s[:block_free],
26
+ used: s[:blocks].-(s[:block_free]) * s[:block_size],
27
+ }
28
+ end
29
+
30
+ # total(fs = '/')
31
+ # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
32
+ # It returns the total size of a given disk in bytes.
33
+ #
34
+ # If the stat can't be acquired, this method will return nil.
35
+ def total(fs = ?/.freeze)
36
+ s = stat_raw(fs)
37
+ return nil if s.empty?
38
+ s.default = 0
39
+ s[:block_size] * s[:blocks]
40
+ end
41
+
42
+ # free(fs = '/')
43
+ # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
44
+ # It returns the total free space in a disk in bytes.
45
+ # It is to be noted that free is not same as available.
46
+ # Free returns the size of free blocks.
47
+ #
48
+ # If the stat can't be acquired, this method will return an empty Hash.
49
+ def free(fs = ?/.freeze)
50
+ s = stat_raw(fs)
51
+ return nil if s.empty?
52
+ s.default = 0
53
+ s[:block_size] * s[:block_free]
54
+ end
55
+
56
+ # used(fs = '/')
57
+ # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
58
+ # It returns the used space of a given disk in bytes.
59
+ #
60
+ # If the stat can't be acquired, this method will return nil.
61
+ def used(fs = ?/.freeze)
62
+ s = stat_raw(fs)
63
+ return nil if s.empty?
64
+ s.default = 0
65
+ s[:blocks].-(s[:block_free]) * s[:block_size]
66
+ end
67
+
68
+ # available(fs = '/')
69
+ # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
70
+ # It returns the total free space in a disk in bytes.
71
+ # It is to be noted that free is not same as available.
72
+ # Available returns the size of free blocks for unpriviledged users.
73
+ #
74
+ # If the stat can't be acquired, this method will return an empty Hash.
75
+ def available(fs = ?/.freeze)
76
+ s = stat_raw(fs)
77
+ return nil if s.empty?
78
+ s.default = 0
79
+ s[:block_size] * s[:block_avail_unpriv]
80
+ end
81
+
82
+ # stat_raw(fs = '/')
83
+ # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
84
+ #
85
+ # It returns a Hash with the following data (for example):
86
+ # {: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}
87
+ #
88
+ # If the stat can't be acquired, this method will return an empty Hash.
89
+ def stat_raw(fs = '/'.freeze)
90
+ FS.stat(fs)
91
+ end
92
+ end
93
+ end
94
+ end
@@ -73,15 +73,15 @@ module LinuxStat
73
73
  joined = x.each(&:strip!).join(?\s.freeze)
74
74
 
75
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*$/]
76
+ if joined[/^\d{1,2}\s\w{3}\s\d{4}\s\d{1,2}:\d{1,2}:\d{1,2}\s\+\d*$/]
77
77
  Time.strptime(joined, '%d %b %Y %H:%M:%S %Z') rescue nil
78
78
 
79
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*$/]
80
+ elsif joined[/^\w{3}\s\d{1,2}\s\d{1,2}:\d{1,2}:\d{2}\s\w+\s\d*$/]
81
81
  Time.strptime(joined, '%b %d %H:%M:%S %z %Y') rescue nil
82
82
 
83
83
  # Match 2017-09-19
84
- elsif joined[/\d{4}-\d{2}-\d{2}/]
84
+ elsif joined[/\d{4}-\d{1,2}-\d{1,2}/]
85
85
  Time.strptime(joined[/\d{4}-\d{2}-\d{2}/] + " +00:00", '%Y-%m-%d %z') rescue nil
86
86
 
87
87
  else
@@ -118,13 +118,13 @@ module LinuxStat
118
118
  joined = x.each(&:strip!).join(?\s.freeze)
119
119
 
120
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)) ||
121
+ if (joined[/^\d{1,2}\s\w{3}\s\d{4}\s\d{1,2}:\d{1,2}:\d{1,2}\s\+\d*$/] && (Time.strptime(joined, '%d %b %Y %H:%M:%S %Z') rescue nil)) ||
122
122
 
123
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)) ||
124
+ (joined[/^\w{3}\s\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}\s\w+\s\d*$/] && (Time.strptime(joined, '%b %d %H:%M:%S %z %Y') rescue nil)) ||
125
125
 
126
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))
127
+ (joined[/\d{4}-\d{1,2}-\d{1,2}/] && (Time.strptime(joined[/\d{4}-\d{2}-\d{2}/] + " +00:00", '%Y-%m-%d %z') rescue nil))
128
128
  joined
129
129
  else
130
130
  nil
@@ -133,6 +133,8 @@ module LinuxStat
133
133
  end
134
134
  end
135
135
 
136
+ alias release version
137
+
136
138
  # Reads maximum 1024 bytes from /proc/version and returns the string.
137
139
  # The output is also cached ; as changing the value in runtime is unexpected.
138
140
  def string
@@ -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
@@ -1,5 +1,7 @@
1
1
  module LinuxStat
2
2
  module OS
3
+ prepend Uname
4
+
3
5
  class << self
4
6
  # Reads /etc/os-release and returns a Hash. For example:
5
7
  # {: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"}
@@ -51,6 +53,18 @@ module LinuxStat
51
53
  end
52
54
  end
53
55
 
56
+ # Uses utsname.h to determine the machine
57
+ # It returns a String but if the info isn't available, it will return an empty String
58
+ def machine
59
+ @@machine ||= Uname.machine
60
+ end
61
+
62
+ # Uses utsname.h to determine the system nodename
63
+ # It returns String but if the info isn't available, it will return an empty String
64
+ def nodename
65
+ @@nodename ||= Uname.nodename
66
+ end
67
+
54
68
  # Reads /etc/hostname and returns the hostname.
55
69
  # The return type is String.
56
70
  # If the info info isn't available, it will return 'localhost'.
@@ -62,10 +76,13 @@ module LinuxStat
62
76
  end
63
77
  end
64
78
 
65
- # Reads ruby configuration and tries to guess if the system is 32 bit or 64 bit.
66
- # The return type is Integer.
79
+ # Reads ruby configuration and tries to guess if the system is 64 bit.
80
+ # If it fails then it runs utsname.h to guess the machine.
81
+ # It the machine is 64 bits, it will return 64, else it returns 32.
82
+ #
83
+ # The return type is strictly Integer and doesn't fail.
67
84
  def bits
68
- @@bits ||= if RbConfig::CONFIG['host_cpu'].end_with?('64') || RUBY_PLATFORM[/x86_64/]
85
+ @@bits ||= if RbConfig::CONFIG['host_cpu'].end_with?('64') || RUBY_PLATFORM.end_with?('64') || machine.end_with?('64')
69
86
  64
70
87
  else
71
88
  32
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.3"
3
3
  end
metadata CHANGED
@@ -1,34 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_stat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.3
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-25 00:00:00.000000000 Z
11
+ date: 2020-12-03 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/utsname/extconf.rb
19
+ - ext/fs_stat/extconf.rb
18
20
  extra_rdoc_files:
19
21
  - README.md
20
22
  files:
21
- - Gemfile
22
23
  - README.md
23
24
  - bin/console
24
25
  - bin/linuxstat.rb
25
26
  - bin/setup
27
+ - ext/fs_stat/extconf.rb
28
+ - ext/fs_stat/fs_stat.c
29
+ - ext/utsname/extconf.rb
30
+ - ext/utsname/utsname.c
26
31
  - lib/linux_stat.rb
27
32
  - lib/linux_stat/battery.rb
28
33
  - lib/linux_stat/bios.rb
29
34
  - lib/linux_stat/cpu.rb
35
+ - lib/linux_stat/filesystem.rb
30
36
  - lib/linux_stat/kernel.rb
31
37
  - lib/linux_stat/memory.rb
38
+ - lib/linux_stat/mounts.rb
32
39
  - lib/linux_stat/net.rb
33
40
  - lib/linux_stat/os.rb
34
41
  - lib/linux_stat/process.rb
@@ -56,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
63
  - !ruby/object:Gem::Version
57
64
  version: '0'
58
65
  requirements: []
59
- rubygems_version: 3.1.2
66
+ rubygems_version: 3.1.4
60
67
  signing_key:
61
68
  specification_version: 4
62
69
  summary: Efficient linux system reporting gem
data/Gemfile DELETED
@@ -1,5 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- # Specify your gem's dependencies in linux_stat.gemspec
4
- gemspec
5
- gem "rake", ">= 12.0"