Skip to content

Instantly share code, notes, and snippets.

@norrs
Created October 6, 2012 13:57
Show Gist options
  • Save norrs/3844991 to your computer and use it in GitHub Desktop.
Save norrs/3844991 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# Copyright (C) 2012 Roy Sindre Norangshol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
import re
import fileinput
import getopt
import getopt, sys
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "f:g:t", ["maillog", "grep", "trace"])
except getopt.GetoptError, err:
print str(err)
sys.exit(2)
maillog = None
grep = None
trace = False
for o, a in opts:
if o in ("-f", "--maillog"):
maillog = a
elif o in ("-g", "--grep"):
grep = a
elif o in ("-t", "--trace"):
trace = True
else:
assert False, "unhandled option"
postfix_check = re.compile("postfix/(?P<program>\w+)\[\d+\]: (?P<from_pf_id>\w+)")
amavis_check = re.compile("amavis.*?queued_as: (?P<to_pf_ids>[\w\/]+)")
# todo: build some kind of trace graph? ncurses view?
postfix_smtp = re.compile("postfix/smtp? ")
trace_list = {}
messages = []
full_log = []
def _analyze(line):
pass
def _add_trace(id_from, id_to):
if id_from in trace_list:
trace_list[id_from].append(id_to)
else:
if id_to:
trace_list[id_from] = [id_to]
else:
trace_list[id_from] = []
fo = open(maillog, 'r')
for line in fo:
if grep in line:
is_postfix_matching = postfix_check.search(line)
if is_postfix_matching:
result = is_postfix_matching.groupdict()
messages.append(result['from_pf_id'])
else:
is_amavis_matching = amavis_check.search(line)
if is_amavis_matching:
result = is_amavis_matching.groupdict()
messages += result['to_pf_ids'].split("/")
fo.close()
fo = open(maillog, 'r')
for line in fo:
if any(mid in line for mid in messages):
full_log.append(line)
if trace:
self._analyze(line)
fo.close()
print messages
for line in full_log:
sys.stdout.write(line)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment