Sha256: 6140a98d803164aa845d9ad9f5bd44e88d69afa16b0a69996320719c91dbab88

Contents?: true

Size: 1.92 KB

Versions: 4

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require 'riemann/tools'
require 'riemann/tools/version'

# Gathers haproxy CSV statistics and submits them to Riemann.
module Riemann
  module Tools
    class 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
      opt :user_agent, 'User-Agent header for HTTP requests', short: :none, default: "#{File.basename($PROGRAM_NAME)}/#{Riemann::Tools::VERSION} (+https://github.com/riemann/riemann-tools)"

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

      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

      def csv
        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
        CSV.parse(res.body.split('# ')[1], { headers: true })
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
riemann-tools-1.8.2 lib/riemann/tools/haproxy.rb
riemann-tools-1.8.1 lib/riemann/tools/haproxy.rb
riemann-tools-1.8.0 lib/riemann/tools/haproxy.rb
riemann-tools-1.7.1 lib/riemann/tools/haproxy.rb