linux_stat 0.6.2 → 0.7.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.
@@ -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,16 +1,21 @@
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
  #
@@ -24,7 +29,9 @@ module LinuxStat
24
29
  }
25
30
  end
26
31
 
32
+ ##
27
33
  # Returns all the user IDs as Hash.
34
+ #
28
35
  # For example:
29
36
  # LinuxStat::User.uids
30
37
  # => {:root=>0, :bin=>1, :daemon=>2, :mail=>8, :ftp=>14}
@@ -37,9 +44,12 @@ module LinuxStat
37
44
  }
38
45
  end
39
46
 
47
+ ##
40
48
  # Returns all the Group identifiers as Hash.
49
+ #
41
50
  # For example:
42
51
  # LinuxStat::User.gids
52
+ #
43
53
  # => {:root=>0, :bin=>1, :daemon=>2, :mail=>12, :ftp=>11}
44
54
  #
45
55
  # But if the status isn't available it will return an empty Hash.
@@ -50,9 +60,12 @@ module LinuxStat
50
60
  }
51
61
  end
52
62
 
63
+ ##
53
64
  # Returns all the home directories as Hash.
65
+ #
54
66
  # For example:
55
67
  # LinuxStat::User.home_directories
68
+ #
56
69
  # => {:root=>"/root", :bin=>"/", :daemon=>"/", :mail=>"/var/spool/mail", :ftp=>"/srv/ftp", :http=>"/srv/http", :nobody=>"/"}
57
70
  #
58
71
  # But if the status isn't available it will return an empty Hash.
@@ -64,19 +77,24 @@ module LinuxStat
64
77
  }
65
78
  end
66
79
 
80
+ ##
67
81
  # Returns the user ID as integer
82
+ #
68
83
  # It directly calls LinuxStat::Sysconf.get_uid and LinuxStat::Sysconf.get_gid
84
+ #
69
85
  # and then reads /etc/passwd and matches the values with UID and GID.
70
86
  #
71
87
  # It doesn't get affected with the assignment of USER environment variable
88
+ #
72
89
  # If either /etc/passwd is readable or LinuxStat::Sysconf.get_login() is not empty.
73
90
  #
74
91
  # 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
92
+ # If that's not available, like in docker, falls back to ENV['USER].to_s
76
93
  #
77
- # Note that this is not cached or memoized so use this at your own processing expense.
78
94
  # It should return the username under most robust circumstances.
79
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.
80
98
  def get_user
81
99
  unless passwd_readable?
82
100
  _l = LinuxStat::Sysconf.get_login().freeze
@@ -96,42 +114,55 @@ module LinuxStat
96
114
  username
97
115
  end
98
116
 
117
+ ##
99
118
  # Returns the user ID as integer
119
+ #
100
120
  # It directly calls LinuxStat::Sysconf.get_uid
101
121
  def get_uid
102
122
  LinuxStat::Sysconf.get_uid
103
123
  end
104
124
 
125
+ ##
105
126
  # Returns the group ID as integer
127
+ #
106
128
  # It directly calls LinuxStat::Sysconf.get_uid
107
129
  def get_gid
108
130
  LinuxStat::Sysconf.get_gid
109
131
  end
110
132
 
133
+ ##
111
134
  # Returns the effective user ID as integer
135
+ #
112
136
  # It directly calls LinuxStat::Sysconf.get_euid
113
137
  def get_euid
114
138
  LinuxStat::Sysconf.get_euid
115
139
  end
116
140
 
141
+ ##
117
142
  # Calls LinuxStat::Sysconf.get_login()
143
+ #
118
144
  # The username is returned as a String.
145
+ #
119
146
  # It doesn't get affected by ENV['USER]
120
147
  #
121
148
  # But if the name isn't available (say inside a container), it will return an empty String.
149
+ #
122
150
  # This is meant for speed but not for reliability.
123
151
  # To get more reliable output, you might try LinuxStat::User.get_user()
124
152
  def get_login
125
153
  LinuxStat::Sysconf.get_login
126
154
  end
127
155
 
128
- # def usernames_by_uid(gid = get_uid)
156
+ ##
157
+ # = def usernames_by_uid(gid = get_uid)
158
+ #
129
159
  # Where uid is the group id of the user. By default it's the uid of the current user.
130
160
  #
131
161
  # It returns an Array containing the username corresponding to the uid.
132
162
  #
133
163
  # For example:
134
164
  # LinuxStat::User.usernames_by_uid(1001)
165
+ #
135
166
  # => ["userx", "usery"]
136
167
  #
137
168
  # But if the info isn't available it will return an empty Array.
@@ -145,10 +176,13 @@ module LinuxStat
145
176
  usernames
146
177
  end
147
178
 
148
- # def username_by_gid(gid = get_gid)
179
+ ##
180
+ # = username_by_gid(gid = get_gid)
181
+ #
149
182
  # Where gid is the group id of the user. By default it's the gid of the current user.
150
183
  #
151
184
  # It returns a String cotaining the username corresponding to the gid
185
+ #
152
186
  # But if the info isn't available it will return an empty frozen String.
153
187
  def username_by_gid(gid = get_gid)
154
188
  return ''.freeze unless passwd_readable?
@@ -164,15 +198,19 @@ module LinuxStat
164
198
  username
165
199
  end
166
200
 
167
- # gid_by_username(username = get_user)
201
+ ##
202
+ # = gid_by_username(username = get_user)
203
+ #
168
204
  # Where username is the username to look for, by default it is the current user.
169
205
  #
170
206
  # It returns the gid by the username.
171
207
  # For example:
172
208
  # LinuxStat::User.gid_by_username('root')
173
- # => "0"
209
+ #
210
+ # => 0
174
211
  #
175
212
  # The return type is Integer.
213
+ #
176
214
  # But if user passed doesn't exist or if the info isn't available, it will return nil.
177
215
  def gid_by_username(username = get_user)
178
216
  return nil unless passwd_readable?
@@ -188,15 +226,20 @@ module LinuxStat
188
226
  gid
189
227
  end
190
228
 
191
- # uid_by_username(username = get_user)
229
+ ##
230
+ # = uid_by_username(username = get_user)
231
+ #
192
232
  # Where username is the username to look for, by default it is the current user.
193
233
  #
194
234
  # It returns the uid by the username.
235
+ #
195
236
  # For example:
196
237
  # LinuxStat::User.uid_by_username('root')
238
+ #
197
239
  # => 0
198
240
  #
199
241
  # The return type is Integer.
242
+ #
200
243
  # But if user passed doesn't exist or if the info isn't available, it will return nil.
201
244
  def uid_by_username(username = get_user)
202
245
  return nil unless passwd_readable?
@@ -212,8 +255,11 @@ module LinuxStat
212
255
  uid
213
256
  end
214
257
 
215
- # home_by_username(user = get_user)
258
+ ##
259
+ # = home_by_username(user = get_user)
260
+ #
216
261
  # Where user is the name of the user.
262
+ #
217
263
  # Returns the user's home. By default it returns the home of the current user.
218
264
  #
219
265
  # If the info isn't available, it will return ENV['HOME].to_s.freeze
@@ -231,10 +277,14 @@ module LinuxStat
231
277
  home
232
278
  end
233
279
 
234
- # home_by_uid(id = get_uid)
280
+ ##
281
+ # = home_by_uid(id = get_uid)
282
+ #
235
283
  # Gets all the users home directory with user id.
284
+ #
236
285
  # It returns an Array in this format:
237
286
  # LinuxStat::User.homes_by_uid(1001)
287
+ #
238
288
  # => ["/home/userx", "/home/usery"]
239
289
  #
240
290
  # Assuming both the users share same UID.
@@ -251,8 +301,11 @@ module LinuxStat
251
301
  home
252
302
  end
253
303
 
254
- # home_by_gid(id = get_uid)
304
+ ##
305
+ # = home_by_gid(id = get_uid)
306
+ #
255
307
  # Gets the home of the user corresponding to the GID.
308
+ #
256
309
  # It returns a String in this format:
257
310
  #
258
311
  # Assuming both the users share same UID.
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "0.6.2"
2
+ VERSION ||= "0.7.2"
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.6.2
4
+ version: 0.7.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-10 00:00:00.000000000 Z
11
+ date: 2020-12-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem