Created
October 6, 2012 13:31
-
-
Save norrs/3844949 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
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 help information and exit: | |
print str(err) # will print something like "option -a not recognized" | |
usage() | |
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