linux_stat 0.6.1 → 0.7.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 +174 -57
- data/exe/linuxstat.rb +1 -1
- data/ext/fs_stat/extconf.rb +5 -1
- data/ext/fs_stat/fs_stat.c +0 -1
- data/ext/sysconf/extconf.rb +5 -1
- data/ext/sysconf/sysconf.c +2 -3
- data/ext/utsname/extconf.rb +5 -1
- data/ext/utsname/utsname.c +0 -1
- data/lib/linux_stat.rb +23 -2
- data/lib/linux_stat/battery.rb +20 -1
- data/lib/linux_stat/bios.rb +8 -0
- data/lib/linux_stat/cpu.rb +20 -3
- data/lib/linux_stat/filesystem.rb +31 -10
- data/lib/linux_stat/kernel.rb +36 -2
- data/lib/linux_stat/memory.rb +7 -0
- data/lib/linux_stat/mounts.rb +24 -0
- data/lib/linux_stat/net.rb +119 -1
- data/lib/linux_stat/os.rb +17 -5
- data/lib/linux_stat/prettify_bytes.rb +47 -12
- data/lib/linux_stat/process.rb +10 -0
- data/lib/linux_stat/process_info.rb +251 -92
- data/lib/linux_stat/swap.rb +15 -1
- data/lib/linux_stat/user.rb +64 -11
- data/lib/linux_stat/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a54eca551d77c1284989f5f8edeea8e363d32228e0ea5a2d28b2df9eefeedf53
|
4
|
+
data.tar.gz: 407820d169fe900f639cd43730bd03eb66c43a2ec0bcd5c3d1399af741418c4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff513a54d80eb9e9c101dd2fb92ff874c3e644da5752e8f7b2c9bdf535d56a1d942e64ed3b083eb051dfcb62be02cb521b51f0b1e08baef7f3ec0a261bacd1f1
|
7
|
+
data.tar.gz: ef5b77eb019ab19976a5799a276f22a6967603c6d03b0219a6314e74e00a275a288102f0ab6d753e38867b96d6621e8673a9b0fa733d772c2d97118749ada933
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@ It only works for Linux, and detecting the OS is upto the user of this gem.
|
|
10
10
|
---
|
11
11
|
|
12
12
|
## Dependencies:
|
13
|
-
+ You need to have the C
|
13
|
+
+ You need to have the C compiler installed to be able to compile the C extensions.
|
14
14
|
On Arch Linux:
|
15
15
|
```
|
16
16
|
# pacman -S gcc make
|
@@ -103,7 +103,7 @@ LinuxStat::CPU.count
|
|
103
103
|
=> 4
|
104
104
|
|
105
105
|
LinuxStat::CPU.cur_freq
|
106
|
-
=> [
|
106
|
+
=> [2000063, 2000108, 2000332, 2000028]
|
107
107
|
|
108
108
|
LinuxStat::CPU.max_freq
|
109
109
|
=> [2000000, 2000000, 2000000, 2000000]
|
@@ -112,38 +112,38 @@ LinuxStat::CPU.model
|
|
112
112
|
=> "Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz"
|
113
113
|
|
114
114
|
LinuxStat::CPU.stat
|
115
|
-
=> {0=>
|
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
118
|
=> 0.0
|
119
119
|
|
120
120
|
LinuxStat::CPU.usage
|
121
|
-
=>
|
121
|
+
=> 20.0
|
122
122
|
|
123
123
|
LinuxStat::CPU.usages
|
124
|
-
=> {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
|
-
=>
|
131
|
+
=> 33120964608
|
132
132
|
|
133
133
|
LinuxStat::Filesystem.free
|
134
|
-
=>
|
134
|
+
=> 33120964608
|
135
135
|
|
136
136
|
LinuxStat::Filesystem.stat
|
137
|
-
=> {:total=>119981191168, :free=>
|
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=>
|
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
|
-
=>
|
146
|
+
=> 86860226560
|
147
147
|
|
148
148
|
```
|
149
149
|
|
@@ -184,35 +184,35 @@ LinuxStat::Kernel.version
|
|
184
184
|
### LinuxStat::Memory
|
185
185
|
```
|
186
186
|
LinuxStat::Memory.available
|
187
|
-
=>
|
187
|
+
=> 579264
|
188
188
|
|
189
189
|
LinuxStat::Memory.percent_available
|
190
|
-
=>
|
190
|
+
=> 15.1
|
191
191
|
|
192
192
|
LinuxStat::Memory.percent_used
|
193
|
-
=>
|
193
|
+
=> 84.9
|
194
194
|
|
195
195
|
LinuxStat::Memory.stat
|
196
|
-
=> {:total=>
|
196
|
+
=> {:total=>3836236, :used=>3256972, :available=>579264, :percent_used=>84.9, :percent_available=>15.1}
|
197
197
|
|
198
198
|
LinuxStat::Memory.total
|
199
|
-
=>
|
199
|
+
=> 3836236
|
200
200
|
|
201
201
|
LinuxStat::Memory.used
|
202
|
-
=>
|
202
|
+
=> 3256972
|
203
203
|
|
204
204
|
```
|
205
205
|
|
206
206
|
### LinuxStat::Mounts
|
207
207
|
```
|
208
208
|
LinuxStat::Mounts.device_stat
|
209
|
-
=> {:mountpoint=>"/", :total=>119981191168, :free=>
|
209
|
+
=> {:mountpoint=>"/", :total=>119981191168, :free=>33120964608, :available=>33120964608, :used=>86860226560, :percent_used=>72.39, :percent_free=>27.61, :percent_available=>27.61}
|
210
210
|
|
211
211
|
LinuxStat::Mounts.devices_stat
|
212
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
213
|
|
214
214
|
LinuxStat::Mounts.list
|
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=
|
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
216
|
|
217
217
|
LinuxStat::Mounts.list_devices
|
218
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...
|
@@ -239,9 +239,24 @@ LinuxStat::Mounts.tmpfs
|
|
239
239
|
|
240
240
|
### LinuxStat::Net
|
241
241
|
```
|
242
|
+
LinuxStat::Net.current_usage
|
243
|
+
=> {:received=>2183400.0, :transmitted=>114860.0}
|
244
|
+
|
242
245
|
LinuxStat::Net.ipv4_private
|
243
246
|
=> "192.168.0.102"
|
244
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}
|
259
|
+
|
245
260
|
```
|
246
261
|
|
247
262
|
### LinuxStat::OS
|
@@ -268,29 +283,29 @@ LinuxStat::OS.os_release
|
|
268
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/"...
|
269
284
|
|
270
285
|
LinuxStat::OS.uptime
|
271
|
-
=> {:hour=>
|
286
|
+
=> {:hour=>7, :minute=>44, :second=>29.92}
|
272
287
|
|
273
288
|
```
|
274
289
|
|
275
290
|
### LinuxStat::Process
|
276
291
|
```
|
277
292
|
LinuxStat::Process.count
|
278
|
-
=>
|
293
|
+
=> 205
|
279
294
|
|
280
295
|
LinuxStat::Process.idle
|
281
|
-
=> [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118,
|
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...
|
282
297
|
|
283
298
|
LinuxStat::Process.list
|
284
|
-
=> [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,
|
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,...
|
285
300
|
|
286
301
|
LinuxStat::Process.names
|
287
|
-
=> {1=>"systemd", 2=>"kthreadd", 3=>"rcu_gp", 4=>"rcu_par_gp", 6=>"kworker/0:0H
|
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/...
|
288
303
|
|
289
304
|
LinuxStat::Process.running
|
290
|
-
=> [
|
305
|
+
=> [21158]
|
291
306
|
|
292
307
|
LinuxStat::Process.sleeping
|
293
|
-
=> [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,
|
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,...
|
294
309
|
|
295
310
|
LinuxStat::Process.types
|
296
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...
|
@@ -309,7 +324,7 @@ LinuxStat::ProcessInfo.command_name
|
|
309
324
|
=> "ruby"
|
310
325
|
|
311
326
|
LinuxStat::ProcessInfo.cpu_stat
|
312
|
-
=> {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>
|
327
|
+
=> {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>1}
|
313
328
|
|
314
329
|
LinuxStat::ProcessInfo.cpu_usage
|
315
330
|
=> 0.0
|
@@ -318,19 +333,19 @@ LinuxStat::ProcessInfo.gid
|
|
318
333
|
=> {:real=>1000, :effective=>1000, :saved_set=>1000, :filesystem_uid=>1000}
|
319
334
|
|
320
335
|
LinuxStat::ProcessInfo.last_executed_cpu
|
321
|
-
=>
|
336
|
+
=> 1
|
322
337
|
|
323
338
|
LinuxStat::ProcessInfo.mem_stat
|
324
|
-
=> {:memory=>
|
339
|
+
=> {:memory=>10051.584, :virtual_memory=>81801.216, :resident_memory=>15986.688}
|
325
340
|
|
326
341
|
LinuxStat::ProcessInfo.memory
|
327
|
-
=>
|
342
|
+
=> 10051.584
|
328
343
|
|
329
344
|
LinuxStat::ProcessInfo.owner
|
330
345
|
=> "sourav"
|
331
346
|
|
332
347
|
LinuxStat::ProcessInfo.resident_memory
|
333
|
-
=>
|
348
|
+
=> 15986.688
|
334
349
|
|
335
350
|
LinuxStat::ProcessInfo.threads
|
336
351
|
=> 1
|
@@ -342,7 +357,7 @@ LinuxStat::ProcessInfo.uid
|
|
342
357
|
=> {:real=>1000, :effective=>1000, :saved_set=>1000, :filesystem_uid=>1000}
|
343
358
|
|
344
359
|
LinuxStat::ProcessInfo.virtual_memory
|
345
|
-
=>
|
360
|
+
=> 81801.216
|
346
361
|
|
347
362
|
```
|
348
363
|
|
@@ -352,25 +367,25 @@ LinuxStat::Swap.any?
|
|
352
367
|
=> true
|
353
368
|
|
354
369
|
LinuxStat::Swap.available
|
355
|
-
=>
|
370
|
+
=> 3383720
|
356
371
|
|
357
372
|
LinuxStat::Swap.list
|
358
|
-
=> {"/dev/zram0"=>[:partition, 4194300,
|
373
|
+
=> {"/dev/zram0"=>[:partition, 4194300, 810580, -2]}
|
359
374
|
|
360
375
|
LinuxStat::Swap.percent_available
|
361
|
-
=>
|
376
|
+
=> 80.67
|
362
377
|
|
363
378
|
LinuxStat::Swap.percent_used
|
364
|
-
=>
|
379
|
+
=> 19.33
|
365
380
|
|
366
381
|
LinuxStat::Swap.stat
|
367
|
-
=> {:total=>4194300, :used=>
|
382
|
+
=> {:total=>4194300, :used=>810580, :available=>3383720, :percent_used=>19.33, :percent_available=>80.67}
|
368
383
|
|
369
384
|
LinuxStat::Swap.total
|
370
385
|
=> 4194300
|
371
386
|
|
372
387
|
LinuxStat::Swap.used
|
373
|
-
=>
|
388
|
+
=> 810580
|
374
389
|
|
375
390
|
```
|
376
391
|
|
@@ -403,7 +418,7 @@ LinuxStat::Sysconf.login_name_max
|
|
403
418
|
LinuxStat::Sysconf.open_max
|
404
419
|
=> 1024
|
405
420
|
|
406
|
-
LinuxStat::Sysconf.
|
421
|
+
LinuxStat::Sysconf.pagesize
|
407
422
|
=> 4096
|
408
423
|
|
409
424
|
LinuxStat::Sysconf.posix_version
|
@@ -499,7 +514,71 @@ LinuxStat::User.usernames_by_uid
|
|
499
514
|
|
500
515
|
---
|
501
516
|
|
502
|
-
## Note 1:
|
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
|
503
582
|
|
504
583
|
Filesystem can take arguments. By default it's '/' or the root of the system...
|
505
584
|
|
@@ -521,7 +600,7 @@ $ irb
|
|
521
600
|
irb(main):001:0> require 'linux_stat'
|
522
601
|
=> true
|
523
602
|
|
524
|
-
irb(main):002:0> LinuxStat::Mounts.
|
603
|
+
irb(main):002:0> LinuxStat::Mounts.mount_point('/dev/sdb1')
|
525
604
|
=> "/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f"
|
526
605
|
|
527
606
|
irb(main):003:0> thumbdrive = _
|
@@ -534,7 +613,7 @@ irb(main):005:0> LinuxStat::Filesystem.total(thumbdrive).fdiv(1024 ** 3).to_s <<
|
|
534
613
|
=> "29.305004119873047 GiB"
|
535
614
|
```
|
536
615
|
|
537
|
-
## Note
|
616
|
+
## Note 3: ProcessInfo
|
538
617
|
|
539
618
|
All the methods LinuxStat::ProcessInfo can take an argument containing the Process ID of a process.
|
540
619
|
By default it's $$ or the PID of the current process, ruby, itself.
|
@@ -586,7 +665,7 @@ irb(main):002:0> LinuxStat::ProcessInfo.memory(LinuxStat::Process.names.find { |
|
|
586
665
|
=> "467.51 MiB"
|
587
666
|
```
|
588
667
|
|
589
|
-
## Note
|
668
|
+
## Note 4: FS
|
590
669
|
|
591
670
|
LinuxStat::FS module gives you the raw info in Hash collected from statvfs.
|
592
671
|
|
@@ -611,7 +690,7 @@ irb(main):003:0> t = Time.now ; puts LinuxStat::FS.stat('/') ; Time.now - t
|
|
611
690
|
|
612
691
|
To learn more about them, just run ri and the method name. To see all available methods.
|
613
692
|
|
614
|
-
## Note
|
693
|
+
## Note 5: User
|
615
694
|
Most of the LinuxStat::User supports arguments.
|
616
695
|
|
617
696
|
For example, to get a user's home by the username:
|
@@ -674,7 +753,26 @@ irb(main):005:0> LinuxStat::User.gid_by_username('InvalidUser')
|
|
674
753
|
=> nil
|
675
754
|
```
|
676
755
|
|
677
|
-
|
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
|
678
776
|
Often times we need to work with KB, MB GB, TB, or KiB, MiB, GiB, TiB, etc.
|
679
777
|
And we need some work to convert bytes to those units.
|
680
778
|
Because LinuxStat provides a lot of data in bytes, and kilobytes, it's quite tedious to convert them all the time.
|
@@ -683,6 +781,7 @@ To avoid such duplication, it comes with a PrettifyBytes module.
|
|
683
781
|
For example, to convert bytes to decimal suffixes:
|
684
782
|
|
685
783
|
```
|
784
|
+
$irb
|
686
785
|
irb(main):001:0> require 'linux_stat'
|
687
786
|
=> true
|
688
787
|
|
@@ -702,52 +801,70 @@ irb(main):005:0> LinuxStat::PrettifyBytes.convert_decimal(10 ** 13)
|
|
702
801
|
To convert bytes to binary suffixes:
|
703
802
|
|
704
803
|
```
|
705
|
-
irb(main):
|
804
|
+
irb(main):006:0> LinuxStat::PrettifyBytes.convert_binary(1000)
|
706
805
|
=> "1000.00 bytes"
|
707
806
|
|
708
|
-
irb(main):
|
807
|
+
irb(main):007:0> LinuxStat::PrettifyBytes.convert_binary(10000)
|
709
808
|
=> "9.77 kibibytes"
|
710
809
|
|
711
|
-
irb(main):
|
810
|
+
irb(main):008:0> LinuxStat::PrettifyBytes.convert_binary(100000)
|
712
811
|
=> "97.66 kibibytes"
|
713
812
|
|
714
|
-
irb(main):
|
813
|
+
irb(main):009:0> LinuxStat::PrettifyBytes.convert_binary(10 ** 13)
|
715
814
|
=> "9.09 tebibytes"
|
716
815
|
```
|
717
816
|
|
718
817
|
To convert them to short Metric decimal suffixes:
|
719
818
|
|
720
819
|
```
|
721
|
-
irb(main):
|
820
|
+
irb(main):010:0> LinuxStat::PrettifyBytes.convert_short_decimal(1000)
|
722
821
|
=> "1.00 kB"
|
723
822
|
|
724
|
-
irb(main):
|
823
|
+
irb(main):011:0> LinuxStat::PrettifyBytes.convert_short_decimal(10000)
|
725
824
|
=> "10.00 kB"
|
726
825
|
|
727
|
-
irb(main):
|
826
|
+
irb(main):012:0> LinuxStat::PrettifyBytes.convert_short_decimal(100000)
|
728
827
|
=> "100.00 kB"
|
729
828
|
|
730
|
-
irb(main):
|
829
|
+
irb(main):013:0> LinuxStat::PrettifyBytes.convert_short_decimal(10 ** 13)
|
731
830
|
=> "10.00 TB"
|
732
831
|
```
|
733
832
|
|
734
833
|
To convert them to short IEC binary suffixes:
|
735
834
|
|
736
835
|
```
|
737
|
-
irb(main):
|
836
|
+
irb(main):014:0> LinuxStat::PrettifyBytes.convert_short_binary(1000)
|
738
837
|
=> "1000 B"
|
739
838
|
|
740
|
-
irb(main):
|
839
|
+
irb(main):015:0> LinuxStat::PrettifyBytes.convert_short_binary(10000)
|
741
840
|
=> "9.77 KiB"
|
742
841
|
|
743
|
-
irb(main):
|
842
|
+
irb(main):016:0> LinuxStat::PrettifyBytes.convert_short_binary(100000)
|
744
843
|
=> "97.66 KiB"
|
745
844
|
|
746
|
-
irb(main):
|
845
|
+
irb(main):017:0> LinuxStat::PrettifyBytes.convert_short_binary(10 ** 13)
|
747
846
|
=> "9.09 TiB"
|
748
847
|
```
|
749
848
|
|
750
|
-
It can support values upto hundreds of yottabytes and yobibytes, or yb and yib.
|
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
|
+
```
|
751
868
|
|
752
869
|
Read the ri documentation for more info.
|
753
870
|
|