linux_stat 1.2.0 → 1.3.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.
@@ -113,10 +113,20 @@ execute.sort.each do |c|
113
113
 
114
114
  meths.each do |meth|
115
115
  arg = nil
116
- params = e.method(meth).parameters
117
116
 
118
- param = ''
117
+ arity = e.method(meth).arity
118
+ if arity > 0 || arity == -2
119
+ if c == :PrettifyBytes
120
+ arg = rand(10 ** 15)
121
+ elsif c == :FS
122
+ arg = '/'
123
+ else
124
+ next
125
+ end
126
+ end
119
127
 
128
+ params = e.method(meth).parameters
129
+ param = ''
120
130
  params.each do |p|
121
131
  case p[0]
122
132
  when :opt
@@ -124,24 +134,14 @@ execute.sort.each do |c|
124
134
  when :key
125
135
  param << "#{p[1]}:, "
126
136
  when :req
127
- param << "#{p[1] || 'arg'}, "
137
+ _arg = arg ? " = #{arg.inspect}" : ''.freeze
138
+ param << "#{p[1] || 'arg'}#{_arg}, "
128
139
  end
129
140
  end
130
-
131
141
  param.delete_suffix!(", ")
132
142
 
133
- if e.method(meth).arity > 0
134
- if c == :PrettifyBytes
135
- arg = rand(10 ** 15)
136
- elsif c == :FS
137
- arg = '/'
138
- else
139
- next
140
- end
141
- end
142
-
143
143
  disp_meth = "#{meth}"
144
- disp_meth.concat(arg ? "(#{param} = #{arg.inspect})" : "(#{param})")
144
+ disp_meth.concat(arg ? "(#{param})" : "(#{param})")
145
145
 
146
146
  time = Time.now
147
147
  ret = arg ? e.send(meth, arg) : e.send(meth)
@@ -28,6 +28,7 @@ require "linux_stat/net"
28
28
  require "linux_stat/pci"
29
29
  require "linux_stat/process"
30
30
  require "linux_stat/swap"
31
+ require "linux_stat/thermal"
31
32
  require "linux_stat/usb"
32
33
 
33
34
  # Dependent Modules
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various battery related information of the current system.
3
+
2
4
  module Battery
3
5
  PATH = "/sys/class/power_supply/BAT0".freeze
4
6
 
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various BIOS related information of the current system.
3
+
2
4
  module BIOS
3
5
  class << self
4
6
  ##
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various CPU related information of the current system.
3
+
2
4
  module CPU
3
5
  class << self
4
6
  ##
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various Filesystem related information of the current system.
3
+
2
4
  module Filesystem
3
5
  class << self
4
6
  ##
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various Kernel related information of the current system.
3
+
2
4
  module Kernel
3
5
  class << self
4
6
  ##
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various Memory related information of the current system.
3
+
2
4
  module Memory
3
5
  class << self
4
6
  ##
@@ -11,7 +13,7 @@ module LinuxStat
11
13
  def stat
12
14
  return {} unless meminfo?
13
15
 
14
- memory = IO.foreach('/proc/meminfo').first(3)
16
+ memory = IO.foreach('/proc/meminfo'.freeze).first(3)
15
17
 
16
18
  total = memory[0].split[1].to_i
17
19
  available = memory[2].split[1].to_i
@@ -39,7 +41,7 @@ module LinuxStat
39
41
  # It retuns an Integer but if the info is not available, it will return nil.
40
42
  def total
41
43
  return nil unless meminfo?
42
- IO.foreach('/proc/meminfo').first.split[1].to_i
44
+ IO.foreach('/proc/meminfo'.freeze).first.split[1].to_i
43
45
  end
44
46
 
45
47
  ##
@@ -49,7 +51,7 @@ module LinuxStat
49
51
  # It retuns an Integer but if the info is not available, it will return nil
50
52
  def available
51
53
  return nil unless meminfo?
52
- IO.foreach('/proc/meminfo').first(3)[-1].split[1].to_i
54
+ IO.foreach('/proc/meminfo'.freeze).first(3)[-1].split[1].to_i
53
55
  end
