linux_stat 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 349ddabe649c7cdec58e01537c6c28ab47f9d30b413208baf5fae4c0c7a26804
4
- data.tar.gz: e66ba391b2264c53f6d2619ee64d8907e5435079b29bcc76bf2d54516ea82949
3
+ metadata.gz: 1c68e104d55d755cd1cef1e8901a7dd67b1a901b132d9a7786fabfd75b41cf5a
4
+ data.tar.gz: 94d102c6244b8da52b5f7aa7b1ea903546b5ef6e615c6317233b510005b6ded7
5
5
  SHA512:
6
- metadata.gz: 1f7274982f6330ed5023b6e7adf33f325163e382c9fdc81be8181c65ad182ba1e77433af792e1167d603673af147ff7e0601a42975f25ac2393206db755a27f5
7
- data.tar.gz: 61d48ba5bcf4598d8df63173bfbecfe4fd6a91552f32195a540e344961ee4e2ebcb9decf492d092b41ca034908b82948b9e6d727e678e3b41f434c5771794070
6
+ metadata.gz: b94d6afea3d307c845f316ce21f66e93944e1b33ad9576ecd8d55e299d6d520732677f99ea8fba3dcb3f6f5c6c8aae98526cc21b2c0218d7a8d49bdd7896cdec
7
+ data.tar.gz: cecbaee79f9844c5a6ad83720071a7977697edeb958d2336a68fa9f3b19ccb28074641c9620c88574dd0b5074ba6069a3457543a2cad86c11d9c5562d6e83da6
data/README.md CHANGED
@@ -13,12 +13,12 @@ It only works for Linux, and detecting the OS is upto the user of this gem.
13
13
  + You need to have the C compile to be able to compile the C extension.
14
14
  On Arch Linux:
15
15
  ```
16
- # pacman -S gcc
16
+ # pacman -S gcc make
17
17
  ```
18
18
 
19
19
  On Debian based systems:
20
20
  ```
21
- # apt install gcc
21
+ # apt install gcc build-essential
22
22
  ```
23
23
 
24
24
  + You might also require ruby-dev in Debian based systems which provides support for ruby.h header file:
@@ -103,7 +103,7 @@ LinuxStat::CPU.count
103
103
  => 4
104
104
 
105
105
  LinuxStat::CPU.cur_freq
106
- => [1999998, 2000003, 2000015, 2000009]
106
+ => [2000050, 2000024, 1998134, 1975870]
107
107
 
108
108
  LinuxStat::CPU.max_freq
109
109
  => [2000000, 2000000, 2000000, 2000000]
@@ -112,38 +112,38 @@ LinuxStat::CPU.model
112
112
  => "Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz"
113
113
 
114
114
  LinuxStat::CPU.stat
115
- => {0=>14.29, 1=>25.0, 2=>0.0, 3=>25.0, 4=>14.29}
115
+ => {0=>3.57, 1=>0.0, 2=>0.0, 3=>0.0, 4=>0.0}
116
116
 
117
117
  LinuxStat::CPU.total_usage
118
- => 18.75
118
+ => 3.45
119
119
 
120
120
  LinuxStat::CPU.usage
121
- => 14.29
121
+ => 9.68
122
122
 
123
123
  LinuxStat::CPU.usages
124
- => {0=>16.67, 1=>14.29, 2=>28.57, 3=>0.0, 4=>25.0}
124
+ => {0=>6.67, 1=>0.0, 2=>25.0, 3=>12.5, 4=>14.29}
125
125
 
126
126
  ```
127
127
 
128
128
  ### LinuxStat::Filesystem
129
129
  ```
130
130
  LinuxStat::Filesystem.available
131
- => 42144575488
131
+ => 36234563584
132
132
 
133
133
  LinuxStat::Filesystem.free
134
- => 42144575488
134
+ => 36234563584
135
135
 
136
136
  LinuxStat::Filesystem.stat
137
- => {:total=>119981191168, :free=>42144575488, :used=>77836615680}
137
+ => {:total=>119981191168, :free=>36234563584, :used=>83746627584}
138
138
 
139
139
  LinuxStat::Filesystem.stat_raw
