linux_stat 0.5.1 → 0.6.4

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: 349ddabe649c7cdec58e01537c6c28ab47f9d30b413208baf5fae4c0c7a26804
4
- data.tar.gz: e66ba391b2264c53f6d2619ee64d8907e5435079b29bcc76bf2d54516ea82949
3
+ metadata.gz: 15bbdf4582dc8a526cde2230d81e2fea8ea67f5684daeae87161bd486e8f73a0
4
+ data.tar.gz: 236125a3f4ac8c85563cf075be40b8ceb0e7e0b4cd6374abce1c6c6f62da8d56
5
5
  SHA512:
6
- metadata.gz: 1f7274982f6330ed5023b6e7adf33f325163e382c9fdc81be8181c65ad182ba1e77433af792e1167d603673af147ff7e0601a42975f25ac2393206db755a27f5
7
- data.tar.gz: 61d48ba5bcf4598d8df63173bfbecfe4fd6a91552f32195a540e344961ee4e2ebcb9decf492d092b41ca034908b82948b9e6d727e678e3b41f434c5771794070
6
+ metadata.gz: 9c2f0f12aa42dbb2c63426a5c7aa384bb68a02dcf283b78953ebc641b6d1803400f571d94e6eb4a812b282672d95a071857fd2e4ab4004d17c163eb2ce12fea7
7
+ data.tar.gz: 448f627019dce4ada6c6c76c75dc3a2d531230a1cf77de7a88d0fd372c43e79ed69b479dc7625385ac9f2e44e1bb8da2ef8405d9737ddccd5b985691959cf294
data/README.md CHANGED
@@ -13,12 +13,12 @@ It only works for Linux, and detecting the OS is upto the user of this gem.
13
13
  + You need to have the C compile to be able to compile the C extension.
14
14
  On Arch Linux:
15
15
  ```
16
- # pacman -S gcc
16
+ # pacman -S gcc make
17
17
  ```
18
18
 
19
19
  On Debian based systems:
20
20
  ```
21
- # apt install gcc
21
+ # apt install gcc build-essential
22
22
  ```
23
23
 
24
24
  + You might also require ruby-dev in Debian based systems which provides support for ruby.h header file:
@@ -103,7 +103,7 @@ LinuxStat::CPU.count
103
103
  => 4
104
104
 
105
105
  LinuxStat::CPU.cur_freq
106
- => [1999998, 2000003, 2000015, 2000009]
106
+ => [2000063, 2000108, 2000332, 2000028]
107
107
 
108
108
  LinuxStat::CPU.max_freq
109
109
  => [2000000, 2000000, 2000000, 2000000]
@@ -112,48 +112,48 @@ LinuxStat::CPU.model
112
112
  => "Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz"
113
113
 
114
114
  LinuxStat::CPU.stat
115
- => {0=>14.29, 1=>25.0, 2=>0.0, 3=>25.0, 4=>14.29}
115
+ => {0=>0.0, 1=>0.0, 2=>0.0, 3=>0.0, 4=>0.0}
116
116
 
117
117
  LinuxStat::CPU.total_usage
118
- => 18.75
118
+ => 0.0
119
119
 
120
120
  LinuxStat::CPU.usage
121
- => 14.29
121
+ => 20.0
122
122
 
123
123
  LinuxStat::CPU.usages
124
- => {0=>16.67, 1=>14.29, 2=>28.57, 3=>0.0, 4=>25.0}
124
+ => {0=>20.0, 1=>0.0, 2=>0.0, 3=>50.0, 4=>100.0}
125
125
 
126
126
  ```
127
127
 
128
128
  ### LinuxStat::Filesystem
129
129
  ```
130
130
  LinuxStat::Filesystem.available
131
- => 42144575488
131
+ => 33120964608
132
132
 
133
133
  LinuxStat::Filesystem.free
134
- => 42144575488
134
+ => 33120964608
135
135
 
136
136
  LinuxStat::Filesystem.stat
137
- => {:total=>119981191168, :free=>42144575488, :used=>77836615680}
137
+ => {:total=>119981191168, :free=>33120964608, :used=>86860226560}
138
138
 
139
139
  LinuxStat::Filesystem.stat_raw
