linux_stat 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d883007405e198d66cdf5f9cd03dc630b6e02d3d0c3a54ff33322a9562fcaa4e
4
- data.tar.gz: c353965cff2a9c0de891cd8ba6d4f0272520b3b40cf48525f802867142855fb7
3
+ metadata.gz: 4b3270fb4ddf3dd436a63233d134f5fecd2ff13d7aa0d4f91b444ac65ed29b84
4
+ data.tar.gz: 8f4987b804b866eb6b0bea4a9de1a507e6529ca6188d54b0def59a7dbf74c73f
5
5
  SHA512:
6
- metadata.gz: affabda2ed39158a40d12710810d5f604bab1c621e55accdc363d3392fd2e651ee860b0abb0dff1d0317e6bd85e045b68a963ea939b04f765fc276517ac039cb
7
- data.tar.gz: 286647ecf50f14f62d37efbd9d2c81cbbf2a79b7b0365fc6c1f96bb3064f0cf3bd2d89fae45e6fcb63442cf563063f6340e295720525bb4f02860b4022856715
6
+ metadata.gz: fd9393533d03fbfe16599d972762aaa6d84aa737de9ceccd7a4b333a3e049742e2a088f29364203dfd5e65199c95ccc790a68d31e8c8cf6c5f6fd718dc655818
7
+ data.tar.gz: b4fbe848f72fe1d51ed8c3d8bba37aba8a1da937c4f1639503dedbb894fc8eebe6516c7c8957e88569b73362d03dfd4a60787af1c4c585fe40b73725e2f5dabe
@@ -11,7 +11,7 @@ module LinuxStat
11
11
  def stat
12
12
  return {} unless meminfo?
13
13
 
14
- memory = IO.foreach('/proc/meminfo').first(3)
14
+ memory = IO.foreach('/proc/meminfo'.freeze).first(3)
15
15
 
16
16
  total = memory[0].split[1].to_i
17
17
  available = memory[2].split[1].to_i
@@ -39,7 +39,7 @@ module LinuxStat
39
39
  # It retuns an Integer but if the info is not available, it will return nil.
40
40
  def total
41
41
  return nil unless meminfo?
42
- IO.foreach('/proc/meminfo').first.split[1].to_i
42
+ IO.foreach('/proc/meminfo'.freeze).first.split[1].to_i
43
43
  end
44
44
 
45
45
  ##
@@ -49,7 +49,7 @@ module LinuxStat
49
49
  # It retuns an Integer but if the info is not available, it will return nil
50
50
  def available
51
51
  return nil unless meminfo?
52
- IO.foreach('/proc/meminfo').first(3)[-1].split[1].to_i
52
+ IO.foreach('/proc/meminfo'.freeze).first(3)[-1].split[1].to_i
53
53
  end
54
54
 
55
55
  ##
@@ -59,7 +59,7 @@ module LinuxStat
59
59
  # It retuns an Integer but if the info is not available, it will return nil.
60
60
  def used
61
61
  return nil unless meminfo?
62
- memory = IO.foreach('/proc/meminfo').first(3)
62
+ memory = IO.foreach('/proc/meminfo'.freeze).first(3)
63
63
  memory[0].split[1].to_i - memory[2].split[1].to_i
64
64
  end
65
65
 
@@ -69,7 +69,7 @@ module LinuxStat
69
69
  # It retuns an Integer but if the info is not available, it will return nil
70
70
  def percent_used
71
71
  return nil unless meminfo?
72
- memory = IO.foreach('/proc/meminfo').first(3)
72
+ memory = IO.foreach('/proc/meminfo'.freeze).first(3)
73
73
  total = memory[0].split[1].to_i
74
74
  total.-(memory[2].split[1].to_i).*(100).fdiv(total).round(2)
75
75
  end
@@ -80,13 +80,13 @@ module LinuxStat
80
80
  # It retuns an Integer but if the info is not available, it will return nil
81
81
  def percent_available
82
82
  return nil unless meminfo?
83
- memory = IO.foreach('/proc/meminfo').first(3)
83
+ memory = IO.foreach('/proc/meminfo'.freeze).first(3)
84
84
  memory[2].split[1].to_i.*(100).fdiv(memory[0].split[1].to_i).round(2)
85
85
  end
86
86
 
87
87
  private
88
88
  def meminfo?
89
- @@readable ||= File.readable?('/proc/meminfo')
89
+ @@readable ||= File.readable?('/proc/meminfo'.freeze)
90
90
  end
91
91
  end
92
92
  end
@@ -209,17 +209,17 @@ module LinuxStat
209
209
 
210
210
  private
211
211
  def mount_readable?
212
- @@mount_readable ||= File.readable?('/proc/mounts')
212
+ @@mount_readable ||= File.readable?('/proc/mounts'.freeze)
213
213
  end
214
214
 
215
215
  def mounts