140
- => {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>10289203, :block_avail_unpriv=>10289203, :inodes=>58612160, :free_inodes=>56716465, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
140
+ => {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>8846329, :block_avail_unpriv=>8846329, :inodes=>58612160, :free_inodes=>56635312, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
141
141
 
142
142
  LinuxStat::Filesystem.total
143
143
  => 119981191168
144
144
 
145
145
  LinuxStat::Filesystem.used
146
- => 77836615680
146
+ => 83746627584
147
147
 
148
148
  ```
149
149
 
@@ -184,22 +184,22 @@ LinuxStat::Kernel.version
184
184
  ### LinuxStat::Memory
185
185
  ```
186
186
  LinuxStat::Memory.available
187
- => 606548
187
+ => 602148
188
188
 
189
189
  LinuxStat::Memory.percent_available
190
- => 15.81
190
+ => 15.7
191
191
 
192
192
  LinuxStat::Memory.percent_used
193
- => 84.19
193
+ => 84.3
194
194
 
195
195
  LinuxStat::Memory.stat
196
- => {:total=>3836256, :used=>3229708, :available=>606548, :percent_used=>84.19, :percent_available=>15.81}
196
+ => {:total=>3836256, :used=>3234108, :available=>602148, :percent_used=>84.3, :percent_available=>15.7}
197
197
 
198
198
  LinuxStat::Memory.total
199
199
  => 3836256
200
200
 
201
201
  LinuxStat::Memory.used
202
- => 3229708
202
+ => 3234108
203
203
 
204
204
  ```
205
205
 
@@ -225,7 +225,7 @@ LinuxStat::Mounts.tmpfs
225
225
  ### LinuxStat::Net
226
226
  ```
227
227
  LinuxStat::Net.ipv4_private
228
- => "192.168.0.101"
228
+ => "192.168.43.107"
229
229
 
230
230
  ```
231
231
 
@@ -253,29 +253,29 @@ LinuxStat::OS.os_release
253
253
  => {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling", :ANSI_COLOR=>"38;2;23;147;209", :HOME_URL=>"https://www.archlinux.org/", :DOCUMENTATION_URL=>"https://wiki.archlinux.org/", :SUPPORT_URL=>"https://bbs.archlinux.org/"...
254
254
 
255
255
  LinuxStat::OS.uptime
256
- => {:hour=>9, :minute=>10, :second=>19.41}
256
+ => {:hour=>15, :minute=>19, :second=>34.72}
257
257
 
258
258
  ```
259
259
 
260
260
  ### LinuxStat::Process
261
261
  ```
262
262
  LinuxStat::Process.count
263
- => 221
263
+ => 228
264
264
 
265
265
  LinuxStat::Process.idle
266
- => [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118, 120, 122, 131, 134, 140, 152, 153, 168, 170, 173, 174, 182, 183, 184, 185, 186, 187, 214, 224, 268, 308, 313, 314, 330, 2357, 2358, 2359, 2360, 2361, 2362, 25094, 2...
266
+ => [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118, 119, 121, 131, 134, 140, 152, 153, 174, 176, 178, 182, 183, 187, 188, 189, 190, 191, 192, 225, 231, 280, 327, 329, 335, 5823, 5824, 5825, 5826, 5827, 5828, 43047, 4...
267
267
 
268
268
  LinuxStat::Process.list
269
- => [1, 2, 3, 4, 6, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40, 41, 42, 46, 47, 48, 49, 50, 51, 52, 102, 103, 104, 106, 107, 108, 109, 110, 112, 114, 115, 117, 118, 120, 122, 131, 134,...
269
+ => [1, 2, 3, 4, 6, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40, 41, 42, 46, 47, 48, 49, 50, 51, 52, 102, 103, 104, 106, 107, 108, 109, 110, 112, 114, 115, 117, 118, 119, 121, 131, 134,...
270
270
 
271
271
  LinuxStat::Process.names
272
272
  => {1=>"systemd", 2=>"kthreadd", 3=>"rcu_gp", 4=>"rcu_par_gp", 6=>"kworker/0:0H-kblockd", 9=>"mm_percpu_wq", 10=>"ksoftirqd/0", 11=>"rcuc/0", 12=>"rcu_preempt", 13=>"rcub/0", 14=>"migration/0", 15=>"idle_inject/0", 16=>"cpuhp/0", 17=>"cpuhp/1", 18=>"idle...
273
273
 
274
274
  LinuxStat::Process.running
275
- => [30804]
275
+ => [49287]
276
276
 
277
277
  LinuxStat::Process.sleeping
278
- => [1, 2, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 38, 40, 41, 42, 46, 47, 48, 50, 51, 52, 112, 114, 115, 167, 169, 188, 215, 228, 307, 309, 311, 316, 317, 319, 320, 354, 355, 356, 357, 364, 368, 370, 384, 397,...
278
+ => [1, 2, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 38, 40, 41, 42, 46, 47, 48, 50, 51, 52, 112, 114, 115, 173, 175, 193, 219, 235, 301, 312, 313, 315, 326, 328, 340, 363, 364, 365, 366, 373, 376, 378, 381, 395,...
279
279
 
280
280
  LinuxStat::Process.types
281
281
  => {1=>:sleeping, 2=>:sleeping, 3=>:idle, 4=>:idle, 6=>:idle, 9=>:idle, 10=>:sleeping, 11=>:sleeping, 12=>:idle, 13=>:sleeping, 14=>:sleeping, 15=>:sleeping, 16=>:sleeping, 17=>:sleeping, 18=>:sleeping, 19=>:sleeping, 20=>:sleeping, 21=>:sleeping, 23=>:i...
@@ -288,37 +288,37 @@ LinuxStat::Process.zombie
288
288
  ### LinuxStat::ProcessInfo
289
289
  ```
290
290
  LinuxStat::ProcessInfo.cmdline
291
- => "ruby exe/linuxstat.rb -md"
291
+ => "/usr/bin/ruby /home/sourav/.gem/ruby/2.7.0/bin/linuxstat.rb --html -md"
292
292
 
293
293
  LinuxStat::ProcessInfo.command_name
294
294
  => "ruby"
295
295
 
296
296
  LinuxStat::ProcessInfo.cpu_stat
297
- => {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>2}
297
+ => {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>3}
298
298
 
299
299
  LinuxStat::ProcessInfo.cpu_usage
300
300
  => 0.0
301
301
 
302
302
  LinuxStat::ProcessInfo.last_executed_cpu
303
- => 2
303
+ => 3
304
304
 
305
305
  LinuxStat::ProcessInfo.mem_stat
306
- => {:memory=>9644, :virtual_memory=>79568, :resident_memory=>15764}
306
+ => {:memory=>11748, :virtual_memory=>81680, :resident_memory=>17928}
307
307
 
308
308
  LinuxStat::ProcessInfo.memory
309
- => 9644
309
+ => 11748
310
310
 
311
311
  LinuxStat::ProcessInfo.resident_memory
312
- => 15764
312
+ => 17928
313
313
 
314
314
  LinuxStat::ProcessInfo.threads
315
315
  => 1
316
316
 
317
317
  LinuxStat::ProcessInfo.total_io
318
- => {:read_bytes=>0, :write_bytes=>0}
318
+ => {:read_bytes=>3932160, :write_bytes=>0}
319
319
 
320
320
  LinuxStat::ProcessInfo.virtual_memory
321
- => 79568
321
+ => 81680
322
322
 
323
323
  ```
324
324
 
@@ -328,25 +328,25 @@ LinuxStat::Swap.any?
328
328
  => true
329
329
 
330
330
  LinuxStat::Swap.available
331
- => 2256212
331
+ => 3358184
332
332
 
333
333
  LinuxStat::Swap.list
334
- => {"/dev/zram0"=>[:partition, 4194300, 1938088, -2]}
334
+ => {"/dev/zram0"=>[:partition, 4194300, 836116, -2]}
335
335
 
336
336
  LinuxStat::Swap.percent_available
337
- => 53.79
337
+ => 80.07
338
338
 
339
339
  LinuxStat::Swap.percent_used
340
- => 46.21
340
+ => 19.93
341
341
 
342
342
  LinuxStat::Swap.stat
343
- => {:total=>4194300, :used=>1938088, :available=>2256212, :percent_used=>46.21, :percent_available=>53.79}
343
+ => {:total=>4194300, :used=>836116, :available=>3358184, :percent_used=>19.93, :percent_available=>80.07}
344
344
 
345
345
  LinuxStat::Swap.total
346
346
  => 4194300
347
347
 
348
348
  LinuxStat::Swap.used
349
- => 1938088
349
+ => 836116
350
350
 
351
351
  ```
352
352
 
@@ -355,6 +355,21 @@ LinuxStat::Swap.used
355
355
  LinuxStat::Sysconf.child_max
356
356
  => 2000000
357
357
 
358
+ LinuxStat::Sysconf.get_euid
359
+ => 1000
360
+
361
+ LinuxStat::Sysconf.get_gid
362
+ => 1000
363
+
364
+ LinuxStat::Sysconf.get_login
365
+ => "sourav"
366
+
367
+ LinuxStat::Sysconf.get_uid
368
+ => 1000
369
+
370
+ LinuxStat::Sysconf.get_user
371
+ => "sourav"
372
+
358
373
  LinuxStat::Sysconf.hostname_max
359
374
  => 64
360
375
 
@@ -362,7 +377,7 @@ LinuxStat::Sysconf.login_name_max
362
377
  => 256
363
378
 
364
379
  LinuxStat::Sysconf.open_max
365
- => 8192
380
+ => 1024
366
381
 
367
382
  LinuxStat::Sysconf.page_size_max
368
383
  => 4096
@@ -564,7 +579,7 @@ irb(main):003:0> t = Time.now ; puts LinuxStat::FS.stat('/') ; Time.now - t
564
579
  => 5.0468e-05
565
580
  ```
566
581
 
567
- To learn more about them, just run ri and the method name. To see all available methods:
582
+ To learn more about them, just run ri and the method name. To see all available methods.
568
583
 
569
584
  ## Note 4: User
570
585
  Most of the LinuxStat::User supports arguments.
@@ -17,11 +17,9 @@ module LinuxStat
17
17
  # But if the status isn't available it will return an empty Hash.
18
18
  def ids
19
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
20
+ passwd_splitted.reduce({}) { |h, x|
21
+ h.merge!(x[0].to_sym => {
22
+ uid: x[2].to_i, gid: x[3].to_i
25
23
  })
26
24
  }
27
25
  end
@@ -34,9 +32,8 @@ module LinuxStat
34
32
  # But if the status isn't available it will return an empty Hash.
35
33
  def uids
36
34
  return {} unless passwd_readable?
37
- passwd.reduce({}) { |h, x|
38
- splitted = x.split(?:)
39
- h.merge!(splitted[0].to_sym => splitted[2].to_i)
35
+ passwd_splitted.reduce({}) { |h, x|
36
+ h.merge!(x[0].to_sym => x[2].to_i)
40
37
  }
41
38
  end
42
39
 
@@ -48,9 +45,8 @@ module LinuxStat
48
45
  # But if the status isn't available it will return an empty Hash.
49
46
  def gids
50
47
  return {} unless passwd_readable?
51
- passwd.reduce({}) { |h, x|
52
- splitted = x.split(?:)
53
- h.merge!(splitted[0].to_sym => splitted[3].to_i)
48
+ passwd_splitted.reduce({}) { |h, x|
49
+ h.merge!(x[0].to_sym => x[3].to_i)
54
50
  }
55
51
  end
56
52
 
@@ -69,11 +65,35 @@ module LinuxStat
69
65
  end
70
66
 
71
67
  # Returns the user ID as integer
72
- # It directly calls LinuxStat::Sysconf.get_user
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
73
  #
74
- # It doesn't get affected with the assignment of USER environment variable.
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.
75
80
  def get_user
76
- LinuxStat::Sysconf.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
77
97
  end
78
98
 
79
99
  # Returns the user ID as integer
@@ -94,6 +114,17 @@ module LinuxStat
94
114
  LinuxStat::Sysconf.get_euid
95
115
  end
96
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
+
97
128
  # def usernames_by_uid(gid = get_uid)
98
129
  # Where uid is the group id of the user. By default it's the uid of the current user.
99
130
  #
@@ -108,10 +139,9 @@ module LinuxStat
108
139
  return [] unless passwd_readable?
109
140
 
110
141
  usernames = []
111
- passwd.each do |x|
112
- splitted = x.split(?:.freeze)
113
- usernames << splitted[0] if splitted[2].to_i == uid
114
- end
142
+ passwd_splitted.each { |x|
143
+ usernames << x[0] if x[2].to_i == uid
144
+ }
115
145
  usernames
116
146
  end
117
147
 
@@ -243,12 +273,22 @@ module LinuxStat
243
273
  home
244
274
  end
245
275
 
276
+ alias get_current_user get_user
277
+
246
278
  private
247
279
  def passwd
248
280
  @@passwd_file ||= '/etc/passwd'.freeze
249
281
  IO.readlines(@@passwd_file)
250
282
  end
251
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
+
252
292
  def passwd_readable?
253
293
  @@passwd_file ||= '/etc/passwd'.freeze
254
294
  @@passwd_readable ||= File.readable?(@@passwd_file)
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.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.5.1
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-05 00:00:00.000000000 Z
11
+ date: 2020-12-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem