timriley (owner)

Fork Of

gist: 169465 by joho Extracted from our health c...

Revisions

gist: 172548 Download_button fork
public
Public Clone URL: git://gist.github.com/172548.git
Embed All Files: show embed
health_check.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require 'open-uri'
require 'hpricot'
 
class HealthCheck
  HAPROXY_MAX_QUEUE_LENGTH = 20
  HAPROXY_SERVER_URLS = %w(localhost:8001 localhost:8002 localhost:8003)
  
  def ok?
    problems.length == 0
  end
  
  def problems
    @problems || run_checks
  end
  
protected
 
  def run_checks
    @problems = []
    
    # run any check_ methods you've defined
    methods.grep(/^check_/).each {|m| send(m.to_sym) }
 
    @problems
  end
  
  def check_haproxy
    HAPROXY_SERVER_URLS.each do |host|
      begin
        doc = Hpricot(open("http://#{host}"))
        current_haproxy_queue = (doc/".backend > td:nth(1)").first.inner_text.to_i
        
        if current_haproxy_queue > HAPROXY_MAX_QUEUE_LENGTH
          @problems << "HAProxy max queue threshold of #{HAPROXY_MAX_QUEUE_LENGTH} exceded on #{host} (current queue length is #{current_haproxy_queue})"
        end
      rescue Exception => exc
        @problems << "Error accessing haproxy stats on #{host} with error #{exc.class.name}:\"#{exc.message}\""
      end
    end
  end
 
end