linux_stat 0.5.1 → 0.6.4

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.
@@ -1,7 +1,9 @@
1
1
  module LinuxStat
2
2
  module Swap
3
3
  class << self
4
+ ##
4
5
  # List all swap devices and returns a Hash.
6
+ #
5
7
  # If the info isn't available, it will return an empty Hash.
6
8
  def list
7
9
  return {} unless swaps_readable?
@@ -13,13 +15,16 @@ module LinuxStat
13
15
  end
14
16
  end
15
17
 
18
+ ##
16
19
  # Returns true if any swap device is available, else returns false.
20
+ #
17
21
  # If the info isn't available, it will return an empty Hash.
18
22
  def any?
19
23
  !!IO.foreach('/proc/swaps').drop(1).first
20
24
  end
21
25
 
22
26
  # Show aggregated used and available swap.
27
+ #
23
28
  # The values are in kilobytes.
24
29
  #
25
30
  # The return type is Hash.
@@ -43,7 +48,9 @@ module LinuxStat
43
48
  }
44
49
  end
45
50
 
46
- # Show total amount of swap.
51
+ ##
52
+ # Shows total amount of swap.
53
+ #
47
54
  # The value is in kilobytes.
48
55
  #
49
56
  # The return type is a Integer but if the info isn't available, it will return nil.
@@ -52,7 +59,9 @@ module LinuxStat
52
59
  read_usage[0].sum
53
60
  end
54
61
 
62
+ ##
55
63
  # Show total amount of available swap.
64
+ #
56
65
  # The value is in kilobytes.
57
66
  #
58
67
  # The return type is a Integer but if the info isn't available, it will return nil.
@@ -62,7 +71,9 @@ module LinuxStat
62
71
  values_t[0].sum - values_t[1].sum
63
72
  end
64
73
 
74
+ ##
65
75
  # Show total amount of used swap.
76
+ #
66
77
  # The value is in kilobytes.
67
78
  #
68
79
  # The return type is a Integer but if the info isn't available, it will return nil.
@@ -71,7 +82,9 @@ module LinuxStat
71
82
  read_usage[-1].sum
72
83
  end
73
84
 
85
+ ##
74
86
  # Show percentage of swap used.
87
+ #
75
88
  # The return type is a Float but if the info isn't available, it will return nil.
76
89
  def percent_used
77
90
  return nil unless swaps_readable?
@@ -83,6 +96,7 @@ module LinuxStat
83
96
  values_t[-1].sum.*(100).fdiv(total).round(2)
84
97
  end
85
98
 
99
+ ##
86
100
  # Shows the percentage of swap available.
87
101
  #
88
102
  # The return type is a Float but if the info isn't available, it will return nil.
@@ -1,32 +1,37 @@
1
1
  module LinuxStat
2
2
  module User
3
3
  class << self
4
+ ##
4
5
  # Returns an array of users as string
6
+ #
5
7
  # For example:
6
- # ["root", "bin", "daemon", "mail", "ftp", "http", "nobody"]
8
+ # ["root", "bin", "daemon", "mail", "ftp", "http", "nobody"]
9
+ #
7
10
  # But if the status isn't available it will return an empty Array.
8
11
  def list
9
12
  return [] unless passwd_readable?
10
13
  passwd.map { |x| x[/.+?:/][0..-2].freeze }
11
14
  end
12
15
 
16
+ ##
13
17
  # Returns all the Group ids directories as Hash.
18
+ #
14
19
  # For example:
15
20
  # {: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
21
  #
17
22
  # But if the status isn't available it will return an empty Hash.
18
23
  def ids
19
24
  return {} unless passwd_readable?
