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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TasksCommon

#action, #activated?, #start

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

#do_startObject



54
55
56
57
58
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_api_observer.rb', line 54

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

#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
# 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[0] == '{'

      with_event_counter('Icinga2 Event API Monitor') do
        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
    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
end

#stopObject



60
61
62
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_api_observer.rb', line 60

def stop
  @thread&.terminate
end