Facebook
From crh, 3 Years ago, written in Ruby.
Embed
Download Paste or View Raw
Hits: 155
  1. # encoding: utf-8
  2. require "logstash/filters/base"
  3. require "logstash/namespace"
  4. require "base62-rb"
  5.  
  6. # This example filter will replace the contents of the default
  7. # message field with whatever you specify in the configuration.
  8. #
  9. # It is only intended to be used as an example.
  10. class LogStash::Filters::Kannel < LogStash::Filters::Base
  11.  
  12.   # Setting the config_name here is required. This is how you
  13.   # configure this filter from your Logstash config.
  14.   #
  15.   # filter {
  16.   #   kannel {
  17.   #     message => "My message..."
  18.   #   }
  19.   # }
  20.   #
  21.   config_name "kannel"
  22.  
  23.   # Replace the message with this value.
  24.   config :message, :validate => :string, :default => "Hello World!"
  25.  
  26.   public
  27.   def register
  28.     # Add instance variables
  29.   end # def register
  30.  
  31.   public
  32.   def filter(event)
  33.  
  34.     if @message
  35.       dec = self.get_campaign_id("RTDM aaaSd2.P.f1", "Receive SMS")
  36.  
  37.       # using the event.set API
  38.       event.set("message", dec)
  39.  
  40.       # correct debugging log statement for reference
  41.       # using the event.get API
  42.       @logger.debug? && @logger.debug("Message is now: #{event.get("message")}")
  43.     end
  44.  
  45.     # filter_matched should go in the last line of our successful code
  46.     filter_matched(event)
  47.   end # def filter
  48.  
  49.   public
  50.   def is_printable(str)
  51.     return str.match(/^[\x20-\x7E]*$/)
  52.   end
  53.  
  54.   public
  55.   def get_campaign_id(text, action)
  56.     if action == "Receive SMS"
  57.         if is_printable(text)
  58.             if text != "ACTIM3GP"
  59.                 if match = text.match(/^(?<keyword>[a-zA-Z0-9]+ )?(?<base62>[a-zA-Z0-9]+).*$/)
  60.                   keyword, base62 = match.captures
  61.                   return Base62.decode(base62)
  62.                 end
  63.             end
  64.         end
  65.     end
  66.     return 0
  67.   end
  68.  
  69. end
  70.