Sha256: 92c4dcb0b3671ed3090c3f28a66f4b3a347c3fde13d5efb15aaa1f509c08a7f5

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

require_relative 'sender_client'

module Riemann
  module Babbler

    class Sender

      include Riemann::Babbler::Logging
      include Riemann::Babbler::Options
      include Riemann::Babbler::Errors

      HOSTNAME_FILE = '/proc/sys/kernel/hostname'
      CHECK_CLIENT_ALIVE = 10

      def initialize
        @hostname = hostname
        @riemanns = create_riemanns
        start
      end

      def <<(event)
        set_event_hostname(event)
        set_event_tags(event)
        @riemanns.each {|r| r << event }
      end

      private

      def start
        Thread.new do
          loop do
            @riemanns.each do |r|
              next if r.alive?
              log :error, "Riemann client #{r.host}:#{r.port} is died, up it"
              r.start
            end
            log :debug, "Check alive of riemann client [#{@riemanns.count}]"
            sleep CHECK_CLIENT_ALIVE
          end
        end  
      end

      def create_riemanns
        riemanns = Array.new
        opts.riemann.host.each { |host| riemanns << Riemann::Babbler::Sender::Client.new(host) }
        riemanns
      end

      def set_event_tags(event)
        unless opts.riemann.tags.nil?
          event[:tags] = opts.riemann.tags unless event[:tags]
        end
      end

      def set_event_hostname(event)
        event[:host] = @hostname unless event[:host]
      end

      def hostname
        if opts.riemann.fqdn
          hostname = Socket.gethostbyname(Socket.gethostname).first
          log :debug, "Get hostname from Socket.gethostname: #{hostname}"
        else
          hostname = File.read(HOSTNAME_FILE).strip.downcase if File.exist? HOSTNAME_FILE
          log :debug, "Get hostname from #{HOSTNAME_FILE}: #{hostname}"
        end
        hostname
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
riemann-babbler-2.5.0 lib/riemann/babbler/sender.rb