Class: LogStash::Outputs::Email

Inherits:
Base show all
Defined in:
lib/logstash/outputs/email.rb

Overview

Send email when any event is received.

Constant Summary

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#handle, #handle_worker, #initialize, #worker_setup, #workers_not_supported

Methods included from Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Outputs::Base

Instance Method Details

#receive(event) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/logstash/outputs/email.rb', line 135

def receive(event)
  return unless output?(event)
    @logger.debug("Event being tested for Email", :tags => @tags, :event => event)
    # Set Intersection - returns a new array with the items that are the same between the two
    if !@tags.empty? && (event["tags"] & @tags).size == 0
       # Skip events that have no tags in common with what we were configured
       @logger.debug("No Tags match for Email Output!")
       return
    end

  @logger.debug? && @logger.debug("Match data for Email - ", :match => @match)
  successful = false
  matchName = ""
  operator = ""

  # TODO(sissel): Delete this once match support is removed.
  @match && @match.each do |name, query|
    if successful
      break
    else
      matchName = name
    end
    # now loop over the csv query
    queryArray = query.split(',')
    index = 1
    while index < queryArray.length
      field = queryArray.at(index -1)
      value = queryArray.at(index)
      index = index + 2
      if field == ""
        if value.downcase == "and"
          operator = "and"
        elsif value.downcase == "or"
          operator = "or"
        else
          operator = "or"
          @logger.error("Operator Provided Is Not Found, Currently We Only Support AND/OR Values! - defaulting to OR")
        end
      else
        hasField = event[field]
        @logger.debug? and @logger.debug("Does Event Contain Field - ", :hasField => hasField)
        isValid = false
        # if we have maching field and value is wildcard - we have a success
        if hasField
          if value == "*"
            isValid = true
          else
            # we get an array so we need to loop over the values and find if we have a match
            eventFieldValues = event[field]
            @logger.debug? and @logger.debug("Event Field Values - ", :eventFieldValues => eventFieldValues)
            eventFieldValues = [eventFieldValues] if not eventFieldValues.respond_to?(:each)
            eventFieldValues.each do |eventFieldValue|
              isValid = validateValue(eventFieldValue, value)
              if isValid # no need to iterate any further
                @logger.debug("VALID CONDITION FOUND - ", :eventFieldValue => eventFieldValue, :value => value) 
                break
              end
            end # end eventFieldValues.each do
          end # end value == "*"
        end # end hasField
        # if we have an AND operator and we have a successful == false break
        if operator == "and" && !isValid
          successful = false
        elsif operator == "or" && (isValid || successful)
          successful = true
        else
          successful = isValid
        end
      end
    end
  end # @match.each do

  # The 'match' setting is deprecated and optional. If not set,
  # default to success.
  successful = true if @match.nil?

  @logger.debug? && @logger.debug("Email Did we match any alerts for event : ", :successful => successful)

  if successful
    # first add our custom field - matchName - so we can use it in the sprintf function
    event["matchName"] = matchName unless matchName.empty?
    @logger.debug? and @logger.debug("Creating mail with these settings : ", :via => @via, :options => @options, :from => @from, :to => @to, :cc => @cc, :subject => @subject, :body => @body, :content_type => @contenttype, :htmlbody => @htmlbody, :attachments => @attachments, :to => to, :to => to)
    formatedSubject = event.sprintf(@subject)
    formattedBody = event.sprintf(@body)
    formattedHtmlBody = event.sprintf(@htmlbody)
    # we have a match(s) - send email
    mail = Mail.new
    mail.from = event.sprintf(@from)
    mail.to = event.sprintf(@to)
    if @replyto
      mail.reply_to = event.sprintf(@replyto)
    end
    mail.cc = event.sprintf(@cc)
    mail.subject = formatedSubject
    if @htmlbody.empty?
      formattedBody.gsub!(/\\n/, "\n") # Take new line in the email
      mail.body = formattedBody
    else
      mail.text_part = Mail::Part.new do
        content_type "text/plain; charset=UTF-8"
        formattedBody.gsub!(/\\n/, "\n") # Take new line in the email
        body formattedBody
      end
      mail.html_part = Mail::Part.new do
        content_type "text/html; charset=UTF-8"
        body formattedHtmlBody
      end
    end
    @attachments.each do |fileLocation|
      mail.add_file(fileLocation)
    end # end @attachments.each
    @logger.debug? and @logger.debug("Sending mail with these values : ", :from => mail.from, :to => mail.to, :cc => mail.cc, :subject => mail.subject)
    mail.deliver!
  end # end if successful
end

#registerObject



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
# File 'lib/logstash/outputs/email.rb', line 102

def register
  require "mail"

  # Mail uses instance_eval which changes the scope of self so @options is
  # inaccessible from inside 'Mail.defaults'. So set a local variable instead.
  options = @options

  if @via == "smtp"
    Mail.defaults do
      delivery_method :smtp, {
        :address              => options.fetch("smtpIporHost", "localhost"),
        :port                 => options.fetch("port", 25),
        :domain               => options.fetch("domain", "localhost"),
        :user_name            => options.fetch("userName", nil),
        :password             => options.fetch("password", nil),
        :authentication       => options.fetch("authenticationType", nil),
        :enable_starttls_auto => options.fetch("starttls", false),
        :debug                => options.fetch("debug", false)
      }
    end
  elsif @via == 'sendmail'
    Mail.defaults do
      delivery_method :sendmail
    end
  else
    Mail.defaults do
      delivery_method :@via, options
    end
  end # @via tests
  @logger.debug("Email Output Registered!", :config => @config)
end