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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue) ⇒ Icinga2ResultUploader

Returns a new instance of Icinga2ResultUploader.



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

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

Instance Attribute Details

#semaphoreObject (readonly)

Returns the value of attribute semaphore.



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

def semaphore
  @semaphore
end

Instance Method Details

#startObject



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

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

#stopObject



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

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

#uploadObject



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
69
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_result_uploader.rb', line 21

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 'AchnowledgementCleared'
        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 => e
        logger.error "Error while uploading monitoring results to Foreman: #{e.message}"
        sleep 1
        retry
      end
    end
  end
end