Skip to content
Browse files

Ruby 1.9.1 compatible + added num_processes and RSS total

  • Loading branch information...
1 parent f0f62ef commit 180273922f5e7a8b3d2f9055acb55cd0d8635cdc Andre Lewis committed
Showing with 18 additions and 11 deletions.
  1. +18 −11 process_memory/process_usage.rb
View
29 process_memory/process_usage.rb
@@ -2,29 +2,36 @@ class ProcessUsage < Scout::Plugin
MEM_CONVERSION = 1024
def build_report
- if @options["command_name"].nil? or @options["command_name"] == ""
+ if option(:command_name).nil? or option(:command_name) == ""
return error("Please specify the name of the process you want to monitor.")
end
- ps_command = @options["ps_command"] || "ps auxww"
- ps_regex = (@options["ps_regex"] || "(?i:\\bCOMMAND\\b)").to_s.
- gsub("COMMAND") { Regexp.escape(@options["command_name"]) }
+ ps_command = option(:ps_command) || "ps auxww"
+ ps_regex = (option(:ps_regex) || "(?i:\\bCOMMAND\\b)").to_s.gsub("COMMAND") { Regexp.escape(option(:command_name)) }
begin
ps_output = `#{ps_command}`
rescue Exception => error
error("Couldn't use `ps` as expected.", error.message)
end
- fields = ps_output.to_a.first.downcase.split
+ ps_lines = ps_output.split(/\n/)
+ fields = ps_lines.first.downcase.split
memory_index = fields.index("rss") or
return error( "RSS field not found.",
"The output from `#{ps_command}` did not include the needed RSS field." )
- highest =
- ps_output.grep(Regexp.new(ps_regex)).
- map { |com| Float(com.split[memory_index]).abs }.max
- if highest
- report(:memory => (highest/MEM_CONVERSION).to_i)
+
+ # narrow the ps lines to just those mentioning the process we're interested in
+ process_lines = ps_lines.grep(Regexp.new(ps_regex))
+
+ if process_lines.any?
+ rss_values = process_lines.map { |com| Float(com.split[memory_index]).abs }
+ highest = rss_values.max
+ total = rss_values.inject(0){|s,value| s=s + value }
+
+ report(:memory => (highest/MEM_CONVERSION).to_i,
+ :total_rss => (total/MEM_CONVERSION).to_i,
+ :num_processes => process_lines.size)
else
error( "Command not found.",
- "No processes found matching #{@options['command_name']}." )
+ "No processes found matching #{option(:command_name)}." )
end
end
end

0 comments on commit 1802739

Please sign in to comment.
Something went wrong with that request. Please try again.