class Flapjack::Gateways::PagerDuty::Notifier

Public Class Methods

new(opts = {}) click to toggle source
# File lib/flapjack/gateways/pager_duty.rb, line 69
def initialize(opts = {})
  @lock = opts[:lock]
  @config = opts[:config]

  # TODO support for config reloading
  @queue = Flapjack::RecordQueue.new(@config['queue'] || 'pagerduty_notifications',
             Flapjack::Data::Alert)

  Flapjack.logger.debug("New PagerDuty::Notifier pikelet with the following options: #{@config.inspect}")
end

Public Instance Methods

start() click to toggle source
# File lib/flapjack/gateways/pager_duty.rb, line 80
def start
  until Flapjack::Gateways::PagerDuty.test_pagerduty_connection
    Flapjack.logger.error("Can't connect to the PagerDuty API, retrying after 10 seconds")
    Kernel.sleep(10)
  end

  begin
    Zermelo.redis = Flapjack.redis

    loop do
      @lock.synchronize do
        @queue.foreach {|alert| handle_alert(alert) }
      end

      @queue.wait
    end
  ensure
    Flapjack.redis.quit
  end
end
stop_type() click to toggle source
# File lib/flapjack/gateways/pager_duty.rb, line 101
def stop_type
  :exception
end

Private Instance Methods

handle_alert(alert) click to toggle source
# File lib/flapjack/gateways/pager_duty.rb, line 107
def handle_alert(alert)
  check = alert.check

  address = alert.address

  Flapjack.logger.debug("processing PagerDuty notification service_key: #{address}, " +
                "check: '#{check.name}', state: #{alert.state}, summary: #{alert.summary}")

  message_template_erb, message_template =
    load_template(@config['templates'], 'alert',
                  'text', File.join(File.dirname(__FILE__), 'pager_duty'))

  @alert = alert
  bnd = binding

  msg = nil
  begin
    msg = message_template_erb.result(bnd).chomp
  rescue => e
    Flapjack.logger.error "Error while executing the ERB for a PagerDuty message, " +
      "ERB being executed: #{message_template}"
    raise
  end

  pagerduty_type = case alert.type
  when 'acknowledgement'
    'acknowledge'
  when 'problem'
    'trigger'
  when 'recovery'
    'resolve'
  when 'test'
    'trigger'
  end

  # quick fix, may not be true in all cases
  host_name, service_name = check.name.split(':', 2)

  # Setting the HOSTNAME and the SERVICE makes them visible in the PagerDuty UI
  Flapjack::Gateways::PagerDuty.send_pagerduty_event(
    :service_key  => address,
    :incident_key => check.name,
    :event_type   => pagerduty_type,
    :description  => msg,
    :details      => {'HOSTNAME' => host_name,
                       'SERVICE' => service_name}
  )

  Flapjack.logger.info "Sent alert successfully: #{alert.to_s}"
rescue => e
  Flapjack.logger.error "Error generating or dispatching PagerDuty message: #{e.class}: #{e.message}\n" +
    e.backtrace.join("\n")
  Flapjack.logger.debug "Alert that could not be processed: \n" + alert.inspect
end