Class: Riemann::Tools::ConsulHealth

Inherits:
Object
  • Object
show all
Includes:
Riemann::Tools
Defined in:
lib/riemann/tools/consul_health.rb

Constant Summary

Constants included from Riemann::Tools

VERSION

Instance Attribute Summary

Attributes included from Riemann::Tools

#argv

Instance Method Summary collapse

Methods included from Riemann::Tools

#attributes, #endpoint_name, included, #options, #report, #riemann, #run

Constructor Details

#initializeConsulHealth

Returns a new instance of ConsulHealth.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/riemann/tools/consul_health.rb', line 22

def initialize
  super

  @hostname = opts[:consul_host]
  @prefix = opts[:prefix]
  @minimum_services_per_node = opts[:minimum_services_per_node]
  @underlying_ip = IPSocket.getaddress(@hostname)
  @consul_leader_url = URI.parse("http://#{opts[:consul_host]}:#{opts[:consul_port]}/v1/status/leader")
  @consul_services_url = URI.parse("http://#{opts[:consul_host]}:#{opts[:consul_port]}/v1/catalog/services")
  @consul_nodes_url = URI.parse("http://#{opts[:consul_host]}:#{opts[:consul_port]}/v1/catalog/nodes")
  @consul_health_url_prefix = "http://#{opts[:consul_host]}:#{opts[:consul_port]}/v1/health/service/"

  @last_services_read = {}
end

Instance Method Details

#alert(hostname, service, state, metric, description) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/riemann/tools/consul_health.rb', line 37

def alert(hostname, service, state, metric, description)
  opts = {
    host: hostname,
    service: service.to_s,
    state: state.to_s,
    metric: metric,
    description: description,
  }

  report(opts)
end

#get(url) ⇒ Object



49
50
51
# File 'lib/riemann/tools/consul_health.rb', line 49

def get(url)
  ::Net::HTTP.new(url.host, url.port).get(url, { 'user-agent' => opts[:user_agent] }).body
end

#tickObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/riemann/tools/consul_health.rb', line 53

def tick
  leader = JSON.parse(get(@consul_leader_url))
  leader_hostname = URI.parse("http://#{leader}").hostname

  return unless leader_hostname == @underlying_ip

  nodes = JSON.parse(get(@consul_nodes_url))
  services = JSON.parse(get(@consul_services_url))
  services_by_nodes = {}

  nodes.each do |node|
    node_name = node['Node']
    services_by_nodes[node_name] = 0
  end

  # For every service
  services.each do |service|
    service_name = service[0]
    health_url = URI.parse(@consul_health_url_prefix + service_name)
    health_nodes = JSON.parse(get(health_url))

    total_count = 0
    ok_count = 0

    health_nodes.each do |node|
      hostname = node['Node']['Node']
      ok = node['Checks'].all? { |check| check['Status'] == 'passing' }
      alert(hostname, "#{@prefix}#{service_name}", ok ? :ok : :critical, ok ? 1 : 0, JSON.generate(node))
      total_count += 1
      ok_count += ok ? 1 : 0

      last_services_by_nodes = services_by_nodes[hostname].to_i
      services_by_nodes[hostname] = last_services_by_nodes + 1
    end

    unless @last_services_read[service_name].nil?
      last_ok = @last_services_read[service_name]
      if last_ok != ok_count
        alert(
          'total', "#{@prefix}#{service_name}-count", ok_count >= last_ok ? :ok : :critical, ok_count,
          "Number of passing #{service_name} is: #{ok_count}/#{total_count}, Last time it was: #{last_ok}",
        )
      end
    end

    @last_services_read[service_name] = ok_count
  end

  # For every node
  services_by_nodes.each do |node, count|
    alert(
      node, "#{@prefix}total-services", count >= @minimum_services_per_node ? :ok : :critical, count,
      "#{count} services in the specified node",
    )
  end
end