class Fluent::MailRelayOutput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_mailrelay.rb, line 11
def initialize
  super
  @transactions = LruRedux::ThreadSafeCache.new(@lrucache_size)
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_mailrelay.rb, line 25
def configure(conf)
  super
  @mynetworks = str2ipaddr(@mynetworks)
end
format(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 38
def format(tag, time, record)
  [tag, time, record].to_msgpack
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_mailrelay.rb, line 34
def shutdown
  super
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_mailrelay.rb, line 30
def start
  super
end
str2ipaddr(sipaddrs) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 16
def str2ipaddr(sipaddrs)
  ipaddrs = Array.new()
  sipaddrs.each do |sipaddr|
    ipaddr = IPAddr.new(sipaddr)
    ipaddrs.push(ipaddr)
  end
  ipaddrs
end
write(chunk) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 42
def write(chunk)
  begin
    # Fluentd doesn't guarantee message order
    # For tracking relay of mail, this plugin should buffer logs and sort it by date order.
    messages = sort_messages(chunk.to_enum(:msgpack_each))
    readys = []
    messages.each do |tag, time, record|
      ready, mail_id = push2relaylog(tag, time, record)
      # this plugin does not output relay log until the mail relayed to outer mynetworks.
      # Therefore, if a mail is deferred, the relay log will not be outputed until the mail is bounced or sent.
      if ready
        readys.push([mail_id, tag, time])
      end
    end
    readys.each { |ready|
      mail_id, tag, time = ready
      log = @transactions[mail_id]
      router.emit(tag, time, log.record)
      @transactions.delete(mail_id)
    }
  rescue
    $log.warn 'mailrelay: error write() ', :error=>$!.to_s
    $log.debug_backtrace
  end
end

Private Instance Methods

get_delay(record) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 138
def get_delay(record)
  record['to']['delay']
end
get_dsn(record) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 134
def get_dsn(record)
  record['to']['dsn']
end
get_fromaddr(record) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 110
def get_fromaddr(record)
  record['from']['from']
end
get_msgid(record) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 118
def get_msgid(record)
  record['from']['msgid']
end
get_mta(record) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 122
def get_mta(record)
  record['mta']
end
get_relay_to(record) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 126
def get_relay_to(record)
  record['to']['relay']
end
get_stat(record) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 130
def get_stat(record)
  record['to']['canonical_status']
end
get_toaddr(record) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 114
def get_toaddr(record)
  record['to']['to']
end
push2relaylog(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 79
def push2relaylog(tag, time, record)
  from = get_fromaddr(record)
  to = get_toaddr(record)
  msgid = get_msgid(record)

  mail_id = from + to + msgid
  $log.warn 'mailrelay: ',  from, ', ', to, ',', msgid, ',', mail_id

  mta = get_mta(record)
  relay_to = get_relay_to(record)
  stat = get_stat(record)
  dsn = get_dsn(record)
  delay = get_delay(record)
  arrived_at_mta = time

  log = nil
  if @transactions.has_key?(mail_id)
    log = @transactions[mail_id]
  else
    log = MailRelayLog.new(from, to, msgid)
    @transactions[mail_id] = log
  end

  log.merge(mta, relay_to, stat, dsn, delay, arrived_at_mta)
  if not relay_to.nil? and not relay_to_mynetworks(relay_to['ip']) or
      stat == 'sent_local'
    return true, mail_id
  end
  return false, mail_id
end
relay_to_mynetworks(ip) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 142
def relay_to_mynetworks(ip)
  @mynetworks.each {|mynetwork|
    if mynetwork.include?(ip)
      return true
    end
  }
  return false
end
sort_by_time(messages) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 73
def sort_by_time(messages)
  messages.sort_by do |tag, time, record|
    record[:time]
  end
end
sort_messages(messages) click to toggle source
# File lib/fluent/plugin/out_mailrelay.rb, line 69
def sort_messages(messages)
  messages = sort_by_time(messages)
end