Class: Fluent::SendmailInput

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

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fluent/plugin/in_sendmail.rb', line 14

def configure(conf)
  super
  @delivers = LruRedux::ThreadSafeCache.new(@lrucache_size)
  if @path_cache_file != nil
    if not File.exists?(@path_cache_file)
      File.open(@path_cache_file, "w+"){|cache_file|
        cache_file.puts('{}')
      }
    end
    if not File.readable?(@path_cache_file)
      raise ConfigError, "cache file exists but not readable."
    end
    if not File.writable?(@path_cache_file)
      raise Fluent::ConfigError, "cache file not writable."
    end
    File.open(@path_cache_file, "r") {|cache_file|
      line = cache_file.read()
      data = JSON.parse(line)
      data.each{|k, v|
        @delivers[k] = SendmailLog.new(v['time'], v['from_line'], v['nrcpts'])
      }
    }
  end
end

#configure_parser(conf) ⇒ Object



39
40
41
# File 'lib/fluent/plugin/in_sendmail.rb', line 39

def configure_parser(conf)
  @parser = SendmailParser.new(conf)
end

#queued(es, deliveryid, time, to_line) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fluent/plugin/in_sendmail.rb', line 127

def queued(es, deliveryid, time, to_line)
  from_line = @delivers[deliveryid].from_line
  record = from_line.merge(to_line)
  delay = to_line["delay_in_sec"]
  # when a queue is expired, the mail will be bounced
  if delay >= queuereturn
    record["canonical_status"] = "bounced"
    sent(es, deliveryid, time, record)
    return
  end
  record = from_line.merge(to_line)
  es.add(time, record)
end

#receive_lines(lines) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fluent/plugin/in_sendmail.rb', line 56

def receive_lines(lines)
  es = Fluent::MultiEventStream.new
  lines.each {|line|
    begin
      line.chomp!  # remove \n
      record = parse_line(line)
      if record.nil?
        next
      end

      type   = record["type"]
      mta    = record["mta"]
      qid    = record["qid"]
      time   = record["time"]
      # a qid is not uniq worldwide.
      # make delivery id uniq even if multiple MTA"s log are mixed.
      deliveryid = mta + qid
      type   = record["type"]
      # remove unnecessary key `type"
      record.delete("type")

      case type
      when "from"
        # new log
        if @delivers.has_key?(deliveryid)
          $log.warn "duplicate sender line found. " + line.dump
        else
          @delivers[deliveryid] = SendmailLog.new(time, record)
        end
      when "to"
        if @delivers.has_key?(deliveryid)
          case record["status_canonical"]
          when "sent", "sent_local", "bounced"
            sent(es, deliveryid, time, record)
          when "deferred"
            queued(es, deliveryid, time, record)
          when "other"
            $log.warn "cannot find this kind of delivery status: " + line.dump
          end
        else
          # cannot find any 'from' line corresponded to the 'to' line
        end
      end
    rescue
      $log.warn line.dump, :error=>$!.to_s
      raise
    end
  }

  unless es.empty?
    begin
      Fluent::Engine.emit_stream(@tag, es)
    rescue
      # ignore errors. Engine shows logs and backtraces.
      raise
    end
  end
end

#sent(es, deliveryid, time, to_line) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fluent/plugin/in_sendmail.rb', line 115

def sent(es, deliveryid, time, to_line)
  from_line = @delivers[deliveryid].from_line
  record = from_line.merge(to_line)
  es.add(time, record)
  nrcpts = @delivers[deliveryid].nrcpts
  @delivers[deliveryid].nrcpts -= to_line["to"].length
  # all done
  if @delivers[deliveryid].nrcpts <= 0
    @delivers.delete(deliveryid)
  end
end

#shutdownObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fluent/plugin/in_sendmail.rb', line 43

def shutdown
  super
  if @path_cache_file != nil
    data = {}
    @delivers.each{|k, v|
      data[k] = v.to_json
    }
    File.open(@path_cache_file, "w+") {|cache_file|
      cache_file.puts(data.to_json)
    }
  end
end