linux_stat 1.1.0 → 1.2.3
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 +208 -74
- data/ext/sysconf/sysconf.c +11 -0
- data/lib/linux_stat.rb +1 -1
- data/lib/linux_stat/memory.rb +7 -7
- data/lib/linux_stat/mounts.rb +3 -3
- data/lib/linux_stat/net.rb +4 -4
- data/lib/linux_stat/os.rb +48 -15
- data/lib/linux_stat/pci.rb +114 -59
- data/lib/linux_stat/process.rb +45 -23
- data/lib/linux_stat/swap.rb +4 -4
- data/lib/linux_stat/usb.rb +103 -49
- data/lib/linux_stat/user.rb +4 -3
- data/lib/linux_stat/version.rb +1 -1
- metadata +2 -2
data/ext/sysconf/sysconf.c
CHANGED
@@ -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
|
}
|
data/lib/linux_stat.rb
CHANGED
@@ -15,8 +15,8 @@
|
|
15
15
|
|
16
16
|
# Miscellaneous Modules
|
17
17
|
# Independed and LinuxStat's miscellaneous modules
|
18
|
-
require "linux_stat/version"
|
19
18
|
require 'linux_stat/prettify_bytes'
|
19
|
+
require "linux_stat/version"
|
20
20
|
|
21
21
|
# Independed Modules
|
22
22
|
# Modules that doesn't have any dependency on its own
|
data/lib/linux_stat/memory.rb
CHANGED
@@ -11,7 +11,7 @@ module LinuxStat
|
|
11
11
|
def stat
|
12
12
|
return {} unless meminfo?
|
13
13
|
|
14
|
-
memory = IO.foreach('/proc/meminfo').first(3)
|
14
|
+
memory = IO.foreach('/proc/meminfo'.freeze).first(3)
|
15
15
|
|
16
16
|
total = memory[0].split[1].to_i
|
17
17
|
available = memory[2].split[1].to_i
|
@@ -39,7 +39,7 @@ module LinuxStat
|
|
39
39
|
# It retuns an Integer but if the info is not available, it will return nil.
|
40
40
|
def total
|
41
41
|
return nil unless meminfo?
|
42
|
-
IO.foreach('/proc/meminfo').first.split[1].to_i
|
42
|
+
IO.foreach('/proc/meminfo'.freeze).first.split[1].to_i
|
43
43
|
end
|
44
44
|
|
45
45
|
##
|
@@ -49,7 +49,7 @@ module LinuxStat
|
|
49
49
|
# It retuns an Integer but if the info is not available, it will return nil
|
50
50
|
def available
|
51
51
|
return nil unless meminfo?
|
52
|
-
IO.foreach('/proc/meminfo').first(3)[-1].split[1].to_i
|
52
|
+
IO.foreach('/proc/meminfo'.freeze).first(3)[-1].split[1].to_i
|
53
53
|
end
|
54
54
|
|
55
55
|
##
|
@@ -59,7 +59,7 @@ module LinuxStat
|
|
59
59
|
# It retuns an Integer but if the info is not available, it will return nil.
|
60
60
|
def used
|
61
61
|
return nil unless meminfo?
|
62
|
-
memory = IO.foreach('/proc/meminfo').first(3)
|
62
|
+
memory = IO.foreach('/proc/meminfo'.freeze).first(3)
|
63
63
|
memory[0].split[1].to_i - memory[2].split[1].to_i
|
64
64
|
end
|
65
65
|
|
@@ -69,7 +69,7 @@ module LinuxStat
|
|
69
69
|
# It retuns an Integer but if the info is not available, it will return nil
|
70
70
|
def percent_used
|
71
71
|
return nil unless meminfo?
|
72
|
-
memory = IO.foreach('/proc/meminfo').first(3)
|
72
|
+
memory = IO.foreach('/proc/meminfo'.freeze).first(3)
|
73
73
|
total = memory[0].split[1].to_i
|
74
74
|
total.-(memory[2].split[1].to_i).*(100).fdiv(total).round(2)
|
75
75
|
end
|
@@ -80,13 +80,13 @@ module LinuxStat
|
|
80
80
|
# It retuns an Integer but if the info is not available, it will return nil
|
81
81
|
def percent_available
|
82
82
|
return nil unless meminfo?
|
83
|
-
memory = IO.foreach('/proc/meminfo').first(3)
|
83
|
+
memory = IO.foreach('/proc/meminfo'.freeze).first(3)
|
84
84
|
memory[2].split[1].to_i.*(100).fdiv(memory[0].split[1].to_i).round(2)
|
85
85
|
end
|
86
86
|
|
87
87
|
private
|
88
88
|
def meminfo?
|
89
|
-
@@readable ||= File.readable?('/proc/meminfo')
|
89
|
+
@@readable ||= File.readable?('/proc/meminfo'.freeze)
|
90
90
|
end
|
91
91
|
end
|
92
92
|
end
|
data/lib/linux_stat/mounts.rb
CHANGED
@@ -209,17 +209,17 @@ module LinuxStat
|
|
209
209
|
|
210
210
|
private
|
211
211
|
def mount_readable?
|
212
|
-
@@mount_readable ||= File.readable?('/proc/mounts')
|
212
|
+
@@mount_readable ||= File.readable?('/proc/mounts'.freeze)
|
213
213
|
end
|
214
214
|
|
215
215
|
def mounts
|
216
216
|
return [] unless mount_readable?
|
217
|
-
IO.readlines('/proc/mounts').each(&:strip!)
|
217
|
+
IO.readlines('/proc/mounts'.freeze).each(&:strip!)
|
218
218
|
end
|
219
219
|
|
220
220
|
def find_root
|
221
221
|
return [] unless mount_readable?
|
222
|
-
@@root ||= IO.foreach('/proc/mounts').find { |x| x.split[1] == '/'.freeze }.split
|
222
|
+
@@root ||= IO.foreach('/proc/mounts'.freeze).find { |x| x.split[1] == '/'.freeze }.split
|
223
223
|
end
|
224
224
|
|
225
225
|
def fs_info(dev)
|
data/lib/linux_stat/net.rb
CHANGED
@@ -86,13 +86,13 @@ module LinuxStat
|
|
86
86
|
|
87
87
|
data = IO.readlines(DEV).drop(2)
|
88
88
|
indices = find_index_of_bytes
|
89
|
-
data.reject! { |x| x.strip.start_with?('lo:') }
|
89
|
+
data.reject! { |x| x.strip.start_with?('lo:'.freeze) }
|
90
90
|
r, t = data.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map(&:sum)
|
91
91
|
|
92
92
|
sleep(interval)
|
93
93
|
|
94
94
|
data2 = IO.readlines(DEV).drop(2)
|
95
|
-
data2.reject! { |x| x.strip.start_with?('lo:') }
|
95
|
+
data2.reject! { |x| x.strip.start_with?('lo:'.freeze) }
|
96
96
|
r2, t2 = data2.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map(&:sum)
|
97
97
|
|
98
98
|
# Measure the difference
|
@@ -118,8 +118,8 @@ module LinuxStat
|
|
118
118
|
|
119
119
|
r.each_with_index { |x, i|
|
120
120
|
downcased = x.downcase
|
121
|
-
h.merge!(:r => i) if downcased.start_with?('receive')
|
122
|
-
h.merge!(:t => i) if downcased.start_with?('transmit')
|
121
|
+
h.merge!(:r => i) if downcased.start_with?('receive'.freeze)
|
122
|
+
h.merge!(:t => i) if downcased.start_with?('transmit'.freeze)
|
123
123
|
}
|
124
124
|
|
125
125
|
data_0 = data.next.gsub(?|.freeze, ' %'.freeze)
|
data/lib/linux_stat/os.rb
CHANGED
@@ -51,14 +51,46 @@ module LinuxStat
|
|
51
51
|
v[:DISTRIB_DESCRIPTION]
|
52
52
|
elsif v.key?(:DISTRIB_ID)
|
53
53
|
v[:DISTRIB_ID]
|
54
|
-
elsif File.readable?('/etc/issue')
|
55
|
-
IO.read('/etc/issue').strip
|
54
|
+
elsif File.readable?('/etc/issue'.freeze)
|
55
|
+
IO.read('/etc/issue'.freeze, encoding: 'ASCII-8bit'.freeze).strip
|
56
56
|
else
|
57
57
|
'Unknown'.freeze
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
+
##
|
63
|
+
# Gives you the version of the OS you are using.
|
64
|
+
#
|
65
|
+
# On ArchLinux for example:
|
66
|
+
# LinuxStat::OS.version
|
67
|
+
#
|
68
|
+
# => "rolling"
|
69
|
+
#
|
70
|
+
# On Ubuntu 20.04:
|
71
|
+
# LinuxStat::OS.version
|
72
|
+
#
|
73
|
+
# => "20.04"
|
74
|
+
#
|
75
|
+
# On CentOS 26:
|
76
|
+
# LinuxStat::OS.version
|
77
|
+
#
|
78
|
+
# => "26"
|
79
|
+
#
|
80
|
+
# The return type is String.
|
81
|
+
# But if the information isn't available, it will return an empty frozen String.
|
82
|
+
def version
|
83
|
+
@@distrib_version ||= if os_release.key?(:VERSION_ID)
|
84
|
+
os_release[:VERSION_ID]
|
85
|
+
elsif lsb_release.key?(:DISTRIB_RELEASE)
|
86
|
+
lsb_release[:DISTRIB_RELEASE]
|
87
|
+
elsif os_release.key?(:VERSION)
|
88
|
+
os_release[:VERSION]
|
89
|
+
else
|
90
|
+
''.freeze
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
62
94
|
##
|
63
95
|
# Uses utsname.h to determine the machine
|
64
96
|
#
|
@@ -70,22 +102,18 @@ module LinuxStat
|
|
70
102
|
##
|
71
103
|
# Uses utsname.h to determine the system nodename
|
72
104
|
#
|
73
|
-
# It returns String but if the info isn't available, it will return an empty String
|
105
|
+
# It returns String but if the info isn't available, it will return an empty String.
|
74
106
|
def nodename
|
75
|
-
|
107
|
+
LinuxStat::Uname.nodename
|
76
108
|
end
|
77
109
|
|
78
110
|
##
|
79
|
-
#
|
111
|
+
# Returns the hostname from LinuxStat::Sysconf.hostname.
|
80
112
|
#
|
81
113
|
# The return type is String.
|
82
|
-
# If the info info isn't available, it will return
|
114
|
+
# If the info info isn't available, it will return an empty String.
|
83
115
|
def hostname
|
84
|
-
|
85
|
-
IO.read('/etc/hostname').strip
|
86
|
-
else
|
87
|
-
'localhost'
|
88
|
-
end
|
116
|
+
LinuxStat::Sysconf.hostname
|
89
117
|
end
|
90
118
|
|
91
119
|
##
|
@@ -114,7 +142,7 @@ module LinuxStat
|
|
114
142
|
def uptime
|
115
143
|
return {} unless uptime_readable?
|
116
144
|
|
117
|
-
uptime = IO.read('/proc/uptime').to_f
|
145
|
+
uptime = IO.read('/proc/uptime'.freeze).to_f
|
118
146
|
uptime_i = uptime.to_i
|
119
147
|
|
120
148
|
h = uptime_i / 3600
|
@@ -128,16 +156,21 @@ module LinuxStat
|
|
128
156
|
}.freeze
|
129
157
|
end
|
130
158
|
|
159
|
+
alias distrib_version version
|
160
|
+
|
131
161
|
private
|
132
162
|
def release(file)
|
133
163
|
IO.readlines(file, 4096).reduce({}) { |h, x|
|
134
164
|
x.strip!
|
135
165
|
next h if x.empty?
|
136
166
|
|
137
|
-
splitted = x.split(
|
167
|
+
splitted = x.split(?=.freeze)
|
168
|
+
|
169
|
+
key = splitted[0].to_s
|
170
|
+
key.strip!
|
138
171
|
|
139
|
-
|
140
|
-
value
|
172
|
+
value = splitted[1..-1].join(?=.freeze).to_s
|
173
|
+
value.strip!
|
141
174
|
|
142
175
|
value[0] = ''.freeze if value[0] == ?".freeze
|
143
176
|
value[-1] = ''.freeze if value[-1] == ?".freeze
|
data/lib/linux_stat/pci.rb
CHANGED
@@ -6,11 +6,11 @@ module LinuxStat
|
|
6
6
|
#
|
7
7
|
# [ Not to be confused with devices_stat ]
|
8
8
|
#
|
9
|
-
# Take a look at
|
9
|
+
# Take a look at LinuxStat::PCI.devices_stat for more info.
|
10
10
|
#
|
11
11
|
# Returns details about the devices found in /proc/bus/pci/devices file.
|
12
12
|
#
|
13
|
-
# The details doesn't contain a lot of details, it
|
13
|
+
# The details doesn't contain a lot of details, it opens just one file.
|
14
14
|
#
|
15
15
|
# The return value is an Array of multiple Hashes. If there's no info available,
|
16
16
|
# it will rather return an empty Array.
|
@@ -130,70 +130,73 @@ module LinuxStat
|
|
130
130
|
return devices_info(hwdata: hwdata) unless @@sys_pci_readable
|
131
131
|
|
132
132
|
Dir['/sys/bus/pci/devices/*/'.freeze].sort!.map! { |x|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
133
|
+
begin
|
134
|
+
_vendor_file = File.join(x, 'vendor'.freeze)
|
135
|
+
next unless File.readable?(_vendor_file)
|
136
|
+
vendor = IO.read(_vendor_file).to_i(16).to_s(16)
|
137
|
+
prepend_0(vendor)
|
138
|
+
|
139
|
+
_device_file = File.join(x, 'device'.freeze)
|
140
|
+
next unless File.readable?(_device_file)
|
141
|
+
device = IO.read(_device_file).to_i(16).to_s(16)
|
142
|
+
prepend_0(device)
|
143
|
+
|
144
|
+
_sub_vendor_file = File.join(x, 'subsystem_vendor'.freeze)
|
145
|
+
sub_vendor = File.readable?(_sub_vendor_file) ? IO.read(_sub_vendor_file).to_i(16).to_s(16) : nil
|
146
|
+
prepend_0(sub_vendor) if sub_vendor
|
147
|
+
|
148
|
+
_sub_device_file = File.join(x, 'subsystem_device'.freeze)
|
149
|
+
sub_device = File.readable?(_sub_device_file) ? IO.read(_sub_device_file).to_i(16).to_s(16) : nil
|
150
|
+
prepend_0(sub_device) if sub_device
|
151
|
+
|
152
|
+
_uevent = File.join(x, 'uevent'.freeze)
|
153
|
+
uevent = File.readable?(_uevent) ? IO.foreach(_uevent) : nil
|
154
|
+
|
155
|
+
kernel_driver = if uevent
|
156
|
+
uevent.find { |_x|
|
157
|
+
_x.split(?=.freeze)[0].to_s.tap(&:strip!) == 'DRIVER'.freeze
|
158
|
+
} &.split(?=) &.[](1) &.tap(&:strip!)
|
159
|
+
else
|
160
|
+
nil
|
161
|
+
end
|
161
162
|
|
162
|
-
|
163
|
-
|
163
|
+
_revision_file = File.join(x, 'revision'.freeze)
|
164
|
+
revision = File.readable?(_revision_file) ? IO.read(_revision_file).tap(&:strip!) : ''.freeze
|
164
165
|
|
165
|
-
|
166
|
-
|
166
|
+
_irq_file = File.join(x, 'irq'.freeze)
|
167
|
+
irq = File.readable?(_irq_file) ? IO.read(_irq_file).to_i : nil
|
167
168
|
|
168
|
-
|
169
|
-
|
169
|
+
_enable_file = File.join(x, 'enable'.freeze)
|
170
|
+
enable = File.readable?(_enable_file) ? IO.read(_enable_file).to_i == 1 : nil
|
170
171
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
172
|
+
query = if hwdata && sub_vendor && sub_device
|
173
|
+
query_hwdata(vendor, device, sub_vendor, sub_device)
|
174
|
+
elsif hwdata && sub_vendor
|
175
|
+
query_hwdata(vendor, device, sub_vendor)
|
176
|
+
elsif hwdata
|
177
|
+
query_hwdata(vendor, device)
|
178
|
+
else
|
179
|
+
{}
|
180
|
+
end
|
180
181
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
182
|
+
ret = {
|
183
|
+
path: x, id: "#{vendor}:#{device}",
|
184
|
+
vendor: vendor, device: device
|
185
|
+
}
|
185
186
|
|
186
|
-
|
187
|
-
|
187
|
+
ret.merge!(sub_vendor: sub_vendor) if sub_vendor
|
188
|
+
ret.merge!(sub_device: sub_device) if sub_device
|
188
189
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
190
|
+
ret.merge!(kernel_driver: kernel_driver) if kernel_driver
|
191
|
+
ret.merge!(revision: revision) unless revision.empty?
|
192
|
+
ret.merge!(irq: irq) if irq
|
193
|
+
ret.merge!(enable: enable) unless enable.nil?
|
194
|
+
ret.merge!(hwdata: query) unless query.empty?
|
194
195
|
|
195
|
-
|
196
|
-
|
196
|
+
ret
|
197
|
+
rescue StandardError
|
198
|
+
end
|
199
|
+
}.tap(&:compact!)
|
197
200
|
end
|
198
201
|
|
199
202
|
##
|
@@ -227,11 +230,63 @@ module LinuxStat
|
|
227
230
|
end
|
228
231
|
end
|
229
232
|
|
233
|
+
##
|
234
|
+
# hwdata_file = file
|
235
|
+
#
|
236
|
+
# Lets you set the hwdata_file about pci.ids.
|
237
|
+
#
|
238
|
+
# The hwdata file about pci.ids contains vendor name and product name information about
|
239
|
+
# devices. This is then mapped by the other methods that utilizes hwdata/pci.ids.
|
240
|
+
#
|
241
|
+
# Do note that this method is intended to run only once, at the beginning.
|
242
|
+
# If you use any other method that utilizes hwdata/pci.ids, before
|
243
|
+
# calling this method, this method will not work.
|
244
|
+
def hwdata_file=(file)
|
245
|
+
@@hwdata_file ||= file.freeze
|
246
|
+
end
|
247
|
+
|
248
|
+
##
|
249
|
+
# Checks if hwdata_file is already initialized or not.
|
250
|
+
# Once it's initialized, calling hwdata_file = 'something/pci.ids' is futile.
|
251
|
+
def hwdata_file_set?
|
252
|
+
@@hwdata_file ||= false
|
253
|
+
!!@@hwdata_file
|
254
|
+
end
|
255
|
+
|
256
|
+
##
|
257
|
+
# Returns the hwdata_file as string.
|
258
|
+
#
|
259
|
+
# If hwdata_file isn't set, it will return an empty frozen string.
|
260
|
+
#
|
261
|
+
# Once it's set, it can't be changed.
|
262
|
+
def hwdata_file
|
263
|
+
@@hwdata_file ||= nil
|
264
|
+
@@hwdata_file ? @@hwdata_file : ''.freeze
|
265
|
+
end
|
266
|
+
|
267
|
+
##
|
268
|
+
# Initializes hwdata
|
269
|
+
#
|
270
|
+
# hwdata can take upto 0.1 to 0.2 seconds to get initialized.
|
271
|
+
#
|
272
|
+
# Calling this method will load hwdata for future use.
|
273
|
+
#
|
274
|
+
# Once it's initialized, hwdata_file can't be changed.
|
275
|
+
#
|
276
|
+
# If this method initializes hwdata, it will return true
|
277
|
+
# Othewise this method will return false.
|
278
|
+
def initialize_hwdata
|
279
|
+
@@hwdata ||= nil
|
280
|
+
init = !@@hwdata
|
281
|
+
hwdata
|
282
|
+
init
|
283
|
+
end
|
284
|
+
|
230
285
|
alias count_devices count
|
231
286
|
|
232
287
|
private
|
233
288
|
def hwdata
|
234
|
-
@@hwdata_file ||= "/usr/share/hwdata/pci.ids"
|
289
|
+
@@hwdata_file ||= "/usr/share/hwdata/pci.ids".freeze
|
235
290
|
|
236
291
|
@@hwdata ||= if File.readable?(@@hwdata_file)
|
237
292
|
ret, vendor_id, device_id, device = {}, nil, nil, nil
|