[Mailman-Developers] Patch to rotate log files and mail to owner

Sean Reifschneider jafo@tummy.com
Sun, 12 Sep 1999 03:22:08 -0600


Here's a patch that adds the file cron/maillogs, which defaults to running
at 5am every morning.  This checks the 'error' and 'smtp-failures' files
and if they contain any information they are rotated and a summary is
sent to the MAILMAN_OWNER address.

Also added is a COMPRESS_LOGFILES_WITH option which can be set to
compress the log files (and specify what command to use for doing
so).

Sean

Index: Mailman/Defaults.py.in
===================================================================
RCS file: /projects/cvsroot/mailman/Mailman/Defaults.py.in,v
retrieving revision 1.83
diff -c -r1.83 Mailman/Defaults.py.in
*** Mailman/Defaults.py.in	1999/09/07 18:32:44	1.83
--- Mailman/Defaults.py.in	1999/09/12 09:12:09
***************
*** 83,88 ****
--- 83,93 ----
  # night to generate the txt.gz file.  See cron/nightly_gzip for details.
  GZIP_ARCHIVE_TXT_FILES = 0
  
+ # Set this to the name of an compression command to run (with "%s" being
+ # substituted for the file name to compress) to compress the logfiles.
+ # If this value is not set, logfiles will not be compressed.
+ #COMPRESS_LOGFILES_WITH = 'gzip "%s"'
+ 
  
  HOME_PAGE         = 'index.html'
  MAILMAN_OWNER     = 'mailman-owner@%s' % DEFAULT_HOST_NAME
Index: Mailman/mm_cfg.py.dist.in
===================================================================
RCS file: /projects/cvsroot/mailman/Mailman/mm_cfg.py.dist.in,v
retrieving revision 1.21
diff -c -r1.21 Mailman/mm_cfg.py.dist.in
*** Mailman/mm_cfg.py.dist.in	1998/06/19 20:11:32	1.21
--- Mailman/mm_cfg.py.dist.in	1999/09/12 09:12:09
***************
*** 55,59 ****
--- 55,61 ----
  PUBLIC_ARCHIVE_URL = '/pipermail'
  PRIVATE_ARCHIVE_URL = '/mailman/private'
  
+ #COMPRESS_LOGFILES_WITH = 'gzip "%s"'
+ 
  # Note - if you're looking for something that is imported from mm_cfg, but you
  # didn't find it above, it's probably in Defaults.py.
Index: cron/crontab.in.in
===================================================================
RCS file: /projects/cvsroot/mailman/cron/crontab.in.in,v
retrieving revision 1.12
diff -c -r1.12 cron/crontab.in.in
*** cron/crontab.in.in	1999/09/04 03:25:36	1.12
--- cron/crontab.in.in	1999/09/12 09:12:09
***************
*** 1,6 ****
--- 1,9 ----
  # At 5PM every day, mail reminders to admins as to pending requests
  0 17 * * * @PYTHON@ @prefix@/cron/checkdbs
  #
+ # At 5AM every day, mail error logs to admin user
+ 0 5 * * * @PYTHON@ @prefix@/cron/maillogs
+ #
  # Noon, mail digests for lists that do periodic as well as threshhold delivery.
  0 12 * * * @PYTHON@ @prefix@/cron/senddigests
  #
===================================================================
*** cron/maillogs.orig	Sun Sep 12 03:08:42 1999
--- cron/maillogs	Sun Sep 12 03:01:52 1999
***************
*** 0 ****
--- 1,83 ----
+ #! /usr/bin/env python
+ #
+ # Copyright (C) 1998 by the Free Software Foundation, Inc.
+ #
+ # 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.
+ 
+ """Check the error logs and send any which have information in them.
+ 
+ If any log entries exist, a message is sent to the mailman owner address
+ and the logs are rotated.
+ """
+ 
+ import sys, os, string, time, errno
+ import paths
+ from Mailman import mm_cfg, Utils
+ import fileinput, socket, time, stat
+ 
+ # Work around known problems with some RedHat cron daemons
+ import signal
+ signal.signal(signal.SIGCHLD, signal.SIG_DFL)
+ 
+ 
+ newLogfiles = []
+ text = []
+ text.append('MailMan Log Report')
+ text.append('Generated: %s' % time.ctime(time.time()))
+ text.append('Host: %s' % socket.gethostname())
+ text.append('')
+ 
+ logDate = time.strftime('%Y%m%d-%H%M%S', time.localtime(time.time()))
+ textSend = 0
+ for log in ( 'error', 'smtp-failures' ):
+ 	fileName = os.path.join(mm_cfg.LOG_DIR, log)
+ 
+ 	#  rotate file if it contains any data
+ 	stats = os.stat(fileName)
+ 	if stats[stat.ST_SIZE] < 1: continue
+ 	fileNameNew = '%s.%s' % ( fileName, logDate )
+ 	newLogfiles.append(fileNameNew)
+ 	os.rename(fileName, fileNameNew)
+ 	open(fileName, 'w')
+ 	os.chmod(fileName, stat.S_IMODE(stats[stat.ST_MODE]))
+ 	os.chown(fileName, stats[stat.ST_UID], stats[stat.ST_GID])
+ 
+ 	textSend = 1
+ 	tmp = '#  FILE: %s  #' % fileNameNew
+ 	text.append('#' * len(tmp))
+ 	text.append(tmp)
+ 	text.append('#' * len(tmp))
+ 	text.append('')
+ 
+ 	linesLeft = 30		#  e-mail first linesLeft of log files
+ 	for line in fileinput.input(fileNameNew):
+ 		if linesLeft == 0:
+ 			text.append('[... truncated ...]')
+ 			break
+ 		linesLeft = linesLeft - 1
+ 		line = string.rstrip(line)
+ 		text.append(line)
+ 	text.append('')
+ 
+ #  send message if we've actually found anything
+ if textSend:
+ 	text = string.join(text, '\n') + '\n'
+ 	Utils.SendTextToUser('MailMan Log Report -- %s' % time.ctime(time.time()),
+ 			text, mm_cfg.MAILMAN_OWNER, mm_cfg.MAILMAN_OWNER)
+ 
+ #  compress any log-files we made
+ if hasattr(mm_cfg, 'COMPRESS_LOGFILES_WITH') and mm_cfg.COMPRESS_LOGFILES_WITH:
+ 	for file in newLogfiles:
+ 		os.system(mm_cfg.COMPRESS_LOGFILES_WITH % file)
-- 
 Linux:  When you need to run like a greased weasel.
                 -- Sean Reifschneider, 1998
Sean Reifschneider, Inimitably Superfluous <jafo@tummy.com>
URL: <http://www.tummy.com/xvscan> HP-UX/Linux/FreeBSD/BSDOS scanning software.