Class: Murakumo::HealthCheckContext

Inherits:
Object
  • Object
show all
Defined in:
lib/srv/murakumo_health_check_context.rb

Overview

ヘルスチェックのコンテキスト

Instance Method Summary collapse

Constructor Details

#initialize(vars = {}) ⇒ HealthCheckContext



13
14
15
16
17
# File 'lib/srv/murakumo_health_check_context.rb', line 13

def initialize(vars = {})
  vars.each do |name, val|
    instance_variable_set("@#{name}", val)
  end
end

Instance Method Details

#http_get(path, statuses = [200], port = 80, host = '127.0.0.1') ⇒ Object

HTTPチェッカー



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/srv/murakumo_health_check_context.rb', line 30

def http_get(path, statuses = [200], port = 80, host = '127.0.0.1')
  res = Net::HTTP.start(host, port) do |http|
    http.read_timeout = @options['timeout']
    http.get(path)
  end

  statuses.include?(res.code.to_i)
rescue => e
  @logger.debug("#{@name}: #{e.message}")
  return false
end

#memcached_check(port = 11211, host = '127.0.0.1') ⇒ Object

memcachedチェッカー



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/srv/murakumo_health_check_context.rb', line 51

def memcached_check(port = 11211, host = '127.0.0.1')
  telnet = Net::Telnet.new('Host' => host, 'Port' => port)
  !!telnet.cmd('String' => 'stats', 'Match' => /END/i, 'Timeout' => @options['timeout'])
rescue =>e 
  @logger.debug("#{@name}: #{e.message}")
  return false
ensure
  if telnet
    telnet.close rescue nil
  end
end

#smtp_check(*args) ⇒ Object

SMTPチェッカー



43
44
45
46
47
48
# File 'lib/srv/murakumo_health_check_context.rb', line 43

def smtp_check(*args)
  Net::SMTP.start(*args) {|smtp| true }
rescue => e
  @logger.debug("#{@name}: #{e.message}")
  return false
end

#tcp_check(port, host = '127.0.0.1') ⇒ Object

TCPチェッカー



20
21
22
23
24
25
26
27
# File 'lib/srv/murakumo_health_check_context.rb', line 20

def tcp_check(port, host = '127.0.0.1')
  s = TCPSocket.new(host, port)
  s.close
  return true
rescue => e
  @logger.debug("#{@name}: #{e.message}")
  return false
end