linux_stat 0.3.3 → 0.6.1

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.
@@ -3,7 +3,7 @@ module LinuxStat
3
3
  class << self
4
4
  # Returns the Linux Kernel version.
5
5
  # If the information isn't available, it will return a frozen empty string.
6
- # The output is also cached ; as changing the value in runtime is unexpected.
6
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
7
7
  def version
8
8
  return ''.freeze if string.empty?
9
9
  @@version ||= splitted[2]
@@ -11,7 +11,7 @@ module LinuxStat
11
11
 
12
12
  # Returns the name of the user who built the kernel using KBUILD_FLAGS.
13
13
  # If the information isn't available, it will return a frozen empty string.
14
- # The output is also cached ; as changing the value in runtime is unexpected.
14
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
15
15
  def build_user
16
16
  @@build_user ||= string.split(/(\(.+\))/).each(&:strip!)
17
17
  .reject(&:empty?).find { |x| x[/^\(.+\)$/] }.to_s
@@ -20,7 +20,7 @@ module LinuxStat
20
20
 
21
21
  # Returns the compiler used to compile the Linux Kernel.
22
22
  # If the information isn't available, it will return a frozen empty string.
23
- # The output is also cached ; as changing the value in runtime is unexpected.
23
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
24
24
  def compiler
25
25
  return ''.freeze if string.empty?
26
26
 
@@ -39,7 +39,7 @@ module LinuxStat
39
39
 
40
40
  # Returns the compiler version used to compile the Linux Kernel.
41
41
  # If the information isn't available, it will return a frozen empty string.
42
- # The output is also cached ; as changing the value in runtime is unexpected.
42
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
43
43
  def compiler_version
44
44
  @@compiler_version ||= string.split(/(\(.+?\))/).each(&:strip!)
45
45
  .reject(&:empty?)[2..4].to_a
@@ -62,7 +62,7 @@ module LinuxStat
62
62
  # You have to use regexp yourself to get the proper zone.
63
63
  # Use LinuxStat::Kernel.build_date_string to get the original string if you need that.
64
64
  #
65
- # The output is also cached ; as changing the value in runtime is unexpected.
65
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
66
66
  def build_date
67
67
  return nil if splitted.empty?
68
68
 
@@ -107,7 +107,7 @@ module LinuxStat
107
107
  # You have to use regexp yourself to get the proper zone.
108
108
  # Use LinuxStat::Kernel.build_date_string to get the original string if you need that.
109
109
  #
110
- # The output is also cached ; as changing the value in runtime is unexpected.
110
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
111
111
  def build_date_string
112
112
  return nil if splitted.empty?
113
113
 
@@ -133,14 +133,22 @@ module LinuxStat
133
133
  end
134
134
  end
135
135
 
136
- alias release version
137
-
138
136
  # Reads maximum 1024 bytes from /proc/version and returns the string.
139
137
  # The output is also cached ; as changing the value in runtime is unexpected.
140
138
  def string
141
139
  @@string ||= File.readable?('/proc/version') ? IO.read('/proc/version', 1024).tap(&:strip!) : ''
142
140
  end
143
141
 
142
+ # Returns the sc_clk_tck or the same output from command `getconf CLK_TCK`.
143
+ # Also, clk_tck is an alias of this method.
144
+ # The output is also cached ; as changing the value in runtime is unexpected.
145
+ def ticks
146
+ @@tick ||= LinuxStat::Sysconf.sc_clk_tck
147
+ end
148
+
149
+ alias release version
150
+ alias clk_tck ticks
151
+
144
152
  private
145
153
  def splitted
146
154
  @@string_splitted ||= string.split
@@ -2,9 +2,10 @@ module LinuxStat
2
2
  module Memory
3
3
  class << self
4
4
  # Returns the memory details reported by /proc/meminfo. In this format:
5
- # {:total=>3836264, :used=>3097952, :available=>738312, :percent_used=>80.75, :percent_available=>19.25}
5
+ # {:total=>3836264, :used=>3097952, :available=>738312, :percent_used=>80.75, :percent_available=>19.25}
6
+ #
7
+ # The values are in Kilobyte.
6
8
  #
7
- # The value is in Kilobyte.
8
9
  # If the statistics is not available, it will return an empty Hash.
9
10
  def stat
10
11
  return {} unless meminfo?
