class Proxy::Monitoring::Icinga2::Icinga2ApiObserver

Attributes

semaphore[R]

Public Class Methods

new(queue) click to toggle source
# File lib/smart_proxy_monitoring_icinga2/icinga2_api_observer.rb, line 13
def initialize(queue)
  @queue = queue.queue
  @semaphore = Mutex.new
end

Public Instance Methods

do_start() click to toggle source
# File lib/smart_proxy_monitoring_icinga2/icinga2_api_observer.rb, line 57
def do_start
  @thread = Thread.new { monitor }
  @thread.abort_on_exception = true
  @thread
end
monitor() click to toggle source
# File lib/smart_proxy_monitoring_icinga2/icinga2_api_observer.rb, line 18
def monitor
  loop do
    logger.debug "Connecting to Icinga event monitoring api: #{Icinga2Client.baseurl}."

    ssl_socket = Icinga2Client.events_socket('/events?queue=foreman&types=StateChange&types=AcknowledgementSet&types=AcknowledgementCleared&types=DowntimeTriggered&types=DowntimeRemoved')

    logger.info 'Icinga event api monitoring started.'

    while line = ssl_socket.gets
      next unless line.chars.first == '{'

      with_event_counter('Icinga2 Event API Monitor') do
        begin
          parsed = JSON.parse(line)
          if @queue.size > 100_000
            @queue.clear
            logger.error 'Queue was full. Flushing. Events were lost.'
          end
          @queue.push(parsed)
        rescue JSON::ParserError => e
          logger.error "Icinga2 Event API Monitor: Malformed JSON: #{e.message}"
        end
      end

    end
    logger.info 'Icinga event api monitoring stopped.'
  end
rescue Errno::ECONNREFUSED => e
  logger.error "Icinga Event Stream: Connection refused. Retrying in 5 seconds. Reason: #{e.message}"
  sleep 5
  retry
rescue Exception => e
  logger.error "Error while monitoring: #{e.message}\n#{e.backtrace.join("\n")}"
  sleep 1
  retry
ensure
  ssl_socket.sysclose unless ssl_socket.nil?
end
stop() click to toggle source
# File lib/smart_proxy_monitoring_icinga2/icinga2_api_observer.rb, line 63
def stop
  @thread.terminate unless @thread.nil?
end