mool 2.0.1 → 3.0.0

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.
data/lib/mool/memory.rb CHANGED
@@ -1,21 +1,54 @@
1
- class MoolMemory
2
- PATH_PROC_MEMORY = "/proc/meminfo"
1
+ module Mool
2
+ class Memory
3
3
 
4
- attr_accessor :unity, :mem_used
4
+ attr_accessor :unity, :mem_used
5
5
 
6
- def initialize()
7
- File.read(PATH_PROC_MEMORY).scan(/(\S+):\s+(\d+)/).each do |meminfo|
8
- var = meminfo[0].gsub("(", "_").gsub(")", "").underscore
9
- instance_variable_set("@#{var}", (meminfo[1].to_f * Mool::PARSE_TYPES[Mool::KBYTES]).round(2))
10
- class_eval{attr_accessor var.to_sym}
6
+ def initialize
7
+ Mool::Command.meminfo_command.scan(/(\S+):\s+(\d+)/).each do |meminfo|
8
+ var = meminfo[0].tr('(', '_').tr(')', '').underscore
9
+ instance_variable_set(
10
+ "@#{var}",
11
+ (meminfo[1].to_f * Mool::PARSE_TYPES[Mool::KBYTES]).round(2)
12
+ )
13
+ class_eval { attr_accessor var.to_sym }
14
+ end
15
+ @unity = Mool::BYTES
16
+ @mem_used = @mem_total - (@mem_free +
17
+ @cached +
18
+ @buffers +
19
+ @swap_cached)
20
+ end
21
+
22
+ def to_b
23
+ Mool.parse_to(
24
+ self,
25
+ (instance_variable_names - ["@unity"]),
26
+ Mool::BYTES
27
+ )
28
+ end
29
+
30
+ def to_kb
31
+ Mool.parse_to(
32
+ self,
33
+ (instance_variable_names - ["@unity"]),
34
+ Mool::KBYTES
35
+ )
11
36
  end
12
- @unity = Mool::BYTES
13
- @mem_used = @mem_total - (@mem_free + @cached + @buffers + @swap_cached )
14
- end
15
37
 
16
- def to_b; Mool.parse_to(self, (instance_variable_names - ["@unity"]), Mool::BYTES); end
17
- def to_kb; Mool.parse_to(self, (instance_variable_names - ["@unity"]), Mool::KBYTES); end
18
- def to_mb; Mool.parse_to(self, (instance_variable_names - ["@unity"]), Mool::MBYTES); end
19
- def to_gb; Mool.parse_to(self, (instance_variable_names - ["@unity"]), Mool::GBYTES); end
38
+ def to_mb
39
+ Mool.parse_to(
40
+ self,
41
+ (instance_variable_names - ["@unity"]),
42
+ Mool::MBYTES
43
+ )
44
+ end
20
45
 
46
+ def to_gb
47
+ Mool.parse_to(
48
+ self,
49
+ (instance_variable_names - ["@unity"]),
50
+ Mool::GBYTES
51
+ )
52
+ end
53
+ end
21
54
  end
