2 # $Cambridge: exim/src/src/exicyclog.src,v 1.3 2005/02/17 11:58:26 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. When the number to keep is
28 # greater than 99 (not common, but some people do it), three digits are used
29 # (e.g. mainlog.001). The same shuffling happens to the reject logs. All
30 # renamed files with numbers greater than 1 are compressed.
32 # This script should be called regularly (e.g. daily) by a root crontab
35 # 1 0 * * * /opt/exim/bin/exicyclog
37 # The following lines are generated from Exim's configuration file when
38 # this source is built into a script, but you can subsequently edit them
39 # without rebuilding things, as long are you are careful not to overwrite
40 # the script in the next Exim rebuild/install. "Keep" is the number of old log
41 # files that are required to be kept. "Compress" and "suffix" define your
42 # chosen compression method. The others are provided because the location
43 # of certain commands varies from OS to OS. Sigh.
46 compress=COMPRESS_COMMAND
47 suffix=COMPRESS_SUFFIX
54 # End of editable lines
55 #########################################################################
57 # Some operating systems have different versions in which the commands live
58 # in different places. We have a fudge that will search the usual suspects if
61 for cmd in chown chgrp mv rm ; do
63 if [ "$oldcmd" != "look_for_it" ] ; then continue ; fi
65 for dir in /bin /usr/bin /usr/sbin /usr/etc ; do
66 if [ -f $dir/$cmd ] ; then
74 # See if this installation is using the esoteric "USE_EUID" feature of Exim,
75 # in which it uses the effective user id as a suffix for the configuration file
76 # name. In order for this to work, exicyclog must be run under the appropriate
79 if [ "CONFIGURE_FILE_USE_EUID" = "yes" ]; then
83 # See if this installation is using the esoteric "USE_NODE" feature of Exim,
84 # in which it uses the host's name as a suffix for the configuration file name.
86 if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then
87 hostsuffix=.`uname -n`
90 # Now find the configuration file name. This has got complicated because the
91 # CONFIGURE_FILE value may now be a list of files. The one that is used is the
92 # first one that exists. Mimic the code in readconf.c by testing first for the
93 # suffixed file in each case.
95 set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End
99 while [ "$config" = "" -a $# -gt 0 ] ; do
100 if [ -f "$1$euid$hostsuffix" ] ; then
101 config="$1$euid$hostsuffix"
102 elif [ -f "$1$euid" ] ; then
104 elif [ -f "$1$hostsuffix" ] ; then
105 config="$1$hostsuffix"
106 elif [ -f "$1" ] ; then
112 # Determine if the log file path is set, and where the spool directory is.
113 # Search for an exim_path setting in the configure file; otherwise use the bin
114 # directory. Call that version of Exim to find the spool directory and log file
115 # path. BEWARE: a tab character is needed in the command below. It has had a
116 # nasty tendency to get lost in the past. Use a variable to hold a space and a
117 # tab to keep the tab in one place.
120 exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"`
121 if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi
123 spool_directory=`$exim_path -C $config -bP spool_directory | sed 's/.*=[ ]*//'`
124 log_file_path=`$exim_path -C $config -bP log_file_path | sed 's/.*=[ ]*//'`
126 # If log_file_path contains only "syslog" then no Exim log files are in use.
127 # We can't cycle anything. Complain and give up.
129 if [ "$log_file_path" = "syslog" ] ; then
130 echo "*** Exim is logging to syslog - no log files to cycle ***"
134 # Otherwise, remove ":syslog" or "syslog:" (some spaces allowed) and inspect
135 # what remains. The simplistic regex originally used failed when a filename
136 # contained "syslog", so we have to use three less general ones, because sed
137 # doesn't have much power in its regexs.
139 log_file_path=`echo "$log_file_path" | \
140 sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`
142 # If log_file_path is empty, then the logs we are interested in are called
143 # "mainlog" and "rejectlog" in the directory called "log" in the spool
144 # directory. Otherwise we fish out the directory from the given path, and
145 # also the names of the logs.
147 if [ "$log_file_path" = "" ]; then
148 logdir=$spool_directory/log
152 logdir=`echo $log_file_path | sed 's?/[^/]*$??'`
153 logbase=`echo $log_file_path | sed 's?^.*/??'`
154 mainlog=`echo $logbase | sed 's/%s/main/'`
155 rejectlog=`echo $logbase | sed 's/%s/reject/'`
158 # Get into the log directory to do the business.
162 # If there is no main log file, do nothing.
164 if [ ! -f $mainlog ]; then exit; fi
166 # Find out the owner and group of the main log file so that we can re-instate
167 # this on moved and compressed files, since some operating systems may change
168 # things. This is a tedious bit of code, but it should work both in operating
169 # systems where the -l option of ls gives the user and group, and those in which
170 # you need -lg. The condition is that, if the fifth field of the output from
171 # ls consists entirely of digits, then the third and fourth fields are the user
177 # These statements work fine in the Bourne or Korn shells, but not in Bash.
178 # So for the benefit of systems whose /bin/sh is really Bash, they have been
179 # changed to a messier form.
181 # user=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $3; }'`
182 # group=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $4; }'`
186 " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $3; exit; } }'`
190 " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $4; exit; } }'`
192 # Now do the job. First remove the files that have "fallen off the bottom".
193 # Look for both the compressed and uncompressed forms.
195 if [ $keep -lt 10 ]; then keept=0$keep; else keept=$keep; fi;
197 if [ -f $mainlog.$keept ]; then $rm $mainlog.$keept; fi;
198 if [ -f $mainlog.$keept.$suffix ]; then $rm $mainlog.$keept.$suffix; fi;
200 if [ -f $rejectlog.$keept ]; then $rm $rejectlog.$keept; fi;
201 if [ -f $rejectlog.$keept.$suffix ]; then $rm $rejectlog.$keept.$suffix; fi;
203 # Now rename all the previous old files by increasing their numbers by 1.
204 # When the number is less than 10, insert a leading zero.
207 if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
209 while [ $count -gt 1 ]; do
210 old=`expr $count - 1`
211 if [ $keep -gt 99 ]; then
212 if [ $old -lt 10 ]; then oldt=00$old
213 elif [ $old -lt 100 ]; then oldt=0$old
217 if [ $old -lt 10 ]; then oldt=0$old; else oldt=$old; fi;
219 if [ -f $mainlog.$oldt ]; then
220 $mv $mainlog.$oldt $mainlog.$countt
221 elif [ -f $mainlog.$oldt.$suffix ]; then
222 $mv $mainlog.$oldt.$suffix $mainlog.$countt.$suffix
224 if [ -f $rejectlog.$oldt ]; then
225 $mv $rejectlog.$oldt $rejectlog.$countt
226 elif [ -f $rejectlog.$oldt.$suffix ]; then
227 $mv $rejectlog.$oldt.$suffix $rejectlog.$countt.$suffix
233 # Now rename the current files as 01 or 001 if keeping more than 99
235 if [ $keep -gt 99 ]; then first=001; else first=01; fi
237 if [ -f $mainlog ]; then
238 $mv $mainlog $mainlog.$first
239 $chown $user:$group $mainlog.$first
242 if [ -f $rejectlog ]; then
243 $mv $rejectlog $rejectlog.$first
244 $chown $user:$group $rejectlog.$first
247 # Now scan the (0)02 and later files, compressing where necessary, and
248 # ensuring that their owners and groups are correct.
252 while [ $count -le $keep ]; do
253 if [ $keep -gt 99 ]; then
254 if [ $count -lt 10 ]; then countt=00$count
255 elif [ $count -lt 100 ]; then countt=0$count
259 if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
261 if [ -f $mainlog.$countt ]; then $compress $mainlog.$countt; fi
262 if [ -f $mainlog.$countt.$suffix ]; then
263 $chown $user:$group $mainlog.$countt.$suffix
265 if [ -f $rejectlog.$countt ]; then $compress $rejectlog.$countt; fi
266 if [ -f $rejectlog.$countt.$suffix ]; then
267 $chown $user:$group $rejectlog.$countt.$suffix
269 count=`expr $count + 1`