linux_stat 1.1.1 → 1.3.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.
@@ -119,6 +119,15 @@ static VALUE getEUID(VALUE obj) {
119
119
  return INT2FIX(geteuid()) ;
120
120
  }
121
121
 
122
+ static VALUE getHostname(VALUE obj) {
123
+ int h_max = sysconf(_SC_HOST_NAME_MAX) + 1 ;
124
+ char hostname[h_max] ;
125
+
126
+ short int status = gethostname(hostname, h_max) ;
127
+
128
+ return (status < 0) ? rb_str_new_cstr("") : rb_str_new_cstr(hostname) ;
129
+ }
130
+
122
131
  void Init_sysconf() {
123
132
  VALUE _linux_stat = rb_define_module("LinuxStat") ;
124
133
  VALUE _sysconf = rb_define_module_under(_linux_stat, "Sysconf") ;
@@ -144,4 +153,6 @@ void Init_sysconf() {
144
153
 
145
154
  rb_define_module_function(_sysconf, "get_user", getUser, 0) ;
146
155
  rb_define_module_function(_sysconf, "get_login", getUser, 0) ;
156
+
157
+ rb_define_module_function(_sysconf, "hostname", getHostname, 0) ;
147
158
  }
@@ -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
  ##
@@ -51,14 +53,46 @@ module LinuxStat
51
53
  v[:DISTRIB_DESCRIPTION]
52
54
  elsif v.key?(:DISTRIB_ID)
53
55
  v[:DISTRIB_ID]
54
- elsif File.readable?('/etc/issue')
55
- IO.read('/etc/issue').strip
56
+ elsif File.readable?('/etc/issue'.freeze)
57
+ IO.read('/etc/issue'.freeze, encoding: 'ASCII-8bit'.freeze).strip
56
58
  else
57
59
  'Unknown'.freeze
58
60
  end
59
61
  end
60
62
  end
61
63
 
64
+ ##
65
+ # Gives you the version of the OS you are using.
66
+ #
67
+ # On ArchLinux for example:
68
+ # LinuxStat::OS.version
69
+ #
70
+ # => "rolling"
71
+ #
72
+ # On Ubuntu 20.04:
73
+ # LinuxStat::OS.version
74
+ #
75
+ # => "20.04"
76
+ #
77
+ # On CentOS 26:
78
+ # LinuxStat::OS.version
79
+ #
80
+ # => "26"
81
+ #
82
+ # The return type is String.
83
+ # But if the information isn't available, it will return an empty frozen String.
84
+ def version
85
+ @@distrib_version ||= if os_release.key?(:VERSION_ID)
86
+ os_release[:VERSION_ID]
87
+ elsif lsb_release.key?(:DISTRIB_RELEASE)
88
+ lsb_release[:DISTRIB_RELEASE]
89
+ elsif os_release.key?(:VERSION)
90
+ os_release[:VERSION]
91
+ else
92
+ ''.freeze
93
+ end
94
+ end
95
+
62
96
  ##
63
97
  # Uses utsname.h to determine the machine
64
98
  #
@@ -70,22 +104,18 @@ module LinuxStat
70
104
  ##
71
105
  # Uses utsname.h to determine the system nodename
72
106
  #
73
- # It returns String but if the info isn't available, it will return an empty String
107
+ # It returns String but if the info isn't available, it will return an empty String.
74
108
  def nodename
75
- @@nodename ||= LinuxStat::Uname.nodename
109
+ LinuxStat::Uname.nodename
76
110
  end
77
111
 
78
112
  ##
79
- # Reads /etc/hostname and returns the hostname.
113
+ # Returns the hostname from LinuxStat::Sysconf.hostname.
80
114
  #
81
115
  # The return type is String.
82
- # If the info info isn't available, it will return 'localhost'.
116
+ # If the info info isn't available, it will return an empty String.
83
117
  def hostname
84
- @@hostname ||= if File.exist?('/etc/hostname')
85
- IO.read('/etc/hostname').strip
86
- else
87
- 'localhost'
88
- end
118
+ LinuxStat::Sysconf.hostname
89
119
  end
90
120
 
91
121
  ##
@@ -114,7 +144,7 @@ module LinuxStat
114
144
  def uptime
115
145
  return {} unless uptime_readable?
116
146
 
117
- uptime = IO.read('/proc/uptime').to_f
147
+ uptime = IO.read('/proc/uptime'.freeze).to_f
118
148
  uptime_i = uptime.to_i
119
149
 
120
150
  h = uptime_i / 3600
@@ -128,16 +158,21 @@ module LinuxStat
128
158
  }.freeze
129
159
  end
130
160
 
161
+ alias distrib_version version
162
+
131
163
  private
132
164
  def release(file)
133
165
  IO.readlines(file, 4096).reduce({}) { |h, x|
134
166
  x.strip!
135
167
  next h if x.empty?
136
168
 
137
- splitted = x.split(?=)
169
+ splitted = x.split(?=.freeze)
170
+
171
+ key = splitted[0].to_s
172
+ key.strip!
138
173
 
139
- key = splitted[0].to_s.strip
140
- value = splitted[1..-1].join(?=).to_s.strip
174
+ value = splitted[1..-1].join(?=.freeze).to_s
175
+ value.strip!
141
176
 
142
177
  value[0] = ''.freeze if value[0] == ?".freeze
143
178
  value[-1] = ''.freeze if value[-1] == ?".freeze
@@ -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
  ##