Sha256: 914be69492974d9cdaf45731cae341edd49e16b4408e17b65ebce075af6f671c

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

require 'json'

module AwsAlertMonitor
  class Alert

    def initialize(args)
      @config = args[:config]
      @logger = @config.logger
    end

    def process(args)
      @name    = args[:name]
      @events  = args[:events]
      @message = args[:message]

      unless process_message
        @logger.error "Unable to process message."
        return false
      end

      @events.each_pair do |event, policy|
        @logger.info "Evaluating '#{@message_event}' against '#{event}'"
        send_alert(policy) if event == @message_event
      end
    end

    private

    def send_alert(policy)
      @message_source      = policy['email']['source']
      @message_destination = policy['email']['destination']

      if @message_destination
        @logger.info "Sending alert to #{@message_destination}."
        send_email
      else
        @logger.info "Destination not set, no message sent."
      end
    end

    def email_options
      {
        'body'    => @message_data,
        'from'    => @message_source,
        'subject' => @message_subject,
        'to'      => Array(@message_destination)
      }
    end

    def event_classifier
      AwsAlertMonitor::EventClassifier.new @message
    end

    def process_message
      begin
        event = event_classifier.event
      rescue JSON::ParserError => e
        @logger.error e.message
        return false
      end

      @message_event   = event.type
      @message_subject = event.subject
      @message_data    = "#{@name} #{event.body}"

      true
    end

    def send_email
      AwsAlertMonitor::Emailer.new(email_options).send_email
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aws-alert-monitor-0.1.0 lib/aws-alert-monitor/alert.rb
aws-alert-monitor-0.0.5 lib/aws-alert-monitor/alert.rb