aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/rotatelogs.py
blob: eb682635375ab1ad06e3b8d71656a1012f69d7f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#! @PYTHON@
#
# Copyright (C) 2000,2001,2002 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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.
"""

# GETTING STARTED
#
#  Run this program as root from cron, preferably at least daily.  Running
#  as root is optional, but will preserve the various modes and ownerships
#  of log files in "~mailman/logs".  If any entries are in "errors" or
#  "smtp-errors", they will be mailed to the mailman owner address.
#
#  Set COMPRESS_LOGFILES_WITH in mm_cfg.py to "gzip" to get rotated logfiles
#  to be compressed.
#
#  Hacked from some existing Mailman code by
#  Sean Reifschneider <jafo-mailman@tummy.com>
#  Please direct questions on this to the above address.
#

showLines = 100		#  lines of log messages to display before truncating

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]))
	try: os.chown(fileName, stats[stat.ST_UID], stats[stat.ST_GID])
	except OSError: pass    #  permission denied, DOH!

	textSend = 1
	tmp = '#  FILE: %s  #' % fileNameNew
	text.append('#' * len(tmp))
	text.append(tmp)
	text.append('#' * len(tmp))
	text.append('')

	linesLeft = showLines	#  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'
	siteowner = Utils.get_site_email()
	Utils.SendTextToUser(
		'Mailman Log Report -- %s' % time.ctime(time.time()),
		text, siteowner, siteowner)

#  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)