class Snipr::ProcessLocator
Responsible for locating running processes and returning an array of KernelProcess objects that represent them. Uses the output of ps to locate processes, so this only works on *nix. Tested on RHEL 6.5 and Linux Mint
Constants
- DAY_SECONDS
- HOUR_SECONDS
- MINUTE_SECONDS
Attributes
Public Class Methods
# File lib/snipr/process_locator.rb, line 19 def initialize @includes = [] @excludes = [] @filters = [] end
Public Instance Methods
Define a time in seconds that processes must have been alive for longer than to be included in results
# File lib/snipr/process_locator.rb, line 67 def alive_longer_than(sec) filter {|process| process.seconds_alive > sec} end
Define a cpu use percentage that processes must be greater than to be included in the result.
# File lib/snipr/process_locator.rb, line 60 def cpu_greater_than(percent) filter {|process| process.cpu > percent} end
Define a pattern that the command portion of the ps command must match to exclude the process. Multiple patterns can be defined and all will be rejected.
# File lib/snipr/process_locator.rb, line 46 def exclude(pattern) excludes << pattern end
Define your own filter using a lambda that receives a KernelProcess object and returns true if the process should be included in results
# File lib/snipr/process_locator.rb, line 74 def filter(&callable) filters << callable end
Define a pattern that the command portion of the ps command must match to include the process. Multiple patterns can be defined and all must be matched
# File lib/snipr/process_locator.rb, line 38 def include(pattern) includes << pattern end
Locates the processes that match all include patterns and do not match all exclude patterns
# File lib/snipr/process_locator.rb, line 28 def locate processes = includes.reduce(all_processes, &by_inclusion_patterns) processes = excludes.reduce(processes, &by_exclusion_patterns) processes = filters.reduce(processes, &by_filter) end
Define a size in bytes that processes must be greater than to be included in the result.
# File lib/snipr/process_locator.rb, line 53 def memory_greater_than(bytes) filter { |process| process.memory > bytes } end
Private Instance Methods
# File lib/snipr/process_locator.rb, line 100 def all_processes Snipr.exec_cmd('ps h -eo pid,ppid,%mem,%cpu,etime,command').map do |line| pid, ppid, mem, cpu, etime, *cmd = line.split cmd = cmd.join(" ") KernelProcess.new( pid.to_i, ppid.to_i, mem.to_i, cpu.to_f, etime, parse_seconds(etime), cmd ) end end
# File lib/snipr/process_locator.rb, line 88 def by_exclusion_patterns lambda {|processes, filter| processes.reject(&match(filter))} end
# File lib/snipr/process_locator.rb, line 92 def by_filter lambda {|processes, filter| processes.select(&filter)} end
# File lib/snipr/process_locator.rb, line 84 def by_inclusion_patterns lambda {|processes, filter| processes.select(&match(filter))} end
# File lib/snipr/process_locator.rb, line 79 def clear! @includes = [] @excludes = [] end
# File lib/snipr/process_locator.rb, line 96 def match(filter) lambda {|process| process.command.match(filter)} end
Parses etime, which is in the format dd-hh:mm:ss dd- will be omitted if the run time is < 24 hours hh: will be omitted if the run time is < 1 hour
# File lib/snipr/process_locator.rb, line 121 def parse_seconds(etime) time, days = etime.split("-").reverse sec, min, hr = time.split(":").reverse total = 0 total += days.to_i * DAY_SECONDS if days total += hr.to_i * HOUR_SECONDS if hr total += min.to_i * MINUTE_SECONDS if min total += sec.to_i if sec total end