3 # Copyright (C) 2000-2007 by the Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 """Check the error logs and send any which have information in them.
22 If any log entries exist, a message is sent to the mailman owner address
23 and the logs are rotated.
28 # Run this program as root from cron, preferably at least daily. Running
29 # as root is optional, but will preserve the various modes and ownerships
30 # of log files in "~mailman/logs". If any entries are in "errors" or
31 # "smtp-errors", they will be mailed to the mailman owner address.
33 # Set COMPRESS_LOGFILES_WITH in mm_cfg.py to "gzip" to get rotated logfiles
36 # Hacked from some existing Mailman code by
37 # Sean Reifschneider <jafo-mailman@tummy.com>
38 # Please direct questions on this to the above address.
41 showLines
= 100 # lines of log messages to display before truncating
43 import sys
, os
, string
, time
, errno
45 from Mailman
import mm_cfg
, Utils
46 import fileinput
, socket
, time
, stat
48 # Work around known problems with some RedHat cron daemons
50 signal
.signal(signal
.SIGCHLD
, signal
.SIG_DFL
)
55 text
.append('Mailman Log Report')
56 text
.append('Generated: %s' % time
.ctime(time
.time()))
57 text
.append('Host: %s' % socket
.gethostname())
60 logDate
= time
.strftime('%Y%m%d-%H%M%S', time
.localtime(time
.time()))
62 for log
in ( 'error', 'smtp-failures' ):
63 fileName
= os
.path
.join(mm_cfg
.LOG_DIR
, log
)
65 # rotate file if it contains any data
66 stats
= os
.stat(fileName
)
67 if stats
[stat
.ST_SIZE
] < 1: continue
68 fileNameNew
= '%s.%s' % ( fileName
, logDate
)
69 newLogfiles
.append(fileNameNew
)
70 os
.rename(fileName
, fileNameNew
)
72 os
.chmod(fileName
, stat
.S_IMODE(stats
[stat
.ST_MODE
]))
73 try: os
.chown(fileName
, stats
[stat
.ST_UID
], stats
[stat
.ST_GID
])
74 except OSError: pass # permission denied, DOH!
77 tmp
= '# FILE: %s #' % fileNameNew
78 text
.append('#' * len(tmp
))
80 text
.append('#' * len(tmp
))
83 linesLeft
= showLines
# e-mail first linesLeft of log files
84 for line
in fileinput
.input(fileNameNew
):
86 text
.append('[... truncated ...]')
88 linesLeft
= linesLeft
- 1
89 line
= string
.rstrip(line
)
93 # send message if we've actually found anything
95 text
= string
.join(text
, '\n') + '\n'
96 siteowner
= Utils
.get_site_email()
98 'Mailman Log Report -- %s' % time
.ctime(time
.time()),
99 text
, siteowner
, siteowner
)
101 # compress any log-files we made
102 if hasattr(mm_cfg
, 'COMPRESS_LOGFILES_WITH') and mm_cfg
.COMPRESS_LOGFILES_WITH
:
103 for file in newLogfiles
:
104 os
.system(mm_cfg
.COMPRESS_LOGFILES_WITH
% file)