Class: Bosh::Agent::AlertProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh_agent/alert_processor.rb

Overview

AlertProcessor is a simple SMTP server + callback for processing alerts. It is primarily meant to be used with Monit.

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, smtp_user, smtp_password) ⇒ AlertProcessor

Returns a new instance of AlertProcessor.



17
18
19
20
21
22
23
# File 'lib/bosh_agent/alert_processor.rb', line 17

def initialize(host, port, smtp_user, smtp_password)
  @host          = host
  @port          = port
  @smtp_user     = smtp_user
  @smtp_password = smtp_password
  @logger        = Config.logger
end

Class Method Details

.start(host, port, user, password) ⇒ Object



11
12
13
14
15
# File 'lib/bosh_agent/alert_processor.rb', line 11

def self.start(host, port, user, password)
  processor = new(host, port, user, password)
  processor.start
  processor
end

Instance Method Details

#create_alert_from_email(raw_email) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bosh_agent/alert_processor.rb', line 68

def create_alert_from_email(raw_email)
  @logger.debug "Received email alert: #{raw_email}"

  attrs = { }

  raw_email.split(/\r?\n/).each do |line|
    case line
    when /^\s*Message-id:\s*<(.*)>$/i
      attrs[:id] = $1.split("@")[0] # Remove host
    when /^\s*Service:\s*(.*)$/i
      attrs[:service] = $1
    when /^\s*Event:\s*(.*)$/i
      attrs[:event] = $1
    when /^\s*Action:\s*(.*)$/i
      attrs[:action] = $1
    when /^\s*Date:\s*(.*)$/i
      attrs[:date] = $1
    when /^\s*Description:\s*(.*)$/i
      attrs[:description] = $1
    end
  end

  @logger.debug("Extracted email alert data: #{attrs}")
  Alert.new(attrs)
end

#process_email_alert(raw_email) ⇒ Object

Returns:

true if succeeded to process the alert, false if failed



64
65
66
# File 'lib/bosh_agent/alert_processor.rb', line 64

def process_email_alert(raw_email)
  create_alert_from_email(raw_email).register
end

#startObject



25
26
27
28
29
30
31
32
# File 'lib/bosh_agent/alert_processor.rb', line 25

def start
  unless EM.reactor_running?
    raise Error, "Cannot start SMTP server as event loop is not running"
  end

  @server = EM.start_server(@host, @port, Bosh::Agent::SmtpServer, :user => @smtp_user, :password => @smtp_password, :processor => self)
  @logger.info "Now accepting SMTP connections on address #{@host}, port #{@port}"
end

#stopObject



34
35
36
37
38
39
40
41
42
# File 'lib/bosh_agent/alert_processor.rb', line 34

def stop
  if @server
    if EM.reactor_running?
      EM.stop_server(@server)
      @logger.info "Stopped alert processor"
    end
    @server = nil
  end
end