Class: Procmon::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/procmon/process.rb

Constant Summary collapse

CONFIGURABLE_ATTRIBUTES =
[
  :pid_file,
  :pid_command,
  :pid
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, checks, options = {}) ⇒ Process

Returns a new instance of Process.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/procmon/process.rb', line 11

def initialize(name, checks, options={})
  @name = name
  @checks = []

  CONFIGURABLE_ATTRIBUTES.each do |attribute_name|
    self.send("#{attribute_name}=", options[attribute_name]) if options.has_key?(attribute_name)
  end

  if @pid_command.nil? or @pid_command.empty?
    @pid_command = "pidof"
  end

  if @pid.nil?
    @pid = fetch_pid
  end

  checks.each do |name, opts|
    self.add_check(name, opts)
  end
end

Instance Attribute Details

#checksObject

Returns the value of attribute checks.



9
10
11
# File 'lib/procmon/process.rb', line 9

def checks
  @checks
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/procmon/process.rb', line 9

def name
  @name
end

Instance Method Details

#add_check(name, options) ⇒ Object



40
41
42
# File 'lib/procmon/process.rb', line 40

def add_check(name, options)
  self.checks << ProcessConditions[name].new(options)
end

#perform_actions(check) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/procmon/process.rb', line 44

def perform_actions(check)
  return if check.actions.nil? or check.actions.empty?

  check.actions.each do |action|
    if action.kind_of?(Proc)
      action.call(self, check)
    elsif action.kind_of?(Class) && action.respond_to?('notify')
      notification = ProcessNotification.new(self, check)
      action.notify(notification)
    else
      warn "Don't know what to do for #{action}."
    end
  end
end

#run_checksObject



32
33
34
35
36
37
38
# File 'lib/procmon/process.rb', line 32

def run_checks
  @checks.each do |check|
    value = check.run(pid)
    condition_met = check.check(value)
    perform_actions(check) if condition_met
  end
end