Sprinkle a few more transaction API calls around to make sure the database
[mailman.git] / contrib / rotatelogs.py
blob168acb5675e2ea9e7b380accd53988f084459545
1 #! @PYTHON@
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.
9 #
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,
18 # USA.
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.
24 """
26 # GETTING STARTED
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
34 # to be compressed.
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
44 import paths
45 from Mailman import mm_cfg, Utils
46 import fileinput, socket, time, stat
48 # Work around known problems with some RedHat cron daemons
49 import signal
50 signal.signal(signal.SIGCHLD, signal.SIG_DFL)
53 newLogfiles = []
54 text = []
55 text.append('Mailman Log Report')
56 text.append('Generated: %s' % time.ctime(time.time()))
57 text.append('Host: %s' % socket.gethostname())
58 text.append('')
60 logDate = time.strftime('%Y%m%d-%H%M%S', time.localtime(time.time()))
61 textSend = 0
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)
71 open(fileName, 'w')
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!
76 textSend = 1
77 tmp = '# FILE: %s #' % fileNameNew
78 text.append('#' * len(tmp))
79 text.append(tmp)
80 text.append('#' * len(tmp))
81 text.append('')
83 linesLeft = showLines # e-mail first linesLeft of log files
84 for line in fileinput.input(fileNameNew):
85 if linesLeft == 0:
86 text.append('[... truncated ...]')
87 break
88 linesLeft = linesLeft - 1
89 line = string.rstrip(line)
90 text.append(line)
91 text.append('')
93 # send message if we've actually found anything
94 if textSend:
95 text = string.join(text, '\n') + '\n'
96 siteowner = Utils.get_site_email()
97 Utils.SendTextToUser(
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)