@@ -0,0 +1,129 @@
1
+ module Mool
2
+ class Process
3
+ attr_reader :messures, :pattern
4
+
5
+ STATUS_PROCESS = {
6
+ 'D' => :uninterruptible_sleep,
7
+ 'R' => :running,
8
+ 'S' => :sleeping,
9
+ 'T' => :stopped_by_job_control_signal,
10
+ 't' => :stopped_by_debugger_during_trace,
11
+ 'Z' => :zombie
12
+ }
13
+
14
+ def initialize(name, pattern, opt = {})
15
+ if name.class != String || pattern.class != String
16
+ raise 'Please only use string types!'
17
+ end
18
+
19
+ @messures = []
20
+ @pattern = pattern
21
+
22
+ result = opt[:result] ||
23
+ Mool::Process.services_status(
24
+ [{ name: name,
25
+ pattern: pattern }]
26
+ )[name]
27
+
28
+ result.each do |res|
29
+ # pid, user, pcpu, pmem, rss, priority, args, nice, memory_in_kb,
30
+ # status, cpu_percetage, men_percentage, time
31
+ @messures << {
32
+ name: name,
33
+ pattern: pattern,
34
+ ruser: res[0], # The real user ID of the process.
35
+ user: res[1], # The effective user ID of the process.
36
+ rgroup: res[2], # The real group ID of the process.
37
+ group: res[3], # The effective group ID of the process.
38
+ pid: res[4], # The decimal value of the process ID.
39
+ ppid: res[5], # The decimal value of the parent process ID.
40
+ pgid: res[6], # The decimal value of the process group ID.
41
+ pcpu: res[7], # The ratio of CPU time used recently to CPU time available in the same period, expressed as a percentage.
42
+ vsz: res[8], # The size of the process in (virtual) memory in 1024 byte units as a decimal integer.
43
+ nice: res[9], # The decimal value of the nice value of the process; see nice.
44
+ etime: res[10], # In the POSIX locale, the elapsed time since the process was started, in the form: [[dd-]hh:]mm:ss
45
+ time: res[11], # In the POSIX locale, the cumulative CPU time of the process in the form: [dd-]hh:mm:ss
46
+ tty: res[12], # The name of the controlling terminal of the process (if any) in the same format used by the who utility.
47
+ comm: res[13], # The name of the command being executed (argv[0] value) as a string.
48
+ args: res[14], # The command with all its arguments as a string.
49
+ priority: res[15], # Priority: The scheduling priority of the task.
50
+ virt: res[17], # Virtual Memory Size (KiB) The total amount of virtual memory used by the task
51
+ res: res[18], # Resident Memory Size (KiB), A subset of the virtual address space (VIRT)
52
+ shr: res[19], # Shared Memory Size (KiB), A subset of resident memory (RES) that may be used by other processes
53
+ status: Mool::Process::STATUS_PROCESS[res[20]],
54
+ cpu_percentage: res[21], # CPU Usage The task's share of the elapsed CPU
55
+ mem_percentage: res[22], # Memory Usage (RES) A task's currently resident share of available physical memory.
56
+ time_plus: res[22] # CPU Time, hundredths The same as TIME, but reflecting more granularity through hundredths of a second
57
+ }
58
+ end
59
+ end
60
+
61
+ def self.all(services)
62
+ raise 'Please only use Array type!' if services.class != Array
63
+ result = {}
64
+
65
+ services_data = Mool::Process.services_status(services)
66
+
67
+ services.each do |service|
68
+ result[service[:name]] = Mool::Process.new(
69
+ service[:name],
70
+ service[:pattern],
71
+ result: services_data[service[:name]]
72
+ )
73
+ end
74
+
75
+ result
76
+ end
77
+
78
+ def self.services_status(services)
79
+ command_ps = Mool::Command.ps_command
80
+ command_top = Mool::Command.top_command
81
+
82
+ result = {}
83
+
84
+ services.each do |service|
85
+ ps_parsed = Mool::Process.ps_parser(
86
+ command_ps,
87
+ service[:pattern]
88
+ )
89
+
90
+ result[service[:name]] = ps_parsed.collect do |data|
91
+ data + Mool::Process.top_parser(command_top, data[4])
92
+ end
93
+ end
94
+
95
+ result
96
+ end
97
+
98
+ private
99
+
100
+ def self.ps_parser(command, pattern)
101
+ pattern = pattern.gsub('/', '\/')
102
+
103
+ results = []
104
+
105
+ # ruser,user,rgroup,group,pid,ppid,pgid,pcpu,vsz,nice,etime,time,tty,comm,args
106
+ command.split("\n").each do |comm|
107
+ match = comm.scan(/^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(#{pattern})\s+(.*)/).flatten
108
+ next if match.empty?
109
+ results << match
110
+ end
111
+ results
112
+ end
113
+
114
+ def self.top_parser(command, pid)
115
+ # memory_in_kb, cpu_percetage, men_percentage
116
+ # command.scan(/[\s+]#{pid}\s+\S+\s+\S+\s+(\S+)\s+\S+\s+(\S+)\s+\S+\s+(\S)\s+(\S+)\s+(\S+)\s+(\S+)\s+.*/)
117
+ results = []
118
+ # 15 16 17 18 19 20 21 22 23 24
119
+ # PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
120
+ command.split("\n").each do |comm|
121
+ match = comm.strip.scan(/#{pid}\s+\S+\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/).flatten
122
+ next if match.empty?
123
+ results = match
124
+ break
125
+ end
126
+ results
127
+ end
128
+ end
129
+ end
data/lib/mool/system.rb CHANGED
@@ -1,42 +1,60 @@
1
- class MoolSystem
2
- attr_reader :kernel, :current_loadavg, :last_5min_loadavg, :last_15min_loadavg, :thread_entities_exec, :total_thread_entities, :last_pid_process_created, :uptime_day, :uptime_hour, :uptime_minute, :uptime_second
1
+ module Mool
2
+ class System
3
+ attr_reader :kernel,
4
+ :current_loadavg,
5
+ :last_5min_loadavg,
6
+ :last_15min_loadavg,
7
+ :thread_entities_exec,
8
+ :total_thread_entities,
9
+ :last_pid_process_created,
10
+ :uptime_day,
11
+ :uptime_hour,
12
+ :uptime_minute,
13
+ :uptime_second
3
14
 
4
- def initialize
5
- @kernel = `uname -r`.chomp
6
- load_avg = File.read("/proc/loadavg").chomp.split(" ")
7
- @current_loadavg = load_avg[0].to_f
8
- @last_5min_loadavg = load_avg[1].to_f
9
- @last_15min_loadavg = load_avg[2].to_f
10
- @thread_entities_exec = load_avg[3].split("/").first.to_i # Currently executing kernel scheduling entities
11
- @total_thread_entities = load_avg[3].split("/").last.to_i # Number of kernel scheduling entities that currently exist on the system
12
- @last_pid_process_created = load_avg[4].to_i
13
- time = `cat /proc/uptime`.split(" ").first.to_f
14
- mm, ss = time.divmod(60)
15
- hh, mm = mm.divmod(60)
16
- dd, hh = hh.divmod(24)
17
- @uptime_day = dd.to_i
18
- @uptime_hour = hh.to_i
19
- @uptime_minute = mm.to_i
20
- @uptime_second = ss.to_i
21
- end
15
+ def initialize
16
+ @kernel = Mool::Command.uname_command
17
+ load_avg = Mool::Command.loadavg_command.split(' ')
18
+ @current_loadavg = load_avg[0].to_f
19
+ @last_5min_loadavg = load_avg[1].to_f
20
+ @last_15min_loadavg = load_avg[2].to_f
21
+ # Currently executing kernel scheduling entities
22
+ @thread_entities_exec = load_avg[3].split('/').first.to_i
23
+ # Number of kernel scheduling entities that currently exist on the system
24
+ @total_thread_entities = load_avg[3].split('/').last.to_i
25
+ @last_pid_process_created = load_avg[4].to_i
26
+ time = Mool::Command.uptime_command.split(' ').first.to_f
27
+ mm, ss = time.divmod(60)
28
+ hh, mm = mm.divmod(60)
29
+ dd, hh = hh.divmod(24)
30
+ @uptime_day = dd.to_i
31
+ @uptime_hour = hh.to_i
32
+ @uptime_minute = mm.to_i
33
+ @uptime_second = ss.to_i
34
+ end
22
35
 
23
- def load_average
24
- { :current_loadavg => @current_loadavg,
25
- :last_5min_loadavg => @last_5min_loadavg,
26
- :last_15min_loadavg => @last_15min_loadavg,
27
- :thread_entities_exec => @thread_entities_exec,
28
- :total_thread_entities => @total_thread_entities,
29
- :last_pid_process_created => @last_pid_process_created }
30
- end
36
+ def load_average
37
+ {
38
+ current_loadavg: @current_loadavg,
39
+ last_5min_loadavg: @last_5min_loadavg,
40
+ last_15min_loadavg: @last_15min_loadavg,
41
+ thread_entities_exec: @thread_entities_exec,
42
+ total_thread_entities: @total_thread_entities,
43
+ last_pid_process_created: @last_pid_process_created
44
+ }
45
+ end
31
46
 
32
- def uptime
33
- { :day => @uptime_day,
34
- :hour => @uptime_hour,
35
- :minute => @uptime_minute,
36
- :second => @uptime_second }
37
- end
47
+ def uptime
48
+ {
49
+ day: @uptime_day,
50
+ hour: @uptime_hour,
51
+ minute: @uptime_minute,
52
+ second: @uptime_second
53
+ }
54
+ end
38
55
 
39
- def kernel_version
40
- @kernel
56
+ def kernel_version
57
+ @kernel
58
+ end
41
59
  end
42
60
  end
data/lib/mool/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mool
2
- VERSION = "2.0.1"
2
+ VERSION = "3.0.0"
3
3
  end
data/mool.gemspec CHANGED
@@ -1,21 +1,36 @@
1
- lib = File.expand_path('../lib', __FILE__)
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
2
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require 'mool/version'
4
+ require "mool/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mool"
8
+ spec.version = Mool::VERSION
9
+ spec.authors = ["g.edera"]
10
+ spec.email = ["gab.edera@gmail.com"]
11
+
12
+ spec.summary = %q{Get operative system information (Linux).}
13
+ spec.description = %q{Get operative system information: Disk, Memory, Cpu, Load-average, Processes.}
14
+ spec.homepage = "https://github.com/gedera/mool"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = 'https://rubygems.org'
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
4
25
 
5
- Gem::Specification.new do |gem|
6
- gem.name = "mool"
7
- gem.version = Mool::VERSION
8
- gem.platform = Gem::Platform::RUBY
9
- gem.authors = ["g.edera", "eserdio"]
10
- gem.email = ["gab.edera@gmail.com"]
11
- gem.description = "Get operative system information: Disk, Memory, Cpu, Load-average, Processes"
12
- gem.summary = "Get operative system information (Linux)"
13
- gem.homepage = "https://github.com/gedera/mool"
14
- gem.license = "MIT"
15
- gem.required_ruby_version = '>= 1.8.6'
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
16
32
 
17
- gem.files = `git ls-files`.split($/)
18
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
- gem.require_paths = ["lib"]
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "minitest", "~> 5.0"
21
36
  end
metadata CHANGED
@@ -1,86 +1,105 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mool
3
- version: !ruby/object:Gem::Version
4
- hash: 13
5
- prerelease:
6
- segments:
7
- - 2
8
- - 0
9
- - 1
10
- version: 2.0.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - g.edera
14
- - eserdio
15
8
  autorequire:
16
- bindir: bin
9
+ bindir: exe
17
10
  cert_chain: []
18
-
19
- date: 2016-08-02 00:00:00 -03:00
20
- default_executable:
21
- dependencies: []
22
-
23
- description: "Get operative system information: Disk, Memory, Cpu, Load-average, Processes"
24
- email:
11
+ date: 2018-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: 'Get operative system information: Disk, Memory, Cpu, Load-average, Processes.'
56
+ email:
25
57
  - gab.edera@gmail.com
26
58
  executables: []
27
-
28
59
  extensions: []
29
-
30
60
  extra_rdoc_files: []
31
-
32
- files:
33
- - .gitignore
61
+ files:
62
+ - ".gitignore"
34
63
  - Gemfile
35
64
  - LICENSE.txt
36
65
  - README.md
37
66
  - Rakefile
67
+ - bin/console
68
+ - bin/setup
38
69
  - lib/locales/en.mool.yml
39
70
  - lib/locales/es.mool.yml
40
71
  - lib/mool.rb
72
+ - lib/mool/command.rb
41
73
  - lib/mool/cpu.rb
42
74
  - lib/mool/disk.rb
43
- - lib/mool/load_average.rb
44
75
  - lib/mool/memory.rb
45
- - lib/mool/service.rb
76
+ - lib/mool/process.rb
46
77
  - lib/mool/system.rb
47
78
  - lib/mool/version.rb
48
79
  - mool.gemspec
49
- has_rdoc: true
50
80
  homepage: https://github.com/gedera/mool
51
- licenses:
81
+ licenses:
52
82
  - MIT
83
+ metadata:
84
+ allowed_push_host: https://rubygems.org
53
85
  post_install_message:
54
86
  rdoc_options: []
55
-
56
- require_paths:
87
+ require_paths:
57
88
  - lib
58
- required_ruby_version: !ruby/object:Gem::Requirement
59
- none: false
60
- requirements:
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
61
91
  - - ">="
62
- - !ruby/object:Gem::Version
63
- hash: 59
64
- segments:
65
- - 1
66
- - 8
67
- - 6
68
- version: 1.8.6
69
- required_rubygems_version: !ruby/object:Gem::Requirement
70
- none: false
71
- requirements:
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
72
96
  - - ">="
73
- - !ruby/object:Gem::Version
74
- hash: 3
75
- segments:
76
- - 0
77
- version: "0"
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
78
99
  requirements: []
79
-
80
100
  rubyforge_project:
81
- rubygems_version: 1.6.2
101
+ rubygems_version: 2.6.11
82
102
  signing_key:
83
- specification_version: 3
84
- summary: Get operative system information (Linux)
103
+ specification_version: 4
104
+ summary: Get operative system information (Linux).
85
105
  test_files: []
86
-