@@ -31,6 +32,7 @@ module LinuxStat
31
32
 
32
33
  # Returns the total memory details reported by /proc/meminfo.
33
34
  # The value is in Kilobyte.
35
+ #
34
36
  # It retuns an Integer but if the info is not available, it will return nil.
35
37
  def total
36
38
  return nil unless meminfo?
@@ -39,6 +41,7 @@ module LinuxStat
39
41
 
40
42
  # Returns the total memory details reported by /proc/meminfo.
41
43
  # The value is in Kilobyte.
44
+ #
42
45
  # It retuns an Integer but if the info is not available, it will return nil
43
46
  def available
44
47
  return nil unless meminfo?
@@ -47,6 +50,7 @@ module LinuxStat
47
50
 
48
51
  # Returns the amount of memory used reported by /proc/meminfo.
49
52
  # The value is in Kilobyte.
53
+ #
50
54
  # It retuns an Integer but if the info is not available, it will return nil.
51
55
  def used
52
56
  return nil unless meminfo?
@@ -55,6 +59,7 @@ module LinuxStat
55
59
  end
56
60
 
57
61
  # Returns the percentage of memory used reported by /proc/meminfo.
62
+ #
58
63
  # It retuns an Integer but if the info is not available, it will return nil
59
64
  def percent_used
60
65
  return nil unless meminfo?
@@ -64,6 +69,7 @@ module LinuxStat
64
69
  end
65
70
 
66
71
  # Returns the percentage of memory used reported by /proc/meminfo.
72
+ #
67
73
  # It retuns an Integer but if the info is not available, it will return nil
68
74
  def percent_available
69
75
  return nil unless meminfo?
@@ -1,7 +1,8 @@
1
1
  module LinuxStat
2
2
  module Mounts
3
3
  class << self
4
- # Reads /proc/mounts and returns list of devices.
4
+ # Reads /proc/mounts and returns the output splitted with \n.
5
+ # In other words, it's same as running IO.readlines('/proc/mounts').each(&:strip!)
5
6
  #
6
7
  # It returns an Array.
7
8
  # If the info isn't available or /proc/mounts is not readable, it will return an empty Array.
@@ -9,6 +10,14 @@ module LinuxStat
9
10
  mounts
10
11
  end
11
12
 
13
+ # Reads /proc/mounts and returns list of devices.
14
+ #
15
+ # It returns an Array.
16
+ # If the info isn't available or /proc/mounts is not readable, it will return an empty Array.
17
+ def list_devices
18
+ mounts.map { |x| x.split(?\s.freeze).first }
19
+ end
20
+
12
21
  # Reads /proc/mounts and returns partition name of the device mounted at /.
13
22
  #
14
23
  # It returns a String.
@@ -45,6 +54,135 @@ module LinuxStat
45
54
  ret
46
55
  end
47
56
 
57
+ # mount_point(dev = root)
58
+ # Where device = block device.
59
+ # The default argument is the root block device.
60
+ #
61
+ # It helps you find the mountpoint of a block device.
62
+ # For example:
63
+ # LinuxStat::Mounts.mount_point('/dev/sdb1')
64
+ # => "/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f"
65
+ #
66
+ # The return type is String.
67
+ # But if the status isn't available or the device isn't mounted, it will return an empty String.
68
+ def mount_point(dev = root)
69
+ m = ''
70
+ mounts.each do |x|
71
+ x.strip!
72
+
73
+ unless x.empty?
74
+ _x = x.split
75
+ if _x[0] == dev
76
+ m.replace(_x[1])
77
+ break
78
+ end
79
+ end
80
+ end
81
+ m
82
+ end
83
+
84
+ # list_devices_mount_point()
85
+ #
86
+ # It shows all the block devices corresponding to mount points.
87
+ #
88
+ # For example:
89
+ # LinuxStat::Mounts.list_devices_mount_point
90
+ # => {"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"=>"/sys/fs/pstore", "none"=>"/sys/fs/bpf", "systemd-1"=>"/proc/sys/fs/binfmt_misc", "debugfs"=>"/sys/kernel/debug", "mqueue"=>"/dev/mqueue", "hugetlbfs"=>"/dev/hugepages", "tracefs"=>"/sys/kernel/tracing", "configfs"=>"/sys/kernel/config", "fusectl"=>"/sys/fs/fuse/connections", "gvfsd-fuse"=>"/run/user/1000/gvfs", "/dev/sdb1"=>"/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f", "binfmt_misc"=>"/proc/sys/fs/binfmt_misc"}
91
+ #
92
+ # The return type is Hash.
93
+ # But if the status isn't available or the device isn't mounted, it will return an empty String.
94
+ def list_devices_mount_point
95
+ m = {}
96
+ mounts.each do |x|
97
+ x.strip!
98
+
99
+ unless x.empty?
100
+ _x = x.split
101
+ m.merge!(_x[0] => _x[1])
102
+ end
103
+ end
104
+ m
105
+ end
106
+
107
+ # devices_stat
108
+ # [ Not to confuse this method with device_stat(dev) which shows only one device's info ]
109
+ #
110
+ # It shows all the block devices corresponding to mount points and data from LinuxStat::FS.stat(arg)
111
+ #
112
+ # For example:
113
+ # LinuxStat::Mounts.devices_stat
114
+ # => {"proc"=>{:mountpoint=>"/proc", :total=>0, :free=>0, :available=>0, :used=>0, :percent_used=>NaN, :percent_free=>NaN, :percent_available=>NaN}, "/dev/sdb1"=>{:mountpoint=>"/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f", :total=>31466008576, :free=>2693931008, :available=>2693931008, :used=>28772077568, :percent_used=>91.44, :percent_free=>8.56, :percent_available=>8.56}}
115
+ #
116
+ # The return type is Hash.
117
+ # But if the status isn't available, it will return an empty Hash.
118
+ def devices_stat
119
+ # Code duplication is fine if it gives maximum performance
120
+ m = {}
121
+ mounts.each do |x|
122
+ x.strip!
123
+
124
+ unless x.empty?
125
+ _x = x.split
126
+ total, free, available, used = fs_info(_x[1])
127
+
128
+ m.merge!(_x[0] => {
129
+ mountpoint: _x[1],
130
+
131
+ total: total,
132
+ free: free,
133
+ available: available,
134
+ used: used,
135
+
136
+ percent_used: used.*(100).fdiv(total).round(2),
137
+ percent_free: free.*(100).fdiv(total).round(2),
138
+ percent_available: available.*(100).fdiv(total).round(2),
139
+ })
140
+ end
141
+ end
142
+ m
143
+ end
144
+
145
+ # device_stat(dev = root)
146
+ # [ Not to confuse this method with devices_stat() which shows all devices ]
147
+ # It shows all the block devices corresponding to mount points and data from LinuxStat::FS.stat(arg)
148
+ #
149
+ # For example:
150
+ # LinuxStat::Mounts.device_stat('/dev/sda2')
151
+ # => {"/dev/sda2"=>{:mountpoint=>"/", :total=>119981191168, :free=>35298562048, :available=>35298562048, :used=>84682629120, :percent_used=>70.58, :percent_free=>29.42, :percent_available=>29.42}}
152
+ #
153
+ # The return type is Hash.
154
+ # But if the status isn't available, it will return an empty Hash.
155
+ def device_stat(dev = root)
156
+ # Code duplication is fine if it gives maximum performance
157
+ m = {}
158
+ mounts.each do |x|
159
+ x.strip!
160
+
161
+ unless x.empty?
162
+ _x = x.split
163
+ next if _x[0] != dev
164
+
165
+ total, free, available, used = fs_info(_x[1])
166
+
167
+ m.merge!({
168
+ mountpoint: _x[1],
169
+
170
+ total: total,
171
+ free: free,
172
+ available: available,
173
+ used: used,
174
+
175
+ percent_used: used.*(100).fdiv(total).round(2),
176
+ percent_free: free.*(100).fdiv(total).round(2),
177
+ percent_available: available.*(100).fdiv(total).round(2),
178
+ })
179
+
180
+ break
181
+ end
182
+ end
183
+ m
184
+ end
185
+
48
186
  private
49
187
  def mount_readable?
50
188
  @@mount_readable ||= File.readable?('/proc/mounts')
@@ -59,6 +197,19 @@ module LinuxStat
59
197
  return [] unless mount_readable?
60
198
  @@root ||= IO.foreach('/proc/mounts').find { |x| x.split[1] == '/'.freeze }.split
61
199
  end
