linux_stat 0.3.1 → 0.5.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/LICENSE.txt +21 -0
- data/README.md +423 -46
- data/bin/console +1 -1
- data/bin/setup +0 -2
- data/{bin → exe}/linuxstat.rb +43 -7
- data/ext/fs_stat/fs_stat.c +14 -11
- data/ext/sysconf/extconf.rb +7 -0
- data/ext/sysconf/sysconf.c +77 -0
- data/ext/utsname/extconf.rb +7 -0
- data/ext/utsname/utsname.c +49 -0
- data/lib/linux_stat.rb +9 -1
- data/lib/linux_stat/battery.rb +2 -2
- data/lib/linux_stat/bios.rb +10 -6
- data/lib/linux_stat/cpu.rb +10 -2
- data/lib/linux_stat/filesystem.rb +10 -14
- data/lib/linux_stat/kernel.rb +16 -6
- data/lib/linux_stat/memory.rb +8 -2
- data/lib/linux_stat/os.rb +27 -8
- data/lib/linux_stat/process_info.rb +321 -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 +17 -9
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
|
data/lib/linux_stat/version.rb
CHANGED
metadata
CHANGED
@@ -1,30 +1,39 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linux_stat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.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-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: Linux only, efficient linux system utilization reporting and system monitoring
|
14
|
+
gem
|
14
15
|
email:
|
15
16
|
- souravgoswami@protonmail.com
|
16
|
-
executables:
|
17
|
+
executables:
|
18
|
+
- linuxstat.rb
|
17
19
|
extensions:
|
18
20
|
- ext/fs_stat/extconf.rb
|
21
|
+
- ext/sysconf/extconf.rb
|
22
|
+
- ext/utsname/extconf.rb
|
19
23
|
extra_rdoc_files:
|
20
24
|
- README.md
|
21
25
|
files:
|
26
|
+
- LICENSE.txt
|
22
27
|
- README.md
|
23
28
|
- bin/console
|
24
|
-
- bin/linuxstat.rb
|
25
29
|
- bin/setup
|
30
|
+
- exe/linuxstat.rb
|
26
31
|
- ext/fs_stat/extconf.rb
|
27
32
|
- ext/fs_stat/fs_stat.c
|
33
|
+
- ext/sysconf/extconf.rb
|
34
|
+
- ext/sysconf/sysconf.c
|
35
|
+
- ext/utsname/extconf.rb
|
36
|
+
- ext/utsname/utsname.c
|
28
37
|
- lib/linux_stat.rb
|
29
38
|
- lib/linux_stat/battery.rb
|
30
39
|
- lib/linux_stat/bios.rb
|
@@ -36,15 +45,14 @@ files:
|
|
36
45
|
- lib/linux_stat/net.rb
|
37
46
|
- lib/linux_stat/os.rb
|
38
47
|
- lib/linux_stat/process.rb
|
48
|
+
- lib/linux_stat/process_info.rb
|
39
49
|
- lib/linux_stat/swap.rb
|
50
|
+
- lib/linux_stat/user.rb
|
40
51
|
- lib/linux_stat/version.rb
|
41
52
|
homepage: https://github.com/Souravgoswami/linux_stat/
|
42
53
|
licenses:
|
43
54
|
- MIT
|
44
|
-
metadata:
|
45
|
-
homepage_uri: https://github.com/Souravgoswami/linux_stat/
|
46
|
-
source_code_uri: https://github.com/Souravgoswami/linux_stat
|
47
|
-
changelog_uri: https://github.com/Souravgoswami/linux_stat/commits/master
|
55
|
+
metadata: {}
|
48
56
|
post_install_message:
|
49
57
|
rdoc_options: []
|
50
58
|
require_paths:
|