Class: Riemann::Tools::Haproxy

Inherits:
Object
  • Object
show all
Includes:
Riemann::Tools
Defined in:
lib/riemann/tools/haproxy.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

#initializeHaproxy

Returns a new instance of Haproxy.



18
19
20
21
22
# File 'lib/riemann/tools/haproxy.rb', line 18

def initialize
  super

  @uri = URI("#{opts[:stats_url]};csv")
end

Instance Method Details

#bodyObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/riemann/tools/haproxy.rb', line 52

def body
  http = ::Net::HTTP.new(@uri.host, @uri.port)
  http.use_ssl = true if @uri.scheme == 'https'
  res = http.start do |h|
    get = ::Net::HTTP::Get.new(@uri.request_uri, { 'user-agent' => opts[:user_agent] })
    unless @uri.userinfo.nil?
      userinfo = @uri.userinfo.split(':')
      get.basic_auth userinfo[0], userinfo[1]
    end
    h.request get
  end
  res.body
end

#csvObject



48
49
50
# File 'lib/riemann/tools/haproxy.rb', line 48

def csv
  CSV.parse(body.split('# ')[1], headers: true)
end

#tickObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/riemann/tools/haproxy.rb', line 24

def tick
  csv.each do |row|
    row = row.to_hash
    ns  = "haproxy #{row['pxname']} #{row['svname']}"
    row.each do |property, metric|
      next if property.nil? || property == 'pxname' || property == 'svname'

      report(
        host: @uri.host,
        service: "#{ns} #{property}",
        metric: metric.to_f,
        tags: ['haproxy'],
      )
    end

    report(
      host: @uri.host,
      service: "#{ns} state",
      state: (%w[UP OPEN].include?(row['status']) ? 'ok' : 'critical'),
      tags: ['haproxy'],
    )
  end
end