#!/usr/bin/env python # -*- coding: iso-8859-1 -*- # Dinko Korunic 'kreator', 2005. # maildir-counter.py # script for procmail log analysis when used with Maildir """This script can be used to emulate `from' program behaviour, but it is to be used with Maildir style format e-mail storage and procmail logs. Required procmail configuration [.procmailrc]: ---------------------------------------------- LOGFILE=$MAILDIR/.procmail.log Typical Procmail log format: ---------------------------- From freeradius-users-bounces@lists.freeradius.org Tue Oct 4 21:46:31 2005 Subject: Re: pam_radius_auth threading issues Folder: radius/new/1128455191.24042_0.esa1 4281 Example output: --------------- New messages since last login: [junkmail] = 1 """ __copyright__ = """Copyright (C) 2005 Dinko Korunic, InfoMAR d.o.o. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ __version__ = '$Id: maildir-counter.py 203 2006-03-02 22:55:05Z kreator $' import sys, re, time, os def dictsortValue(dict): """Sort dictionary descending per values and return touples >>> a = {'a': 1, 'b': 5, 'c': -10, 'd': 12, 'e': 30, 'f': 3} >>> dictsortValue(a) [('e', 30), ('d', 12), ('b', 5), ('f', 3), ('a', 1), ('c', -10)] >>> a = {1: 1, 2: -10, 3: 20} >>> dictsortValue(a) [(3, 20), (1, 1), (2, -10)] """ items = [(v, k) for k, v in dict.items()] items.sort() items.reverse() return [(k, v) for v, k in items] def main(): """Main implementation. Oh well...""" # where in $HOME is your procmail logfile procmail_log = '/Mail/.procmail.log' # what is the expected format of logfile procmail_format = r'^\s*Folder: (\w+)/new' try: procmail_log = os.getenv('HOME') + procmail_log except OSError: print 'Oops! Cannot determine $HOME.' sys.exit(1) localtime = int(time.time()) procmail_log_local = procmail_log + str(localtime) try: os.rename(procmail_log, procmail_log_local) procmail_file = file(procmail_log_local, 'r') except OSError, IOError: print 'No new mail.' sys.exit(0) folders = {} regex = re.compile(procmail_format) for line in procmail_file: result = regex.match(line) if result: folder = result.group(1) if folder in folders: folders[folder] += 1 else: folders[folder] = 1 procmail_file.close() try: os.unlink(procmail_log_local) except OSError: print 'Oops, %s went missing!' % procmail_file_local sys.exit(1) print 'New messages since last login:' for k, v in dictsortValue(folders): print ' [%s] = %s' % (k, v) if __name__ == '__main__': sys.exit(main())