Class: Bosh::Monitor::Plugins::Email

Inherits:
Base
  • Object
show all
Defined in:
lib/bosh/monitor/plugins/email.rb

Constant Summary collapse

DEFAULT_INTERVAL =
10

Instance Attribute Summary

Attributes inherited from Base

#event_kinds, #logger, #options

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Email

Returns a new instance of Email.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bosh/monitor/plugins/email.rb', line 6

def initialize(options = {})
  @queues = {}
  @lock  = Mutex.new

  if options.has_key?("interval")
    @delivery_interval = options["interval"].to_f
  else
    @delivery_interval = DEFAULT_INTERVAL
  end

  @started = false
  super
end

Instance Method Details

#process(event) ⇒ Object



62
63
64
65
66
67
# File 'lib/bosh/monitor/plugins/email.rb', line 62

def process(event)
  @lock.synchronize do
    @queues[event.kind] ||= []
    @queues[event.kind] << event
  end
end

#process_queuesObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bosh/monitor/plugins/email.rb', line 69

def process_queues
  logger.info("Proccessing queues...")
  @queues.each_pair do |kind, queue|
    next if queue.empty?
    logger.info("Creating email...")
    email_subject = "%s from BOSH Health Monitor" % [ pluralize(queue_size(kind), kind) ]
    email_body = ""

    @lock.synchronize do
      while event = queue.shift
        logger.info("Dequeueing...")
        email_body << event.to_plain_text << "\n"
      end
    end

    send_email_async(email_subject, email_body)
  end
end

#queue_size(kind) ⇒ Object



20
21
22
23
# File 'lib/bosh/monitor/plugins/email.rb', line 20

def queue_size(kind)
  return 0 if @queues[kind].nil?
  @queues[kind].size
end

#recipientsObject



54
55
56
# File 'lib/bosh/monitor/plugins/email.rb', line 54

def recipients
  options["recipients"]
end

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bosh/monitor/plugins/email.rb', line 25

def run
  unless EM.reactor_running?
    logger.error("Email plugin can only be started when event loop is running")
    return false
  end

  return true if @started
  logger.info("Email plugin is running...")

  EM.add_periodic_timer(@delivery_interval) do
    begin
      process_queues
    rescue => e
      logger.error("Problem processing email queues: #{e}")
    end
  end
  @started = true
end

#send_email_async(subject, body, date = Time.now) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/bosh/monitor/plugins/email.rb', line 88

def send_email_async(subject, body, date = Time.now)
  started = Time.now
  logger.info("Sending email...")

  headers = {
    "From"         => smtp_options["from"],
    "To"           => recipients.join(", "),
    "Subject"      => subject,
    "Date"         => date,
    "Content-Type" => "text/plain; charset=\"iso-8859-1\""
  }

  smtp_client_options = {
    :domain   => smtp_options["domain"],
    :host     => smtp_options["host"],
    :port     => smtp_options["port"],
    :from     => smtp_options["from"],
    :to       => recipients,
    :header   => headers,
    :body     => body
  }

  if smtp_options["tls"]
    smtp_client_options[:starttls] = true
  end

  if smtp_options["auth"]
    smtp_client_options[:auth] = {
      # FIXME: EM SMTP client will only work with plain auth
      :type     => smtp_options["auth"].to_sym,
      :username => smtp_options["user"],
      :password => smtp_options["password"]
    }
  end

  email = EM::Protocols::SmtpClient.send(smtp_client_options)

  email.callback do
    logger.debug("Email sent (took #{Time.now - started} seconds)")
  end

  email.errback do |e|
    logger.error("Failed to send email: #{e}")
  end

rescue => e
  logger.error("Error sending email: #{e}")
end

#smtp_optionsObject



58
59
60
# File 'lib/bosh/monitor/plugins/email.rb', line 58

def smtp_options
  options["smtp"]
end

#validate_optionsObject



44
45
46
47
48
49
50
51
52
# File 'lib/bosh/monitor/plugins/email.rb', line 44

def validate_options
  options.kind_of?(Hash) &&
    options["recipients"].kind_of?(Array) &&
    options["smtp"].kind_of?(Hash) &&
    options["smtp"]["host"] &&
    options["smtp"]["port"] &&
    options["smtp"]["from"] &&
    true # force the whole method to return Boolean
end