linux_stat 0.1.3 → 0.1.4

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: 48d667ee1df458052019071ea8b1991f34984e7e508ffd53ab5a10ecab8a9e39
4
- data.tar.gz: 6a554e8a5a1a840e5dd5d2a34fef28f863da6ca083234b5ae6fcd2cde7258098
3
+ metadata.gz: 214a2a52170b56710d91ad64ee52dc616e6dd4075c9c03640727adbdd9814b32
4
+ data.tar.gz: c7e5eef64f6849823a56a62ca75dca1b0e7555908ec12234a3ffc0d06b4bb075
5
5
  SHA512:
6
- metadata.gz: b7ae93e1d6f146c68f189dc34c53f5149d8dff3088e0fa5d71bc65465f497477cb17a535fd7e28bc717fcd4aa08c2afc924c3bb88e0933abf5a95d93086d1154
7
- data.tar.gz: 813c7c545a771d42bf7f369d3bcfac8595a875510a3bbe652e106b42719d7f37c259b9fa037a467ebec88098859df0b389a0dec626379d85e83a7cfd3ea4ce0f
6
+ metadata.gz: a5e4dd2b4e9a86c6ffd3ffdfc50663594978f40c9d94a8a9af7d3c3d828f6c3c1c31f36408826844c713f35870d434b07fe6bab57b45f755a758ffa7c7ee152c
7
+ data.tar.gz: 754e223628903b9d3e241d79242ab55b79bcc6b79f2b1b64e941d98f096291f84aeac433bff104e34bcddcc40527e1aef73525228dd5cb98748f669b8fdfe2a4
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- linux_stat (0.1.3)
4
+ linux_stat (0.1.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -21,6 +21,21 @@ module LinuxStat
21
21
  end
22
22
  end
23
23
 
24
+ def total_usage(sleep = 0.075)
25
+ return {} unless stat?
26
+
27
+ data = IO.foreach('/proc/stat').first.split.tap(&:shift).map!(&:to_f)
28
+ sleep(sleep)
29
+ data2 = IO.foreach('/proc/stat').first.split.tap(&:shift).map!(&:to_f)
30
+
31
+ user, nice, sys, idle, iowait, irq, softirq, steal = *data
32
+ user2, nice2, sys2, idle2, iowait2, irq2, softirq2, steal2 = *data2
33
+
34
+ idle_then, idle_now = idle + iowait, idle2 + iowait2
35
+ totald = idle_now.+(user2 + nice2 + sys2 + irq2 + softirq2 + steal2) - idle_then.+(user + nice + sys + irq + softirq + steal)
36
+ totald.-(idle_now - idle_then).fdiv(totald).*(100).round(2).abs
37
+ end
38
+
24
39
  def count
25
40
  # CPU count can change during the program runtime
26
41
  cpuinfo.count { |x| x.start_with?('processor') }
@@ -50,6 +65,9 @@ module LinuxStat
50
65
  @@max_freqs.map { |x| IO.read(x).to_i }
51
66
  end
52
67
 
68
+ alias usages stat
69
+ alias usage total_usage
70
+
53
71
  private
54
72
  def cpuinfo
55
73
  File.readable?('/proc/cpuinfo') ? IO.readlines('/proc/cpuinfo') : []
@@ -2,18 +2,36 @@ module LinuxStat
2
2
  module Kernel
3
3
  class << self
4
4
  def version
5
- return @@version ||= ''.freeze if string.empty?
6
- @@version ||= string.split[2]
5
+ return ''.freeze if string.empty?
6
+ @@version ||= splitted[2]
7
+ end
8
+
9
+ def build_user
10
+ @@build_user ||= string.split(/(\(.+\))/).each(&:strip!)
11
+ .reject(&:empty?).find { |x| x[/^\(.+\)$/] }.to_s
12
+ .split[0].to_s[1..-2].to_s
7
13
  end
8
14
 
9
15
  def compiler
10
- return @@compiler ||= ''.freeze if string.empty?
16
+ return ''.freeze if string.empty?
17
+
18
+ @@compiler ||= string.split(/(\(.+\))/).each(&:strip!)
19
+ .reject(&:empty?)
20
+ .find { |x| x[/^\(.+\)$/] }.to_s
21
+ .split.find { |x| !x[/^(.+@.+)$/] }.to_s[/\w+/].to_s
11
22
 
12
- @@compiler ||= case string.split[4].to_s
23
+ @@compiler_val ||= case @@compiler
13
24
  when /gcc/i then [:gcc ]
14
25
  when /clang/i then [:clang]
15
26
  when /icc/i then [:icc]
16
- end << string[/\(.*\)/].split.drop(1).find { |x| x[/^\d.*\d/] }[/^\d.*\d/]
27
+ else [@@compiler &.to_sym]
28
+ end << compiler_version
29
+ end
30
+
31
+ def compiler_version
32
+ @@compiler_version ||= string.split(/(\(.+?\))/).each(&:strip!)
33
+ .reject(&:empty?)[2..4].to_a
34
+ .find { |x| x[/[\d.]+/] }.to_s[/[\d.]+/].to_s
17
35
  end
18
36
 
19
37
  def build_date
@@ -21,9 +39,13 @@ module LinuxStat
21
39
 
22
40
  @@time ||= begin
23
41
  require 'time'
24
- Time.strptime(string.split[16..-1].join(' '), "%d %b %Y %H:%M:%S %z")
25
- rescue StandardError
26
- Time.new(0)
42
+
43
+ time = splitted.each_slice(8).find do |x|
44
+ x.each(&:strip!)
45
+ p Time.strptime(x.join(?\s.freeze), '%d %b %Y %H:%M:%S %z'.freeze) rescue nil
46
+ end
47
+
48
+ time ? Time.strptime(time.join(?\s.freeze), "%d %b %Y %H:%M:%S %z") : Time.new(0)
27
49
  end
28
50
  end
29
51
 
@@ -31,7 +53,12 @@ module LinuxStat
31
53
  # Cached ; as changing the value in runtime is unexpected
32
54
  # Hotfix update can be problem, but it's rare and might not
33
55
  # affect the version string during program runtime.
34
- @@string ||= File.readable?('/proc/version') ? IO.read('/proc/version').tap(&:strip!) : ''
56
+ @@string ||= File.readable?('/proc/version') ? IO.read('/proc/version', 1000).tap(&:strip!) : ''
57
+ end
58
+
59
+ private
60
+ def splitted
61
+ @@string_splitted ||= string.split
35
62
  end
36
63
  end
37
64
  end
@@ -64,7 +64,7 @@ module LinuxStat
64
64
 
65
65
  private
66
66
  def release(file)
67
- IO.readlines(file).reduce({}) { |h, x|
67
+ IO.readlines(file, 3000).reduce({}) { |h, x|
68
68
  x.strip!
69
69
  next h if x.empty?
70
70
 
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -1,20 +1,19 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'bundler/setup'
3
3
  require 'linux_stat'
4
- require 'io/console'
4
+
5
+ $-v = true
5
6
 
6
7
  # Print time each method takes unless --no-time or -nt option is passed
7
8
  MARKDOWN = ARGV.any? { |x| x[/^\-\-markdown$/] || x[/^\-md$/] }
8
9
  PRINT_TIME = MARKDOWN ? false : !ARGV.any? { |x| x[/^\-\-no-time$/] || x[/^\-nt$/] }
9
10
 
10
- $-v = true
11
-
12
11
  LinuxStat.constants.sort.each do |c|
13
12
  e = eval("LinuxStat::#{c}")
14
13
 
15
14
  next if e.class != Module && e.class != Class
16
15
 
17
- meths = e.methods(false)
16
+ meths = e.methods(false).sort
18
17
 
19
18
  if meths.length > 0
20
19
  if MARKDOWN
@@ -26,13 +25,13 @@ LinuxStat.constants.sort.each do |c|
26
25
 
27
26
  meths.each do |meth|
28
27
  time = Time.now
29
- v = e.send(meth).to_s
28
+ v = e.send(meth).inspect
30
29
  time = Time.now.-(time).*(1000).round(3)
31
30
 
32
31
  dis = v.length > 253 ? v[0..250].strip + '...'.freeze : v
33
32
 
34
33
  if MARKDOWN
35
- puts "#{e}.#{meth}\n=> #{dis}"
34
+ puts "#{e}.#{meth}\n=> #{dis.inspect}"
36
35
  else
37
36
  puts "\e[1;38;2;80;80;255m#{e}.#{meth}\e[0m\n=> #{dis}"
38
37
  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.1.3
4
+ version: 0.1.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-11-22 00:00:00.000000000 Z
11
+ date: 2020-11-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Efficient linux system reporting gem. Linux Only | Efficient | Reliable
14
14
  email: