summaryrefslogtreecommitdiff
path: root/bin/parse-email-logs
blob: b0fd8247bb6ff68d413ae4122f9c1c8b8aff0727 (plain)
1
2
3
4
5
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
44
45
46
47
48
49
50
51
52
53
54
55
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env ruby

require_relative '../config/initializer'

class Message < ActiveRecord::Base
  self.inheritance_column = 'disabled'
end

$input = nil

def parse_command_line
  while ARGV.any? do
    case ARGV[0]
    when '-h' then
      usage
    when '--help' then
      usage
    when '--debug' then
      require 'byebug'
      ARGV.shift
    else
      if ARGV[0] && File.exist?(ARGV[0])
        $input = File.open(ARGV[0])
      end
      ARGV.shift
    end
  end
  $input ||= ARGF
end

def usage
  puts "USAGE: "
  puts " option 1: parse-email-logs [LOGFILE]"
  puts " option 2: cat log | parse-email-logs"
  exit(0)
end

def parse_timestamp(str)
  # e.g. May 20 20:17:14
  DateTime.strptime(str, "%b %d %H:%M:%S")
end

def hash_addresses(str)
  str.split(',').map {|address|
    address = address.sub(/.*</, '').sub(/[>\"\']/, '')
    address.split('@').map {|segment|
      Digest::HMAC.hexdigest(segment, CONFIG['secret'], Digest::MD5)
    }.join('@')
  }.join(',')
end

def get_message(queue_id, timestamp)
  msg = Message.find_or_create_by(queue_id: queue_id)
  if msg.first_seen_at.nil?
    putc '.'; STDOUT.flush()
    msg.first_seen_at = parse_timestamp(timestamp)
    msg.save
  end
  return msg
end

#
# if we see this:
#  relay=0.0.0.0[0.0.0.0]:25
# then the message was an incoming message
#
# if the queue id is the same, but the recipient is different
# then duplicate the record.
#
def do_sent(m, ts, matches, line)
  recipient = hash_addresses(matches["to"])
  return if m.recipient == recipient
  if m.recipient.nil?
    if matches['relay'] == "relay=0.0.0.0[0.0.0.0]:25"
      m.is_outgoing = false
    else
      m.is_outgoing = true
    end
    if m.is_outgoing?
      m.sent_at     = m.first_seen_at
      m.received_at = parse_timestamp(ts)
    else
      m.sent_at     = m.date
      m.received_at = m.first_seen_at
    end
    m.recipient = recipient
    m.orig_to   = hash_addresses(matches["orig_to"]) if matches["orig_to"]
    m.delay     = matches['delay'].to_f
    m.delays    = matches['delays']
    m.status    = "sent"
    m.save
  else
    new_m = m.dup
    new_m.received_at = parse_timestamp(ts)
    new_m.recipient   = recipient
    new_m.orig_to     = hash_addresses(matches["orig_to"]) if matches["orig_to"]
    new_m.delay       = matches['delay'].to_f
    new_m.delays      = matches['delays']
    new_m.status      = "sent"
    new_m.save
  end
end

#
# for status=x where x != sent
#
def do_status(m, ts, matches)
  m.delay = matches['delay'].to_f
  m.delays = matches['delays']
  m.status = matches['status']
  m.save
end

#
# save the message size and the envelope sender
#
def do_queue(m, ts, matches)
  return if m.size != nil
  m.size = matches['size'].to_i
  m.sender = hash_addresses(matches['from'])
  m.save
end

#
# save the message id
#
def do_message_id(m, ts, matches, line)
  return if m.message_id
  m_id = matches['message_id'].gsub(/[<>]/,'').strip
  m.message_id = hash_addresses(m_id)
  m.save
end

#
# headers
#
def do_subject(m, ts, matches)
  m.subject_length = matches['subject'].length
  m.save
end
def do_date(m, ts, matches)
  m.date = matches['date']
  m.save
end
def do_from(m, ts, matches)
  m.from = hash_addresses(matches['from'])
  m.save
end
def do_to(m, ts, matches)
  m.to = hash_addresses(matches['to'])
  m.save
end
def do_cc(m, ts, matches)
  m.cc = hash_addresses(matches['cc'])
  m.save
end
def do_bcc(m, ts, matches)
  m.bcc = hash_addresses(matches['bcc'])
  m.save
end
def do_list(m, ts, matches)
  m.is_list = true
  m.save
end
def do_in_reply_to(m, ts, matches)
  m.re_message_id = hash_addresses(matches['in-reply-to'])
  m.save
end
def do_precedence(m, ts, matches)
  if matches['precedence'] =~ /(bulk|list)/i
    m.is_list = true
    m.save
  end
end

#
# the message was rejected, likely because milter scan thinks it is a virus.
# so we remove the record from the database
#
def do_purge_message(m, ts, matches)
  m.destroy
end

def do_error(line)
  puts
  puts "ERROR: unmatched line!"
  puts line
  puts
end

FROM_HOST = /(local|[A-Za-z0-9\.\-]+\[0.0.0.0\]);/

LINE_PARSE_MAP = {
  'postfix/smtp' => {
    /to=<(?<to>.*?)>, (orig_to=<(?<orig_to>.*?)>, )?(?<relay>relay=.*?), delay=(?<delay>[0-9\.]+), delays=(?<delays>[0-9\.\/]+), .*status=sent/ => method(:do_sent),
    /delay=(?<delay>[0-9\.]+), delays=(?<delays>[0-9\.\/]+), .*status=(?<status>[a-z]+) / => method(:do_status),
    /status=/ => :error
  },
  'postfix/qmgr' => {
    /from=<(?<from>.*)>, size=(?<size>\d+),.*\(queue active\)/ => method(:do_queue),
    /\(queue active\)/ => :error
  },
  'postfix/cleanup' => {
    /message-id=(?<message_id>.*)$/ => method(:do_message_id),
    /info: header Subject: (?<subject>.*) from #{FROM_HOST}/i => method(:do_subject),
    /info: header From: (?<from>.*) from #{FROM_HOST}/i => method(:do_from),
    /info: header To: (?<to>.*) from #{FROM_HOST}/i => method(:do_to),
    /info: header Cc: (?<cc>.*) from #{FROM_HOST}/i => method(:do_cc),
    /info: header Bcc: (?<bcc>.*) from #{FROM_HOST}/i => method(:do_bcc),
    /info: header In-Reply-To: (?<in-reply-to>.*) from #{FROM_HOST}/i => method(:do_in_reply_to),
    /info: header Precedence: (?<precedence>.*) from #{FROM_HOST}/i => method(:do_precedence),
    /info: header List-ID/i => method(:do_list),
    /info: header Mailing-list/i => method(:do_list),
    /info: header Date: (?<date>.*) from #{FROM_HOST}/i => method(:do_date),
    /info: header / => :error,
    /milter-reject/ => method(:do_purge_message),
    // => :error
  }
}

def process_line(line)
  splits    = line.split(' ')
  timestamp = splits[0..2].join(' ')
  daemon    = splits[4].split('[').first
  queue_id  = splits[5].sub(':', '')
  message   = splits[6..-1].join(' ')
  LINE_PARSE_MAP.fetch(daemon, {}).each do |re, method|
    match = re.match(line)
    next unless match
    if method == :error
      do_error(line)
    elsif !method.nil?
      msg = get_message(queue_id, timestamp)
      if method.arity == 3
        method.call(msg, timestamp, match)
      else
        method.call(msg, timestamp, match, line)
      end
    end
    break
  end
end

def main
  parse_command_line
  start_time  = Time.now
  start_msg   = Message.count
  line_count  = 0
  Message.transaction do
    $input.each_line do |line|
      process_line(line)
      line_count += 1
    end
  end
  end_time = Time.now
  end_msg = Message.count
  puts
  puts "FINISHED"
  puts "   Time: %s minutes" % ((end_time - start_time).to_i / 60)
  puts "Records: %s" % (end_msg - start_msg)
  puts "  Lines: %s" % line_count
end

main()