linux_stat 0.6.3 → 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 +4 -4
- data/README.md +69 -5
- data/ext/fs_stat/extconf.rb +5 -1
- data/ext/sysconf/extconf.rb +5 -1
- data/ext/utsname/extconf.rb +5 -1
- 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 +12 -0
- data/lib/linux_stat/os.rb +15 -3
- data/lib/linux_stat/prettify_bytes.rb +47 -12
- data/lib/linux_stat/process.rb +10 -0
- data/lib/linux_stat/process_info.rb +129 -45
- data/lib/linux_stat/swap.rb +15 -1
- data/lib/linux_stat/user.rb +63 -10
- 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: 15bbdf4582dc8a526cde2230d81e2fea8ea67f5684daeae87161bd486e8f73a0
|
4
|
+
data.tar.gz: 236125a3f4ac8c85563cf075be40b8ceb0e7e0b4cd6374abce1c6c6f62da8d56
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c2f0f12aa42dbb2c63426a5c7aa384bb68a02dcf283b78953ebc641b6d1803400f571d94e6eb4a812b282672d95a071857fd2e4ab4004d17c163eb2ce12fea7
|
7
|
+
data.tar.gz: 448f627019dce4ada6c6c76c75dc3a2d531230a1cf77de7a88d0fd372c43e79ed69b479dc7625385ac9f2e44e1bb8da2ef8405d9737ddccd5b985691959cf294
|
data/README.md
CHANGED
@@ -514,7 +514,71 @@ LinuxStat::User.usernames_by_uid
|
|
514
514
|
|
515
515
|
---
|
516
516
|
|
517
|
-
## 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
|
518
582
|
|
519
583
|
Filesystem can take arguments. By default it's '/' or the root of the system...
|
520
584
|
|
@@ -549,7 +613,7 @@ irb(main):005:0> LinuxStat::Filesystem.total(thumbdrive).fdiv(1024 ** 3).to_s <<
|
|
549
613
|
=> "29.305004119873047 GiB"
|
550
614
|
```
|
551
615
|
|
552
|
-
## Note
|
616
|
+
## Note 3: ProcessInfo
|
553
617
|
|
554
618
|
All the methods LinuxStat::ProcessInfo can take an argument containing the Process ID of a process.
|
555
619
|
By default it's $$ or the PID of the current process, ruby, itself.
|
@@ -601,7 +665,7 @@ irb(main):002:0> LinuxStat::ProcessInfo.memory(LinuxStat::Process.names.find { |
|
|
601
665
|
=> "467.51 MiB"
|
602
666
|
```
|
603
667
|
|
604
|
-
## Note
|
668
|
+
## Note 4: FS
|
605
669
|
|
606
670
|
LinuxStat::FS module gives you the raw info in Hash collected from statvfs.
|
607
671
|
|
@@ -626,7 +690,7 @@ irb(main):003:0> t = Time.now ; puts LinuxStat::FS.stat('/') ; Time.now - t
|
|
626
690
|
|
627
691
|
To learn more about them, just run ri and the method name. To see all available methods.
|
628
692
|
|
629
|
-
## Note
|
693
|
+
## Note 5: User
|
630
694
|
Most of the LinuxStat::User supports arguments.
|
631
695
|
|
632
696
|
For example, to get a user's home by the username:
|
@@ -708,7 +772,7 @@ irb(main):004:0> LinuxStat::User.get_login
|
|
708
772
|
|
709
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.
|
710
774
|
|
711
|
-
## Note
|
775
|
+
## Note 6: PrettifyBytes
|
712
776
|
Often times we need to work with KB, MB GB, TB, or KiB, MiB, GiB, TiB, etc.
|
713
777
|
And we need some work to convert bytes to those units.
|
714
778
|
Because LinuxStat provides a lot of data in bytes, and kilobytes, it's quite tedious to convert them all the time.
|
data/ext/fs_stat/extconf.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require 'mkmf'
|
2
2
|
|
3
|
-
unless (
|
3
|
+
unless have_const('linux') || RbConfig::CONFIG['arch'].to_s[/linux/]
|
4
|
+
abort('Platform is not linux')
|
5
|
+
end
|
6
|
+
|
7
|
+
unless have_header('sys/statvfs.h') && have_header('ruby.h')
|
4
8
|
abort('Missing header')
|
5
9
|
end
|
6
10
|
|
data/ext/sysconf/extconf.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require 'mkmf'
|
2
2
|
|
3
|
-
unless (
|
3
|
+
unless have_const('linux') || RbConfig::CONFIG['arch'].to_s[/linux/]
|
4
|
+
abort('Platform is not linux')
|
5
|
+
end
|
6
|
+
|
7
|
+
unless have_header('sys/unistd.h') && have_header('ruby.h')
|
4
8
|
abort('Missing header')
|
5
9
|
end
|
6
10
|
|
data/ext/utsname/extconf.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require 'mkmf'
|
2
2
|
|
3
|
-
unless (
|
3
|
+
unless have_const('linux') || RbConfig::CONFIG['arch'].to_s[/linux/]
|
4
|
+
abort('Platform is not linux')
|
5
|
+
end
|
6
|
+
|
7
|
+
unless have_header('sys/utsname.h') && have_header('ruby.h')
|
4
8
|
abort('Missing header')
|
5
9
|
end
|
6
10
|
|
data/lib/linux_stat/battery.rb
CHANGED
@@ -3,12 +3,15 @@ module LinuxStat
|
|
3
3
|
PATH = "/sys/class/power_supply/BAT0"
|
4
4
|
|
5
5
|
class << self
|
6
|
+
##
|
6
7
|
# Returns true or false based on the presence of the battery.
|
7
8
|
def present?
|
8
9
|
@@present ||= Dir.exist?(PATH)
|
9
10
|
end
|
10
11
|
|
12
|
+
##
|
11
13
|
# Returns the details of the battery.
|
14
|
+
#
|
12
15
|
# If the battery is not present it will return an empty Hash.
|
13
16
|
def stat
|
14
17
|
st = status.downcase
|
@@ -26,57 +29,73 @@ module LinuxStat
|
|
26
29
|
}
|
27
30
|
end
|
28
31
|
|
32
|
+
##
|
29
33
|
# Returns the model of the battery.
|
34
|
+
#
|
30
35
|
# If the battery is not present or the information isn't available it will return an empty String.
|
31
36
|
def model
|
32
37
|
return ''.freeze unless model_readable?
|
33
38
|
IO.read(File.join(PATH, 'model_name')).tap(&:strip!)
|
34
39
|
end
|
35
40
|
|
41
|
+
##
|
36
42
|
# Returns the manufacturer of the battery.
|
43
|
+
#
|
37
44
|
# If the battery is not present or the information is not available, it will return an empty String.
|
38
45
|
def manufacturer
|
39
46
|
return ''.freeze unless manufacturer_readable?
|
40
47
|
IO.read(File.join(PATH, 'manufacturer')).tap(&:strip!)
|
41
48
|
end
|
42
49
|
|
50
|
+
##
|
43
51
|
# Returns the technology of the battery.
|
52
|
+
#
|
44
53
|
# If the battery is not present or the information is not available, it will return an empty String.
|
45
54
|
def technology
|
46
55
|
return ''.freeze unless tech_readable?
|
47
56
|
IO.read(File.join(PATH, 'technology')).tap(&:strip!)
|
48
57
|
end
|
49
58
|
|
59
|
+
##
|
50
60
|
# Returns the status of the battery.
|
51
|
-
# If the battery is not present or the information is not available, it will return an empty String.
|
52
61
|
# The status generally includes either of the full, charging, discharging and unknown states in most cases.
|
62
|
+
#
|
63
|
+
# If the battery is not present or the information is not available, it will return an empty frozen String.
|
53
64
|
def status
|
54
65
|
return ''.freeze unless status_readable?
|
55
66
|
IO.read(File.join(PATH, 'status')).tap(&:strip!)
|
56
67
|
end
|
57
68
|
|
69
|
+
##
|
58
70
|
# Returns true if the battery is charging, false if the battery is not charging.
|
71
|
+
#
|
59
72
|
# If the battery is not present or the information is not available, it will return nil.
|
60
73
|
def charging?
|
61
74
|
return nil if status.empty?
|
62
75
|
%w(full charging unknown).each(&:freeze).include?(status.downcase)
|
63
76
|
end
|
64
77
|
|
78
|
+
##
|
65
79
|
# Returns true if the battery is discharging, false if the battery is not discharging.
|
80
|
+
#
|
66
81
|
# If the battery is not present or the information is not available, it will return nil.
|
67
82
|
def discharging?
|
68
83
|
return nil if status.empty?
|
69
84
|
status.downcase == 'discharging'
|
70
85
|
end
|
71
86
|
|
87
|
+
##
|
72
88
|
# Returns true if the battery status if full, false if the battery status is not full.
|
89
|
+
#
|
73
90
|
# If the battery is not present or the information is not available, it will return nil.
|
74
91
|
def full?
|
75
92
|
return nil if status.empty?
|
76
93
|
status.downcase == 'full'
|
77
94
|
end
|
78
95
|
|
96
|
+
##
|
79
97
|
# Returns the charge of the battery.
|
98
|
+
#
|
80
99
|
# If the battery is not present or the information is not available, it will return nil.
|
81
100
|
def charge
|
82
101
|
return nil unless charge_now_readable?
|
data/lib/linux_stat/bios.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
module LinuxStat
|
2
2
|
module BIOS
|
3
3
|
class << self
|
4
|
+
##
|
4
5
|
# Returns the model of the BIOS.
|
6
|
+
#
|
5
7
|
# If the information is not available it will return a frozen empty string.
|
6
8
|
#
|
7
9
|
# The output is also cached (memoized) ; as changing the value in runtime is unexpected.
|
@@ -16,7 +18,9 @@ module LinuxStat
|
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
21
|
+
##
|
19
22
|
# Returns the vendor of the BIOS.
|
23
|
+
#
|
20
24
|
# If the information is not available it will return a frozen empty string.
|
21
25
|
#
|
22
26
|
# The output is also cached (memoized) ; as changing the value in runtime is unexpected.
|
@@ -29,7 +33,9 @@ module LinuxStat
|
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
36
|
+
##
|
32
37
|
# Returns the version of the BIOS.
|
38
|
+
#
|
33
39
|
# If the information is not available it will return a frozen empty string.
|
34
40
|
#
|
35
41
|
# The output is also cached (memoized) ; as changing the value in runtime is unexpected.
|
@@ -41,7 +47,9 @@ module LinuxStat
|
|
41
47
|
end
|
42
48
|
end
|
43
49
|
|
50
|
+
##
|
44
51
|
# Returns the date of the BIOS.
|
52
|
+
#
|
45
53
|
# If the information is not available it will return a frozen empty string.
|
46
54
|
#
|
47
55
|
# The output is also cached (memoized) ; as changing the value in runtime is unexpected.
|
data/lib/linux_stat/cpu.rb
CHANGED
@@ -1,16 +1,21 @@
|
|
1
1
|
module LinuxStat
|
2
2
|
module CPU
|
3
3
|
class << self
|
4
|
-
|
4
|
+
##
|
5
|
+
# = stat(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
|
6
|
+
#
|
5
7
|
# Where sleep is the delay to gather the data.
|
8
|
+
#
|
6
9
|
# The minimum possible value at anytime is 1.0 / LinuxStat::Sysconf.sc_clk_tck
|
10
|
+
#
|
7
11
|
# This method returns the cpu usage of all threads.
|
8
12
|
#
|
9
13
|
# The first one is aggregated CPU usage reported by the Linux kernel.
|
14
|
+
#
|
10
15
|
# And the consecutive ones are the real core usages.
|
11
16
|
#
|
12
17
|
# On a system with 4 threads, the output will be like::
|
13
|
-
#
|
18
|
+
# {0=>84.38, 1=>100.0, 2=>50.0, 3=>87.5, 4=>87.5}
|
14
19
|
#
|
15
20
|
# If the information is not available, it will return an empty Hash
|
16
21
|
def stat(sleep = ticks_to_ms)
|
@@ -43,9 +48,13 @@ module LinuxStat
|
|
43
48
|
end
|
44
49
|
end
|
45
50
|
|
46
|
-
|
51
|
+
##
|
52
|
+
# = total_usage(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
|
53
|
+
#
|
47
54
|
# Where sleep is the delay to gather the data.
|
55
|
+
#
|
48
56
|
# The minimum possible value at anytime is 1.0 / LinuxStat::Sysconf.sc_clk_tck
|
57
|
+
#
|
49
58
|
# This method returns the cpu usage of all threads.
|
50
59
|
#
|
51
60
|
# 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.
|
@@ -66,14 +75,18 @@ module LinuxStat
|
|
66
75
|
totald.-(idle_now - idle_then).fdiv(totald).*(100).round(2).abs
|
67
76
|
end
|
68
77
|
|
78
|
+
##
|
69
79
|
# Returns the total number of CPU threads.
|
80
|
+
#
|
70
81
|
# If the information isn't available, it will return 0.
|
71
82
|
def count
|
72
83
|
# CPU count can change during the program runtime
|
73
84
|
cpuinfo.count { |x| x.start_with?('processor') }
|
74
85
|
end
|
75
86
|
|
87
|
+
##
|
76
88
|
# Returns the model of processor.
|
89
|
+
#
|
77
90
|
# If the information isn't available, it will return en empty string.
|
78
91
|
#
|
79
92
|
# The output is also cached (memoized) ; as changing the value in runtime is unexpected.
|
@@ -81,7 +94,9 @@ module LinuxStat
|
|
81
94
|
@@name ||= cpuinfo.find { |x| x.start_with?('model name') }.to_s.split(?:)[-1].to_s.strip
|
82
95
|
end
|
83
96
|
|
97
|
+
##
|
84
98
|
# Returns an array with current core frequencies corresponding to the usages.
|
99
|
+
#
|
85
100
|
# If the information isn't available, it will return an empty array.
|
86
101
|
def cur_freq
|
87
102
|
@@cpu_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_cur_freq"]
|
@@ -94,7 +109,9 @@ module LinuxStat
|
|
94
109
|
end
|
95
110
|
end
|
96
111
|
|
112
|
+
##
|
97
113
|
# Returns an array with max core frequencies corresponding to the usages.
|
114
|
+
#
|
98
115
|
# If the information isn't available, it will return an empty array.
|
99
116
|
def max_freq
|
100
117
|
@@max_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_max_freq"]
|
@@ -1,13 +1,16 @@
|
|
1
1
|
module LinuxStat
|
2
2
|
module Filesystem
|
3
3
|
class << self
|
4
|
-
|
4
|
+
##
|
5
|
+
# = stat(fs = '/')
|
6
|
+
#
|
5
7
|
# Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
|
6
8
|
#
|
7
|
-
# It returns a Hash with the following info:
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
9
|
+
# * It returns a Hash with the following info:
|
10
|
+
#
|
11
|
+
# 1. total size of the device (in bytes)
|
12
|
+
# 2. free space (in kilobytes)
|
13
|
+
# 3. used space (in kilobytes)
|
11
14
|
#
|
12
15
|
# In a hash format:
|
13
16
|
# {:total=>119981191168, :free=>43155574784, :used=>76825616384, :available=>43155574784}
|
@@ -25,8 +28,11 @@ module LinuxStat
|
|
25
28
|
}
|
26
29
|
end
|
27
30
|
|
28
|
-
|
31
|
+
##
|
32
|
+
# = total(fs = '/')
|
33
|
+
#
|
29
34
|
# Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
|
35
|
+
#
|
30
36
|
# It returns the total size of a given disk in bytes.
|
31
37
|
#
|
32
38
|
# If the stat can't be acquired, this method will return nil.
|
@@ -37,10 +43,15 @@ module LinuxStat
|
|
37
43
|
s[:block_size] * s[:blocks]
|
38
44
|
end
|
39
45
|
|
40
|
-
|
46
|
+
##
|
47
|
+
# = free(fs = '/')
|
48
|
+
#
|
41
49
|
# Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
|
50
|
+
#
|
42
51
|
# It returns the total free space in a disk in bytes.
|
52
|
+
#
|
43
53
|
# It is to be noted that free is not same as available.
|
54
|
+
#
|
44
55
|
# Free returns the size of free blocks.
|
45
56
|
#
|
46
57
|
# If the stat can't be acquired, this method will return an empty Hash.
|
@@ -51,8 +62,11 @@ module LinuxStat
|
|
51
62
|
s[:block_size] * s[:block_free]
|
52
63
|
end
|
53
64
|
|
54
|
-
|
65
|
+
##
|
66
|
+
# = used(fs = '/')
|
67
|
+
#
|
55
68
|
# Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
|
69
|
+
#
|
56
70
|
# It returns the used space of a given disk in bytes.
|
57
71
|
#
|
58
72
|
# If the stat can't be acquired, this method will return nil.
|
@@ -63,10 +77,15 @@ module LinuxStat
|
|
63
77
|
s[:blocks].-(s[:block_free]) * s[:block_size]
|
64
78
|
end
|
65
79
|
|
66
|
-
|
80
|
+
##
|
81
|
+
# = available(fs = '/')
|
82
|
+
#
|
67
83
|
# Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
|
84
|
+
#
|
68
85
|
# It returns the total free space in a disk in bytes.
|
86
|
+
#
|
69
87
|
# It is to be noted that free is not same as available.
|
88
|
+
#
|
70
89
|
# Available returns the size of free blocks for unpriviledged users.
|
71
90
|
#
|
72
91
|
# If the stat can't be acquired, this method will return an empty Hash.
|
@@ -77,7 +96,9 @@ module LinuxStat
|
|
77
96
|
s[:block_size] * s[:block_avail_unpriv]
|
78
97
|
end
|
79
98
|
|
80
|
-
|
99
|
+
##
|
100
|
+
# = stat_raw(fs = '/')
|
101
|
+
#
|
81
102
|
# Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
|
82
103
|
#
|
83
104
|
# It returns a Hash with the following data (for example):
|