class Riemann::Tools::Haproxy

Public Class Methods

new() click to toggle source
Calls superclass method Riemann::Tools::new
# File lib/riemann/tools/haproxy.rb, line 18
def initialize
  super

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

Public Instance Methods

body() click to toggle source
# 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
csv() click to toggle source
# File lib/riemann/tools/haproxy.rb, line 48
def csv
  CSV.parse(body.split('# ')[1], headers: true)
end
tick() click to toggle source
# 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