Sha256: 5551091ca034de7cf9cc0a03024a53d85ba002ef983b4db0f05194bd139b7d67

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 KB

Contents

require 'time'

module SyslogProto
  
  def self.parse(msg, origin=nil)
    packet = Packet.new
    original_msg = msg.dup
    pri = parse_pri(msg)
    if pri and (pri = pri.to_i).is_a? Integer and (0..191).include?(pri)
      packet.pri = pri
    else
      # If there isn't a valid PRI, treat the entire message as content
      packet.pri = 13
      packet.time = Time.now
      packet.hostname = origin || 'unknown'
      packet.msg = original_msg
      return packet
    end
    time = parse_time(msg)
    if time
      packet.time = Time.parse(time)
    else
      packet.time = Time.now
    end
    hostname = parse_hostname(msg)
    packet.hostname = hostname || origin
    packet.msg = msg
    packet
  end
  
  private
  
  def self.parse_pri(msg)
    pri = msg.slice!(/<(\d\d?\d?)>/)
    pri = pri.slice(/\d\d?\d?/) if pri
    if !pri or (pri =~ /^0/ and pri !~ /^0$/)
      return nil
    else
      return pri
    end
  end
  
  def self.parse_time(msg)
    msg.slice!(/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s(\s|[1-9])\d\s\d\d:\d\d:\d\d\s/)
  end
  
  def self.parse_hostname(msg)
    msg.slice!(/^[\x21-\x7E]+\s/).rstrip
  end
  
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
errplane-0.3.7 lib/errplane/syslogproto/parser.rb
errplane-0.2.7 lib/errplane/syslogproto/parser.rb
errplane-0.2.6 lib/errplane/syslogproto/parser.rb
errplane-0.2.5 lib/errplane/syslogproto/parser.rb
errplane-0.2.4 lib/errplane/syslogproto/parser.rb
errplane-0.2.3 lib/errplane/syslogproto/parser.rb