Class: Bosh::Monitor::Plugins::Email
- 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
-
#initialize(options = {}) ⇒ Email
constructor
A new instance of Email.
- #process(event) ⇒ Object
- #process_queues ⇒ Object
- #queue_size(kind) ⇒ Object
- #recipients ⇒ Object
- #run ⇒ Object
- #send_email_async(subject, body, date = Time.now) ⇒ Object
- #smtp_options ⇒ Object
- #validate_options ⇒ Object
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( = {}) @queues = {} @lock = Mutex.new if .has_key?("interval") @delivery_interval = ["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_queues ⇒ Object
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 |
#recipients ⇒ Object
54 55 56 |
# File 'lib/bosh/monitor/plugins/email.rb', line 54 def recipients ["recipients"] end |
#run ⇒ Object
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" => ["from"], "To" => recipients.join(", "), "Subject" => subject, "Date" => date, "Content-Type" => "text/plain; charset=\"iso-8859-1\"" } = { :domain => ["domain"], :host => ["host"], :port => ["port"], :from => ["from"], :to => recipients, :header => headers, :body => body } if ["tls"] [:starttls] = true end if ["auth"] [:auth] = { # FIXME: EM SMTP client will only work with plain auth :type => ["auth"].to_sym, :username => ["user"], :password => ["password"] } end email = EM::Protocols::SmtpClient.send() 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_options ⇒ Object
58 59 60 |
# File 'lib/bosh/monitor/plugins/email.rb', line 58 def ["smtp"] end |
#validate_options ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/bosh/monitor/plugins/email.rb', line 44 def .kind_of?(Hash) && ["recipients"].kind_of?(Array) && ["smtp"].kind_of?(Hash) && ["smtp"]["host"] && ["smtp"]["port"] && ["smtp"]["from"] && true # force the whole method to return Boolean end |