linux_stat 0.4.0 → 0.4.2
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 +132 -1
- data/bin/linuxstat.rb +43 -6
- data/lib/linux_stat/version.rb +1 -1
- metadata +4 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62a54b165b53e0fe6fddad20ab6b39b8a0d57e1a02186f995fcf0341cc69aae0
|
4
|
+
data.tar.gz: 8bc58cc3c8b72e117892cd8f987c924337d2ab10995d14b3fb7735064e4b3685
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5490c31315132793840e3e849996f0545b08b3b416eadef3ba6979dcbad89d31329b69dad403295f8cc7e17a677f6d8df032c42706162b251b6ed921a3d11e4
|
7
|
+
data.tar.gz: 0100faa162b904042e8143b8dec85b45a3cb82543c693514049df17834890b12034cb1561d8c69ba606e9ebc81fba1550b255ad6d17d70a552011eb9aaf5300f
|
data/README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# LinuxStat
|
2
2
|

|
3
3
|
|
4
|
-
For reading the HTML version, visit [linux_stat](https://souravgoswami.github.io/linux_stat/).
|
4
|
+
For reading the eyecandy HTML version, visit [linux_stat](https://souravgoswami.github.io/linux_stat/).
|
5
5
|
|
6
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.
|
7
7
|
|
8
8
|
It only works for Linux, and detecting the OS is upto the user of this gem.
|
9
9
|
|
10
|
+
---
|
11
|
+
|
10
12
|
## Dependencies:
|
11
13
|
+ You need to have the C compile to be able to compile the C extension.
|
12
14
|
On Arch Linux:
|
@@ -26,6 +28,8 @@ On Debian based systems:
|
|
26
28
|
|
27
29
|
+ Once your are done, and you can compile the C file, you can follow the installation!
|
28
30
|
|
31
|
+
---
|
32
|
+
|
29
33
|
## Installation
|
30
34
|
|
31
35
|
Add this line to your application's Gemfile:
|
@@ -395,6 +399,121 @@ LinuxStat::Uname.version
|
|
395
399
|
=> "#1 SMP PREEMPT Wed, 21 Oct 2020 01:11:20 +0000"
|
396
400
|
|
397
401
|
```
|
402
|
+
---
|
403
|
+
|
404
|
+
## Note 1: Filesystem
|
405
|
+
|
406
|
+
Filesystem can take arguments. By default it's '/' or the root of the system...
|
407
|
+
|
408
|
+
But for the sake of example, to get the free disk space of /, you do:
|
409
|
+
|
410
|
+
```
|
411
|
+
$ irb
|
412
|
+
irb(main):001:0> require 'linux_stat'
|
413
|
+
=> true
|
414
|
+
|
415
|
+
irb(main):002:0> LinuxStat::Filesystem.free('/').fdiv(1024 ** 3).to_s << " GiB"
|
416
|
+
=> "35.666873931884766 GiB"
|
417
|
+
```
|
418
|
+
|
419
|
+
To see the free and total space of a thumbdrive:
|
420
|
+
|
421
|
+
```
|
422
|
+
$ irb
|
423
|
+
irb(main):001:0> require 'linux_stat'
|
424
|
+
=> true
|
425
|
+
|
426
|
+
irb(main):002:0> LinuxStat::Mounts.list.find { |x| x.include?('/run/media/sourav') }.split[1]
|
427
|
+
=> "/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f"
|
428
|
+
|
429
|
+
irb(main):003:0> thumbdrive = _
|
430
|
+
=> "/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f"
|
431
|
+
|
432
|
+
irb(main):004:0> LinuxStat::Filesystem.free(thumbdrive).fdiv(1024 ** 3).to_s << " GiB"
|
433
|
+
=> "2.504791259765625 GiB"
|
434
|
+
|
435
|
+
irb(main):005:0> LinuxStat::Filesystem.total(thumbdrive).fdiv(1024 ** 3).to_s << " GiB"
|
436
|
+
=> "29.305004119873047 GiB"
|
437
|
+
```
|
438
|
+
|
439
|
+
## Note 2: ProcessInfo
|
440
|
+
|
441
|
+
All the methods LinuxStat::ProcessInfo can take an argument containing the Process ID of a process.
|
442
|
+
By default it's $$ or the PID of the current process, ruby, itself.
|
443
|
+
|
444
|
+
Example:
|
445
|
+
Say you want to see how much CPU Firefox is consuming, for that you have to do the following (firefox can create a lot of child process though):
|
446
|
+
|
447
|
+
1. Get the PID of Firefox:
|
448
|
+
```
|
449
|
+
LinuxStat::Process.names.find { |x| x[1].include? 'firefox' }[0]
|
450
|
+
=> 770 # but this differs all the time
|
451
|
+
```
|
452
|
+
|
453
|
+
2. Get the CPU usage:
|
454
|
+
```
|
455
|
+
$ irb
|
456
|
+
irb(main):001:0> require 'linux_stat'
|
457
|
+
=> true
|
458
|
+
|
459
|
+
irb(main):002:0> pid = LinuxStat::Process.names.find { |x| x[1].include? 'firefox' }[0]
|
460
|
+
=> 770
|
461
|
+
|
462
|
+
irb(main):003:0> LinuxStat::ProcessInfo.cpu_usage(pid: pid)
|
463
|
+
=> 0.0
|
464
|
+
|
465
|
+
irb(main):004:0> LinuxStat::ProcessInfo.cpu_usage(pid: pid)
|
466
|
+
=> 15.0
|
467
|
+
```
|
468
|
+
|
469
|
+
To get the memory usage of Firefox (for example):
|
470
|
+
|
471
|
+
```
|
472
|
+
$ irb
|
473
|
+
irb(main):001:0> require 'linux_stat'
|
474
|
+
=> true
|
475
|
+
|
476
|
+
irb(main):002:0> LinuxStat::ProcessInfo.mem_stat(LinuxStat::Process.names.find { |x| x[1].include? 'firefox'.freeze }[0])
|
477
|
+
=> {:memory=>468472, :virtual_memory=>4754080, :resident_memory=>814388}
|
478
|
+
```
|
479
|
+
|
480
|
+
To get ONLY the memory usage in MiB:
|
481
|
+
|
482
|
+
```
|
483
|
+
$ irb
|
484
|
+
irb(main):001:0> require 'linux_stat'
|
485
|
+
=> true
|
486
|
+
|
487
|
+
irb(main):002:0> LinuxStat::ProcessInfo.memory(LinuxStat::Process.names.find { |x| x[1].include? 'firefox'.freeze }[0]).fdiv(1024).round(2).to_s << " MiB"
|
488
|
+
=> "467.51 MiB"
|
489
|
+
```
|
490
|
+
|
491
|
+
## Note 3: FS
|
492
|
+
|
493
|
+
LinuxStat::FS module gives you the raw info in Hash collected from statvfs.
|
494
|
+
|
495
|
+
It's not documented above because it's not suggested to run this directly. But it shouldn't cause any issue. `LinuxStat::Filesystem.stat_raw(fs = '/')` does that automatically.
|
496
|
+
|
497
|
+
It always requires an argument, and it's very fast. It directly calls the C API without any intermediate Ruby code.
|
498
|
+
|
499
|
+
For example, to get the info about '/' or root:
|
500
|
+
|
501
|
+
```
|
502
|
+
$ irb
|
503
|
+
irb(main):001:0> require 'linux_stat'
|
504
|
+
=> true
|
505
|
+
|
506
|
+
irb(main):002:0> LinuxStat::FS.stat('/')
|
507
|
+
=> {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>9349843, :block_avail_unpriv=>9349843, :inodes=>58612160, :free_inodes=>56708247, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
|
508
|
+
|
509
|
+
irb(main):003:0> t = Time.now ; puts LinuxStat::FS.stat('/') ; Time.now - t
|
510
|
+
{:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>9349843, :block_avail_unpriv=>9349843, :inodes=>58612160, :free_inodes=>56708247, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
|
511
|
+
=> 5.0468e-05
|
512
|
+
```
|
513
|
+
|
514
|
+
To learn more about them, just run ri and the method name. To see all available methods:
|
515
|
+
|
516
|
+
---
|
398
517
|
|
399
518
|
## Return Types
|
400
519
|
+ 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.
|
@@ -409,6 +528,8 @@ LinuxStat::Uname.version
|
|
409
528
|
|
410
529
|
If some error is *raised* it should be reported as a bug.
|
411
530
|
|
531
|
+
---
|
532
|
+
|
412
533
|
## Ruby on Rails
|
413
534
|
|
414
535
|
1. Just add `gem linux_stat`:
|
@@ -421,23 +542,29 @@ You can use LinuxStat directly in rails.
|
|
421
542
|
|
422
543
|

|
423
544
|
|
545
|
+
---
|
424
546
|
|
425
547
|
## Android
|
426
548
|
|
427
549
|
LinuxStat does support Android OS. But it's not rigorously tested on all device like android apps.
|
428
550
|
|
429
551
|
But in Termux you can just run LinuxStat without facing issues.
|
552
|
+
Note that the CPU count can differ due to hotplugging feature. So if you see the CPU count changes, there's not really nothing to do about that.
|
430
553
|
|
431
554
|

|
432
555
|
|
433
556
|
Issues regarding running LinuxStat on termux are also welcomed.
|
434
557
|
|
558
|
+
---
|
559
|
+
|
435
560
|
## Development
|
436
561
|
|
437
562
|
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.
|
438
563
|
|
439
564
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
440
565
|
|
566
|
+
---
|
567
|
+
|
441
568
|
## Testing
|
442
569
|
Like other gems, this doesn't have a test like RSpec. We suggest using the bin/linuxstat.rb file on various systems.
|
443
570
|
If you need to test a specific module, say the CPU, just run it like this:
|
@@ -459,10 +586,14 @@ $ ruby bin/linuxstat.rb upc
|
|
459
586
|
```
|
460
587
|
This is not a valid module and can't be run.
|
461
588
|
|
589
|
+
---
|
590
|
+
|
462
591
|
## Contributing
|
463
592
|
|
464
593
|
Bug reports and pull requests are welcome on GitHub at https://github.com/Souravgoswami/linux_stat.
|
465
594
|
|
595
|
+
---
|
596
|
+
|
466
597
|
## License
|
467
598
|
|
468
599
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/bin/linuxstat.rb
CHANGED
@@ -2,17 +2,45 @@
|
|
2
2
|
begin
|
3
3
|
require 'linux_stat'
|
4
4
|
rescue LoadError
|
5
|
-
|
6
|
-
require 'linux_stat'
|
5
|
+
puts "The Gem needs to be installed before this test can be run!"
|
7
6
|
end
|
8
7
|
|
9
8
|
$-v = true
|
10
9
|
|
11
10
|
# Print time each method takes unless --no-time or -nt option is passed
|
12
|
-
|
13
|
-
|
11
|
+
markdown = ARGV.any? { |x| x[/^\-(\-markdown|md)$/] }
|
12
|
+
html = ARGV.any? { |x| x[/^\-(\-html|html)$/] }
|
13
|
+
|
14
|
+
# Check which conflicting argument (e.g., `-md -html` together) is passed last
|
15
|
+
# Always use the last argument
|
16
|
+
conflicting, hash = [
|
17
|
+
"markdown|html", /^\-(\-markdown|md)$/, /^\-(\-html|html)$/
|
18
|
+
].each_slice(3).to_a, {}
|
19
|
+
|
20
|
+
conflicting.each do |x, y, z|
|
21
|
+
o1, o2 = *x.split(?|.freeze).map(&:to_sym)
|
22
|
+
m1, m2 = ARGV.any? { |_x| _x[y] }, ARGV.any? { |_x| _x[z] }
|
23
|
+
|
24
|
+
if m1 && m2
|
25
|
+
rev = ARGV.reverse
|
26
|
+
|
27
|
+
if rev.index { |_x| _x[y] } < rev.index { |_x| _x[z] }
|
28
|
+
hash.merge!(o1 => true)
|
29
|
+
else
|
30
|
+
hash.merge!(o2 => true)
|
31
|
+
end
|
32
|
+
elsif m1
|
33
|
+
hash.merge!(o1 => true)
|
34
|
+
elsif m2
|
35
|
+
hash.merge!(o2 => true)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
MARKDOWN, HTML = hash[:markdown], hash[:html]
|
40
|
+
|
41
|
+
PRINT_TIME = (MARKDOWN || HTML) ? false : !ARGV.any? { |x| x[/^\-\-no-time$/] || x[/^\-nt$/] }
|
14
42
|
|
15
|
-
%w(--markdown -md --no-time -nt).each(&ARGV.method(:delete))
|
43
|
+
%w(--markdown -md --no-time -nt --html -html).each(&ARGV.method(:delete))
|
16
44
|
|
17
45
|
# Run only desired classes / modules
|
18
46
|
constants = LinuxStat.constants
|
@@ -34,6 +62,8 @@ execute.sort.each do |c|
|
|
34
62
|
if meths.length > 0
|
35
63
|
if MARKDOWN
|
36
64
|
puts "### LinuxStat::#{c}\n```"
|
65
|
+
elsif HTML
|
66
|
+
puts "<h3>LinuxStat::#{c}</h3>\n<pre>"
|
37
67
|
else
|
38
68
|
puts "\e[1;4;38;2;255;240;0mLinuxStat::#{c}\e[0m"
|
39
69
|
end
|
@@ -50,6 +80,8 @@ execute.sort.each do |c|
|
|
50
80
|
|
51
81
|
if MARKDOWN
|
52
82
|
puts "#{e}.#{meth}\n=> #{dis}"
|
83
|
+
elsif HTML
|
84
|
+
puts "#{e}.#{meth}\n=> #{dis}"
|
53
85
|
else
|
54
86
|
puts "\e[1;38;2;80;80;255m#{e}.#{meth}\e[0m\n=> #{dis}"
|
55
87
|
end
|
@@ -67,5 +99,10 @@ execute.sort.each do |c|
|
|
67
99
|
puts
|
68
100
|
end
|
69
101
|
|
70
|
-
|
102
|
+
meths.length > 0
|
103
|
+
if MARKDOWN
|
104
|
+
puts "```\n\n"
|
105
|
+
elsif HTML
|
106
|
+
puts "</pre>"
|
107
|
+
end
|
71
108
|
end
|
data/lib/linux_stat/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linux_stat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sourav Goswami
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-12-
|
11
|
+
date: 2020-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Efficient linux system reporting gem. Linux Only | Efficient | Reliable
|
14
14
|
email:
|
@@ -48,10 +48,7 @@ files:
|
|
48
48
|
homepage: https://github.com/Souravgoswami/linux_stat/
|
49
49
|
licenses:
|
50
50
|
- MIT
|
51
|
-
metadata:
|
52
|
-
homepage_uri: https://github.com/Souravgoswami/linux_stat/
|
53
|
-
source_code_uri: https://github.com/Souravgoswami/linux_stat
|
54
|
-
changelog_uri: https://github.com/Souravgoswami/linux_stat/commits/master
|
51
|
+
metadata: {}
|
55
52
|
post_install_message:
|
56
53
|
rdoc_options: []
|
57
54
|
require_paths:
|