class Proxy::Monitoring::Icinga2::Icinga2InitialImporter

Public Class Methods

new(queue) click to toggle source
# File lib/smart_proxy_monitoring_icinga2/icinga2_initial_importer.rb, line 10
def initialize(queue)
  @queue = queue.queue
end

Public Instance Methods

do_start() click to toggle source
# File lib/smart_proxy_monitoring_icinga2/icinga2_initial_importer.rb, line 85
def do_start
  @thread = Thread.new { monitor }
  @thread.abort_on_exception = true
  @thread
end
import_downtimes() click to toggle source
# File lib/smart_proxy_monitoring_icinga2/icinga2_initial_importer.rb, line 69
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_hosts() click to toggle source
# File lib/smart_proxy_monitoring_icinga2/icinga2_initial_importer.rb, line 34
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_services() click to toggle source
# 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
monitor() click to toggle source
# File lib/smart_proxy_monitoring_icinga2/icinga2_initial_importer.rb, line 14
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
stop() click to toggle source
# File lib/smart_proxy_monitoring_icinga2/icinga2_initial_importer.rb, line 91
def stop
  @thread.terminate unless @thread.nil?
end

Private Instance Methods

around_action(task) { || ... } click to toggle source
# File lib/smart_proxy_monitoring_icinga2/icinga2_initial_importer.rb, line 97
def around_action(task)
  beginning_time = Time.now
  logger.info "Starting Task: #{task}."
  yield
  end_time = Time.now
  logger.info "Finished Task: #{task} in #{end_time - beginning_time} seconds."
rescue Errno::ECONNREFUSED => e
  logger.error "Icinga Initial Importer: Connection refused in task #{task}. Reason: #{e.message}"
  logger.error "Icinga Initial Importer: Restarting #{task} in 5 seconds."
  sleep 5
  retry
rescue JSON::ParserError => e
  logger.error "Icinga Initial Importer: Failed to parse JSON: #{e.message}"
end