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.
- checksums.yaml +4 -4
- data/LICENSE.txt +21 -0
- data/README.md +522 -45
- data/bin/console +1 -1
- data/bin/setup +0 -2
- data/{bin → exe}/linuxstat.rb +43 -7
- data/ext/fs_stat/fs_stat.c +4 -1
- data/ext/sysconf/extconf.rb +7 -0
- data/ext/sysconf/sysconf.c +81 -0
- data/ext/utsname/utsname.c +27 -5
- data/lib/linux_stat.rb +15 -5
- data/lib/linux_stat/battery.rb +2 -2
- data/lib/linux_stat/bios.rb +10 -6
- data/lib/linux_stat/cpu.rb +24 -7
- data/lib/linux_stat/filesystem.rb +5 -7
- data/lib/linux_stat/kernel.rb +16 -8
- data/lib/linux_stat/memory.rb +8 -2
- data/lib/linux_stat/mounts.rb +152 -1
- data/lib/linux_stat/os.rb +11 -9
- data/lib/linux_stat/prettify_bytes.rb +75 -0
- data/lib/linux_stat/process_info.rb +381 -0
- data/lib/linux_stat/swap.rb +6 -1
- data/lib/linux_stat/user.rb +298 -0
- data/lib/linux_stat/version.rb +1 -1
- metadata +16 -10
@@ -0,0 +1,381 @@
|
|
1
|
+
module LinuxStat
|
2
|
+
module ProcessInfo
|
3
|
+
class << self
|
4
|
+
# total_io(pid = $$)
|
5
|
+
# Where pid is the process ID.
|
6
|
+
# By default it is the id of the current process ($$)
|
7
|
+
#
|
8
|
+
# It retuns the total read/write caused by a process.
|
9
|
+
# The output is Hash. For example, a sample output:
|
10
|
+
# {:read_bytes=>0, :write_bytes=>0}
|
11
|
+
#
|
12
|
+
# The output is only based on the total disk IO the process has done.
|
13
|
+
#
|
14
|
+
# If the info isn't available it will return an empty Hash.
|
15
|
+
def total_io(pid = $$)
|
16
|
+
return {} unless File.readable?("/proc/#{pid}/io".freeze)
|
17
|
+
out = {}
|
18
|
+
|
19
|
+
IO.readlines("/proc/#{pid}/io".freeze).each { |x|
|
20
|
+
x.strip!
|
21
|
+
|
22
|
+
if x[/^(read|write)_bytes:\s*\d*$/]
|
23
|
+
splitted = x.split
|
24
|
+
out.merge!(splitted[0].split(?:)[0].to_sym => splitted[-1].to_i)
|
25
|
+
end
|
26
|
+
}
|
27
|
+
|
28
|
+
out
|
29
|
+
end
|
30
|
+
|
31
|
+
# cmdline(pid = $$)
|
32
|
+
# Where pid is the process ID.
|
33
|
+
# By default it is the id of the current process ($$)
|
34
|
+
#
|
35
|
+
# It retuns the total command of the process.
|
36
|
+
# The output is String. For example, a sample output:
|
37
|
+
# "ruby -r linux_stat -e p LinuxStat::ProcessInfo.cmdline"
|
38
|
+
#
|
39
|
+
# If the info isn't available it will return an empty frozen String.
|
40
|
+
def cmdline(pid = $$)
|
41
|
+
file = "/proc/#{pid}/cmdline".freeze
|
42
|
+
return ''.freeze unless File.readable?(file)
|
43
|
+
|
44
|
+
_cmdline = IO.read(file)
|
45
|
+
_cmdline.gsub!(?\u0000, ?\s)
|
46
|
+
_cmdline.tap(&:strip!)
|
47
|
+
end
|
48
|
+
|
49
|
+
# command_name(pid = $$)
|
50
|
+
# Where pid is the process ID.
|
51
|
+
# By default it is the id of the current process ($$)
|
52
|
+
#
|
53
|
+
# It retuns the total command name of the process.
|
54
|
+
# The output is String. For example, a sample output:
|
55
|
+
# "ruby"
|
56
|
+
#
|
57
|
+
# If the info isn't available it will return an empty frozen String.
|
58
|
+
def command_name(pid = $$)
|
59
|
+
# Do note that the /proc/ppid/comm may not contain the full name
|
60
|
+
file = "/proc/#{pid}/cmdline".freeze
|
61
|
+
return ''.freeze unless File.readable?(file)
|
62
|
+
|
63
|
+
_cmdline = IO.read(file)
|
64
|
+
_cmdline.gsub!(?\u0000, ?\s)
|
65
|
+
File.split(_cmdline.tap(&:strip!).split[0])[-1]
|
66
|
+
end
|
67
|
+
|
68
|
+
# mem_stat(pid = $$)
|
69
|
+
# Where pid is the process ID.
|
70
|
+
# By default it is the id of the current process ($$)
|
71
|
+
#
|
72
|
+
# It retuns the memory, virtual memory, and resident memory of the process.
|
73
|
+
# All values are in Kilobytes.
|
74
|
+
#
|
75
|
+
# The output is a Hash. For example, a sample output:
|
76
|
+
# {:memory=>8656, :virtual_memory=>78272, :resident_memory=>14072}
|
77
|
+
#
|
78
|
+
# Note:
|
79
|
+
# If you need only memory usage of a process, run LinuxStat::ProcessInfo.memory(pid)
|
80
|
+
# If you need only virtual memory for a process, run LinuxStat::ProcessInfo.virtual_memory(pid)
|
81
|
+
# If you need only resident memory of a process, run LinuxStat::ProcessInfo.resident_memory(pid)
|
82
|
+
#
|
83
|
+
# This method opens opens multiple files.
|
84
|
+
# But if you need all of the info, then running this method once is efficient.
|
85
|
+
#
|
86
|
+
# If the info isn't available it will return an empty Hash.
|
87
|
+
def mem_stat(pid = $$)
|
88
|
+
stat_file = "/proc/#{pid}/stat".freeze
|
89
|
+
status_file = "/proc/#{pid}/status".freeze
|
90
|
+
|
91
|
+
stat = if File.readable?(stat_file)
|
92
|
+
IO.read(stat_file).split
|
93
|
+
else
|
94
|
+
[]
|
95
|
+
end
|
96
|
+
|
97
|
+
status = if File.readable?(status_file)
|
98
|
+
IO.readlines(status_file)
|
99
|
+
else
|
100
|
+
[]
|
101
|
+
end
|
102
|
+
|
103
|
+
_rss_anon = status.find { |x| x.start_with?('RssAnon') }
|
104
|
+
rss_anon = _rss_anon ? _rss_anon.split[1].to_i : nil
|
105
|
+
|
106
|
+
_virtual_memory = stat[22]
|
107
|
+
vm = _virtual_memory ? _virtual_memory.to_i.fdiv(1024).to_i : nil
|
108
|
+
|
109
|
+
_vm_rss = status.find { |x| x.start_with?('VmRSS') }
|
110
|
+
vm_rss = _vm_rss ? _vm_rss.split[1].to_i : nil
|
111
|
+
|
112
|
+
{
|
113
|
+
memory: rss_anon,
|
114
|
+
virtual_memory: vm,
|
115
|
+
resident_memory: vm_rss
|
116
|
+
}
|
117
|
+
end
|
118
|
+
|
119
|
+
# memory(pid = $$)
|
120
|
+
# Where pid is the process ID.
|
121
|
+
# By default it is the id of the current process ($$)
|
122
|
+
#
|
123
|
+
# It retuns the memory of the process.
|
124
|
+
# The value is in Kilobytes.
|
125
|
+
# The output is an Integer. For example, a sample output:
|
126
|
+
# 8664
|
127
|
+
#
|
128
|
+
# If the info isn't available it will return nil.
|
129
|
+
def memory(pid = $$)
|
130
|
+
file = "/proc/#{pid}/status".freeze
|
131
|
+
return nil unless File.readable?(file)
|
132
|
+
|
133
|
+
_rss_anon = IO.readlines(file).find { |x| x.start_with?('RssAnon') }
|
134
|
+
_rss_anon ? _rss_anon.split[1].to_i : nil
|
135
|
+
end
|
136
|
+
|
137
|
+
# virtual_memory(pid = $$)
|
138
|
+
# Where pid is the process ID.
|
139
|
+
# By default it is the id of the current process ($$)
|
140
|
+
#
|
141
|
+
# It retuns the virtual memory for the process.
|
142
|
+
# The value is in Kilobytes.
|
143
|
+
# The output is an Integer. For example, a sample output:
|
144
|
+
# 78376
|
145
|
+
#
|
146
|
+
# If the info isn't available it will return nil.
|
147
|
+
def virtual_memory(pid = $$)
|
148
|
+
file = "/proc/#{pid}/stat".freeze
|
149
|
+
return nil unless File.readable?(file)
|
150
|
+
|
151
|
+
_virtual_memory = IO.read(file).split[22]
|
152
|
+
_virtual_memory ? _virtual_memory.to_i.fdiv(1024).to_i : nil
|
153
|
+
end
|
154
|
+
|
155
|
+
# resident_memory(pid = $$)
|
156
|
+
# Where pid is the process ID.
|
157
|
+
# By default it is the id of the current process ($$)
|
158
|
+
#
|
159
|
+
# It retuns the resident memory for the process.
|
160
|
+
# The value is in Kilobytes.
|
161
|
+
# The output is an Integer. For example, a sample output:
|
162
|
+
# 14012
|
163
|
+
#
|
164
|
+
# If the info isn't available it will return nil.
|
165
|
+
def resident_memory(pid = $$)
|
166
|
+
file = "/proc/#{pid}/status".freeze
|
167
|
+
return nil unless File.readable?(file)
|
168
|
+
|
169
|
+
_vm_rss = IO.readlines(file)
|
170
|
+
.find { |x| x.start_with?('VmRSS') }
|
171
|
+
|
172
|
+
_vm_rss ? _vm_rss.split[1].to_i : nil
|
173
|
+
end
|
174
|
+
|
175
|
+
# cpu_stat(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
|
176
|
+
# Where pid is the process ID and sleep time is the interval between measurements.
|
177
|
+
#
|
178
|
+
# By default it is the id of the current process ($$), and sleep is LinuxStat::Sysconf.sc_clk_tck
|
179
|
+
# The smallest amount of available sleep time is 1.0 / LinuxStat::Sysconf.sc_clk_tck.
|
180
|
+
#
|
181
|
+
# Note 1:
|
182
|
+
# Do note that the sleep time can slow down your application.
|
183
|
+
# And it's only needed for the cpu usage calculation.
|
184
|
+
#
|
185
|
+
# It retuns the CPU usage, threads, and the last executed CPU in Hash.
|
186
|
+
# For example:
|
187
|
+
# {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>1}
|
188
|
+
#
|
189
|
+
# But if the info isn't available, it will return an empty Hash.
|
190
|
+
#
|
191
|
+
# The :cpu_usage is in percentage. It's also divided with the number
|
192
|
+
# of CPU. :cpu_usage for example, will return 25.0 if the CPU count
|
193
|
+
# is 4, and the process is using 100% of a thread / core.
|
194
|
+
# A value of 100.0 indicates it is using 100% processing power.
|
195
|
+
#
|
196
|
+
# The :threads returns the number of threads for the process.
|
197
|
+
# The value is a Integer.
|
198
|
+
#
|
199
|
+
# Note 2:
|
200
|
+
# If you just need the CPU usage run LinuxStat::ProcessInfo.cpu_usage(pid = $$)
|
201
|
+
# If you just need the threads run LinuxStat::ProcessInfo.threads(pid = $$)
|
202
|
+
# If you just need the last executed CPU run LinuxStat::ProcessInfo.last_executed_cpu(pid = $$)
|
203
|
+
# Running this method is slower and it opens multiple files at once
|
204
|
+
#
|
205
|
+
# Only use this method if you need all of the data at once, in such case, it's more efficient to use this method.
|
206
|
+
#
|
207
|
+
# The :last_executed_cpu also returns an Integer indicating
|
208
|
+
# the last executed cpu of the process.
|
209
|
+
def cpu_stat(pid: $$, sleep: ticks_to_ms)
|
210
|
+
file = "/proc/#{pid}/stat"
|
211
|
+
return {} unless File.readable?(file)
|
212
|
+
|
213
|
+
ticks = get_ticks
|
214
|
+
|
215
|
+
utime, stime, starttime = IO.read(file)
|
216
|
+
.split.values_at(13, 14, 21).map(&:to_f)
|
217
|
+
uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
|
218
|
+
|
219
|
+
total_time = utime + stime
|
220
|
+
idle1 = uptime - starttime - total_time
|
221
|
+
|
222
|
+
sleep(sleep)
|
223
|
+
stat = IO.read(file).split
|
224
|
+
|
225
|
+
utime2, stime2, starttime2 = stat.values_at(13, 14, 21).map(&:to_f)
|
226
|
+
uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
|
227
|
+
|
228
|
+
total_time2 = utime2 + stime2
|
229
|
+
idle2 = uptime - starttime2 - total_time2
|
230
|
+
|
231
|
+
totald = idle2.+(total_time2).-(idle1 + total_time)
|
232
|
+
cpu = totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs./(LinuxStat::CPU.count)
|
233
|
+
|
234
|
+
{
|
235
|
+
cpu_usage: cpu,
|
236
|
+
threads: stat[19].to_i,
|
237
|
+
last_executed_cpu: stat[38].to_i
|
238
|
+
}
|
239
|
+
end
|
240
|
+
|
241
|
+
# cpu_usage(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
|
242
|
+
# Where pid is the process ID and sleep time is the interval between measurements.
|
243
|
+
#
|
244
|
+
# By default it is the id of the current process ($$), and sleep is 1.0 / LinuxStat::Sysconf.sc_clk_tck
|
245
|
+
# The smallest amount of available sleep time is LinuxStat::Sysconf.sc_clk_tck.
|
246
|
+
#
|
247
|
+
# It retuns the CPU usage in Float.
|
248
|
+
# For example:
|
249
|
+
# 10.0
|
250
|
+
# A value of 100.0 indicates it is using 100% processing power.
|
251
|
+
#
|
252
|
+
# But if the info isn't available, it will return nil.
|
253
|
+
#
|
254
|
+
# This method is more efficient than running LinuxStat::ProcessInfo.cpu_stat()
|
255
|
+
def cpu_usage(pid: $$, sleep: ticks_to_ms)
|
256
|
+
file = "/proc/#{pid}/stat"
|
257
|
+
return nil unless File.readable?(file)
|
258
|
+
|
259
|
+
ticks = get_ticks
|
260
|
+
|
261
|
+
utime, stime, starttime = IO.read(file)
|
262
|
+
.split.values_at(13, 14, 21).map(&:to_f)
|
263
|
+
uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
|
264
|
+
|
265
|
+
total_time = utime + stime
|
266
|
+
idle1 = uptime - starttime - total_time
|
267
|
+
|
268
|
+
sleep(sleep)
|
269
|
+
|
270
|
+
utime2, stime2, starttime2 = IO.read(file)
|
271
|
+
.split.values_at(13, 14, 21).map(&:to_f)
|
272
|
+
uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
|
273
|
+
|
274
|
+
total_time2 = utime2 + stime2
|
275
|
+
idle2 = uptime - starttime2 - total_time2
|
276
|
+
|
277
|
+
totald = idle2.+(total_time2).-(idle1 + total_time)
|
278
|
+
totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs./(LinuxStat::CPU.count)
|
279
|
+
end
|
280
|
+
|
281
|
+
# threads(pid = $$)
|
282
|
+
# Where pid is the process ID.
|
283
|
+
# By default it is the id of the current process ($$)
|
284
|
+
#
|
285
|
+
# It retuns the threads for the current process in Integer.
|
286
|
+
# For example:
|
287
|
+
# 1
|
288
|
+
# But if the info isn't available, it will return nil.
|
289
|
+
#
|
290
|
+
# This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
|
291
|
+
def threads(pid = $$)
|
292
|
+
file = "/proc/#{pid}/stat".freeze
|
293
|
+
return nil unless File.readable?(file)
|
294
|
+
|
295
|
+
IO.read(file).split[19].to_i
|
296
|
+
end
|
297
|
+
|
298
|
+
# last_executed_cpu(pid = $$)
|
299
|
+
# Where pid is the process ID.
|
300
|
+
# By default it is the id of the current process ($$)
|
301
|
+
#
|
302
|
+
# It retuns the last executed CPU in Integer.
|
303
|
+
# For example:
|
304
|
+
# 2
|
305
|
+
# But if the info isn't available, it will return nil.
|
306
|
+
#
|
307
|
+
# This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
|
308
|
+
def last_executed_cpu(pid = $$)
|
309
|
+
file = "/proc/#{pid}/stat".freeze
|
310
|
+
return nil unless File.readable?(file)
|
311
|
+
|
312
|
+
IO.read(file).split[38].to_i
|
313
|
+
end
|
314
|
+
|
315
|
+
# uid(pid = $$)
|
316
|
+
# returns the UIDs of the process as an Array of Integers.
|
317
|
+
#
|
318
|
+
# If the info isn't available it returns an empty Array.
|
319
|
+
def uid(pid = $$)
|
320
|
+
file = "/proc/#{pid}/status".freeze
|
321
|
+
return nil unless File.readable?(file)
|
322
|
+
|
323
|
+
data = IO.readlines(file.freeze).find { |x|
|
324
|
+
x[/Uid.*\d*/]
|
325
|
+
}.to_s.split.drop(1)
|
326
|
+
|
327
|
+
{
|
328
|
+
real: data[0].to_i,
|
329
|
+
effective: data[1].to_i,
|
330
|
+
saved_set: data[2].to_i,
|
331
|
+
filesystem_uid: data[3].to_i
|
332
|
+
}
|
333
|
+
end
|
334
|
+
|
335
|
+
# gid(pid = $$)
|
336
|
+
# returns the GIDs of the process as an Hash containing the following data:
|
337
|
+
# :real, :effective, :saved_set, :filesystem_uid
|
338
|
+
#
|
339
|
+
# If the info isn't available it returns an empty Hash.
|
340
|
+
def gid(pid = $$)
|
341
|
+
file = "/proc/#{pid}/status".freeze
|
342
|
+
return nil unless File.readable?(file)
|
343
|
+
|
344
|
+
data = IO.readlines(file.freeze).find { |x|
|
345
|
+
x[/Gid.*\d*/]
|
346
|
+
}.split.drop(1)
|
347
|
+
|
348
|
+
{
|
349
|
+
real: data[0].to_i,
|
350
|
+
effective: data[1].to_i,
|
351
|
+
saved_set: data[2].to_i,
|
352
|
+
filesystem_uid: data[3].to_i
|
353
|
+
}
|
354
|
+
end
|
355
|
+
|
356
|
+
# owner(pid = $$)
|
357
|
+
# Returns the owner of the process
|
358
|
+
# But if the status is not available, it will return an empty frozen String.
|
359
|
+
def owner(pid = $$)
|
360
|
+
file = "/proc/#{pid}/status".freeze
|
361
|
+
return ''.freeze unless File.readable?(file)
|
362
|
+
|
363
|
+
gid = IO.readlines(file.freeze).find { |x|
|
364
|
+
x[/Gid.*\d*/]
|
365
|
+
}.split.drop(1)[2].to_i
|
366
|
+
|
367
|
+
LinuxStat::User.username_by_gid(gid)
|
368
|
+
end
|
369
|
+
|
370
|
+
private
|
371
|
+
def get_ticks
|
372
|
+
@@ticks ||= Sysconf.sc_clk_tck
|
373
|
+
end
|
374
|
+
|
375
|
+
# Just to avoid multiple calculations!...
|
376
|
+
def ticks_to_ms
|
377
|
+
@@ms ||= 1.0 / get_ticks
|
378
|
+
end
|
379
|
+
end
|
380
|
+
end
|
381
|
+
end
|
data/lib/linux_stat/swap.rb
CHANGED
@@ -21,6 +21,7 @@ module LinuxStat
|
|
21
21
|
|
22
22
|
# Show aggregated used and available swap.
|
23
23
|
# The values are in kilobytes.
|
24
|
+
#
|
24
25
|
# The return type is Hash.
|
25
26
|
# If the info isn't available, the return type is an empty Hash.
|
26
27
|
def stat
|
@@ -44,6 +45,7 @@ module LinuxStat
|
|
44
45
|
|
45
46
|
# Show total amount of swap.
|
46
47
|
# The value is in kilobytes.
|
48
|
+
#
|
47
49
|
# The return type is a Integer but if the info isn't available, it will return nil.
|
48
50
|
def total
|
49
51
|
return nil unless swaps_readable?
|
@@ -52,6 +54,7 @@ module LinuxStat
|
|
52
54
|
|
53
55
|
# Show total amount of available swap.
|
54
56
|
# The value is in kilobytes.
|
57
|
+
#
|
55
58
|
# The return type is a Integer but if the info isn't available, it will return nil.
|
56
59
|
def available
|
57
60
|
return nil unless swaps_readable?
|
@@ -61,6 +64,7 @@ module LinuxStat
|
|
61
64
|
|
62
65
|
# Show total amount of used swap.
|
63
66
|
# The value is in kilobytes.
|
67
|
+
#
|
64
68
|
# The return type is a Integer but if the info isn't available, it will return nil.
|
65
69
|
def used
|
66
70
|
return nil unless swaps_readable?
|
@@ -79,7 +83,8 @@ module LinuxStat
|
|
79
83
|
values_t[-1].sum.*(100).fdiv(total).round(2)
|
80
84
|
end
|
81
85
|
|
82
|
-
#
|
86
|
+
# Shows the percentage of swap available.
|
87
|
+
#
|
83
88
|
# The return type is a Float but if the info isn't available, it will return nil.
|
84
89
|
def percent_available
|
85
90
|
return nil unless swaps_readable?
|
@@ -0,0 +1,298 @@
|
|
1
|
+
module LinuxStat
|
2
|
+
module User
|
3
|
+
class << self
|
4
|
+
# Returns an array of users as string
|
5
|
+
# For example:
|
6
|
+
# ["root", "bin", "daemon", "mail", "ftp", "http", "nobody"]
|
7
|
+
# But if the status isn't available it will return an empty Array.
|
8
|
+
def list
|
9
|
+
return [] unless passwd_readable?
|
10
|
+
passwd.map { |x| x[/.+?:/][0..-2].freeze }
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns all the Group ids directories as Hash.
|
14
|
+
# For example:
|
15
|
+
# {:root=>{:uid=>0, :gid=>0}, :bin=>{:uid=>1, :gid=>1}, :daemon=>{:uid=>2, :gid=>2}, :mail=>{:uid=>8, :gid=>12}, :ftp=>{:uid=>14, :gid=>11}}
|
16
|
+
#
|
17
|
+
# But if the status isn't available it will return an empty Hash.
|
18
|
+
def ids
|
19
|
+
return {} unless passwd_readable?
|
20
|
+
passwd_splitted.reduce({}) { |h, x|
|
21
|
+
h.merge!(x[0].to_sym => {
|
22
|
+
uid: x[2].to_i, gid: x[3].to_i
|
23
|
+
})
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns all the user IDs as Hash.
|
28
|
+
# For example:
|
29
|
+
# LinuxStat::User.uids
|
30
|
+
# => {:root=>0, :bin=>1, :daemon=>2, :mail=>8, :ftp=>14}
|
31
|
+
#
|
32
|
+
# But if the status isn't available it will return an empty Hash.
|
33
|
+
def uids
|
34
|
+
return {} unless passwd_readable?
|
35
|
+
passwd_splitted.reduce({}) { |h, x|
|
36
|
+
h.merge!(x[0].to_sym => x[2].to_i)
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns all the Group identifiers as Hash.
|
41
|
+
# For example:
|
42
|
+
# LinuxStat::User.gids
|
43
|
+
# => {:root=>0, :bin=>1, :daemon=>2, :mail=>12, :ftp=>11}
|
44
|
+
#
|
45
|
+
# But if the status isn't available it will return an empty Hash.
|
46
|
+
def gids
|
47
|
+
return {} unless passwd_readable?
|
48
|
+
passwd_splitted.reduce({}) { |h, x|
|
49
|
+
h.merge!(x[0].to_sym => x[3].to_i)
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
# Returns all the home directories as Hash.
|
54
|
+
# For example:
|
55
|
+
# LinuxStat::User.home_directories
|
56
|
+
# => {:root=>"/root", :bin=>"/", :daemon=>"/", :mail=>"/var/spool/mail", :ftp=>"/srv/ftp", :http=>"/srv/http", :nobody=>"/"}
|
57
|
+
#
|
58
|
+
# But if the status isn't available it will return an empty Hash.
|
59
|
+
def home_directories
|
60
|
+
return {} unless passwd_readable?
|
61
|
+
passwd.reduce({}) { |h, x|
|
62
|
+
splitted = x.split(?:)
|
63
|
+
h.merge!(splitted[0].to_sym => splitted[5])
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
# Returns the user ID as integer
|
68
|
+
# It directly calls LinuxStat::Sysconf.get_uid and LinuxStat::Sysconf.get_gid
|
69
|
+
# and then reads /etc/passwd and matches the values with UID and GID.
|
70
|
+
#
|
71
|
+
# It doesn't get affected with the assignment of USER environment variable
|
72
|
+
# If either /etc/passwd is readable or LinuxStat::Sysconf.get_login() is not empty.
|
73
|
+
#
|
74
|
+
# But if /etc/passwd isn't readable (which is weird), it will fall back to sysconf.h's get_login()
|
75
|
+
# It that's not available, like in docker, falls back to ENV['USER].to_s
|
76
|
+
#
|
77
|
+
# Note that this is not cached or memoized so use this at your own processing expense.
|
78
|
+
# It should return the username under most robust circumstances.
|
79
|
+
# But if nothing is available for some reason, it will return an empty String.
|
80
|
+
def get_user
|
81
|
+
unless passwd_readable?
|
82
|
+
_l = LinuxStat::Sysconf.get_login().freeze
|
83
|
+
return _l.empty? ? ENV['USER'.freeze].to_s : _l
|
84
|
+
end
|
85
|
+
|
86
|
+
uid, gid = LinuxStat::Sysconf.get_uid, LinuxStat::Sysconf.get_gid
|
87
|
+
|
88
|
+
username = ''
|
89
|
+
passwd.each { |x|
|
90
|
+
splitted = x.split(?:).freeze
|
91
|
+
if splitted[2].to_i == uid && splitted[3].to_i == gid
|
92
|
+
username = splitted[0]
|
93
|
+
break
|
94
|
+
end
|
95
|
+
}
|
96
|
+
username
|
97
|
+
end
|
98
|
+
|
99
|
+
# Returns the user ID as integer
|
100
|
+
# It directly calls LinuxStat::Sysconf.get_uid
|
101
|
+
def get_uid
|
102
|
+
LinuxStat::Sysconf.get_uid
|
103
|
+
end
|
104
|
+
|
105
|
+
# Returns the group ID as integer
|
106
|
+
# It directly calls LinuxStat::Sysconf.get_uid
|
107
|
+
def get_gid
|
108
|
+
LinuxStat::Sysconf.get_gid
|
109
|
+
end
|
110
|
+
|
111
|
+
# Returns the effective user ID as integer
|
112
|
+
# It directly calls LinuxStat::Sysconf.get_euid
|
113
|
+
def get_euid
|
114
|
+
LinuxStat::Sysconf.get_euid
|
115
|
+
end
|
116
|
+
|
117
|
+
# Calls LinuxStat::Sysconf.get_login()
|
118
|
+
# The username is returned as a String.
|
119
|
+
# It doesn't get affected by ENV['USER]
|
120
|
+
#
|
121
|
+
# But if the name isn't available (say inside a container), it will return an empty String.
|
122
|
+
# This is meant for speed but not for reliability.
|
123
|
+
# To get more reliable output, you might try LinuxStat::User.get_user()
|
124
|
+
def get_login
|
125
|
+
LinuxStat::Sysconf.get_login
|
126
|
+
end
|
127
|
+
|
128
|
+
# def usernames_by_uid(gid = get_uid)
|
129
|
+
# Where uid is the group id of the user. By default it's the uid of the current user.
|
130
|
+
#
|
131
|
+
# It returns an Array containing the username corresponding to the uid.
|
132
|
+
#
|
133
|
+
# For example:
|
134
|
+
# LinuxStat::User.usernames_by_uid(1001)
|
135
|
+
# => ["userx", "usery"]
|
136
|
+
#
|
137
|
+
# But if the info isn't available it will return an empty Array.
|
138
|
+
def usernames_by_uid(uid = get_uid)
|
139
|
+
return [] unless passwd_readable?
|
140
|
+
|
141
|
+
usernames = []
|
142
|
+
passwd_splitted.each { |x|
|
143
|
+
usernames << x[0] if x[2].to_i == uid
|
144
|
+
}
|
145
|
+
usernames
|
146
|
+
end
|
147
|
+
|
148
|
+
# def username_by_gid(gid = get_gid)
|
149
|
+
# Where gid is the group id of the user. By default it's the gid of the current user.
|
150
|
+
#
|
151
|
+
# It returns a String cotaining the username corresponding to the gid
|
152
|
+
# But if the info isn't available it will return an empty frozen String.
|
153
|
+
def username_by_gid(gid = get_gid)
|
154
|
+
return ''.freeze unless passwd_readable?
|
155
|
+
|
156
|
+
username = ''
|
157
|
+
passwd.each do |x|
|
158
|
+
splitted = x.split(?:.freeze)
|
159
|
+
if splitted[2].to_i == gid
|
160
|
+
username = splitted[0]
|
161
|
+
break
|
162
|
+
end
|
163
|
+
end
|
164
|
+
username
|
165
|
+
end
|
166
|
+
|
167
|
+
# gid_by_username(username = get_user)
|
168
|
+
# Where username is the username to look for, by default it is the current user.
|
169
|
+
#
|
170
|
+
# It returns the gid by the username.
|
171
|
+
# For example:
|
172
|
+
# LinuxStat::User.gid_by_username('root')
|
173
|
+
# => "0"
|
174
|
+
#
|
175
|
+
# The return type is Integer.
|
176
|
+
# But if user passed doesn't exist or if the info isn't available, it will return nil.
|
177
|
+
def gid_by_username(username = get_user)
|
178
|
+
return nil unless passwd_readable?
|
179
|
+
|
180
|
+
gid = nil
|
181
|
+
passwd.each do |x|
|
182
|
+
splitted = x.split(?:.freeze)
|
183
|
+
if splitted[0] == username
|
184
|
+
gid = splitted[3].to_i
|
185
|
+
break
|
186
|
+
end
|
187
|
+
end
|
188
|
+
gid
|
189
|
+
end
|
190
|
+
|
191
|
+
# uid_by_username(username = get_user)
|
192
|
+
# Where username is the username to look for, by default it is the current user.
|
193
|
+
#
|
194
|
+
# It returns the uid by the username.
|
195
|
+
# For example:
|
196
|
+
# LinuxStat::User.uid_by_username('root')
|
197
|
+
# => 0
|
198
|
+
#
|
199
|
+
# The return type is Integer.
|
200
|
+
# But if user passed doesn't exist or if the info isn't available, it will return nil.
|
201
|
+
def uid_by_username(username = get_user)
|
202
|
+
return nil unless passwd_readable?
|
203
|
+
|
204
|
+
uid = nil
|
205
|
+
passwd.each do |x|
|
206
|
+
splitted = x.split(?:.freeze)
|
207
|
+
if splitted[0] == username
|
208
|
+
uid = splitted[2].to_i
|
209
|
+
break
|
210
|
+
end
|
211
|
+
end
|
212
|
+
uid
|
213
|
+
end
|
214
|
+
|
215
|
+
# home_by_username(user = get_user)
|
216
|
+
# Where user is the name of the user.
|
217
|
+
# Returns the user's home. By default it returns the home of the current user.
|
218
|
+
#
|
219
|
+
# If the info isn't available, it will return ENV['HOME].to_s.freeze
|
220
|
+
def home_by_username(user = get_user)
|
221
|
+
return ENV['HOME'].to_s.freeze unless passwd_readable?
|
222
|
+
|
223
|
+
home = ''
|
224
|
+
passwd.each { |x|
|
225
|
+
splitted = x.split(?:)
|
226
|
+
if splitted[0] == user
|
227
|
+
home = splitted[5]
|
228
|
+
break
|
229
|
+
end
|
230
|
+
}
|
231
|
+
home
|
232
|
+
end
|
233
|
+
|
234
|
+
# home_by_uid(id = get_uid)
|
235
|
+
# Gets all the users home directory with user id.
|
236
|
+
# It returns an Array in this format:
|
237
|
+
# LinuxStat::User.homes_by_uid(1001)
|
238
|
+
# => ["/home/userx", "/home/usery"]
|
239
|
+
#
|
240
|
+
# Assuming both the users share same UID.
|
241
|
+
#
|
242
|
+
# If the info isn't available, it will return an empty Array.
|
243
|
+
def homes_by_uid(id = get_uid)
|
244
|
+
return [] unless passwd_readable?
|
245
|
+
|
246
|
+
home = []
|
247
|
+
passwd.each do |x|
|
248
|
+
splitted = x.split(?:.freeze)
|
249
|
+
home << splitted[5] if splitted[2].to_i == id
|
250
|
+
end
|
251
|
+
home
|
252
|
+
end
|
253
|
+
|
254
|
+
# home_by_gid(id = get_uid)
|
255
|
+
# Gets the home of the user corresponding to the GID.
|
256
|
+
# It returns a String in this format:
|
257
|
+
#
|
258
|
+
# Assuming both the users share same UID.
|
259
|
+
#
|
260
|
+
# If the info isn't available, it will return an empty frozen String.
|
261
|
+
def home_by_gid(id = get_gid)
|
262
|
+
return ''.freeze unless passwd_readable?
|
263
|
+
|
264
|
+
home = ''
|
265
|
+
passwd.each do |x|
|
266
|
+
splitted = x.split(?:.freeze)
|
267
|
+
|
268
|
+
if splitted[3].to_i == id
|
269
|
+
home = splitted[5]
|
270
|
+
break
|
271
|
+
end
|
272
|
+
end
|
273
|
+
home
|
274
|
+
end
|
275
|
+
|
276
|
+
alias get_current_user get_user
|
277
|
+
|
278
|
+
private
|
279
|
+
def passwd
|
280
|
+
@@passwd_file ||= '/etc/passwd'.freeze
|
281
|
+
IO.readlines(@@passwd_file)
|
282
|
+
end
|
283
|
+
|
284
|
+
# Only use this method where we are sure that the whole array is going to be used.
|
285
|
+
# In cases like find() or a loop with `break` this is a lot of overhead.
|
286
|
+
# In cases like reduce({}) or select, this is not helpful.
|
287
|
+
def passwd_splitted
|
288
|
+
@@passwd_file ||= '/etc/passwd'.freeze
|
289
|
+
IO.readlines(@@passwd_file).map { |x| x.split(?:.freeze) }
|
290
|
+
end
|
291
|
+
|
292
|
+
def passwd_readable?
|
293
|
+
@@passwd_file ||= '/etc/passwd'.freeze
|
294
|
+
@@passwd_readable ||= File.readable?(@@passwd_file)
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
end
|