2 # $Cambridge: exim/src/src/exicyclog.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $
4 # Copyright (c) 2004 University of Cambridge.
5 # See the file NOTICE for conditions of use and distribution.
7 # Except when they appear in comments, the following placeholders in this
8 # source are replaced when it is turned into a runnable script:
10 # CONFIGURE_FILE_USE_NODE
11 # CONFIGURE_FILE_USE_EUID
24 # This is a shell script for cycling exim main and reject log files. Each time
25 # it is run, the files get "shuffled down" by one, the current one (e.g.
26 # mainlog) becoming mainlog.01, the previous mainlog.01 becoming mainlog.02,
27 # and so on, up to the limit configured here. The same happens to the reject
28 # logs. All those with numbers greater than 1 are compressed.
30 # This script should be called regularly (e.g. daily) by a root crontab
33 # 1 0 * * * /opt/exim/bin/exicyclog
35 # The following lines are generated from Exim's configuration file when
36 # this source is built into a script, but you can subsequently edit them
37 # without rebuilding things, as long are you are careful not to overwrite
38 # the script in the next Exim rebuild/install. "Keep" is the number of old log
39 # files that are required to be kept. "Compress" and "suffix" define your
40 # chosen compression method. The others are provided because the location
41 # of certain commands varies from OS to OS. Sigh.
44 compress=COMPRESS_COMMAND
45 suffix=COMPRESS_SUFFIX
52 # End of editable lines
53 #########################################################################
55 # Some operating systems have different versions in which the commands live
56 # in different places. We have a fudge that will search the usual suspects if
59 for cmd in chown chgrp mv rm ; do
61 if [ "$oldcmd" != "look_for_it" ] ; then continue ; fi
63 for dir in /bin /usr/bin /usr/sbin /usr/etc ; do
64 if [ -f $dir/$cmd ] ; then
72 # See if this installation is using the esoteric "USE_EUID" feature of Exim,
73 # in which it uses the effective user id as a suffix for the configuration file
74 # name. In order for this to work, exicyclog must be run under the appropriate
77 if [ "CONFIGURE_FILE_USE_EUID" = "yes" ]; then
81 # See if this installation is using the esoteric "USE_NODE" feature of Exim,
82 # in which it uses the host's name as a suffix for the configuration file name.
84 if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then
85 hostsuffix=.`uname -n`
88 # Now find the configuration file name. This has got complicated because the
89 # CONFIGURE_FILE value may now be a list of files. The one that is used is the
90 # first one that exists. Mimic the code in readconf.c by testing first for the
91 # suffixed file in each case.
93 set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End
97 while [ "$config" = "" -a $# -gt 0 ] ; do
98 if [ -f "$1$euid$hostsuffix" ] ; then
99 config="$1$euid$hostsuffix"
100 elif [ -f "$1$euid" ] ; then
102 elif [ -f "$1$hostsuffix" ] ; then
103 config="$1$hostsuffix"
104 elif [ -f "$1" ] ; then
110 # Determine if the log file path is set, and where the spool directory is.
111 # Search for an exim_path setting in the configure file; otherwise use the bin
112 # directory. Call that version of Exim to find the spool directory and log file
113 # path. BEWARE: a tab character is needed in the command below. It has had a
114 # nasty tendency to get lost in the past. Use a variable to hold a space and a
115 # tab to keep the tab in one place.
118 exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"`
119 if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi
121 spool_directory=`$exim_path -C $config -bP spool_directory | sed 's/.*=[ ]*//'`
122 log_file_path=`$exim_path -C $config -bP log_file_path | sed 's/.*=[ ]*//'`
124 # If log_file_path contains only "syslog" then no Exim log files are in use.
125 # We can't cycle anything. Complain and give up.
127 if [ "$log_file_path" = "syslog" ] ; then
128 echo "*** Exim is logging to syslog - no log files to cycle ***"
132 # Otherwise, remove ":syslog" or "syslog:" (some spaces allowed) and inspect
133 # what remains. The simplistic regex originally used failed when a filename
134 # contained "syslog", so we have to use three less general ones, because sed
135 # doesn't have much power in its regexs.
137 log_file_path=`echo "$log_file_path" | \
138 sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`
140 # If log_file_path is empty, then the logs we are interested in are called
141 # "mainlog" and "rejectlog" in the directory called "log" in the spool
142 # directory. Otherwise we fish out the directory from the given path, and
143 # also the names of the logs.
145 if [ "$log_file_path" = "" ]; then
146 logdir=$spool_directory/log
150 logdir=`echo $log_file_path | sed 's?/[^/]*$??'`
151 logbase=`echo $log_file_path | sed 's?^.*/??'`
152 mainlog=`echo $logbase | sed 's/%s/main/'`
153 rejectlog=`echo $logbase | sed 's/%s/reject/'`
156 # Get into the log directory to do the business.
160 # If there is no main log file, do nothing.
162 if [ ! -f $mainlog ]; then exit; fi
164 # Find out the owner and group of the main log file so that we can re-instate
165 # this on moved and compressed files, since some operating systems may change
166 # things. This is a tedious bit of code, but it should work both in operating
167 # systems where the -l option of ls gives the user and group, and those in which
168 # you need -lg. The condition is that, if the fifth field of the output from
169 # ls consists entirely of digits, then the third and fourth fields are the user
175 # These statements work fine in the Bourne or Korn shells, but not in Bash.
176 # So for the benefit of systems whose /bin/sh is really Bash, they have been
177 # changed to a messier form.
179 # user=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $3; }'`
180 # group=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $4; }'`
184 " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $3; exit; } }'`
188 " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $4; exit; } }'`
190 # Now do the job. First remove the files that have "fallen off the bottom".
191 # Look for both the compressed and uncompressed forms.
193 if [ $keep -lt 10 ]; then keept=0$keep; else keept=$keep; fi;
195 if [ -f $mainlog.$keept ]; then $rm $mainlog.$keept; fi;
196 if [ -f $mainlog.$keept.$suffix ]; then $rm $mainlog.$keept.$suffix; fi;
198 if [ -f $rejectlog.$keept ]; then $rm $rejectlog.$keept; fi;
199 if [ -f $rejectlog.$keept.$suffix ]; then $rm $rejectlog.$keept.$suffix; fi;
201 # Now rename all the previous old files by increasing their numbers by 1.
202 # When the number is less than 10, insert a leading zero.
205 if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi;
207 while [ $count -gt 1 ]; do
208 old=`expr $count - 1`
209 if [ $old -lt 10 ]; then oldt=0$old; else oldt=$old; fi;
210 if [ -f $mainlog.$oldt ]; then
211 $mv $mainlog.$oldt $mainlog.$countt
212 elif [ -f $mainlog.$oldt.$suffix ]; then
213 $mv $mainlog.$oldt.$suffix $mainlog.$countt.$suffix
215 if [ -f $rejectlog.$oldt ]; then
216 $mv $rejectlog.$oldt $rejectlog.$countt
217 elif [ -f $rejectlog.$oldt.$suffix ]; then
218 $mv $rejectlog.$oldt.$suffix $rejectlog.$countt.$suffix
224 # Now rename the current files as 01
226 if [ -f $mainlog ]; then
227 $mv $mainlog $mainlog.01
228 $chown $user:$group $mainlog.01
231 if [ -f $rejectlog ]; then
232 $mv $rejectlog $rejectlog.01
233 $chown $user:$group $rejectlog.01
236 # Now scan the 02 and later files, compressing where necessary, and
237 # ensuring that their owners and groups are correct.
241 while [ $count -le $keep ]; do
242 if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
243 if [ -f $mainlog.$countt ]; then $compress $mainlog.$countt; fi
244 if [ -f $mainlog.$countt.$suffix ]; then
245 $chown $user:$group $mainlog.$countt.$suffix
247 if [ -f $rejectlog.$countt ]; then $compress $rejectlog.$countt; fi
248 if [ -f $rejectlog.$countt.$suffix ]; then
249 $chown $user:$group $rejectlog.$countt.$suffix
251 count=`expr $count + 1`