Sha256: b818e67a6d6fbdbb17bc707b7d2d49ed6c04f310613eaceb4301b9599451d7e8

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

require 'riemann/tools'

# 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

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

2 entries across 2 versions & 1 rubygems

Version Path
riemann-tools-1.6.0 lib/riemann/tools/haproxy.rb
riemann-tools-1.5.0 lib/riemann/tools/haproxy.rb