Sha256: 546c1c3d751c10199d6c5962c2d20a1f780fa85a26cb5dfaf252d0d47d02f657

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 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
        super

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

      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
riemann-tools-1.11.0 lib/riemann/tools/haproxy.rb