Class: Proxy::Monitoring::Icinga2::Icinga2InitialImporter

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

Instance Method Summary collapse

Methods included from TasksCommon

#action, #activated?, #start

Constructor Details

#initialize(queue) ⇒ Icinga2InitialImporter

Returns a new instance of Icinga2InitialImporter.



9
10
11
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_initial_importer.rb', line 9

def initialize(queue)
  @queue = queue.queue
end

Instance Method Details

#do_startObject



87
88
89
90
91
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_initial_importer.rb', line 87

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

#import_downtimesObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_initial_importer.rb', line 70

def import_downtimes
  results = Icinga2Client.get('/objects/downtimes?attrs=host_name&attrs=service_name&attrs=trigger_time')
  results = JSON.parse(results)
  results['results'].each do |result|
    next unless result['attrs']['trigger_time'] != 0

    parsed = {
      host: result['attrs']['host_name'],
      service: result['attrs']['service_name'],
      downtime: true,
      initial: true,
      type: '_parsed',
    }
    @queue.push(parsed)
  end
end

#import_hostsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_initial_importer.rb', line 33

def import_hosts
  results = Icinga2Client.get('/objects/hosts?attrs=name&attrs=last_check_result&attrs=acknowledgement')
  results = JSON.parse(results)
  results['results'].each do |result|
    next if result['attrs']['last_check_result'].nil?

    parsed = {
      host: result['attrs']['name'],
      result: result['attrs']['last_check_result']['state'],
      timestamp: result['attrs']['last_check_result']['schedule_end'],
      acknowledged: (result['attrs']['acknowledgement'] != 0),
      initial: true,
      type: '_parsed',
    }
    @queue.push(parsed)
  end
end

#import_servicesObject



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_initial_importer.rb', line 51

def import_services
  results = Icinga2Client.get('/objects/services?attrs=name&attrs=last_check_result&attrs=acknowledgement&attrs=host_name')
  results = JSON.parse(results)
  results['results'].each do |result|
    next if result['attrs']['last_check_result'].nil?

    parsed = {
      host: result['attrs']['host_name'],
      service: result['attrs']['name'],
      result: result['attrs']['last_check_result']['state'],
      timestamp: result['attrs']['last_check_result']['schedule_end'],
      acknowledged: (result['attrs']['acknowledgement'] != 0),
      initial: true,
      type: '_parsed',
    }
    @queue.push(parsed)
  end
end

#monitorObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_initial_importer.rb', line 13

def monitor
  logger.debug 'Starting initial icinga import.'

  around_action('Initial Host Import') do
    import_hosts
  end

  around_action('Initial Services Import') do
    import_services
  end

  around_action('Initial Downtimes Import') do
    import_downtimes
  end

  logger.info 'Finished initial icinga import.'
rescue Exception => e
  logger.error "Error during initial import: #{e.message}\n#{e.backtrace}"
end

#stopObject



93
94
95
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_initial_importer.rb', line 93

def stop
  @thread&.terminate
end