54
56
 
55
57
  ##
@@ -59,7 +61,7 @@ module LinuxStat
59
61
  # It retuns an Integer but if the info is not available, it will return nil.
60
62
  def used
61
63
  return nil unless meminfo?
62
- memory = IO.foreach('/proc/meminfo').first(3)
64
+ memory = IO.foreach('/proc/meminfo'.freeze).first(3)
63
65
  memory[0].split[1].to_i - memory[2].split[1].to_i
64
66
  end
65
67
 
@@ -69,7 +71,7 @@ module LinuxStat
69
71
  # It retuns an Integer but if the info is not available, it will return nil
70
72
  def percent_used
71
73
  return nil unless meminfo?
72
- memory = IO.foreach('/proc/meminfo').first(3)
74
+ memory = IO.foreach('/proc/meminfo'.freeze).first(3)
73
75
  total = memory[0].split[1].to_i
74
76
  total.-(memory[2].split[1].to_i).*(100).fdiv(total).round(2)
75
77
  end
@@ -80,13 +82,13 @@ module LinuxStat
80
82
  # It retuns an Integer but if the info is not available, it will return nil
81
83
  def percent_available
82
84
  return nil unless meminfo?
83
- memory = IO.foreach('/proc/meminfo').first(3)
85
+ memory = IO.foreach('/proc/meminfo'.freeze).first(3)
84
86
  memory[2].split[1].to_i.*(100).fdiv(memory[0].split[1].to_i).round(2)
85
87
  end
86
88
 
87
89
  private
88
90
  def meminfo?
89
- @@readable ||= File.readable?('/proc/meminfo')
91
+ @@readable ||= File.readable?('/proc/meminfo'.freeze)
90
92
  end
91
93
  end
92
94
  end
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various Mounted device related information of the current system.
3
+
2
4
  module Mounts
3
5
  class << self
4
6
  ##
@@ -209,17 +211,17 @@ module LinuxStat
209
211
 
210
212
  private
211
213
  def mount_readable?
212
- @@mount_readable ||= File.readable?('/proc/mounts')
214
+ @@mount_readable ||= File.readable?('/proc/mounts'.freeze)
213
215
  end
214
216
 
215
217
  def mounts
216
218
  return [] unless mount_readable?
217
- IO.readlines('/proc/mounts').each(&:strip!)
219
+ IO.readlines('/proc/mounts'.freeze).each(&:strip!)
218
220
  end
219
221
 
220
222
  def find_root
221
223
  return [] unless mount_readable?
222
- @@root ||= IO.foreach('/proc/mounts').find { |x| x.split[1] == '/'.freeze }.split
224
+ @@root ||= IO.foreach('/proc/mounts'.freeze).find { |x| x.split[1] == '/'.freeze }.split
223
225
  end
224
226
 
225
227
  def fs_info(dev)
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various Net related information of the current system.
3
+
2
4
  module Net
3
5
  class << self
4
6
  DEV = '/proc/net/dev'.freeze
@@ -86,13 +88,13 @@ module LinuxStat
86
88
 
87
89
  data = IO.readlines(DEV).drop(2)
88
90
  indices = find_index_of_bytes
89
- data.reject! { |x| x.strip.start_with?('lo:') }
91
+ data.reject! { |x| x.strip.start_with?('lo:'.freeze) }
90
92
  r, t = data.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map(&:sum)
91
93
 
92
94
  sleep(interval)
93
95
 
94
96
  data2 = IO.readlines(DEV).drop(2)
95
- data2.reject! { |x| x.strip.start_with?('lo:') }
97
+ data2.reject! { |x| x.strip.start_with?('lo:'.freeze) }
96
98
  r2, t2 = data2.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map(&:sum)
97
99
 
98
100
  # Measure the difference
@@ -118,8 +120,8 @@ module LinuxStat
118
120
 
119
121
  r.each_with_index { |x, i|
120
122
  downcased = x.downcase
121
- h.merge!(:r => i) if downcased.start_with?('receive')
122
- h.merge!(:t => i) if downcased.start_with?('transmit')
123
+ h.merge!(:r => i) if downcased.start_with?('receive'.freeze)
124
+ h.merge!(:t => i) if downcased.start_with?('transmit'.freeze)
123
125
  }
