Sha256: 93d765275d5f36b68cfcf362e5e9989c3ea3c878b8af7ba2a01c8790cabdfaa5

Contents?: true

Size: 1.51 KB

Versions: 6

Compression:

Stored size: 1.51 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.nil? || property == 'pxname' || property == 'svname')
          report(
            :host    => @uri.host,
            :service => "#{ns} #{property}",
            :metric  => metric.to_f,
            :tags    => ['haproxy']
          )
        end
      end

      report(
        :host    => @uri.host,
        :service => "#{ns} state",
        :state   => (['UP', 'OPEN'].include?(row['status']) ? 'ok' : 'critical'),
        :tags    => ['haproxy']
      )
    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

6 entries across 6 versions & 1 rubygems

Version Path
riemann-tools-0.2.14 bin/riemann-haproxy
riemann-tools-0.2.13 bin/riemann-haproxy
riemann-tools-0.2.11 bin/riemann-haproxy
riemann-tools-0.2.10 bin/riemann-haproxy
riemann-tools-0.2.9 bin/riemann-haproxy
riemann-tools-0.2.8 bin/riemann-haproxy