linux_stat 1.0.3 → 1.2.2
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 +250 -92
- data/ext/sysconf/sysconf.c +11 -0
- data/lib/linux_stat.rb +2 -1
- data/lib/linux_stat/os.rb +48 -15
- data/lib/linux_stat/pci.rb +366 -0
- data/lib/linux_stat/usb.rb +113 -57
- data/lib/linux_stat/version.rb +1 -1
- metadata +3 -2
data/lib/linux_stat/usb.rb
CHANGED
@@ -14,13 +14,13 @@ module LinuxStat
|
|
14
14
|
#
|
15
15
|
# It can have information like:
|
16
16
|
#
|
17
|
-
# id, vendor id, product id, manufacturer, serial, bus number, dev number,
|
17
|
+
# path, id, vendor id, product id, manufacturer, serial, bus number, dev number,
|
18
18
|
# b_max_power, b_max_packet_size, etc.
|
19
19
|
#
|
20
20
|
# An example of the returned sample from a test machine is:
|
21
21
|
# LinuxStat::USB.devices_stat
|
22
22
|
#
|
23
|
-
# [{:path=>"/sys/bus/usb/devices/1-1.2/", :id=>"04d9:1203", :vendor_id=>"04d9", :product_id=>"1203", :bus_num=>1, :dev_num=>4, :hwdata=>{:vendor=>"Holtek Semiconductor, Inc.", :product=>"Keyboard"}, :authorized=>true, :b_max_power=>"100mA", :b_max_packet_size0=>8}
|
23
|
+
# => [{:path=>"/sys/bus/usb/devices/1-1.2/", :id=>"04d9:1203", :vendor_id=>"04d9", :product_id=>"1203", :bus_num=>1, :dev_num=>4, :hwdata=>{:vendor=>"Holtek Semiconductor, Inc.", :product=>"Keyboard"}, :authorized=>true, :b_max_power=>"100mA", :b_max_packet_size0=>8}
|
24
24
|
#
|
25
25
|
# Right, it's an array of Hashes.
|
26
26
|
#
|
@@ -49,71 +49,75 @@ module LinuxStat
|
|
49
49
|
return [] unless @@sys_usb_readable
|
50
50
|
|
51
51
|
Dir['/sys/bus/usb/devices/*/'.freeze].sort!.map! { |x|
|
52
|
-
|
53
|
-
|
54
|
-
|
52
|
+
begin
|
53
|
+
id_vendor_file = File.join(x, 'idVendor'.freeze)
|
54
|
+
next unless File.readable?(id_vendor_file)
|
55
|
+
id_vendor = IO.read(id_vendor_file).strip
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
57
|
+
id_product_file = File.join(x, 'idProduct'.freeze)
|
58
|
+
next unless File.readable?(id_vendor_file)
|
59
|
+
id_product = IO.read(id_product_file).strip
|
59
60
|
|
60
|
-
|
61
|
-
|
61
|
+
bus_num_file = File.join(x, 'busnum'.freeze)
|
62
|
+
bus_num = File.readable?(bus_num_file) ? IO.read(bus_num_file).strip : ''.freeze
|
62
63
|
|
63
|
-
|
64
|
-
|
64
|
+
dev_num_file = File.join(x, 'devnum'.freeze)
|
65
|
+
dev_num = File.readable?(dev_num_file) ? IO.read(dev_num_file).strip : ''.freeze
|
65
66
|
|
66
|
-
|
67
|
-
|
67
|
+
serial_file = File.join(x, 'serial'.freeze)
|
68
|
+
serial = File.readable?(serial_file) ? IO.read(serial_file).strip : ''.freeze
|
68
69
|
|
69
|
-
|
70
|
-
|
70
|
+
product_file = File.join(x, 'product'.freeze)
|
71
|
+
product = File.readable?(product_file) ? IO.read(product_file).strip : ''.freeze
|
71
72
|
|
72
|
-
|
73
|
-
|
73
|
+
manufacturer_file = File.join(x, 'manufacturer'.freeze)
|
74
|
+
manufacturer = File.readable?(manufacturer_file) ? IO.read(manufacturer_file).strip : ''.freeze
|
74
75
|
|
75
|
-
|
76
|
-
|
76
|
+
removable_file = File.join(x, 'removable'.freeze)
|
77
|
+
removable = File.readable?(removable_file) ? IO.read(removable_file).strip.downcase : ''.freeze
|
77
78
|
|
78
|
-
|
79
|
-
|
79
|
+
authorized_file = File.join(x, 'authorized'.freeze)
|
80
|
+
authorized = File.readable?(authorized_file) ? IO.read(authorized_file).to_i : ''.freeze
|
80
81
|
|
81
|
-
|
82
|
-
|
82
|
+
b_max_power_file = File.join(x, 'bMaxPower'.freeze)
|
83
|
+
b_max_power = File.readable?(b_max_power_file) ? IO.read(b_max_power_file).strip : ''.freeze
|
83
84
|
|
84
|
-
|
85
|
-
|
85
|
+
b_max_packet_size0_file = File.join(x, 'bMaxPacketSize0'.freeze)
|
86
|
+
b_max_packet_size0 = File.readable?(b_max_packet_size0_file) ? IO.read(b_max_packet_size0_file).to_i : ''.freeze
|
86
87
|
|
87
|
-
|
88
|
+
query = hwdata ? query_hwdata(id_vendor, id_product) : {}
|
88
89
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
90
|
+
is_removable = if removable == 'removable'.freeze
|
91
|
+
true
|
92
|
+
elsif removable == 'unknown'.freeze
|
93
|
+
nil
|
94
|
+
else
|
95
|
+
false
|
96
|
+
end
|
97
|
+
|
98
|
+
ret = {
|
99
|
+
path: x, id: "#{id_vendor}:#{id_product}",
|
100
|
+
vendor_id: id_vendor, product_id: id_product
|
101
|
+
}
|
97
102
|
|
98
|
-
|
99
|
-
|
100
|
-
vendor_id: id_vendor, product_id: id_product
|
101
|
-
}
|
103
|
+
ret.merge!(bus_num: bus_num.to_i) unless bus_num.empty?
|
104
|
+
ret.merge!(dev_num: dev_num.to_i) unless dev_num.empty?
|
102
105
|
|
103
|
-
|
104
|
-
ret.merge!(dev_num: dev_num.to_i) unless dev_num.empty?
|
106
|
+
ret.merge!(serial: serial) unless serial.empty?
|
105
107
|
|
106
|
-
|
108
|
+
ret.merge!(hwdata: query) unless query.empty?
|
109
|
+
ret.merge!(product: product) unless product.empty?
|
110
|
+
ret.merge!(manufacturer: manufacturer) unless manufacturer.empty?
|
107
111
|
|
108
|
-
|
109
|
-
|
110
|
-
ret.merge!(manufacturer: manufacturer) unless manufacturer.empty?
|
112
|
+
ret.merge!(removable: is_removable) unless is_removable.nil?
|
113
|
+
ret.merge!(authorized: authorized == 1)
|
111
114
|
|
112
|
-
|
113
|
-
|
115
|
+
ret.merge!(b_max_power: b_max_power) unless b_max_power.empty?
|
116
|
+
ret.merge!(b_max_packet_size0: b_max_packet_size0) if b_max_packet_size0
|
114
117
|
|
115
|
-
|
116
|
-
|
118
|
+
ret
|
119
|
+
rescue StandardError
|
120
|
+
end
|
117
121
|
}.tap(&:compact!)
|
118
122
|
end
|
119
123
|
|
@@ -141,11 +145,63 @@ module LinuxStat
|
|
141
145
|
}
|
142
146
|
end
|
143
147
|
|
148
|
+
##
|
149
|
+
# hwdata_file = file
|
150
|
+
#
|
151
|
+
# Lets you set the hwdata_file about usb.ids.
|
152
|
+
#
|
153
|
+
# The hwdata file about usb.ids contains vendor name and product name information about
|
154
|
+
# devices. This is then mapped by the other methods that utilizes hwdata/usb.ids.
|
155
|
+
#
|
156
|
+
# Do note that this method is intended to run only once, at the beginning.
|
157
|
+
# If you use any other method that utilizes hwdata/usb.ids, before
|
158
|
+
# calling this method, this method will not work.
|
159
|
+
def hwdata_file=(file)
|
160
|
+
@@hwdata_file ||= file.freeze
|
161
|
+
end
|
162
|
+
|
163
|
+
##
|
164
|
+
# Checks if hwdata_file is already initialized or not.
|
165
|
+
# Once it's initialized, calling hwdata_file = 'something/usb.ids' is futile.
|
166
|
+
def hwdata_file_set?
|
167
|
+
@@hwdata_file ||= nil
|
168
|
+
!!@@hwdata_file
|
169
|
+
end
|
170
|
+
|
171
|
+
##
|
172
|
+
# Returns the hwdata_file as string.
|
173
|
+
#
|
174
|
+
# If hwdata_file isn't set, it will return an empty frozen string.
|
175
|
+
#
|
176
|
+
# Once it's set, it can't be changed.
|
177
|
+
def hwdata_file
|
178
|
+
@@hwdata_file ||= nil
|
179
|
+
@@hwdata_file ? @@hwdata_file : ''.freeze
|
180
|
+
end
|
181
|
+
|
182
|
+
##
|
183
|
+
# Initializes hwdata
|
184
|
+
#
|
185
|
+
# hwdata can take upto 0.1 to 0.2 seconds to get initialized.
|
186
|
+
#
|
187
|
+
# Calling this method will load hwdata for future use.
|
188
|
+
#
|
189
|
+
# Once it's initialized, hwdata_file can't be changed.
|
190
|
+
#
|
191
|
+
# If this method initializes hwdata, it will return true
|
192
|
+
# Othewise this method will return false.
|
193
|
+
def initialize_hwdata
|
194
|
+
@@hwdata ||= nil
|
195
|
+
init = !@@hwdata
|
196
|
+
hwdata
|
197
|
+
init
|
198
|
+
end
|
199
|
+
|
144
200
|
alias count_devices count
|
145
201
|
|
146
202
|
private
|
147
203
|
def hwdata
|
148
|
-
@@hwdata_file ||= "/usr/share/hwdata/usb.ids"
|
204
|
+
@@hwdata_file ||= "/usr/share/hwdata/usb.ids".freeze
|
149
205
|
|
150
206
|
@@hwdata ||= if File.readable?(@@hwdata_file)
|
151
207
|
file_data = IO.readlines(@@hwdata_file, encoding: 'ASCII-8BIT')
|
@@ -163,19 +219,19 @@ module LinuxStat
|
|
163
219
|
if x[0] == ?\t.freeze
|
164
220
|
next unless vendor_id
|
165
221
|
|
166
|
-
|
167
|
-
device_id =
|
168
|
-
device =
|
222
|
+
x.strip!
|
223
|
+
device_id = x[/\A.*?\s/].to_s.strip
|
224
|
+
device = x[device_id.length..-1].to_s.strip
|
169
225
|
ret[vendor_id][1][device_id] = device
|
170
226
|
else
|
171
|
-
|
172
|
-
vendor_id =
|
173
|
-
vendor =
|
227
|
+
x.strip!
|
228
|
+
vendor_id = x[/\A.*?\s/].to_s.strip
|
229
|
+
vendor = x[vendor_id.length..-1].to_s.strip
|
174
230
|
ret[vendor_id] = [vendor, {}]
|
175
231
|
end
|
176
232
|
end
|
177
233
|
|
178
|
-
ret
|
234
|
+
ret.freeze
|
179
235
|
else
|
180
236
|
{}
|
181
237
|
end
|
data/lib/linux_stat/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linux_stat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sourav Goswami
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-12-
|
11
|
+
date: 2020-12-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Linux only, efficient linux system utilization reporting and system monitoring
|
14
14
|
gem
|
@@ -47,6 +47,7 @@ files:
|
|
47
47
|
- lib/linux_stat/mounts.rb
|
48
48
|
- lib/linux_stat/net.rb
|
49
49
|
- lib/linux_stat/os.rb
|
50
|
+
- lib/linux_stat/pci.rb
|
50
51
|
- lib/linux_stat/prettify_bytes.rb
|
51
52
|
- lib/linux_stat/process.rb
|
52
53
|
- lib/linux_stat/process_info.rb
|