140
- => {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>10289203, :block_avail_unpriv=>10289203, :inodes=>58612160, :free_inodes=>56716465, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
140
+ => {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>8086173, :block_avail_unpriv=>8086173, :inodes=>58612160, :free_inodes=>56567338, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
141
141
 
142
142
  LinuxStat::Filesystem.total
143
143
  => 119981191168
144
144
 
145
145
  LinuxStat::Filesystem.used
146
- => 77836615680
146
+ => 86860226560
147
147
 
148
148
  ```
149
149
 
150
150
  ### LinuxStat::Kernel
151
151
  ```
152
152
  LinuxStat::Kernel.build_date
153
- => 2020-10-21 01:11:20 +0000
153
+ => 2020-11-20 07:44:55 +0000
154
154
 
155
155
  LinuxStat::Kernel.build_date_string
156
- => "21 Oct 2020 01:11:20 +0000"
156
+ => "20 Nov 2020 07:44:55 +0000"
157
157
 
158
158
  LinuxStat::Kernel.build_user
159
159
  => "souravgoswami@archlinux"
@@ -168,45 +168,60 @@ LinuxStat::Kernel.compiler_version
168
168
  => "10.2.0"
169
169
 
170
170
  LinuxStat::Kernel.release
171
- => "5.9.1-xanmod1-1"
171
+ => "5.9.9-xanmod1-1"
172
172
 
173
173
  LinuxStat::Kernel.string
174
- => "Linux version 5.9.1-xanmod1-1 (souravgoswami@archlinux) (gcc (GCC) 10.2.0, GNU ld (GNU Binutils) 2.35.1) #1 SMP PREEMPT Wed, 21 Oct 2020 01:11:20 +0000"
174
+ => "Linux version 5.9.9-xanmod1-1 (souravgoswami@archlinux) (gcc (GCC) 10.2.0, GNU ld (GNU Binutils) 2.35.1) #1 SMP PREEMPT Fri, 20 Nov 2020 07:44:55 +0000"
175
175
 
176
176
  LinuxStat::Kernel.ticks
177
177
  => 100
178
178
 
179
179
  LinuxStat::Kernel.version
180
- => "5.9.1-xanmod1-1"
180
+ => "5.9.9-xanmod1-1"
181
181
 
182
182
  ```
183
183
 
184
184
  ### LinuxStat::Memory
185
185
  ```
186
186
  LinuxStat::Memory.available
187
- => 606548
187
+ => 579264
188
188
 
189
189
  LinuxStat::Memory.percent_available
190
- => 15.81
190
+ => 15.1
191
191
 
192
192
  LinuxStat::Memory.percent_used
193
- => 84.19
193
+ => 84.9
194
194
 
195
195
  LinuxStat::Memory.stat
196
- => {:total=>3836256, :used=>3229708, :available=>606548, :percent_used=>84.19, :percent_available=>15.81}
196
+ => {:total=>3836236, :used=>3256972, :available=>579264, :percent_used=>84.9, :percent_available=>15.1}
197
197
 
198
198
  LinuxStat::Memory.total
199
- => 3836256
199
+ => 3836236
200
200
 
201
201
  LinuxStat::Memory.used
202
- => 3229708
202
+ => 3256972
203
203
 
204
204
  ```
205
205
 
206
206
  ### LinuxStat::Mounts
207
207
  ```
208
+ LinuxStat::Mounts.device_stat
209
+ => {:mountpoint=>"/", :total=>119981191168, :free=>33120964608, :available=>33120964608, :used=>86860226560, :percent_used=>72.39, :percent_free=>27.61, :percent_available=>27.61}
210
+
211
+ LinuxStat::Mounts.devices_stat
212
+ => {"proc"=>{:mountpoint=>"/proc", :total=>0, :free=>0, :available=>0, :used=>0, :percent_used=>NaN, :percent_free=>NaN, :percent_available=>NaN}, "sys"=>{:mountpoint=>"/sys", :total=>0, :free=>0, :available=>0, :used=>0, :percent_used=>NaN, :percent_fre...
213
+
208
214
  LinuxStat::Mounts.list
209
- => ["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=1891804k,nr_inodes=472951,mode=755 0 0", "run /run tmpfs rw,nosuid,nodev,relatime,mode=755 0 0", "...
215
+ => ["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=1892908k,nr_inodes=473227,mode=755 0 0", "run /run tmpfs rw,nosuid,nodev,relatime,mode=755 0 0", "...
216
+
217
+ LinuxStat::Mounts.list_devices
218
+ => ["proc", "sys", "dev", "run", "/dev/sda2", "securityfs", "tmpfs", "devpts", "tmpfs", "cgroup2", "cgroup", "pstore", "none", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "systemd-1", "deb...
219
+
220
+ LinuxStat::Mounts.list_devices_mount_point
221
+ => {"proc"=>"/proc", "sys"=>"/sys", "dev"=>"/dev", "run"=>"/run", "/dev/sda2"=>"/", "securityfs"=>"/sys/kernel/security", "tmpfs"=>"/run/user/1000", "devpts"=>"/dev/pts", "cgroup2"=>"/sys/fs/cgroup/unified", "cgroup"=>"/sys/fs/cgroup/perf_event", "pstore...
222
+
223
+ LinuxStat::Mounts.mount_point
224
+ => "/"
210
225
 
211
226
  LinuxStat::Mounts.root
212
227
  => "/dev/sda2"
@@ -224,8 +239,23 @@ LinuxStat::Mounts.tmpfs
224
239
 
225
240
  ### LinuxStat::Net
226
241
  ```
242
+ LinuxStat::Net.current_usage
243
+ => {:received=>2183400.0, :transmitted=>114860.0}
244
+
227
245
  LinuxStat::Net.ipv4_private
228
- => "192.168.0.101"
246
+ => "192.168.0.102"
247
+
248
+ LinuxStat::Net.total_bytes
249
+ => {:received=>294316415, :transmitted=>45313147}
250
+
251
+ LinuxStat::Net.total_bytes_received
252
+ => 294317913
253
+
254
+ LinuxStat::Net.total_bytes_transmitted
255
+ => 45313147
256
+
257
+ LinuxStat::Net.usage
258
+ => {:received=>1643690.0, :transmitted=>146030.0}
229
259
 
230
260
  ```
231
261
 
@@ -253,29 +283,29 @@ LinuxStat::OS.os_release
253
283
  => {: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/"...
254
284
 
255
285
  LinuxStat::OS.uptime
256
- => {:hour=>9, :minute=>10, :second=>19.41}
286
+ => {:hour=>7, :minute=>44, :second=>29.92}
257
287
 
258
288
  ```
259
289
 
260
290
  ### LinuxStat::Process
261
291
  ```
262
292
  LinuxStat::Process.count
263
- => 221
293
+ => 205
264
294
 
265
295
  LinuxStat::Process.idle
266
- => [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, 168, 170, 173, 174, 182, 183, 184, 185, 186, 187, 214, 224, 268, 308, 313, 314, 330, 2357, 2358, 2359, 2360, 2361, 2362, 25094, 2...
296
+ => [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, 172, 174, 176, 178, 182, 183, 184, 185, 186, 188, 230, 271, 324, 328, 337, 14228, 14757, 14998, 14999, 15000, 15001, 15...
267
297
 
268
298
  LinuxStat::Process.list
269
- => [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, 120, 122, 131, 134,...
299
+ => [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,...
270
300
 
271
301
  LinuxStat::Process.names
272
- => {1=>"systemd", 2=>"kthreadd", 3=>"rcu_gp", 4=>"rcu_par_gp", 6=>"kworker/0:0H-kblockd", 9=>"mm_percpu_wq", 10=>"ksoftirqd/0", 11=>"rcuc/0", 12=>"rcu_preempt", 13=>"rcub/0", 14=>"migration/0", 15=>"idle_inject/0", 16=>"cpuhp/0", 17=>"cpuhp/1", 18=>"idle...
302
+ => {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/...
273
303
 
274
304
  LinuxStat::Process.running
275
- => [30804]
305
+ => [21158]
276
306
 
277
307
  LinuxStat::Process.sleeping
278
- => [1, 2, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 38, 40, 41, 42, 46, 47, 48, 50, 51, 52, 112, 114, 115, 167, 169, 188, 215, 228, 307, 309, 311, 316, 317, 319, 320, 354, 355, 356, 357, 364, 368, 370, 384, 397,...
308
+ => [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, 189, 217, 231, 302, 307, 308, 309, 310, 320, 321, 322, 323, 325, 326, 350, 353, 356, 374, 394,...
279
309
 
280
310
  LinuxStat::Process.types
281
311
  => {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...
@@ -294,22 +324,28 @@ LinuxStat::ProcessInfo.command_name
294
324
  => "ruby"
295
325
 
296
326
  LinuxStat::ProcessInfo.cpu_stat
297
- => {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>2}
327
+ => {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>1}
298
328
 
299
329
  LinuxStat::ProcessInfo.cpu_usage
300
330
  => 0.0
301
331
 
332
+ LinuxStat::ProcessInfo.gid
333
+ => {:real=>1000, :effective=>1000, :saved_set=>1000, :filesystem_uid=>1000}
334
+
302
335
  LinuxStat::ProcessInfo.last_executed_cpu
303
- => 2
336
+ => 1
304
337
 
305
338
  LinuxStat::ProcessInfo.mem_stat
306
- => {:memory=>9644, :virtual_memory=>79568, :resident_memory=>15764}
339
+ => {:memory=>10051.584, :virtual_memory=>81801.216, :resident_memory=>15986.688}
307
340
 
308
341
  LinuxStat::ProcessInfo.memory
309
- => 9644
342
+ => 10051.584
343
+
344
+ LinuxStat::ProcessInfo.owner
345
+ => "sourav"
310
346
 
311
347
  LinuxStat::ProcessInfo.resident_memory
312
- => 15764
348
+ => 15986.688
313
349
 
314
350
  LinuxStat::ProcessInfo.threads
315
351
  => 1
@@ -317,8 +353,11 @@ LinuxStat::ProcessInfo.threads
317
353
  LinuxStat::ProcessInfo.total_io
318
354
  => {:read_bytes=>0, :write_bytes=>0}
319
355
 
356
+ LinuxStat::ProcessInfo.uid
357
+ => {:real=>1000, :effective=>1000, :saved_set=>1000, :filesystem_uid=>1000}
358
+
320
359
  LinuxStat::ProcessInfo.virtual_memory
321
- => 79568
360
+ => 81801.216
322
361
 
323
362
  ```
324
363
 
@@ -328,25 +367,25 @@ LinuxStat::Swap.any?
328
367
  => true
329
368
 
330
369
  LinuxStat::Swap.available
331
- => 2256212
370
+ => 3383720
332
371
 
333
372
  LinuxStat::Swap.list
334
- => {"/dev/zram0"=>[:partition, 4194300, 1938088, -2]}
373
+ => {"/dev/zram0"=>[:partition, 4194300, 810580, -2]}
335
374
 
336
375
  LinuxStat::Swap.percent_available
337
- => 53.79
376
+ => 80.67
338
377
 
339
378
  LinuxStat::Swap.percent_used
340
- => 46.21
379
+ => 19.33
341
380
 
342
381
  LinuxStat::Swap.stat
343
- => {:total=>4194300, :used=>1938088, :available=>2256212, :percent_used=>46.21, :percent_available=>53.79}
382
+ => {:total=>4194300, :used=>810580, :available=>3383720, :percent_used=>19.33, :percent_available=>80.67}
344
383
 
345
384
  LinuxStat::Swap.total
346
385
  => 4194300
347
386
 
348
387
  LinuxStat::Swap.used
349
- => 1938088
388
+ => 810580
350
389
 
351
390
  ```
352
391
 
@@ -355,6 +394,21 @@ LinuxStat::Swap.used
355
394
  LinuxStat::Sysconf.child_max
356
395
  => 2000000
357
396
 
397
+ LinuxStat::Sysconf.get_euid
398
+ => 1000
399
+
400
+ LinuxStat::Sysconf.get_gid
401
+ => 1000
402
+
403
+ LinuxStat::Sysconf.get_login
404
+ => "sourav"
405
+
406
+ LinuxStat::Sysconf.get_uid
407
+ => 1000
408
+
409
+ LinuxStat::Sysconf.get_user
410
+ => "sourav"
411
+
358
412
  LinuxStat::Sysconf.hostname_max
359
413
  => 64
360
414
 
@@ -362,9 +416,9 @@ LinuxStat::Sysconf.login_name_max
362
416
  => 256
363
417
 
364
418
  LinuxStat::Sysconf.open_max
365
- => 8192
419
+ => 1024
366
420
 
367
- LinuxStat::Sysconf.page_size_max
421
+ LinuxStat::Sysconf.pagesize
368
422
  => 4096
369
423
 
370
424
  LinuxStat::Sysconf.posix_version
@@ -390,24 +444,30 @@ LinuxStat::Uname.nodename
390
444
  => "archlinux"
391
445
 
392
446
  LinuxStat::Uname.release
393
- => "5.9.1-xanmod1-1"
447
+ => "5.9.9-xanmod1-1"
394
448
 
395
449
  LinuxStat::Uname.sysname
396
450
  => "Linux"
397
451
 
398
452
  LinuxStat::Uname.version
399
- => "#1 SMP PREEMPT Wed, 21 Oct 2020 01:11:20 +0000"
453
+ => "#1 SMP PREEMPT Fri, 20 Nov 2020 07:44:55 +0000"
400
454
 
401
455
  ```
402
456
 
403
457
  ### LinuxStat::User
404
458
  ```
459
+ LinuxStat::User.get_current_user
460
+ => "sourav"
461
+
405
462
  LinuxStat::User.get_euid
406
463
  => 1000
407
464
 
408
465
  LinuxStat::User.get_gid
409
466
  => 1000
410
467
 
468
+ LinuxStat::User.get_login
469
+ => "sourav"
470
+
411
471
  LinuxStat::User.get_uid
412
472
  => 1000
413
473
 
@@ -454,7 +514,71 @@ LinuxStat::User.usernames_by_uid
454
514
 
455
515
  ---
456
516
 
457
- ## Note 1: Filesystem
517
+ ## Note 1: CPU usage, and Net usage
518
+ To calculate the current usage, we need to get two usages at a given interval, and subtract the 2nd from the 1st.
519
+ For example, if the current download (`LinuxStat::Net.total_bytes_received`) is 1000 bytes, and if 0.1 seconds ago, it was 100 bytes, that means 900 bytes was received in 0.1 seconds.
520
+ That means the current speed is 9000 bytes/s or 9 kB/s.
521
+
522
+ Without the polling, it's not really possible to calculate the current usage. Although the total usage can be calculated.
523
+ A system monitor does that, too...
524
+
525
+ Thus these methods requires a polling interval:
526
+
527
+ 1. LinuxStat::CPU.stat, usage, total_usage, usage.
528
+ 2. LinuxStat::ProcessInfo.cpu_usage, cpu_stat.
529
+ 3. LinuxStat::Net.usage, current_usage.
530
+
531
+ They sleep for a given interval and then differentiate between the data.
532
+
533
+ For more info look at the ri documentation for the above methods.
534
+
535
+ These methods can slow down your application a bit unless you implement them in a thread.
536
+
537
+ Other methods doesn't have the sleep implemented, and they just works under a millisecond.
538
+
539
+ For example:
540
+
541
+ ```
542
+ LinuxStat::CPU.stat(0.1)
543
+ => {0=>7.69, 1=>0.0, 2=>0.0, 3=>18.18, 4=>10.0}
544
+ ```
545
+ This will sleep for 0.1 seconds. To be reliable, use a time like 0.05 seconds or so.
546
+
547
+ If you want to build a system monitor and don't want to wait, you have to do something like this:
548
+
549
+ ```
550
+ #!/usr/bin/ruby
551
+ require 'linux_stat'
552
+
553
+ usages = []
554
+ thread = Thread.new { }
555
+ counter = 0
556
+
557
+ while true
558
+ thread = Thread.new { usages = LinuxStat::CPU.usages(0.5).values } unless thread.alive?
559
+
560
+ # clears the screen and prints the info
561
+ puts "\e[2J\e[H\e[3J"\
562
+ "#{counter += 1}\n"\
563
+ "\e[1;33mTotal CPU Usage:\e[0m #{usages[0]}%\n"\
564
+ "#{usages[1..-1].to_a.map.with_index { |x, i| "\e[1;33mCore #{i}\e[0m => #{x}%\n" }.join}"\
565
+ "Total Download: #{LinuxStat::PrettifyBytes.convert_decimal LinuxStat::Net.total_bytes_received}\n"\
566
+ "Total Upload: #{LinuxStat::PrettifyBytes.convert_decimal LinuxStat::Net.total_bytes_transmitted}"
567
+ end
568
+ ```
569
+
570
+ This will not wait in every loop for 0.5 seconds, but it will not update the cpu usage in every loop either.
571
+ So what you will be seeing in the CPU usage in every 0.5 seconds interval.
572
+
573
+ You will also see the counter increases like crazy. Which means it's not getting waited for 0.5 seconds.
574
+
575
+ But the other methods doesn't have this delay, thus in this example,
576
+ you will be able see the "Total Download" and "Total Upload" in real time,
577
+ well as soon as the Linux kernel updates the data and ruby executes the loop.
578
+
579
+ Just run the linuxstat.rb command to test what method takes what time measured in microseconds.
580
+
581
+ ## Note 2: Filesystem
458
582
 
459
583
  Filesystem can take arguments. By default it's '/' or the root of the system...
460
584
 
@@ -476,7 +600,7 @@ $ irb
476
600
  irb(main):001:0> require 'linux_stat'
477
601
  => true
478
602
 
479
- irb(main):002:0> LinuxStat::Mounts.list.find { |x| x.include?('/run/media/sourav') }.split[1]
603
+ irb(main):002:0> LinuxStat::Mounts.mount_point('/dev/sdb1')
480
604
  => "/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f"
481
605
 
482
606
  irb(main):003:0> thumbdrive = _
@@ -489,7 +613,7 @@ irb(main):005:0> LinuxStat::Filesystem.total(thumbdrive).fdiv(1024 ** 3).to_s <<
489
613
  => "29.305004119873047 GiB"
490
614
  ```
491
615
 
492
- ## Note 2: ProcessInfo
616
+ ## Note 3: ProcessInfo
493
617
 
494
618
  All the methods LinuxStat::ProcessInfo can take an argument containing the Process ID of a process.
495
619
  By default it's $$ or the PID of the current process, ruby, itself.
@@ -541,7 +665,7 @@ irb(main):002:0> LinuxStat::ProcessInfo.memory(LinuxStat::Process.names.find { |
541
665
  => "467.51 MiB"
542
666
  ```
543
667
 
544
- ## Note 3: FS
668
+ ## Note 4: FS
545
669
 
546
670
  LinuxStat::FS module gives you the raw info in Hash collected from statvfs.
547
671
 
@@ -564,9 +688,9 @@ irb(main):003:0> t = Time.now ; puts LinuxStat::FS.stat('/') ; Time.now - t
564
688
  => 5.0468e-05
565
689
  ```
566
690
 
567
- To learn more about them, just run ri and the method name. To see all available methods:
691
+ To learn more about them, just run ri and the method name. To see all available methods.
568
692
 
569
- ## Note 4: User
693
+ ## Note 5: User
570
694
  Most of the LinuxStat::User supports arguments.
571
695
 
572
696
  For example, to get a user's home by the username:
@@ -612,7 +736,7 @@ irb(main):006:0> LinuxStat::User.home_by_gid(0)
612
736
  Or to get the UID/GID by username:
613
737
 
614
738
  ```
615
- $ irb
739
+ $ irb
616
740
  irb(main):001:0> require 'linux_stat'
617
741
  => true
618
742
 
@@ -629,6 +753,119 @@ irb(main):005:0> LinuxStat::User.gid_by_username('InvalidUser')
629
753
  => nil
630
754
  ```
631
755
 
756
+ Or to get the current user (in docker for example):
757
+
758
+ ```
759
+ $ irb
760
+ irb(main):001:0> require 'linux_stat'
761
+ => true
762
+
763
+ irb(main):002:0> LinuxStat::User.get_current_user
764
+ => "x"
765
+
766
+ irb(main):003:0> LinuxStat::User.get_user
767
+ => "x"
768
+
769
+ irb(main):004:0> LinuxStat::User.get_login
770
+ => ""
771
+ ```
772
+
773
+ Right, the get_login() can return an empty string. But LinuxStat::User.get_user also aliased as LinuxStat::User.get_current_user shouldn't return an empty string under most circumstances.
774
+
775
+ ## Note 6: PrettifyBytes
776
+ Often times we need to work with KB, MB GB, TB, or KiB, MiB, GiB, TiB, etc.
777
+ And we need some work to convert bytes to those units.
778
+ Because LinuxStat provides a lot of data in bytes, and kilobytes, it's quite tedious to convert them all the time.
779
+ To avoid such duplication, it comes with a PrettifyBytes module.
780
+
781
+ For example, to convert bytes to decimal suffixes:
782
+
783
+ ```
784
+ $irb
785
+ irb(main):001:0> require 'linux_stat'
786
+ => true
787
+
788
+ irb(main):002:0> LinuxStat::PrettifyBytes.convert_decimal(1000)
789
+ => "1.00 kilobyte"
790
+
791
+ irb(main):003:0> LinuxStat::PrettifyBytes.convert_decimal(10000)
792
+ => "10.00 kilobytes"
793
+
794
+ irb(main):004:0> LinuxStat::PrettifyBytes.convert_decimal(100000)
795
+ => "100.00 kilobytes"
796
+
797
+ irb(main):005:0> LinuxStat::PrettifyBytes.convert_decimal(10 ** 13)
798
+ => "10.00 terabytes"
799
+ ```
800
+
801
+ To convert bytes to binary suffixes:
802
+
803
+ ```
804
+ irb(main):006:0> LinuxStat::PrettifyBytes.convert_binary(1000)
805
+ => "1000.00 bytes"
806
+
807
+ irb(main):007:0> LinuxStat::PrettifyBytes.convert_binary(10000)
808
+ => "9.77 kibibytes"
809
+
810
+ irb(main):008:0> LinuxStat::PrettifyBytes.convert_binary(100000)
811
+ => "97.66 kibibytes"
812
+
813
+ irb(main):009:0> LinuxStat::PrettifyBytes.convert_binary(10 ** 13)
814
+ => "9.09 tebibytes"
815
+ ```
816
+
817
+ To convert them to short Metric decimal suffixes:
818
+
819
+ ```
820
+ irb(main):010:0> LinuxStat::PrettifyBytes.convert_short_decimal(1000)
821
+ => "1.00 kB"
822
+
823
+ irb(main):011:0> LinuxStat::PrettifyBytes.convert_short_decimal(10000)
824
+ => "10.00 kB"
825
+
826
+ irb(main):012:0> LinuxStat::PrettifyBytes.convert_short_decimal(100000)
827
+ => "100.00 kB"
828
+
829
+ irb(main):013:0> LinuxStat::PrettifyBytes.convert_short_decimal(10 ** 13)
830
+ => "10.00 TB"
831
+ ```
832
+
833
+ To convert them to short IEC binary suffixes:
834
+
835
+ ```
836
+ irb(main):014:0> LinuxStat::PrettifyBytes.convert_short_binary(1000)
837
+ => "1000 B"
838
+
839
+ irb(main):015:0> LinuxStat::PrettifyBytes.convert_short_binary(10000)
840
+ => "9.77 KiB"
841
+
842
+ irb(main):016:0> LinuxStat::PrettifyBytes.convert_short_binary(100000)
843
+ => "97.66 KiB"
844
+
845
+ irb(main):017:0> LinuxStat::PrettifyBytes.convert_short_binary(10 ** 13)
846
+ => "9.09 TiB"
847
+ ```
848
+
849
+ It can support values upto hundreds of yottabytes and yobibytes, or yb and yib. You can also do stuff like:
850
+
851
+ ```
852
+ $ irb
853
+ irb(main):001:0> require 'linux_stat'
854
+ => true
855
+
856
+ irb(main):002:0> LinuxStat::PrettifyBytes.convert_short_decimal(LinuxStat::Mounts.device_stat('/dev/sdb1')[:total])
857
+ => "31.47 GB"
858
+
859
+ irb(main):003:0> LinuxStat::PrettifyBytes.convert_short_binary(LinuxStat::Mounts.device_stat('/dev/sdb1')[:total])
860
+ => "29.31 GiB"
861
+
862
+ irb(main):004:0> LinuxStat::PrettifyBytes.convert_short_binary(LinuxStat::Mounts.device_stat('/dev/sdb1')[:used])
863
+ => "26.80 GiB"
864
+
865
+ irb(main):005:0> LinuxStat::PrettifyBytes.convert_short_binary(LinuxStat::Mounts.device_stat('/dev/sdb1')[:available])
866
+ => "2.51 GiB"
867
+ ```
868
+
632
869
  Read the ri documentation for more info.
633
870
 
634
871
  ---