216
216
  return [] unless mount_readable?
217
- IO.readlines('/proc/mounts').each(&:strip!)
217
+ IO.readlines('/proc/mounts'.freeze).each(&:strip!)
218
218
  end
219
219
 
220
220
  def find_root
221
221
  return [] unless mount_readable?
222
- @@root ||= IO.foreach('/proc/mounts').find { |x| x.split[1] == '/'.freeze }.split
222
+ @@root ||= IO.foreach('/proc/mounts'.freeze).find { |x| x.split[1] == '/'.freeze }.split
223
223
  end
224
224
 
225
225
  def fs_info(dev)
@@ -86,13 +86,13 @@ module LinuxStat
86
86
 
87
87
  data = IO.readlines(DEV).drop(2)
88
88
  indices = find_index_of_bytes
89
- data.reject! { |x| x.strip.start_with?('lo:') }
89
+ data.reject! { |x| x.strip.start_with?('lo:'.freeze) }
90
90
  r, t = data.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map(&:sum)
91
91
 
92
92
  sleep(interval)
93
93
 
94
94
  data2 = IO.readlines(DEV).drop(2)
95
- data2.reject! { |x| x.strip.start_with?('lo:') }
95
+ data2.reject! { |x| x.strip.start_with?('lo:'.freeze) }
96
96
  r2, t2 = data2.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map(&:sum)
97
97
 
98
98
  # Measure the difference
@@ -118,8 +118,8 @@ module LinuxStat
118
118
 
119
119
  r.each_with_index { |x, i|
120
120
  downcased = x.downcase
121
- h.merge!(:r => i) if downcased.start_with?('receive')
122
- h.merge!(:t => i) if downcased.start_with?('transmit')
121
+ h.merge!(:r => i) if downcased.start_with?('receive'.freeze)
122
+ h.merge!(:t => i) if downcased.start_with?('transmit'.freeze)
123
123
  }
124
124
 
125
125
  data_0 = data.next.gsub(?|.freeze, ' %'.freeze)
@@ -52,7 +52,7 @@ module LinuxStat
52
52
  elsif v.key?(:DISTRIB_ID)
53
53
  v[:DISTRIB_ID]
54
54
  elsif File.readable?('/etc/issue'.freeze)
55
- IO.read('/etc/issue'.freeze, encoding: 'ASCII-8bit').strip
55
+ IO.read('/etc/issue'.freeze, encoding: 'ASCII-8bit'.freeze).strip
56
56
  else
57
57
  'Unknown'.freeze
58
58
  end
@@ -6,10 +6,17 @@ module LinuxStat
6
6
  #
7
7
  # The return type is an Array of Integers.
8
8
  def list
9
- Dir['/proc/*'].select { |x|
10
- pid = File.split(x)[1]
11
- pid.to_i.to_s == pid
12
- }.map! { |x| File.split(x)[-1].to_i }
9
+ d = Dir['/proc/*'.freeze]
10
+ ret, i = [], -1
11
+ count = d.length
12
+
13
+ while(i += 1) < count
14
+ pid = File.split(d[i])[1]
15
+ pid_i = pid.to_i
16
+ ret << pid_i if pid_i.to_s == pid
17
+ end
18
+
19
+ ret
13
20
  end
14
21
 
15
22
  ##
@@ -17,28 +24,42 @@ module LinuxStat
17
24
  #
18
25
  # The return type is Integer.
19
26
  def count
20
- list.count
27
+ list.length
21
28
  end
22
29
 
23
30
  ##
24
31
  # Returns all the id of processes mapped with their names as a Hash.
25
32
  def names
26
- list.reduce({}) { |h, x|
33
+ h, i = {}, -1
34
+
35
+ l = list
36
+ count = l.length
37
+
38
+ while(i += 1) < count
39
+ x = l[i]
40
+
27
41
  begin
28
- h.merge!( x => IO.foreach(File.join('/proc', x.to_s, 'status')).first.split[1] )
29
- rescue Exception
30
- h
42
+ h.merge!( x => IO.foreach("/proc/#{x}/status").first.split[1])
43
+ rescue StandardError
31
44
  end
32
- }
45
+ end
46
+ h
33
47
  end
34
48
 
35
49
  ##
36
50
  # Returns all the id of processes mapped with their status as a Hash.
37
51
  def types
38
- list.reduce({}) { |h, x|
52
+ h, i = {}, -1
53
+
54
+ l = list
55
+ count = l.length
56
+
57
+ while(i += 1) < count
58
+ x = l[i]
59
+
39
60
  begin
40
61
  h.merge!(x =>
41
- case IO.read(File.join('/proc', x.to_s, 'stat')).split[2]
62
+ case IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0]
42
63
  when ?S.freeze then :sleeping
43
64
  when ?I.freeze then :idle
44
65
  when ?Z.freeze then :zombie
@@ -46,10 +67,11 @@ module LinuxStat
46
67
  else :unknown
47
68
  end
48
69
  )