200
+
201
+ def fs_info(dev)
202
+ # => [total, free, available, used]
203
+ s = LinuxStat::FS.stat(dev)
204
+ s.default = 0
205
+
206
+ [
207
+ s[:block_size] * s[:blocks],
208
+ s[:block_size] * s[:block_free],
209
+ s[:block_size] * s[:block_avail_unpriv],
210
+ s[:blocks].-(s[:block_free]) * s[:block_size]
211
+ ]
212
+ end
62
213
  end
63
214
  end
64
215
  end
@@ -1,10 +1,8 @@
1
1
  module LinuxStat
2
2
  module OS
3
- prepend Uname
4
-
5
3
  class << self
6
4
  # Reads /etc/os-release and returns a Hash. For example:
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"}
5
+ # {: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"}
8
6
  #
9
7
  # If the info isn't available, it will return an empty Hash.
10
8
  #
@@ -13,12 +11,12 @@ module LinuxStat
13
11
  # The information is also cached, and once loaded, won't change in runtime. Because changing the /etc/lsb-release
14
12
  # isn't expected in runtime.
15
13
  def os_release
16
- # Cached ; as changing the value in runtime is unexpected
14
+ # cached (memoized) ; as changing the value in runtime is unexpected
17
15
  @@os_release ||= File.readable?('/etc/os-release') ? release('/etc/os-release') : {}
18
16
  end
19
17
 
20
18
  # Reads /etc/lsb-release and returns a Hash. For example:
21
- # {:LSB_VERSION=>"1.4", :DISTRIB_ID=>"Arch", :DISTRIB_RELEASE=>"rolling", :DISTRIB_DESCRIPTION=>"Arch Linux"}
19
+ # {:LSB_VERSION=>"1.4", :DISTRIB_ID=>"Arch", :DISTRIB_RELEASE=>"rolling", :DISTRIB_DESCRIPTION=>"Arch Linux"}
22
20
  #
23
21
  # If the info isn't available, it will return an empty Hash.
24
22
  #
@@ -27,12 +25,13 @@ module LinuxStat
27
25
  # The information is also cached, and once loaded, won't change in runtime. Because changing the /etc/lsb-release
28
26
  # isn't expected in runtime.
29
27
  def lsb_release
30
- # Cached ; as changing the value in runtime is unexpected
28
+ # cached (memoized) ; as changing the value in runtime is unexpected
31
29
  @@lsb_release ||= File.readable?('/etc/lsb-release') ? release('/etc/lsb-release') : {}
32
30
  end
33
31
 
34
32
  # Reads /etc/lsb-release or /etc/os-release tries to get information about the distribution.
35
33
  # If the information isn't available, it will read and return /etc/issue.
34
+ #
36
35
  # The return type is String.
37
36
  # If none of the info is available, it will return an empty frozen String.
38
37
  def distribution
@@ -54,18 +53,21 @@ module LinuxStat
54
53
  end
55
54
 
56
55
  # Uses utsname.h to determine the machine
56
+ #
57
57
  # It returns a String but if the info isn't available, it will return an empty String
58
58
  def machine
59
- @@machine ||= Uname.machine
59
+ @@machine ||= LinuxStat::Uname.machine
60
60
  end
61
61
 
62
62
  # Uses utsname.h to determine the system nodename
63
+ #
63
64
  # It returns String but if the info isn't available, it will return an empty String
64
65
  def nodename
65
- @@nodename ||= Uname.nodename
66
+ @@nodename ||= LinuxStat::Uname.nodename
66
67
  end
67
68
 
68
69
  # Reads /etc/hostname and returns the hostname.
70
+ #
69
71
  # The return type is String.
70
72
  # If the info info isn't available, it will return 'localhost'.
71
73
  def hostname
@@ -90,7 +92,7 @@ module LinuxStat
90
92
  end
91
93
 
92
94
  # Reads /proc/uptime and returns the system uptime:
93
- # {:hour=>10, :minute=>34, :second=>12.59}
95
+ # {:hour=>10, :minute=>34, :second=>12.59}
94
96
  #
95
97
  # If the stat isn't available, an empty hash is returned.
96
98
  def uptime
