Class: Proxy::Monitoring::Icinga2::Icinga2ResultUploader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TasksCommon

#action, #activated?, #start

Constructor Details

#initialize(queue) ⇒ Icinga2ResultUploader

Returns a new instance of Icinga2ResultUploader.



15
16
17
18
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_result_uploader.rb', line 15

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

Instance Attribute Details

#semaphoreObject (readonly)

Returns the value of attribute semaphore.



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

def semaphore
  @semaphore
end

Instance Method Details

#do_startObject



70
71
72
73
74
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_result_uploader.rb', line 70

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

#stopObject



76
77
78
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_result_uploader.rb', line 76

def stop
  @thread&.terminate
end

#uploadObject



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
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_result_uploader.rb', line 20

def upload
  while change = @queue.pop
    with_event_counter('Icinga2 Result Uploader') do
      symbolize_keys_deep!(change)

      change[:timestamp] = change[:check_result][:schedule_end] if change.key?(:check_result)
      if change.key?(:downtime) && change[:downtime].is_a?(Hash)
        change[:host] = change[:downtime][:host_name] if change[:host].nil? || change[:host].empty?
        change[:service] = change[:downtime][:service_name] if change[:service].nil? || change[:service].empty?
      end

      if change[:service].nil? || change[:service].empty?
        change[:service] = 'Host Check'
      end

      case change[:type]
      when 'StateChange'
        transformed = { result: change[:check_result][:state] }
      when 'AcknowledgementSet'
        transformed = { acknowledged: true }
      when 'AcknowledgementCleared'
        transformed = { acknowledged: false }
      when 'DowntimeTriggered'
        transformed = { downtime: true }
      when 'DowntimeRemoved'
        transformed = { downtime: false }
      when '_parsed'
        transformed = change.dup.reject! { |k, _v| k == :type }
      else
        next
      end
      transformed.merge!(
        host: change[:host],
        service: change[:service],
        timestamp: change[:timestamp]
      )
      begin
        MonitoringResult.new.push_result(transformed.to_json)
      rescue Errno::ECONNREFUSED => e
        logger.error "Foreman refused connection when tried to upload monitoring result: #{e.message}"
        sleep 10
      rescue StandardError => e
        logger.error "Error while uploading monitoring results to Foreman: #{e.message}"
        sleep 1
        retry
      end
    end
  end
end