Class: AlertMachine::Process

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

Overview

Checks if processes are living, and have their ports open.

Class Method Summary collapse

Methods inherited from Watcher

assert, run_command, watch_process

Class Method Details

.check(entity, machines, opts, caller) ⇒ Object



58
59
60
61
62
# File 'lib/process.rb', line 58

def check(entity, machines, opts, caller)
  [opts[entity]].flatten.each { |val|
    Process.send("check_#{entity}".to_sym, machines, val, caller)
  } if opts[entity]
end

.check_command(machines, cmd, check_msg, error_msg, caller) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/process.rb', line 38

def check_command(machines, cmd, check_msg, error_msg, caller)
  puts check_msg % machines.join(", ")
  bad_machines = []
  run_command(machines,
    "#{cmd} || echo BAD"
  ).each { |machine, output|
    bad_machines << machine if output.join(" ").match(/BAD/)
  }
  check_command_failed(bad_machines, error_msg, caller) unless
    bad_machines.empty?
rescue Exception => e
  puts "Exception: #{e.to_s}"
  puts "#{e.backtrace.join("\n")}"
  check_command_failed(machines, error_msg, caller)
end

.check_command_failed(machines, error_msg, caller) ⇒ Object



54
55
56
# File 'lib/process.rb', line 54

def check_command_failed(machines, error_msg, caller)
  assert false, error_msg % machines.join(", "), caller
end

.check_grep(machines, grep, caller) ⇒ Object



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

def check_grep(machines, grep, caller)
  check_command(machines, "ps aux | grep '#{grep}' | grep -v grep",
    "Grepping the process list for '#{grep}' in %s",
    "Grepping the process list for '#{grep}' failed at %s", caller)
end

.check_pid_file(machines, file, caller) ⇒ Object



26
27
28
29
30
# File 'lib/process.rb', line 26

def check_pid_file(machines, file, caller)
  check_command(machines, "ps -p `cat #{file}`",
    "Checking if valid pidfile #{file} exists in %s",
    "Pidfile #{file} doesnt seem valid at %s", caller)
end

.check_port(machines, port, caller) ⇒ Object



19
20
21
22
23
24
# File 'lib/process.rb', line 19

def check_port(machines, port, caller)
  check_command(machines, 
    "netstat -na | grep 'LISTEN' | grep '\\(\\:\\|\\.\\)#{port} ' | grep -v grep",
    "Checking if port #{port} is open on %s",
    "Port #{port} seems down on %s", caller)
end

.watch(machines, opts, caller) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/process.rb', line 7

def watch(machines, opts, caller)
  raise ArgumentError, "Must mention atleast one of (port, pid_file, grep)" unless
    opts[:port] || opts[:pid_file] || opts[:grep]
    raise ArgumentError, "Must not be passed a block" if block_given?

  super(opts, caller) do
    check(:port, machines, opts, caller)
    check(:pid_file, machines, opts, caller)
    check(:grep, machines, opts, caller)
  end
end