lib/logstash/outputs/email.rb in logstash-output-email-1.1.0 vs lib/logstash/outputs/email.rb in logstash-output-email-2.0.0

- old
+ new

@@ -6,18 +6,10 @@ # exclude the email output execution using conditionals. class LogStash::Outputs::Email < LogStash::Outputs::Base config_name "email" - # This setting is deprecated in favor of Logstash's "conditionals" feature - # If you were using this setting previously, please use conditionals instead. - # - # If you need help converting your older `match` setting to a conditional, - # I welcome you to join the #logstash irc channel on freenode or to post - # a message on https://discuss.elastic.co/c/logstash and ask for help! :) - config :match, :validate => :hash, :deprecated => true - # The fully-qualified email address to send the email to. # # This field also accepts a comma-separated string of addresses, for example: # `"me@host.com, you@host.com"` # @@ -37,56 +29,34 @@ config :cc, :validate => :string # How Logstash should send the email, either via SMTP or by invoking sendmail. config :via, :validate => :string, :default => "smtp" - # Specify the options to use: - # - # Via SMTP: `smtpIporHost`, `port`, `domain`, `userName`, `password`, `authenticationType`, `starttls` - # - # Via sendmail: `location`, `arguments` - # - # If you do not specify any `options`, you will get the following equivalent code set in - # every new mail object: - # [source,ruby] - # Mail.defaults do - # delivery_method :smtp, { :smtpIporHost => "localhost", - # :port => 25, - # :domain => 'localhost.localdomain', - # :userName => nil, - # :password => nil, - # :authenticationType => nil,(plain, login and cram_md5) - # :starttls => true } - # - # retriever_method :pop3, { :address => "localhost", - # :port => 995, - # :user_name => nil, - # :password => nil, - # :enable_ssl => true } - # - # Mail.delivery_method.new #=> Mail::SMTP instance - # Mail.retriever_method.new #=> Mail::POP3 instance - # end - # - # Each mail object inherits the defaults set in Mail.delivery_method. However, on - # a per email basis, you can override the method: - # [source,ruby] - # mail.delivery_method :sendmail - # - # Or you can override the method and pass in settings: - # [source,ruby] - # mail.delivery_method :sendmail, { :address => 'some.host' } - # - # You can also just modify the settings: - # [source,ruby] - # mail.delivery_settings = { :address => 'some.host' } - # - # The hash you supply is just merged against the defaults with "merge!" and the result - # assigned to the mail object. For instance, the above example will change only the - # `:address` value of the global `smtp_settings` to be 'some.host', retaining all other values. - config :options, :validate => :hash, :default => {} + # The address used to connect to the mail server + config :address, :validate => :string, :default => "localhost" + # Port used to communicate with the mail server + config :port, :validate => :number, :default => 25 + + # Domain used to send the email messages + config :domain, :validate => :string, :default => "localhost" + + # Username to authenticate with the server + config :username, :validate => :string + + # Password to authenticate with the server + config :password, :validate => :string + + # Authentication method used when identifying with the server + config :authentication, :validate => :string + + # Enables TLS when communicating with the server + config :use_tls, :validate => :boolean, :default => false + + # Run the mail relay in debug mode + config :debug, :validate => :boolean, :default => false + # Subject: for the email. config :subject, :validate => :string, :default => "" # Body for the email - plain text only. config :body, :validate => :string, :default => "" @@ -103,126 +73,45 @@ public 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 + options = { + :address => @address, + :port => @port, + :domain => @domain, + :user_name => @username, + :password => @password, + :authentication => @authentication, + :enable_starttls_auto => @use_tls, + :debug => @debug + } 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) - } + delivery_method :smtp, options 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) + @logger.debug("Email Output Registered!", :config => options, :via => @via) end # def register public 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) @@ -251,57 +140,7 @@ mail.deliver! rescue StandardError => e @logger.error("Something happen while delivering an email", :exception => e) @logger.debug? && @logger.debug("Processed event: ", :event => event) end - end # end if successful end # def receive - - - private - def validateValue(eventFieldValue, value) - valid = false - # order of this if-else is important - please don't change it - if value.start_with?(">=")# greater than or equal - value.gsub!(">=","") - if eventFieldValue.to_i >= value.to_i - valid = true - end - elsif value.start_with?("<=")# less than or equal - value.gsub!("<=","") - if eventFieldValue.to_i <= value.to_i - valid = true - end - elsif value.start_with?(">")# greater than - value.gsub!(">","") - if eventFieldValue.to_i > value.to_i - valid = true - end - elsif value.start_with?("<")# less than - value.gsub!("<","") - if eventFieldValue.to_i < value.to_i - valid = true - end - elsif value.start_with?("*")# contains - value.gsub!("*","") - if eventFieldValue.include?(value) - valid = true - end - elsif value.start_with?("!*")# does not contain - value.gsub!("!*","") - if !eventFieldValue.include?(value) - valid = true - end - elsif value.start_with?("!")# not equal - value.gsub!("!","") - if eventFieldValue != value - valid = true - end - else # default equal - if eventFieldValue == value - valid = true - end - end - return valid - end # end validateValue() - end # class LogStash::Outputs::Email