@@ -0,0 +1,75 @@
1
+ module LinuxStat
2
+ module PrettifyBytes
3
+ class << self
4
+ # Converts a number to decimal byte units and outputs with the metric prefix
5
+ # For example,
6
+ #
7
+ # LinuxStat::PrettifyBytes.convert_decimal(1000) # => "1.0 kilobyte"
8
+ # LinuxStat::PrettifyBytes.convert_decimal(1000 ** 3) # => "1.0 gigabyte"
9
+ # LinuxStat::PrettifyBytes.convert_decimal(1024 ** 3) # => "1.07 gigabytes"
10
+ def convert_decimal(n)
11
+ @@d_units ||= %W(#{''} kilo mega giga tera peta exa zetta)
12
+ .map.with_index { |x, i| [x, 1000.**(i + 1)] }
13
+ unit = @@d_units.find { |x| n < x[1] } || ['yotta'.freeze, 10 ** 27]
14
+
15
+ converted = n.fdiv(unit[1] / 1000).round(2)
16
+ "#{pad_left(converted)} #{unit[0]}byte#{?s.freeze if converted != 1}"
17
+ end
18
+
19
+ # Converts a number to binary byte units and outputs with the IEC prefix
20
+ # For example,
21
+ #
22
+ # LinuxStat::PrettifyBytes.convert_binary(1000) # => "1000.0 bytes"
23
+ # LinuxStat::PrettifyBytes.convert_binary(1000 ** 3) # => "953.67 mebibytes"
24
+ # LinuxStat::PrettifyBytes.convert_binary(1024 ** 3) # => "1.0 gibibyte"
25
+ def convert_binary(n)
26
+ @@b_units ||= %W(#{''} kibi mebi gibi tebi pebi exbi zebi)
27
+ .map.with_index { |x, i| [x, 1024.**(i + 1)] }
28
+ unit = @@b_units.find { |x| n < x[1] } || ['yobi'.freeze, 10 ** 27]
29
+
30
+ converted = n.fdiv(unit[1] / 1024).round(2)
31
+ "#{pad_left(converted)} #{unit[0]}byte#{?s.freeze if converted != 1}"
32
+ end
33
+
34
+ # Converts a number to decimal byte units
35
+ # For example,
36
+ #
37
+ # LinuxStat::PrettifyBytes.convert_short_decimal(1000) # => "1.0 kB"
38
+ # LinuxStat::PrettifyBytes.convert_short_decimal(1000 ** 3) # => "1.0 GB"
39
+ # LinuxStat::PrettifyBytes.convert_short_decimal(1024 ** 3) # => "1.07 GB"
40
+ def convert_short_decimal(n)
41
+ @@sd_units ||= %W(#{''} k M G T P E Z)
42
+ .map.with_index { |x, i| [x, 1000.**(i + 1)] }
43
+ unit = @@sd_units.find { |x| n < x[1] } || [?Y.freeze, 10 ** 27]
44
+
45
+ converted = n.fdiv(unit[1] / 1000).round(2)
46
+ "#{pad_left(converted)} #{unit[0]}B"
47
+ end
48
+
49
+ # Converts a number to binary byte units
50
+ # For example,
51
+ #
52
+ # LinuxStat::PrettifyBytes.convert_short_binary(1000) # => "1000 B"
53
+ # LinuxStat::PrettifyBytes.convert_short_binary(1000 ** 3) # => "953.67 MiB"
54
+ # LinuxStat::PrettifyBytes.convert_short_binary(1024 ** 3) # => "1.0 GiB"
55
+ def convert_short_binary(n)
56
+ return "#{n} B" if n < 1024
57
+
58
+ @@sb_units ||= %W(#{''} K M G T P E Z)
59
+ .map.with_index { |x, i| [x, 1024.**(i + 1)] }
60
+ unit = @@sb_units.find { |x| n < x[1] } || [?Y.freeze, 1024 ** 9]
61
+
62
+ converted = n.fdiv(unit[1] / 1024).round(2)
63
+ "#{pad_left(converted)} #{unit[0]}iB"
64
+ end
65
+
66
+ private
67
+ def pad_left(n, mantissa_length = 2)
68
+ n = n.round(mantissa_length)
69
+ exp, mant = n.to_s.split(?..freeze)
70
+ m = mant.length < mantissa_length ? mant + ?0.freeze * (mantissa_length - mant.length) : mant
71
+ exp + ?..freeze + m
72
+ end
73
+ end
74
+ end
75
+ end