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.



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

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

Instance Attribute Details

#semaphoreObject (readonly)

Returns the value of attribute semaphore.



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

def semaphore
  @semaphore
end

Instance Method Details

#do_startObject



57
58
59
60
61
# 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

#monitorObject



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
55
# 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

#stopObject



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

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