124
126
 
125
127
  data_0 = data.next.gsub(?|.freeze, ' %'.freeze)
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various OS related information of the current system.
3
+
2
4
  module OS
3
5
  class << self
4
6
  ##
@@ -52,7 +54,7 @@ module LinuxStat
52
54
  elsif v.key?(:DISTRIB_ID)
53
55
  v[:DISTRIB_ID]
54
56
  elsif File.readable?('/etc/issue'.freeze)
55
- IO.read('/etc/issue'.freeze, encoding: 'ASCII-8bit').strip
57
+ IO.read('/etc/issue'.freeze, encoding: 'ASCII-8bit'.freeze).strip
56
58
  else
57
59
  'Unknown'.freeze
58
60
  end
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various PCI device related information of the current system.
3
+
2
4
  module PCI
3
5
  class << self
4
6
  ##
@@ -130,70 +132,73 @@ module LinuxStat
130
132
  return devices_info(hwdata: hwdata) unless @@sys_pci_readable
131
133
 
132
134
  Dir['/sys/bus/pci/devices/*/'.freeze].sort!.map! { |x|
133
- _vendor_file = File.join(x, 'vendor'.freeze)
134
- next unless File.readable?(_vendor_file)
135
- vendor = IO.read(_vendor_file).to_i(16).to_s(16)
136
- prepend_0(vendor)
137
-
138
- _device_file = File.join(x, 'device'.freeze)
139
- next unless File.readable?(_device_file)
140
- device = IO.read(_device_file).to_i(16).to_s(16)
141
- prepend_0(device)
142
-
143
- _sub_vendor_file = File.join(x, 'subsystem_vendor'.freeze)
144
- sub_vendor = File.readable?(_sub_vendor_file) ? IO.read(_sub_vendor_file).to_i(16).to_s(16) : nil
145
- prepend_0(sub_vendor) if sub_vendor
146
-
147
- _sub_device_file = File.join(x, 'subsystem_device'.freeze)
148
- sub_device = File.readable?(_sub_device_file) ? IO.read(_sub_device_file).to_i(16).to_s(16) : nil
149
- prepend_0(sub_device) if sub_device
150
-
151
- _uevent = File.join(x, 'uevent'.freeze)
152
- uevent = File.readable?(_uevent) ? IO.foreach(_uevent) : nil
153
-
154
- kernel_driver = if uevent
155
- uevent.find { |_x|
156
- _x.split(?=.freeze)[0].to_s.tap(&:strip!) == 'DRIVER'.freeze
157
- } &.split(?=) &.[](1) &.tap(&:strip!)
158
- else
159
- nil
160
- end
135
+ begin
136
+ _vendor_file = File.join(x, 'vendor'.freeze)
137
+ next unless File.readable?(_vendor_file)
138
+ vendor = IO.read(_vendor_file).to_i(16).to_s(16)
139
+ prepend_0(vendor)
140
+
141
+ _device_file = File.join(x, 'device'.freeze)
142
+ next unless File.readable?(_device_file)
143
+ device = IO.read(_device_file).to_i(16).to_s(16)
144
+ prepend_0(device)
145
+
146
+ _sub_vendor_file = File.join(x, 'subsystem_vendor'.freeze)
147
+ sub_vendor = File.readable?(_sub_vendor_file) ? IO.read(_sub_vendor_file).to_i(16).to_s(16) : nil
148
+ prepend_0(sub_vendor) if sub_vendor
149
+
150
+ _sub_device_file = File.join(x, 'subsystem_device'.freeze)
151
+ sub_device = File.readable?(_sub_device_file) ? IO.read(_sub_device_file).to_i(16).to_s(16) : nil
152
+ prepend_0(sub_device) if sub_device
153
+
154
+ _uevent = File.join(x, 'uevent'.freeze)
155
+ uevent = File.readable?(_uevent) ? IO.foreach(_uevent) : nil
156
+
157
+ kernel_driver = if uevent
158
+ uevent.find { |_x|
159
+ _x.split(?=.freeze)[0].to_s.tap(&:strip!) == 'DRIVER'.freeze
160
+ } &.split(?=) &.[](1) &.tap(&:strip!)
161
+ else
162
+ nil
163
+ end
161
164
 
162
- _revision_file = File.join(x, 'revision'.freeze)
163
- revision = File.readable?(_revision_file) ? IO.read(_revision_file).tap(&:strip!) : ''.freeze
165
+ _revision_file = File.join(x, 'revision'.freeze)
166
+ revision = File.readable?(_revision_file) ? IO.read(_revision_file).tap(&:strip!) : ''.freeze
164
167
 
165
- _irq_file = File.join(x, 'irq'.freeze)
166
- irq = File.readable?(_irq_file) ? IO.read(_irq_file).to_i : nil
168
+ _irq_file = File.join(x, 'irq'.freeze)
169
+ irq = File.readable?(_irq_file) ? IO.read(_irq_file).to_i : nil
167
170
 
168
- _enable_file = File.join(x, 'enable'.freeze)
169
- enable = File.readable?(_enable_file) ? IO.read(_enable_file).to_i == 1 : nil
171
+ _enable_file = File.join(x, 'enable'.freeze)
172
+ enable = File.readable?(_enable_file) ? IO.read(_enable_file).to_i == 1 : nil
170
173
 
171
- query = if hwdata && sub_vendor && sub_device
172
- query_hwdata(vendor, device, sub_vendor, sub_device)
173
- elsif hwdata && sub_vendor
174
- query_hwdata(vendor, device, sub_vendor)
175
- elsif hwdata
176
- query_hwdata(vendor, device)
177
- else
178
- {}
179
- end
174
+ query = if hwdata && sub_vendor && sub_device
175
+ query_hwdata(vendor, device, sub_vendor, sub_device)
176
+ elsif hwdata && sub_vendor
177
+ query_hwdata(vendor, device, sub_vendor)
178
+ elsif hwdata
179
+ query_hwdata(vendor, device)
180
+ else
181
+ {}
182
+ end
180
183
 
181
- ret = {
182
- path: x, id: "#{vendor}:#{device}",
183
- vendor: vendor, device: device
184
- }
184
+ ret = {
185
+ path: x, id: "#{vendor}:#{device}",
186
+ vendor: vendor, device: device
187
+ }
185
188
 
186
- ret.merge!(sub_vendor: sub_vendor) if sub_vendor
187
- ret.merge!(sub_device: sub_device) if sub_device
189
+ ret.merge!(sub_vendor: sub_vendor) if sub_vendor
190
+ ret.merge!(sub_device: sub_device) if sub_device
188
191
 
189
- ret.merge!(kernel_driver: kernel_driver) if kernel_driver
190
- ret.merge!(revision: revision) unless revision.empty?
191
- ret.merge!(irq: irq) if irq
192
- ret.merge!(enable: enable) unless enable.nil?
193
- ret.merge!(hwdata: query) unless query.empty?
192
+ ret.merge!(kernel_driver: kernel_driver) if kernel_driver
193
+ ret.merge!(revision: revision) unless revision.empty?
194
+ ret.merge!(irq: irq) if irq
195
+ ret.merge!(enable: enable) unless enable.nil?
196
+ ret.merge!(hwdata: query) unless query.empty?
194
197
 
195
- ret
196
- }
198
+ ret
199
+ rescue StandardError
200
+ end
201
+ }.tap(&:compact!)
197
202
  end
