Class: SendmailParser

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/sendmailparser.rb

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ SendmailParser

Returns a new instance of SendmailParser.



2
3
4
# File 'lib/fluent/plugin/sendmailparser.rb', line 2

def initialize(conf)
  @base_regexp = /^(?<time>\w+\s+\w+\s+\d+:\d+:\d+) (?<host>[^ ]+) (?<procowner>[^\[]+)\[(?<procid>\d+)\]: (?<qid>[^ ]+): (?<entry>(?<type>[^=]+).+)$/;
end

Instance Method Details

#from_line(entry) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fluent/plugin/sendmailparser.rb', line 63

def from_line(entry)
  record = {}

  entry.split(", ").each {|param|
    key, val = param.split("=")
    record[key] = val
  }
  if record.has_key?("relay")
    record["relay"] = relay_parser(record["relay"])
  end
  FromLine.new(record)
end

#from_parser(entry) ⇒ Object



80
81
82
# File 'lib/fluent/plugin/sendmailparser.rb', line 80

def from_parser(entry)
  from_line(entry)
end

#parse(value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fluent/plugin/sendmailparser.rb', line 6

def parse(value)
  m = @base_regexp.match(value)
  unless m
    # $log.warn "sendmail: pattern not match: #{value.inspect}"
    return nil
  end

  logtype = m["type"]
  entry = m["entry"]
  mta = m["host"]
  qid = m["qid"]
  time = Time.parse(m["time"]).to_i || Fluent::Engine.now.to_i

  logline = {
    "type" => :from,
    "mta" => mta,
    "qid" => qid,
    "time" => time,
    "type" => nil,
    "noncommon" => nil
  }

  case logtype
  when "from"
    fromline = self.from_parser(entry)
    logline["type"] = :from
    logline["noncommon"] = fromline
  when "to"
    toline = self.to_parser(entry)
    logline["type"] = :to
    logline["noncommon"] = toline
  else
    # not match
    logline =  nil
  end

  logline
end

#relay_parser(relays) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fluent/plugin/sendmailparser.rb', line 84

def relay_parser(relays)
  relay_host = nil
  relay_ip   = nil
  relays.split(" ").each {|relay|
    if relay.index("[") == 0
      return {"ip" => trim_bracket(relay), "host" => relay_host}
    else
      relay_host = relay
    end
  }
  return {"ip" => relay_ip, "host" => relay_host}
end

#status_parser(entry) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fluent/plugin/sendmailparser.rb', line 101

def status_parser(entry)
  if entry.include?("stat=Sent")
    if entry.include?("mailer=local,")
      return :sent_local
    else
      return :sent
    end
  elsif entry.include?("dsn=5.")
    return :bounced
  elsif entry.include?("stat=Deferred")
    return :deferred
  else
    return :other
  end
end

#to_line(entry) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fluent/plugin/sendmailparser.rb', line 45

def to_line(entry)
  record = {}
  status = nil

  status = status_parser(entry)

  entry.split(", ").each {|param|
    key, val = param.split("=")
    record[key] = val
  }
  record["to"] = record["to"].split(",")

  if record.has_key?("relay")
    record["relay"] = relay_parser(record["relay"])
  end
  ToLine.new(status, record)
end

#to_parser(entry) ⇒ Object



76
77
78
# File 'lib/fluent/plugin/sendmailparser.rb', line 76

def to_parser(entry)
  to_line(entry)
end

#trim_bracket(val) ⇒ Object



97
98
99
# File 'lib/fluent/plugin/sendmailparser.rb', line 97

def trim_bracket(val)
  val[1..-2]
end