Class: Proxy::Monitoring::Icinga2::Icinga2ApiObserver

Inherits:
Object
  • Object
show all
Includes:
Log, Common
Defined in:
lib/smart_proxy_monitoring_icinga2/icinga2_api_observer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue) ⇒ Icinga2ApiObserver

Returns a new instance of Icinga2ApiObserver.



12
13
14
15
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_api_observer.rb', line 12

def initialize(queue)
  @queue = queue.queue
  @semaphore = Mutex.new
end

Instance Attribute Details

#semaphoreObject (readonly)

Returns the value of attribute semaphore.



10
11
12
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_api_observer.rb', line 10

def semaphore
  @semaphore
end

Instance Method Details

#monitorObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_api_observer.rb', line 17

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

#startObject



56
57
58
59
60
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_api_observer.rb', line 56

def start
  @thread = Thread.new { monitor }
  @thread.abort_on_exception = true
  @thread
end

#stopObject



62
63
64
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_api_observer.rb', line 62

def stop
  @thread.terminate unless @thread.nil?
end