49
- rescue Exception
50
- h
70
+ rescue StandardError
51
71
  end
52
- }
72
+ end
73
+
74
+ h
53
75
  end
54
76
 
55
77
  ##
@@ -58,8 +80,8 @@ module LinuxStat
58
80
  def sleeping
59
81
  list.select { |x|
60
82
  begin
61
- IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?S
62
- rescue Exception
83
+ IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0] == ?S.freeze
84
+ rescue StandardError
63
85
  nil
64
86
  end
65
87
  }
@@ -71,8 +93,8 @@ module LinuxStat
71
93
  def idle
72
94
  list.select { |x|
73
95
  begin
74
- IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?I
75
- rescue Exception
96
+ IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0] == ?I.freeze
97
+ rescue StandardError
76
98
  nil
77
99
  end
78
100
  }
@@ -84,8 +106,8 @@ module LinuxStat
84
106
  def zombie
85
107
  list.select { |x|
86
108
  begin
87
- IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?Z
88
- rescue Exception
109
+ IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0] == ?Z.freeze
110
+ rescue StandardError
89
111
  nil
90
112
  end
91
113
  }
@@ -97,8 +119,8 @@ module LinuxStat
97
119
  def running
98
120
  list.select { |x|
99
121
  begin
100
- IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?R
101
- rescue Exception
122
+ IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0] == ?R.freeze
123
+ rescue StandardError
102
124
  nil
103
125
  end
104
126
  }
@@ -8,7 +8,7 @@ module LinuxStat
8
8
  def list
9
9
  return {} unless swaps_readable?
10
10
 
11
- file = IO.readlines('/proc/swaps').drop(1)
11
+ file = IO.readlines('/proc/swaps'.freeze).drop(1)
12
12
  file.reduce({}) do |h, x|
13
13
  name, *stats = x.strip.split
14
14
  h.merge!(name => stats.map! { |v| v.to_i.to_s == v ? v.to_i : v.to_sym })
@@ -20,7 +20,7 @@ module LinuxStat
20
20
  #
21
21
  # If the info isn't available, it will return an empty Hash.
22
22
  def any?
23
- !!IO.foreach('/proc/swaps').drop(1).first
23
+ !!IO.foreach('/proc/swaps'.freeze).first(2)[1]
24
24
  end
25
25
 
26
26
  # Show aggregated used and available swap.
@@ -114,7 +114,7 @@ module LinuxStat
114
114
  def read_usage
115
115
  return [[], []] unless swaps_readable?
116
116
 
117
- val = IO.readlines('/proc/swaps').drop(1)
117
+ val = IO.readlines('/proc/swaps'.freeze).drop(1)
118
118
  return [[], []] if val.empty?
119
119
 
120
120
  val.map! { |x|
@@ -123,7 +123,7 @@ module LinuxStat
123
123
  end
124
124
 
125
125
  def swaps_readable?
126
- @@swaps_readable ||= File.readable?('/proc/swaps')
126
+ @@swaps_readable ||= File.readable?('/proc/swaps'.freeze)
127
127
  end
128
128
  end
129
129
  end
@@ -55,6 +55,7 @@ module LinuxStat
55
55
  # But if the status isn't available it will return an empty Hash.
56
56
  def gids
57
57
  return {} unless passwd_readable?
58
+
58
59
  passwd_splitted.reduce({}) { |h, x|
59
60
  h.merge!(x[0].to_sym => x[3].to_i)
60
61
  }
@@ -103,7 +104,7 @@ module LinuxStat
103
104
 
104
105
  uid, gid = LinuxStat::Sysconf.get_uid, LinuxStat::Sysconf.get_gid
105
106
 
106
- username = ''
107
+ username = ''.freeze
107
108
  passwd.each { |x|
108
109
  splitted = x.split(?:).freeze
109
110
  if splitted[2].to_i == uid && splitted[3].to_i == gid
@@ -187,7 +188,7 @@ module LinuxStat
187
188
  def username_by_gid(gid = get_gid)
188
189
  return ''.freeze unless passwd_readable?
189
190
 
190
- username = ''
191
+ username = ''.freeze
191
192
  passwd.each do |x|
192
193
  splitted = x.split(?:.freeze)
193
194
  if splitted[2].to_i == gid
@@ -314,7 +315,7 @@ module LinuxStat
314
315
  def home_by_gid(id = get_gid)
315
316
  return ''.freeze unless passwd_readable?
316
317
 
317
- home = ''
318
+ home = ''.freeze
318
319
  passwd.each do |x|
319
320
  splitted = x.split(?:.freeze)
320
321
 
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION ||= "1.2.2"
2
+ VERSION ||= "1.2.3"
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: 1.2.2
4
+ version: 1.2.3
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-28 00:00:00.000000000 Z
11
+ date: 2020-12-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem