equal
deleted
inserted
replaced
1 #!/usr/bin/env python |
1 #!/usr/bin/env python |
2 # -*- coding: utf-8 -*- |
2 # -*- coding: utf-8 -*- |
3 # Copyright 2009 - 2010 Pascal Volk |
3 # Copyright 2009 - 2011 Pascal Volk |
4 # See COPYING for distribution information. |
4 # See COPYING for distribution information. |
5 |
5 |
6 __author__ = 'Pascal Volk' |
6 __author__ = 'Pascal Volk' |
7 __version__ = '0.1.3' |
7 __version__ = '0.1.3' |
8 __date__ = '2010-04-11' |
8 __date__ = '2010-04-11' |
10 import os |
10 import os |
11 import re |
11 import re |
12 import fileinput |
12 import fileinput |
13 |
13 |
14 |
14 |
15 class NiXSapmSum(object): |
15 class NiXSpamSum(object): |
16 """ |
16 """ |
17 Small log parser class to parse and summarize NiX Spam DNSBL lookup |
17 Small log parser class to parse and summarize NiX Spam DNSBL lookup |
18 based rejects from a mail log file. |
18 based rejects from a mail log file. |
19 """ |
19 """ |
20 __slots__ = ('_doms', '_mxs', '_repo') |
20 __slots__ = ('_doms', '_mxs', '_repo') |
32 self._mxs = {} |
32 self._mxs = {} |
33 self._repo = None |
33 self._repo = None |
34 |
34 |
35 def setLogFormat(self, format='postfix'): |
35 def setLogFormat(self, format='postfix'): |
36 if format == 'postfix': |
36 if format == 'postfix': |
37 self._repo = re.compile(NiXSapmSum.RE_PF, re.VERBOSE) |
37 self._repo = re.compile(NiXSpamSum.RE_PF, re.VERBOSE) |
38 else: |
38 else: |
39 raise Exception('MTA/Logformat not supported yet.') |
39 raise Exception('MTA/Logformat not supported yet.') |
40 |
40 |
41 def parseLog(self, filehandle): |
41 def parseLog(self, filehandle): |
42 for l in filehandle: |
42 for l in filehandle: |
47 self._mxs[mx] += 1 |
47 self._mxs[mx] += 1 |
48 except KeyError: |
48 except KeyError: |
49 self._mxs[mx] = 1 |
49 self._mxs[mx] = 1 |
50 |
50 |
51 def countByDom(self): |
51 def countByDom(self): |
52 ipv4po = re.compile(NiXSapmSum.RE_IPv4) |
52 ipv4po = re.compile(NiXSpamSum.RE_IPv4) |
53 for mx in self._mxs.keys(): |
53 for mx in self._mxs.keys(): |
54 mo = ipv4po.match(mx) |
54 mo = ipv4po.match(mx) |
55 if mo: |
55 if mo: |
56 dom = mo.group(0) |
56 dom = mo.group(0) |
57 else: |
57 else: |
181 # remove inexistent/unreadable files |
181 # remove inexistent/unreadable files |
182 if not check_files(log_files): |
182 if not check_files(log_files): |
183 os.sys.stderr.write('No readable log files found\n') |
183 os.sys.stderr.write('No readable log files found\n') |
184 return 1 |
184 return 1 |
185 |
185 |
186 nixss = NiXSapmSum() |
186 nixss = NiXSpamSum() |
187 nixss.setLogFormat(opts.format) |
187 nixss.setLogFormat(opts.format) |
188 |
188 |
189 fi = fileinput.FileInput(log_files, openhook=fileinput.hook_compressed) |
189 fi = fileinput.FileInput(log_files, openhook=fileinput.hook_compressed) |
190 nixss.parseLog(fi) |
190 nixss.parseLog(fi) |
191 fi.close() |
191 fi.close() |