linux_stat 0.3.1 → 0.5.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.
@@ -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
- # Show percentage of swap available.
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,258 @@
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.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
+ })
26
+ }
27
+ end
28
+
29
+ # Returns all the user IDs as Hash.
30
+ # For example:
31
+ # LinuxStat::User.uids
32
+ # => {:root=>0, :bin=>1, :daemon=>2, :mail=>8, :ftp=>14}
33
+ #
34
+ # But if the status isn't available it will return an empty Hash.
35
+ def uids
36
+ return {} unless passwd_readable?
37
+ passwd.reduce({}) { |h, x|
38
+ splitted = x.split(?:)
39
+ h.merge!(splitted[0].to_sym => splitted[2].to_i)
40
+ }
41
+ end
42
+
43
+ # Returns all the Group identifiers as Hash.
44
+ # For example:
45
+ # LinuxStat::User.gids
46
+ # => {:root=>0, :bin=>1, :daemon=>2, :mail=>12, :ftp=>11}
47
+ #
48
+ # But if the status isn't available it will return an empty Hash.
49
+ def gids
50
+ return {} unless passwd_readable?
51
+ passwd.reduce({}) { |h, x|
52
+ splitted = x.split(?:)
53
+ h.merge!(splitted[0].to_sym => splitted[3].to_i)
54
+ }
55
+ end
56
+
57
+ # Returns all the home directories as Hash.
58
+ # For example:
59
+ # LinuxStat::User.home_directories
60
+ # => {:root=>"/root", :bin=>"/", :daemon=>"/", :mail=>"/var/spool/mail", :ftp=>"/srv/ftp", :http=>"/srv/http", :nobody=>"/"}
61
+ #
62
+ # But if the status isn't available it will return an empty Hash.
63
+ def home_directories
64
+ return {} unless passwd_readable?
65
+ passwd.reduce({}) { |h, x|
66
+ splitted = x.split(?:)
67
+ h.merge!(splitted[0].to_sym => splitted[5])
68
+ }
69
+ end
70
+
71
+ # Returns the user ID as integer
72
+ # It directly calls LinuxStat::Sysconf.get_user
73
+ #
74
+ # It doesn't get affected with the assignment of USER environment variable.
75
+ def get_user
76
+ LinuxStat::Sysconf.get_user
77
+ end
78
+
79
+ # Returns the user ID as integer
80
+ # It directly calls LinuxStat::Sysconf.get_uid
81
+ def get_uid
82
+ LinuxStat::Sysconf.get_uid
83
+ end
84
+
85
+ # Returns the group ID as integer
86
+ # It directly calls LinuxStat::Sysconf.get_uid
87
+ def get_gid
88
+ LinuxStat::Sysconf.get_gid
89
+ end
90
+
91
+ # Returns the effective user ID as integer
92
+ # It directly calls LinuxStat::Sysconf.get_euid
93
+ def get_euid
94
+ LinuxStat::Sysconf.get_euid
95
+ end
96
+
97
+ # def usernames_by_uid(gid = get_uid)
98
+ # Where uid is the group id of the user. By default it's the uid of the current user.
99
+ #
100
+ # It returns an Array containing the username corresponding to the uid.
101
+ #
102
+ # For example:
103
+ # LinuxStat::User.usernames_by_uid(1001)
104
+ # => ["userx", "usery"]
105
+ #
106
+ # But if the info isn't available it will return an empty Array.
107
+ def usernames_by_uid(uid = get_uid)
108
+ return [] unless passwd_readable?
109
+
110
+ usernames = []
111
+ passwd.each do |x|
112
+ splitted = x.split(?:.freeze)
113
+ usernames << splitted[0] if splitted[2].to_i == uid
114
+ end
115
+ usernames
116
+ end
117
+
118
+ # def username_by_gid(gid = get_gid)
119
+ # Where gid is the group id of the user. By default it's the gid of the current user.
120
+ #
121
+ # It returns a String cotaining the username corresponding to the gid
122
+ # But if the info isn't available it will return an empty frozen String.
123
+ def username_by_gid(gid = get_gid)
124
+ return ''.freeze unless passwd_readable?
125
+
126
+ username = ''
127
+ passwd.each do |x|
128
+ splitted = x.split(?:.freeze)
129
+ if splitted[2].to_i == gid
130
+ username = splitted[0]
131
+ break
132
+ end
133
+ end
134
+ username
135
+ end
136
+
137
+ # gid_by_username(username = get_user)
138
+ # Where username is the username to look for, by default it is the current user.
139
+ #
140
+ # It returns the gid by the username.
141
+ # For example:
142
+ # LinuxStat::User.gid_by_username('root')
143
+ # => "0"
144
+ #
145
+ # The return type is Integer.
146
+ # But if user passed doesn't exist or if the info isn't available, it will return nil.
147
+ def gid_by_username(username = get_user)
148
+ return nil unless passwd_readable?
149
+
150
+ gid = nil
151
+ passwd.each do |x|
152
+ splitted = x.split(?:.freeze)
153
+ if splitted[0] == username
154
+ gid = splitted[3].to_i
155
+ break
156
+ end
157
+ end
158
+ gid
159
+ end
160
+
161
+ # uid_by_username(username = get_user)
162
+ # Where username is the username to look for, by default it is the current user.
163
+ #
164
+ # It returns the uid by the username.
165
+ # For example:
166
+ # LinuxStat::User.uid_by_username('root')
167
+ # => 0
168
+ #
169
+ # The return type is Integer.
170
+ # But if user passed doesn't exist or if the info isn't available, it will return nil.
171
+ def uid_by_username(username = get_user)
172
+ return nil unless passwd_readable?
173
+
174
+ uid = nil
175
+ passwd.each do |x|
176
+ splitted = x.split(?:.freeze)
177
+ if splitted[0] == username
178
+ uid = splitted[2].to_i
179
+ break
180
+ end
181
+ end
182
+ uid
183
+ end
184
+
185
+ # home_by_username(user = get_user)
186
+ # Where user is the name of the user.
187
+ # Returns the user's home. By default it returns the home of the current user.
188
+ #
189
+ # If the info isn't available, it will return ENV['HOME].to_s.freeze
190
+ def home_by_username(user = get_user)
191
+ return ENV['HOME'].to_s.freeze unless passwd_readable?
192
+
193
+ home = ''
194
+ passwd.each { |x|
195
+ splitted = x.split(?:)
196
+ if splitted[0] == user
197
+ home = splitted[5]
198
+ break
199
+ end
200
+ }
201
+ home
202
+ end
203
+
204
+ # home_by_uid(id = get_uid)
205
+ # Gets all the users home directory with user id.
206
+ # It returns an Array in this format:
207
+ # LinuxStat::User.homes_by_uid(1001)
208
+ # => ["/home/userx", "/home/usery"]
209
+ #
210
+ # Assuming both the users share same UID.
211
+ #
212
+ # If the info isn't available, it will return an empty Array.
213
+ def homes_by_uid(id = get_uid)
214
+ return [] unless passwd_readable?
215
+
216
+ home = []
217
+ passwd.each do |x|
218
+ splitted = x.split(?:.freeze)
219
+ home << splitted[5] if splitted[2].to_i == id
220
+ end
221
+ home
222
+ end
223
+
224
+ # home_by_gid(id = get_uid)
225
+ # Gets the home of the user corresponding to the GID.
226
+ # It returns a String in this format:
227
+ #
228
+ # Assuming both the users share same UID.
229
+ #
230
+ # If the info isn't available, it will return an empty frozen String.
231
+ def home_by_gid(id = get_gid)
232
+ return ''.freeze unless passwd_readable?
233
+
234
+ home = ''
235
+ passwd.each do |x|
236
+ splitted = x.split(?:.freeze)
237
+
238
+ if splitted[3].to_i == id
239
+ home = splitted[5]
240
+ break
241
+ end
242
+ end
243
+ home
244
+ end
245
+
246
+ private
247
+ def passwd
248
+ @@passwd_file ||= '/etc/passwd'.freeze
249
+ IO.readlines(@@passwd_file)
250
+ end
251
+
252
+ def passwd_readable?
253
+ @@passwd_file ||= '/etc/passwd'.freeze
254
+ @@passwd_readable ||= File.readable?(@@passwd_file)
255
+ end
256
+ end
257
+ end
258
+ end
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "0.3.1"
2
+ VERSION = "0.5.1"
3
3
  end
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.3.1
4
+ version: 0.5.1
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-01 00:00:00.000000000 Z
11
+ date: 2020-12-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Efficient linux system reporting gem. Linux Only | Efficient | Reliable
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: