logporter 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/logporter/protocol/syslog3164.rb +69 -5
- data/lib/logporter/server/connection.rb +12 -8
- metadata +8 -8
| @@ -1,5 +1,60 @@ | |
| 1 1 | 
             
            require "logporter/namespace"
         | 
| 2 | 
            -
             | 
| 2 | 
            +
             | 
| 3 | 
            +
            # Ruby's core/stdlib Time.strptime is embarrasingly slow.
         | 
| 4 | 
            +
            # Let's do our own.
         | 
| 5 | 
            +
            class TimeParser
         | 
| 6 | 
            +
              @@re_cache = {}
         | 
| 7 | 
            +
              @@re_formats = {
         | 
| 8 | 
            +
                "%b" => "(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)",
         | 
| 9 | 
            +
                "%d" => "[ 1-3]?[0-9]",
         | 
| 10 | 
            +
                "%H" => "[0-9]{2}",
         | 
| 11 | 
            +
                "%M" => "[0-9]{2}",
         | 
| 12 | 
            +
                "%S" => "[0-9]{2}",
         | 
| 13 | 
            +
              }
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              def self.strptime(string, format)
         | 
| 16 | 
            +
                if @@re_cache.include?(format)
         | 
| 17 | 
            +
                  obj = @@re_cache[format]
         | 
| 18 | 
            +
                else
         | 
| 19 | 
            +
                  captures = []
         | 
| 20 | 
            +
                  pattern = format.gsub(/%[A-z]/) do |spec|
         | 
| 21 | 
            +
                    if @@re_formats.include?(spec)
         | 
| 22 | 
            +
                      captures << spec
         | 
| 23 | 
            +
                      "(#{@@re_formats[spec]})"
         | 
| 24 | 
            +
                    else
         | 
| 25 | 
            +
                      spec
         | 
| 26 | 
            +
                    end
         | 
| 27 | 
            +
                  end
         | 
| 28 | 
            +
                  re = Regexp.new(pattern)
         | 
| 29 | 
            +
                  obj = @@re_cache[format] = {
         | 
| 30 | 
            +
                    :re => re,
         | 
| 31 | 
            +
                    :captures => captures,
         | 
| 32 | 
            +
                  }
         | 
| 33 | 
            +
                end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                #m = obj[:re].match(string)
         | 
| 36 | 
            +
                #return nil if !m
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                now = Time.new
         | 
| 39 | 
            +
                time_array = [now.year, now.month, now.day, 0, 0, 0, 0]
         | 
| 40 | 
            +
                return nil unless string.scan(obj[:re]) do |*captures|
         | 
| 41 | 
            +
                  #obj[:captures].each_with_index do |spec, i|
         | 
| 42 | 
            +
                    #p spec => m[i + 1]
         | 
| 43 | 
            +
                  captures.each_with_index do |spec, i|
         | 
| 44 | 
            +
                    case spec
         | 
| 45 | 
            +
                      when "%y"; time_array[0] = m[i + 1].to_i
         | 
| 46 | 
            +
                      when "%b"; time_array[1] = m[i + 1]
         | 
| 47 | 
            +
                      when "%d"; time_array[2] = m[i + 1].to_i
         | 
| 48 | 
            +
                      when "%H"; time_array[3] = m[i + 1].to_i
         | 
| 49 | 
            +
                      when "%M"; time_array[4] = m[i + 1].to_i
         | 
| 50 | 
            +
                      when "%S"; time_array[5] = m[i + 1].to_i
         | 
| 51 | 
            +
                    end # case spec
         | 
| 52 | 
            +
                  end # each capture
         | 
| 53 | 
            +
                end # string.scan
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                return Time.local(*time_array)
         | 
| 56 | 
            +
              end # def strptime
         | 
| 57 | 
            +
            end # class Time
         | 
| 3 58 |  | 
| 4 59 | 
             
            module LogPorter::Protocol::Syslog3164
         | 
| 5 60 | 
             
              def syslog3164_init
         | 
| @@ -10,6 +65,13 @@ module LogPorter::Protocol::Syslog3164 | |
| 10 65 | 
             
                minute = "(?:[0-5][0-9])"
         | 
| 11 66 | 
             
                second = "(?:[0-5][0-9])"
         | 
| 12 67 |  | 
| 68 | 
            +
                #pri = "(?:<(?<pri>[0-9]{1,3})>)?"
         | 
| 69 | 
            +
                #month = "(?:[A-z]{3})"
         | 
| 70 | 
            +
                #day = "[ 1-9][0-9]"
         | 
| 71 | 
            +
                #hour = "[0-9]{2}"
         | 
| 72 | 
            +
                #minute = "[0-9]{2}"
         | 
| 73 | 
            +
                #second = "[0-9]{2}"
         | 
| 74 | 
            +
             | 
| 13 75 | 
             
                time = [hour, minute, second].join(":")
         | 
| 14 76 |  | 
| 15 77 | 
             
                timestamp = "(?<timestamp>#{month} #{day} #{time})"
         | 
| @@ -23,19 +85,21 @@ module LogPorter::Protocol::Syslog3164 | |
| 23 85 | 
             
                  # replace (?<foo> with (
         | 
| 24 86 | 
             
                  re = re.gsub(/\(\?<[^>]+>/, "(")
         | 
| 25 87 | 
             
                end
         | 
| 26 | 
            -
             | 
| 27 88 | 
             
                @syslog3164_re = Regexp.new(re)
         | 
| 28 89 | 
             
              end
         | 
| 29 90 |  | 
| 30 | 
            -
              def parse_rfc3164(line, event)
         | 
| 91 | 
            +
              def parse_rfc3164(line, event, opts)
         | 
| 31 92 | 
             
                syslog3164_init if !@syslog3164_re
         | 
| 32 93 | 
             
                m = @syslog3164_re.match(line)
         | 
| 33 94 | 
             
                if m
         | 
| 34 95 | 
             
                  # RFC3164 section 4.3.3 No PRI or Unidentifiable PRI
         | 
| 35 96 | 
             
                  event.pri = m[1] || "13"
         | 
| 36 97 |  | 
| 37 | 
            -
                   | 
| 38 | 
            -
             | 
| 98 | 
            +
                  if opts[:parse_time] 
         | 
| 99 | 
            +
                    event.timestamp = TimeParser.strptime(m[2], "%b %d %H:%M:%S")
         | 
| 100 | 
            +
                  else
         | 
| 101 | 
            +
                    event.timestamp = Time.now
         | 
| 102 | 
            +
                  end
         | 
| 39 103 | 
             
                  event.hostname = m[3]
         | 
| 40 104 | 
             
                  event.message = m[4]
         | 
| 41 105 | 
             
                  return true
         | 
| @@ -42,8 +42,12 @@ class LogPorter::Server::Connection < EventMachine::Connection | |
| 42 42 | 
             
                    class << self
         | 
| 43 43 | 
             
                      alias_method :receive_line, :receive_line_syslog
         | 
| 44 44 | 
             
                    end
         | 
| 45 | 
            +
                  when :syslog_no_parse_time
         | 
| 46 | 
            +
                    class << self
         | 
| 47 | 
            +
                      alias_method :receive_line, :receive_line_syslog
         | 
| 48 | 
            +
                    end
         | 
| 45 49 | 
             
                  else
         | 
| 46 | 
            -
                    raise "Unsupported protocol #{@server. | 
| 50 | 
            +
                    raise "Unsupported protocol #{@server.wire}"
         | 
| 47 51 | 
             
                end
         | 
| 48 52 |  | 
| 49 53 | 
             
                  #@server.logger.error "Exception: #{e.inspect}"
         | 
| @@ -72,16 +76,16 @@ class LogPorter::Server::Connection < EventMachine::Connection | |
| 72 76 | 
             
                  else
         | 
| 73 77 | 
             
                    client_port, client_address = Socket.unpack_sockaddr_in(peer)
         | 
| 74 78 | 
             
                  end
         | 
| 79 | 
            +
                  receive_line(data, client_address, client_port)
         | 
| 75 80 | 
             
                else
         | 
| 76 81 | 
             
                  client_port = @client_port
         | 
| 77 82 | 
             
                  client_address = @client_address
         | 
| 83 | 
            +
                  @buffer ||= BufferedTokenizer.new
         | 
| 84 | 
            +
                  @buffer.extract(data).each do |line|
         | 
| 85 | 
            +
                    receive_line(line.chomp, client_address, client_port)
         | 
| 86 | 
            +
                  end
         | 
| 78 87 | 
             
                end
         | 
| 79 | 
            -
             | 
| 80 | 
            -
                @buffer ||= BufferedTokenizer.new
         | 
| 81 | 
            -
                @buffer.extract(data).each do |line|
         | 
| 82 | 
            -
                  receive_line(line.chomp, client_address, client_port)
         | 
| 83 | 
            -
                end
         | 
| 84 | 
            -
              end
         | 
| 88 | 
            +
              end # def receive_data
         | 
| 85 89 |  | 
| 86 90 | 
             
              def receive_line_raw(line, address, port)
         | 
| 87 91 | 
             
                event = LogPorter::Event.new
         | 
| @@ -100,7 +104,7 @@ class LogPorter::Server::Connection < EventMachine::Connection | |
| 100 104 |  | 
| 101 105 | 
             
              def receive_line_syslog(line, address, port)
         | 
| 102 106 | 
             
                event = LogPorter::Event.new
         | 
| 103 | 
            -
                if parse_rfc3164(line, event)
         | 
| 107 | 
            +
                if parse_rfc3164(line, event, { :parse_time => (@server.wire == :syslog) })
         | 
| 104 108 | 
             
                #elsif parse_rfc5424(line, event)
         | 
| 105 109 | 
             
                else 
         | 
| 106 110 | 
             
                  # Unknown message format, add syslog headers.
         | 
    
        metadata
    CHANGED
    
    | @@ -1,13 +1,13 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: logporter
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              hash:  | 
| 5 | 
            -
              prerelease:  | 
| 4 | 
            +
              hash: 29
         | 
| 5 | 
            +
              prerelease: 
         | 
| 6 6 | 
             
              segments: 
         | 
| 7 7 | 
             
              - 0
         | 
| 8 8 | 
             
              - 1
         | 
| 9 | 
            -
              -  | 
| 10 | 
            -
              version: 0.1. | 
| 9 | 
            +
              - 3
         | 
| 10 | 
            +
              version: 0.1.3
         | 
| 11 11 | 
             
            platform: ruby
         | 
| 12 12 | 
             
            authors: 
         | 
| 13 13 | 
             
            - Jordan Sissel
         | 
| @@ -15,7 +15,7 @@ autorequire: | |
| 15 15 | 
             
            bindir: bin
         | 
| 16 16 | 
             
            cert_chain: []
         | 
| 17 17 |  | 
| 18 | 
            -
            date: 2011-05- | 
| 18 | 
            +
            date: 2011-05-20 00:00:00 -07:00
         | 
| 19 19 | 
             
            default_executable: 
         | 
| 20 20 | 
             
            dependencies: 
         | 
| 21 21 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| @@ -41,12 +41,12 @@ extensions: [] | |
| 41 41 | 
             
            extra_rdoc_files: []
         | 
| 42 42 |  | 
| 43 43 | 
             
            files: 
         | 
| 44 | 
            -
            - lib/logporter/server.rb
         | 
| 45 44 | 
             
            - lib/logporter/protocol/syslog3164.rb
         | 
| 46 45 | 
             
            - lib/logporter/namespace.rb
         | 
| 47 | 
            -
            - lib/logporter/ | 
| 46 | 
            +
            - lib/logporter/server.rb
         | 
| 48 47 | 
             
            - lib/logporter/server/defaulthandler.rb
         | 
| 49 48 | 
             
            - lib/logporter/server/connection.rb
         | 
| 49 | 
            +
            - lib/logporter/event.rb
         | 
| 50 50 | 
             
            - test/syslog.rb
         | 
| 51 51 | 
             
            has_rdoc: true
         | 
| 52 52 | 
             
            homepage: https://github.com/loggly/logporter
         | 
| @@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 79 79 | 
             
            requirements: []
         | 
| 80 80 |  | 
| 81 81 | 
             
            rubyforge_project: 
         | 
| 82 | 
            -
            rubygems_version: 1. | 
| 82 | 
            +
            rubygems_version: 1.5.1
         | 
| 83 83 | 
             
            signing_key: 
         | 
| 84 84 | 
             
            specification_version: 3
         | 
| 85 85 | 
             
            summary: logporter - a log server
         |