nixspamsum
author Pascal Volk <user@localhost.localdomain.org>
Fri, 27 Nov 2009 13:52:11 +0000
changeset 6 723b4c4e15cb
parent 5 65590f05bb97
child 7 83c4798e291d
permissions -rwxr-xr-x
Set/updated descriptions and error messages. NiXSapmSum: Converted to new-style class, added __slots__
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     1
#!/usr/bin/env python
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     3
# Copyright 2009 Pascal Volk
5
65590f05bb97 Added COPYING to the repository
Pascal Volk <user@localhost.localdomain.org>
parents: 4
diff changeset
     4
# See COPYING for distribution information.
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     5
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     6
__author__ = 'Pascal Volk'
4
ae58d9881be5 showResult(): count matches; break if no matches were found
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
     7
__version__ = '0.1.2'
ae58d9881be5 showResult(): count matches; break if no matches were found
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
     8
__date__ = '2009-07-03'
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     9
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    10
import os
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    11
import re
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    12
6
723b4c4e15cb Set/updated descriptions and error messages.
Pascal Volk <user@localhost.localdomain.org>
parents: 5
diff changeset
    13
class NiXSapmSum(object):
723b4c4e15cb Set/updated descriptions and error messages.
Pascal Volk <user@localhost.localdomain.org>
parents: 5
diff changeset
    14
    """
723b4c4e15cb Set/updated descriptions and error messages.
Pascal Volk <user@localhost.localdomain.org>
parents: 5
diff changeset
    15
    Small log parser class to parse and summarize NiX Spam DNSBL lookup
723b4c4e15cb Set/updated descriptions and error messages.
Pascal Volk <user@localhost.localdomain.org>
parents: 5
diff changeset
    16
    based rejects from a mail log file.
723b4c4e15cb Set/updated descriptions and error messages.
Pascal Volk <user@localhost.localdomain.org>
parents: 5
diff changeset
    17
    """
723b4c4e15cb Set/updated descriptions and error messages.
Pascal Volk <user@localhost.localdomain.org>
parents: 5
diff changeset
    18
    __slots__ = ('_doms', '_mxs', '_repo')
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    19
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    20
    """Regular expression pattern for mail logs from Postfix"""
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    21
    RE_PF = '''^[\w\s:-]{17,80}\spostfix\/smtpd\[[\d]{3,5}\]: NOQUEUE: reject:.*blocked using ix.dnsbl.manitu.net; Spam sent to the mailhost ((?:[a-z0-9-]{1,63}\.){1,}[a-z]{2,6}) was detected by NiX Spam.*$'''
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    22
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    23
    def __init__(self):
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    24
        self._doms = {}
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    25
        self._mxs  = {}
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    26
        self._repo = None
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    27
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    28
    def setLogFormat(self, format='postfix'):
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    29
        if format == 'postfix':
1
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
    30
            self._repo = re.compile(NiXSapmSum.RE_PF)
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    31
        else:
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    32
            raise Exception('MTA/Logformat not supported yet.')
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    33
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    34
    def parseLog(self, filehandle):
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    35
        for l in filehandle:
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    36
            mo = self._repo.match(l)
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    37
            if mo:
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    38
                mx = mo.group(1)
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    39
                try:
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    40
                    self._mxs[mx] += 1
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    41
                except KeyError:
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    42
                    self._mxs[mx]  = 1
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    43
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    44
    def countByDom(self):
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    45
        for mx in self._mxs.keys():
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    46
            dom = '.'.join(mx.split('.')[-2:])
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    47
            try:
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    48
                self._doms[dom] += self._mxs[mx]
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    49
            except KeyError:
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    50
                self._doms[dom]  = self._mxs[mx]
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    51
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    52
    def getDomains(self):
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    53
        return self._doms
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    54
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    55
    def getMXs(self):
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    56
        return self._mxs
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    57
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    58
def getOptionParser():
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    59
    from optparse import OptionParser
6
723b4c4e15cb Set/updated descriptions and error messages.
Pascal Volk <user@localhost.localdomain.org>
parents: 5
diff changeset
    60
    description = 'NiX Spam DNSBL lookup based rejects summarizer'
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    61
    usage  = 'usage: %prog [options] maillog [maillog [...]]'
1
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
    62
    version = '%prog '+__version__
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
    63
    parser = OptionParser(description=description,usage=usage,version=version)
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    64
    parser.add_option('-d', action='store_true', dest='countByDom',
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    65
            default=False, help='summarize all MX by domain')
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    66
    parser.add_option('-m', action='store_false', dest='countByDom',
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    67
            help='count per MX host [default]')
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    68
    parser.add_option('-o', dest='oFormat', default='table',metavar='FORMAT',
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    69
            help='the output format: table or csv [default: %default]')
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    70
    parser.add_option('-p', action='store_true', dest='percent', default=False,
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    71
            help='show also percentages in table output [default: %default]')
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    72
    parser.add_option('-s', dest='order', default='name', metavar='SORTBY',
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    73
            help='arrange output by: name or count [default: %default]')
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    74
    parser.add_option('-t', dest='format', default='postfix',metavar='MTA',
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    75
            help='MTA that generated the maillog [default: %default]')
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    76
    return parser
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    77
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    78
def openLogFile(fname):
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    79
    try:
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    80
        fh = open(fname)
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    81
        return fh
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    82
    except IOError, e:
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    83
        os.sys.stderr.write('Warning: %s\nskipped file: %s\n' % (e.strerror,
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    84
            fname))
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    85
1
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
    86