198
203
 
199
204
  ##
@@ -1,4 +1,11 @@
1
1
  module LinuxStat
2
+ # Helps you convert bytes to a unit like:
3
+ #
4
+ # 1. kilobyte, megabyte, gigabyte, terabyte, petabyte, exabyte, zettabyte, yottabyte
5
+ # 2. kibibyte, mebibyte, gibibyte, tebibyte, pebibyte, exbibyte, zebibyte, yobibyte
6
+ # 3. kB, MB, GB, TB, PB, EB, ZB, YB
7
+ # 4. kiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB
8
+
2
9
  module PrettifyBytes
3
10
  class << self
4
11
  ##
@@ -16,13 +23,13 @@ module LinuxStat
16
23
  # LinuxStat::PrettifyBytes.convert_decimal(1024 ** 3)
17
24
  #
18
25
  # => "1.07 gigabytes"
19
- def convert_decimal(n)
26
+ def convert_decimal(n, precision: 2)
20
27
  @@d_units ||= %W(#{''} kilo mega giga tera peta exa zetta)
21
28
  .map.with_index { |x, i| [x, 1000.**(i + 1)] }
22
29
  unit = @@d_units.find { |x| n < x[1] } || ['yotta'.freeze, 10 ** 27]
23
30
 
24
31
  converted = n.fdiv(unit[1] / 1000).round(2)
25
- "#{pad_left(converted)} #{unit[0]}byte#{?s.freeze if converted != 1}"
32
+ "#{pad_left(converted, precision)} #{unit[0]}byte#{?s.freeze if converted != 1}"
26
33
  end
27
34
 
28
35
  # Converts a number to binary byte units and outputs with the IEC prefix
@@ -39,13 +46,13 @@ module LinuxStat
39
46
  # LinuxStat::PrettifyBytes.convert_binary(1024 ** 3)
40
47
  #
41
48
  # => "1.0 gibibyte"
42
- def convert_binary(n)
49
+ def convert_binary(n, precision: 2)
43
50
  @@b_units ||= %W(#{''} kibi mebi gibi tebi pebi exbi zebi)
44
51
  .map.with_index { |x, i| [x, 1024.**(i + 1)] }
45
52
  unit = @@b_units.find { |x| n < x[1] } || ['yobi'.freeze, 10 ** 27]
46
53
 
47
54
  converted = n.fdiv(unit[1] / 1024).round(2)
48
- "#{pad_left(converted)} #{unit[0]}byte#{?s.freeze if converted != 1}"
55
+ "#{pad_left(converted, precision)} #{unit[0]}byte#{?s.freeze if converted != 1}"
49
56
  end
50
57
 
51
58
  # Converts a number to decimal byte units
@@ -62,13 +69,13 @@ module LinuxStat
62
69
  # LinuxStat::PrettifyBytes.convert_short_decimal(1024 ** 3)
63
70
  #
64
71
  # => "1.07 GB"
65
- def convert_short_decimal(n)
72
+ def convert_short_decimal(n, precision: 2)
66
73
  @@sd_units ||= %W(#{''} k M G T P E Z)
67
74
  .map.with_index { |x, i| [x, 1000.**(i + 1)] }
68
75
  unit = @@sd_units.find { |x| n < x[1] } || [?Y.freeze, 10 ** 27]
69
76
 
70
77
  converted = n.fdiv(unit[1] / 1000).round(2)
71
- "#{pad_left(converted)} #{unit[0]}B"
78
+ "#{pad_left(converted, precision)} #{unit[0]}B"
72
79
  end
73
80
 
74
81
  ##
@@ -87,15 +94,15 @@ module LinuxStat
87
94
  # LinuxStat::PrettifyBytes.convert_short_binary(1024 ** 3)
88
95
  #
89
96
  # => "1.0 GiB"
90
- def convert_short_binary(n)
91
- return "#{n} B" if n < 1024
97
+ def convert_short_binary(n, precision: 2)
98
+ return "#{pad_left(n, precision)} B" if n < 1024
92
99
 
93
100
  @@sb_units ||= %W(#{''} K M G T P E Z)
94
101
  .map.with_index { |x, i| [x, 1024.**(i + 1)] }
95
102
  unit = @@sb_units.find { |x| n < x[1] } || [?Y.freeze, 1024 ** 9]
96
103
 
97
104
  converted = n.fdiv(unit[1] / 1024).round(2)
98
- "#{pad_left(converted)} #{unit[0]}iB"
105
+ "#{pad_left(converted, precision)} #{unit[0]}iB"
99
106
  end
100
107
 
101
108
  private