Sha256: ac54eab5d50955f5bbebe56817920d2e844074b17c849e4f8db84cdbf371b7dd

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

#!/usr/bin/env ruby

# Gathers haproxy CSV statistics and submits them to Riemann.

require File.expand_path('../../lib/riemann/tools', __FILE__)

class Riemann::Tools::Haproxy
  include Riemann::Tools
  require 'net/http'
  require 'csv'

  opt :stats_url, "Full url to haproxy stats (eg: https://user:password@host.com:9999/stats)", :required => true, :type => :string

  def initialize
    @uri = URI(opts[:stats_url]+';csv')
  end

  def tick
    csv = CSV.parse(get_csv.body.split("# ")[1], { :headers => true })
    csv.each do |row|
      row = row.to_hash
      ns  = "haproxy #{row['pxname']} #{row['svname']}"
      row.each do |property, metric|
        unless property == 'pxname' || property == 'svname'
          report(
            :host    => @uri.host,
            :service => "#{ns} #{property}",
            :metric  => metric.to_f,
            :state   =>  (['UP', 'OPEN'].include?(row['status']) ? 'ok' : 'critical'),
            :tags    => ['haproxy']
          )
        end
      end
    end
  end

  def get_csv
    http = Net::HTTP.new(@uri.host, @uri.port)
    http.use_ssl = true if @uri.scheme == 'https'
    http.start do |h|
      get = Net::HTTP::Get.new(@uri.request_uri)
      unless @uri.userinfo.nil?
        userinfo = @uri.userinfo.split(":")
        get.basic_auth userinfo[0], userinfo[1]
      end
      h.request get
    end
  end

end

Riemann::Tools::Haproxy.run

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
riemann-tools-0.1.2 bin/riemann-haproxy
riemann-tools-0.1.1 bin/riemann-haproxy
riemann-tools-0.1.0 bin/riemann-haproxy
riemann-tools-0.0.9 bin/riemann-haproxy