def getDomLen(domainnames):
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
    87
    dlen = 0
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
    88
    for d in domainnames:
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
    89
        l = len(d)
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
    90
        if l > dlen:
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
    91
            dlen = l
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
    92
    return dlen
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
    93
2
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    94
def buildTable(output, domains, percent, orderBy):
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    95
    k = 0 if orderBy == 'name' else 1
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    96
    doms = sorted(domains.items(), lambda d,c: cmp(d[k],c[k]), reverse=k)
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    97
    dlen = getDomLen(domains.keys())+1
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    98
    clen = len(str(max(domains.values())))
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    99
    total = sum(domains.values())
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   100
    if percent:
3
6b0d09cdfbdb buildTable(): small cosmetics/enhancements
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   101
        format = ' %%%ds  %%%dd  %%6.2f %%%%\n' % (-dlen, clen)
2
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   102
        for d, c in doms:
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   103
            dfrac = 100./total*c
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   104
            output.write(format % (d, c, dfrac))
3
6b0d09cdfbdb buildTable(): small cosmetics/enhancements
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   105
        output.write('%s\n' % ((clen+dlen+14)*'-'))
6b0d09cdfbdb buildTable(): small cosmetics/enhancements
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   106
        output.write(format % ('total', total, 100))
2
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   107
    else:
3
6b0d09cdfbdb buildTable(): small cosmetics/enhancements
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   108
        format = ' %%%ds  %%%dd\n' % (-dlen, clen)
2
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   109
        for d in doms:
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   110
            output.write(format % d)
3
6b0d09cdfbdb buildTable(): small cosmetics/enhancements
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   111
        output.write('%s\n' % ((clen+dlen+4)*'-'))
6b0d09cdfbdb buildTable(): small cosmetics/enhancements
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   112
        output.write(format % ('total', total))
2
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   113
1
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
   114
def showResult(nixspamsum, options):
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   115
    if options.countByDom:
1
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
   116
        nixspamsum.countByDom()
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
   117
        domains = nixspamsum.getDomains()
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   118
    else:
1
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
   119
        domains = nixspamsum.getMXs()
4
ae58d9881be5 showResult(): count matches; break if no matches were found
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
   120
    if not len(domains):
6
723b4c4e15cb Set/updated descriptions and error messages.
Pascal Volk <user@localhost.localdomain.org>
parents: 5
diff changeset
   121
        print "No NiX Spam DNSBL rejects found"
4
ae58d9881be5 showResult(): count matches; break if no matches were found
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
   122
        return
ae58d9881be5 showResult(): count matches; break if no matches were found
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
   123
ae58d9881be5 showResult(): count matches; break if no matches were found
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
   124
    from cStringIO import StringIO
ae58d9881be5 showResult(): count matches; break if no matches were found
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
   125
    output = StringIO()
2
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   126
    # build the table
1
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
   127
    if options.oFormat == 'table':
2
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   128
        buildTable(output, domains, options.percent, options.order)
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   129
    # generate comma separated values
1
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
   130
    elif options.oFormat == 'csv':
4
ae58d9881be5 showResult(): count matches; break if no matches were found
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
   131
        order = domains.keys()
ae58d9881be5 showResult(): count matches; break if no matches were found
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
   132
        order.sort()
ae58d9881be5 showResult(): count matches; break if no matches were found
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
   133
        for d in order:
ae58d9881be5 showResult(): count matches; break if no matches were found
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
   134
            output.write("'%s',%d\n" % (d, domains[d]))
2
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   135
    # should never be reached
1
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
   136
    else:
2
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   137
        print "Oops, error in function showResult() happend"
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   138
    # show the result
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   139
    print output.getvalue()
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   140
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   141
def main():
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   142
    parser = getOptionParser()
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   143
    opts, args = parser.parse_args()
2
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   144
    if opts.oFormat not in ('csv', 'table'):
a72ea07394cc NiXSapmSum.getTotal(): deleted
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   145
        parser.error("Output format '%s' is not supported" % opts.oFormat)
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   146
    if len(args) < 1:
6
723b4c4e15cb Set/updated descriptions and error messages.
Pascal Volk <user@localhost.localdomain.org>
parents: 5
diff changeset
   147
        parser.error('No log file specified')
1
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
   148
    nixss = NiXSapmSum()
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
   149
    nixss.setLogFormat(opts.format)
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   150
    for fn in args:
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   151
        fh = openLogFile(fn)
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   152
        if fh is not None:
1
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
   153
            nixss.parseLog(fh)
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   154
            fh.close()
1
7d5cee19c20a Renamed class NiXSpamPlot to NiXSapmSum. Added getDomLen(). Rewrote showResult()
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
   155
    showResult(nixss, opts)
0
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   156
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   157
if __name__ == '__main__':
2d97e75f16cf initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   158
    main()