Class: Snipr::ProcessLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/snipr/process_locator.rb

Overview

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

Constant Summary collapse

DAY_SECONDS =
86400
HOUR_SECONDS =
3600
MINUTE_SECONDS =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessLocator

Returns a new instance of ProcessLocator.



19
20
21
22
23
# File 'lib/snipr/process_locator.rb', line 19

def initialize
  @includes = []
  @excludes = []
  @filters = []
end

Instance Attribute Details

#excludesObject (readonly)

Returns the value of attribute excludes.



17
18
19
# File 'lib/snipr/process_locator.rb', line 17

def excludes
  @excludes
end

#filtersObject (readonly)

Returns the value of attribute filters.



17
18
19
# File 'lib/snipr/process_locator.rb', line 17

def filters
  @filters
end

#includesObject (readonly)

Returns the value of attribute includes.



17
18
19
# File 'lib/snipr/process_locator.rb', line 17

def includes
  @includes
end

Instance Method Details

#alive_longer_than(sec) ⇒ Object

Define a time in seconds that processes must have been alive for longer than to be included in results



67
68
69
# File 'lib/snipr/process_locator.rb', line 67

def alive_longer_than(sec)
  filter {|process| process.seconds_alive > sec}
end

#cpu_greater_than(percent) ⇒ Object

Define a cpu use percentage that processes must be greater than to be included in the result.



60
61
62
# File 'lib/snipr/process_locator.rb', line 60

def cpu_greater_than(percent)
  filter {|process| process.cpu > percent}
end

#exclude(pattern) ⇒ Object

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.



46
47
48
# File 'lib/snipr/process_locator.rb', line 46

def exclude(pattern)
  excludes << pattern
end

#filter(&callable) ⇒ Object

Define your own filter using a lambda that receives a KernelProcess object and returns true if the process should be included in results



74
75
76
# File 'lib/snipr/process_locator.rb', line 74

def filter(&callable)
  filters << callable
end

#include(pattern) ⇒ Object

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



38
39
40
# File 'lib/snipr/process_locator.rb', line 38

def include(pattern)
  includes << pattern
end

#locateObject

Locates the processes that match all include patterns and do not match all exclude patterns



28
29
30
31
32
# 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

#memory_greater_than(bytes) ⇒ Object

Define a size in bytes that processes must be greater than to be included in the result.



53
54
55
# File 'lib/snipr/process_locator.rb', line 53

def memory_greater_than(bytes)
  filter { |process| process.memory > bytes }
end