20
- passwd.reduce({}) { |h, x|
21
- splitted = x.split(?:)
22
-
23
- h.merge!(splitted[0].to_sym => {
24
- uid: splitted[2].to_i, gid: splitted[3].to_i
25
+ passwd_splitted.reduce({}) { |h, x|
26
+ h.merge!(x[0].to_sym => {
27
+ uid: x[2].to_i, gid: x[3].to_i
25
28
  })
26
29
  }
27
30
  end
28
31
 
32
+ ##
29
33
  # Returns all the user IDs as Hash.
34
+ #
30
35
  # For example:
31
36
  # LinuxStat::User.uids
32
37
  # => {:root=>0, :bin=>1, :daemon=>2, :mail=>8, :ftp=>14}
@@ -34,29 +39,33 @@ module LinuxStat
34
39
  # But if the status isn't available it will return an empty Hash.
35
40
  def uids
36
41
  return {} unless passwd_readable?
37
- passwd.reduce({}) { |h, x|
38
- splitted = x.split(?:)
39
- h.merge!(splitted[0].to_sym => splitted[2].to_i)
42
+ passwd_splitted.reduce({}) { |h, x|
43
+ h.merge!(x[0].to_sym => x[2].to_i)
40
44
  }
41
45
  end
42
46
 
47
+ ##
43
48
  # Returns all the Group identifiers as Hash.
49
+ #
44
50
  # For example:
45
51
  # LinuxStat::User.gids
52
+ #
46
53
  # => {:root=>0, :bin=>1, :daemon=>2, :mail=>12, :ftp=>11}
47
54
  #
48
55
  # But if the status isn't available it will return an empty Hash.
49
56
  def gids
50
57
  return {} unless passwd_readable?
51
- passwd.reduce({}) { |h, x|
52
- splitted = x.split(?:)
53
- h.merge!(splitted[0].to_sym => splitted[3].to_i)
58
+ passwd_splitted.reduce({}) { |h, x|
59
+ h.merge!(x[0].to_sym => x[3].to_i)
54
60
  }
55
61
  end
56
62
 
63
+ ##
57
64
  # Returns all the home directories as Hash.
65
+ #
58
66
  # For example:
59
67
  # LinuxStat::User.home_directories
68
+ #
60
69
  # => {:root=>"/root", :bin=>"/", :daemon=>"/", :mail=>"/var/spool/mail", :ftp=>"/srv/ftp", :http=>"/srv/http", :nobody=>"/"}
61
70
  #
62
71
  # But if the status isn't available it will return an empty Hash.
@@ -68,39 +77,92 @@ module LinuxStat
68
77
  }
69
78
  end
70
79
 
80
+ ##
71
81
  # Returns the user ID as integer
72
- # It directly calls LinuxStat::Sysconf.get_user
73
82
  #
74
- # It doesn't get affected with the assignment of USER environment variable.
83
+ # It directly calls LinuxStat::Sysconf.get_uid and LinuxStat::Sysconf.get_gid
84
+ #
85
+ # and then reads /etc/passwd and matches the values with UID and GID.
86
+ #
87
+ # It doesn't get affected with the assignment of USER environment variable
88
+ #
89
+ # If either /etc/passwd is readable or LinuxStat::Sysconf.get_login() is not empty.
90
+ #
91
+ # But if /etc/passwd isn't readable (which is weird), it will fall back to sysconf.h's get_login()
92
+ # If that's not available, like in docker, falls back to ENV['USER].to_s
93
+ #
94
+ # It should return the username under most robust circumstances.
95
+ # But if nothing is available for some reason, it will return an empty String.
96
+ #
97
+ # Note that this is not cached or memoized so use this at your own processing expense.
75
98
  def get_user
76
- LinuxStat::Sysconf.get_user
99
+ unless passwd_readable?
100
+ _l = LinuxStat::Sysconf.get_login().freeze
101
+ return _l.empty? ? ENV['USER'.freeze].to_s : _l
102
+ end
103
+
104
+ uid, gid = LinuxStat::Sysconf.get_uid, LinuxStat::Sysconf.get_gid
105
+
106
+ username = ''
107
+ passwd.each { |x|
108
+ splitted = x.split(?:).freeze
109
+ if splitted[2].to_i == uid && splitted[3].to_i == gid
110
+ username = splitted[0]
111
+ break
112
+ end
113
+ }
114
+ username
77
115
  end
78
116
 
117
+ ##
79
118
  # Returns the user ID as integer
119
+ #
80
120
  # It directly calls LinuxStat::Sysconf.get_uid
81
121
  def get_uid
82
122
  LinuxStat::Sysconf.get_uid
83
123
  end
84
124
 
125
+ ##
85
126
  # Returns the group ID as integer
127
+ #
86
128
  # It directly calls LinuxStat::Sysconf.get_uid
87
129
  def get_gid
88
130
  LinuxStat::Sysconf.get_gid
89
131
  end
90
132
 
133
+ ##
91
134
  # Returns the effective user ID as integer
135
+ #
92
136
  # It directly calls LinuxStat::Sysconf.get_euid
93
137
  def get_euid
94
138
  LinuxStat::Sysconf.get_euid
95
139
  end
96
140
 
97
- # def usernames_by_uid(gid = get_uid)
141
+ ##
142
+ # Calls LinuxStat::Sysconf.get_login()
143
+ #
144
+ # The username is returned as a String.
145
+ #
146
+ # It doesn't get affected by ENV['USER]
147
+ #
148
+ # But if the name isn't available (say inside a container), it will return an empty String.
149
+ #
150
+ # This is meant for speed but not for reliability.
151
+ # To get more reliable output, you might try LinuxStat::User.get_user()
152
+ def get_login
153
+ LinuxStat::Sysconf.get_login
154
+ end
155
+
156
+ ##
157
+ # = def usernames_by_uid(gid = get_uid)
158
+ #
98
159
  # Where uid is the group id of the user. By default it's the uid of the current user.
99
160
  #
100
161
  # It returns an Array containing the username corresponding to the uid.
101
162
  #
102
163
  # For example:
103
164
  # LinuxStat::User.usernames_by_uid(1001)
165
+ #
104
166
  # => ["userx", "usery"]
105
167
  #
106
168
  # But if the info isn't available it will return an empty Array.
@@ -108,17 +170,19 @@ module LinuxStat
108
170
  return [] unless passwd_readable?
109
171
 
110
172
  usernames = []
111
- passwd.each do |x|
112
- splitted = x.split(?:.freeze)
113
- usernames << splitted[0] if splitted[2].to_i == uid
114
- end
173
+ passwd_splitted.each { |x|
174
+ usernames << x[0] if x[2].to_i == uid
175
+ }
115
176
  usernames
116
177
  end
117
178
 
118
- # def username_by_gid(gid = get_gid)
179
+ ##
180
+ # = username_by_gid(gid = get_gid)
181
+ #
119
182
  # Where gid is the group id of the user. By default it's the gid of the current user.
120
183
  #
121
184
  # It returns a String cotaining the username corresponding to the gid
185
+ #
122
186
  # But if the info isn't available it will return an empty frozen String.
123
187
  def username_by_gid(gid = get_gid)
124
188
  return ''.freeze unless passwd_readable?
@@ -134,15 +198,19 @@ module LinuxStat
134
198
  username
135
199
  end
136
200
 
137
- # gid_by_username(username = get_user)
201
+ ##
202
+ # = gid_by_username(username = get_user)
203
+ #
138
204
  # Where username is the username to look for, by default it is the current user.
139
205
  #
140
206
  # It returns the gid by the username.
141
207
  # For example:
142
208
  # LinuxStat::User.gid_by_username('root')
209
+ #
143
210
  # => "0"
144
211
  #
145
212
  # The return type is Integer.
213
+ #
146
214
  # But if user passed doesn't exist or if the info isn't available, it will return nil.
147
215
  def gid_by_username(username = get_user)
148
216
  return nil unless passwd_readable?
@@ -158,15 +226,20 @@ module LinuxStat
158
226
  gid
159
227
  end
160
228
 
161
- # uid_by_username(username = get_user)
229
+ ##
230
+ # = uid_by_username(username = get_user)
231
+ #
162
232
  # Where username is the username to look for, by default it is the current user.
163
233
  #
164
234
  # It returns the uid by the username.
235
+ #
165
236
  # For example:
166
237
  # LinuxStat::User.uid_by_username('root')
238
+ #
167
239
  # => 0
168
240
  #
169
241
  # The return type is Integer.
242
+ #
170
243
  # But if user passed doesn't exist or if the info isn't available, it will return nil.
171
244
  def uid_by_username(username = get_user)
172
245
  return nil unless passwd_readable?
@@ -182,8 +255,11 @@ module LinuxStat
182
255
  uid
183
256
  end
184
257
 
185
- # home_by_username(user = get_user)
258
+ ##
259
+ # = home_by_username(user = get_user)
260
+ #
186
261
  # Where user is the name of the user.
262
+ #
187
263
  # Returns the user's home. By default it returns the home of the current user.
188
264
  #
189
265
  # If the info isn't available, it will return ENV['HOME].to_s.freeze
@@ -201,10 +277,14 @@ module LinuxStat
201
277
  home
202
278
  end
203
279
 
204
- # home_by_uid(id = get_uid)
280
+ ##
281
+ # = home_by_uid(id = get_uid)
282
+ #
205
283
  # Gets all the users home directory with user id.
284
+ #
206
285
  # It returns an Array in this format:
207
286
  # LinuxStat::User.homes_by_uid(1001)
287
+ #
208
288
  # => ["/home/userx", "/home/usery"]
209
289
  #
210
290
  # Assuming both the users share same UID.
@@ -221,8 +301,11 @@ module LinuxStat
221
301
  home
222
302
  end
223
303
 
224
- # home_by_gid(id = get_uid)
304
+ ##
305
+ # = home_by_gid(id = get_uid)
306
+ #
225
307
  # Gets the home of the user corresponding to the GID.
308
+ #
226
309
  # It returns a String in this format:
227
310
  #
228
311
  # Assuming both the users share same UID.
@@ -243,12 +326,22 @@ module LinuxStat
243
326
  home
244
327
  end
245
328
 
329
+ alias get_current_user get_user
330
+
246
331
  private
247
332
  def passwd
248
333
  @@passwd_file ||= '/etc/passwd'.freeze
249
334
  IO.readlines(@@passwd_file)
250
335
  end
251
336
 
337
+ # Only use this method where we are sure that the whole array is going to be used.
338
+ # In cases like find() or a loop with `break` this is a lot of overhead.
339
+ # In cases like reduce({}) or select, this is not helpful.
340
+ def passwd_splitted
341
+ @@passwd_file ||= '/etc/passwd'.freeze
342
+ IO.readlines(@@passwd_file).map { |x| x.split(?:.freeze) }
343
+ end
344
+
252
345
  def passwd_readable?
253
346
  @@passwd_file ||= '/etc/passwd'.freeze
254
347
  @@passwd_readable ||= File.readable?(@@passwd_file)
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "0.5.1"
2
+ VERSION ||= "0.6.4"
3
3
  end
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: 0.5.1
4
+ version: 0.6.4
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-05 00:00:00.000000000 Z
11
+ date: 2020-12-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem
@@ -44,6 +44,7 @@ files:
44
44
  - lib/linux_stat/mounts.rb
45
45
  - lib/linux_stat/net.rb
46
46
  - lib/linux_stat/os.rb
47
+ - lib/linux_stat/prettify_bytes.rb
47
48
  - lib/linux_stat/process.rb
48
49
  - lib/linux_stat/process_info.rb
49
50
  - lib/linux_stat/swap.rb