Class: MoolService
- Inherits:
-
Object
- Object
- MoolService
- Defined in:
- lib/mool/service.rb
Constant Summary collapse
- STATUS_PROCESS =
{ "D" => I18n.t("process.status.uninterruptible_sleep"), "R" => I18n.t("process.status.running"), "S" => I18n.t("process.status.sleeping"), "T" => I18n.t("process.status.traced_or_stopped"), "Z" => I18n.t("process.status.zombie") }
Instance Attribute Summary collapse
-
#messure ⇒ Object
readonly
Returns the value of attribute messure.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Class Method Summary collapse
- .all(services) ⇒ Object
- .ps ⇒ Object
- .ps_parser(command, pattern) ⇒ Object
- .services_status(services) ⇒ Object
- .top ⇒ Object
- .top_parser(command, pid) ⇒ Object
Instance Method Summary collapse
-
#initialize(name, pattern, opt = {}) ⇒ MoolService
constructor
A new instance of MoolService.
Constructor Details
#initialize(name, pattern, opt = {}) ⇒ MoolService
Returns a new instance of MoolService.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mool/service.rb', line 10 def initialize(name, pattern, opt={}) raise "Please only use string types!" if (name.class != String or pattern.class != String) @messure = [] @pattern = pattern result = opt[:result] || MoolService.services_status([{:name => name, :pattern => pattern}])[name] result.each do |res| #pid,user,pcpu,pmem,rss,priority,args,nice, memory_in_kb, status, cpu_percetage, men_percentage, time @messure << { :name => name, :pattern => pattern, :pid => res[0], :user => res[1], :cpu_average => res[2], :mem_average => res[3], :resident_set_size => res[4], :priority => res[5], :time => res[6], :status => MoolService::STATUS_PROCESS[res[7]], :nice => res[8], :args => res[9], :memory_in_kb => res[10], :cpu_instant => (res[11] || 0), :mem_instant => (res[12] || 0.0)} end end |
Instance Attribute Details
#messure ⇒ Object (readonly)
Returns the value of attribute messure.
8 9 10 |
# File 'lib/mool/service.rb', line 8 def messure @messure end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
8 9 10 |
# File 'lib/mool/service.rb', line 8 def pattern @pattern end |
Class Method Details
.all(services) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mool/service.rb', line 38 def self.all(services) raise "Please only use Array type!" if services.class != Array _services = {} services_data = MoolService.services_status services services.each do |service| _services[service[:name]] = MoolService.new(service[:name], service[:pattern], { :result => services_data[service[:name]] }) end _services end |
.ps ⇒ Object
62 |
# File 'lib/mool/service.rb', line 62 def self.ps; `ps --no-headers -o pid,user,pcpu,pmem,rss,priority,time,stat,nice,args -A`; end |
.ps_parser(command, pattern) ⇒ Object
68 69 70 71 72 |
# File 'lib/mool/service.rb', line 68 def self.ps_parser(command, pattern) pattern = pattern.gsub('/','\/') # pid,user,pcpu,pmem,rss,priority,time,stat,nice,args command.scan(/^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S)\S*\s+(\S+)\s+.*(#{pattern}).*\n/) end |
.services_status(services) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/mool/service.rb', line 52 def self.services_status services command_ps = MoolService.ps command_top = MoolService.top result = {} services.each do |service| result[service[:name]] = MoolService.ps_parser(command_ps, service[:pattern]).collect{ |data| data += MoolService.top_parser(command_top, data[0]) } end result end |
.top ⇒ Object
64 |
# File 'lib/mool/service.rb', line 64 def self.top; `top -c -b -n1`; end |
.top_parser(command, pid) ⇒ Object
74 75 76 77 78 |
# File 'lib/mool/service.rb', line 74 def self.top_parser(command, pid) # memory_in_kb, cpu_percetage, men_percentage # 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+.*/) result = command.scan(/#{pid}\s+\S+\s+\S+\s+\S+\s+(\S+)\s+\S+\s+\S+\s+\S+\s+(\S+)\s+(\S+)\s+\S+\s+\S+/).flatten end |