2 # $Cambridge: exim/src/src/exicyclog.src,v 1.6 2008/01/30 20:09:24 nm4 Exp $
4 # Copyright (c) 2006 University of Cambridge.
5 # See the file NOTICE for conditions of use and distribution.
7 # This script takes the following command line arguments:
8 # -l dir Log file directory
9 # -k days Number of days to keep the log files
11 # Except when they appear in comments, the following placeholders in this
12 # source are replaced when it is turned into a runnable script:
14 # CONFIGURE_FILE_USE_NODE
15 # CONFIGURE_FILE_USE_EUID
30 # This is a shell script for cycling exim main and reject log files. Each time
31 # it is run, the files get "shuffled down" by one, the current one (e.g.
32 # mainlog) becoming mainlog.01, the previous mainlog.01 becoming mainlog.02,
33 # and so on, up to the limit configured here. When the number to keep is
34 # greater than 99 (not common, but some people do it), three digits are used
35 # (e.g. mainlog.001). The same shuffling happens to the reject logs. All
36 # renamed files with numbers greater than 1 are compressed.
38 # This script should be called regularly (e.g. daily) by a root crontab
41 # 1 0 * * * /opt/exim/bin/exicyclog
43 # The following lines are generated from Exim's configuration file when
44 # this source is built into a script, but you can subsequently edit them
45 # without rebuilding things, as long are you are careful not to overwrite
46 # the script in the next Exim rebuild/install. "Keep" is the number of old log
47 # files that are required to be kept. Its value can be overridden by the -k
48 # command line option. "Compress" and "suffix" define your chosen compression
49 # method. The others are provided because the location of certain commands
50 # varies from OS to OS. Sigh.
53 compress=COMPRESS_COMMAND
54 suffix=COMPRESS_SUFFIX
63 # End of editable lines
64 #########################################################################
66 # Sort out command line options.
68 while [ $# -gt 0 ] ; do
76 *) echo "** exicyclog: unknown option $1"
83 # Some operating systems have different versions in which the commands live
84 # in different places. We have a fudge that will search the usual suspects if
87 for cmd in chown chgrp mv rm ; do
89 if [ "$oldcmd" != "look_for_it" ] ; then continue ; fi
91 for dir in /bin /usr/bin /usr/sbin /usr/etc ; do
92 if [ -f $dir/$cmd ] ; then
100 # See if this installation is using the esoteric "USE_EUID" feature of Exim,
101 # in which it uses the effective user id as a suffix for the configuration file
102 # name. In order for this to work, exicyclog must be run under the appropriate
105 if [ "CONFIGURE_FILE_USE_EUID" = "yes" ]; then
109 # See if this installation is using the esoteric "USE_NODE" feature of Exim,
110 # in which it uses the host's name as a suffix for the configuration file name.
112 if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then
113 hostsuffix=.`uname -n`
116 # Now find the configuration file name. This has got complicated because the
117 # CONFIGURE_FILE value may now be a list of files. The one that is used is the
118 # first one that exists. Mimic the code in readconf.c by testing first for the
119 # suffixed file in each case.
121 set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End
125 while [ "$config" = "" -a $# -gt 0 ] ; do
126 if [ -f "$1$euid$hostsuffix" ] ; then
127 config="$1$euid$hostsuffix"
128 elif [ -f "$1$euid" ] ; then
130 elif [ -f "$1$hostsuffix" ] ; then
131 config="$1$hostsuffix"
132 elif [ -f "$1" ] ; then
138 # Determine if the log file path is set, and where the spool directory is.
139 # Search for an exim_path setting in the configure file; otherwise use the bin
140 # directory. Call that version of Exim to find the spool directory and log file
141 # path, unless log_file_path was set above by a command line option. BEWARE: a
142 # tab character is needed in the command below. It has had a nasty tendency to
143 # get lost in the past. Use a variable to hold a space and a tab to keep the
147 exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"`
148 if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi
150 spool_directory=`$exim_path -C $config -bP spool_directory | sed 's/.*=[ ]*//'`
152 if [ "$log_file_path" = "" ] ; then
153 log_file_path=`$exim_path -C $config -bP log_file_path | sed 's/.*=[ ]*//'`
156 # If log_file_path contains only "syslog" then no Exim log files are in use.
157 # We can't cycle anything. Complain and give up.
159 if [ "$log_file_path" = "syslog" ] ; then
160 echo "*** Exim is logging to syslog - no log files to cycle ***"
164 # Otherwise, remove ":syslog" or "syslog:" (some spaces allowed) and inspect
165 # what remains. The simplistic regex originally used failed when a filename
166 # contained "syslog", so we have to use three less general ones, because sed
167 # doesn't have much power in its regexs.
169 log_file_path=`echo "$log_file_path" | \
170 sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`
172 # If log_file_path is empty, try and get the compiled in default by using
173 # /dev/null as the configuration file.
175 if [ "$log_file_path" = "" ]; then
176 log_file_path=`$exim_path -C /dev/null -bP log_file_path | sed 's/.*=[ ]*//'`
177 log_file_path=`echo "$log_file_path" | \
178 sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`
181 # If log_file_path is still empty, the logs we are interested in are probably
182 # called "mainlog" and "rejectlog" in the directory called "log" in the spool
183 # directory. Otherwise we fish out the directory from the given path, and also
184 # the names of the logs.
186 if [ "$log_file_path" = "" ]; then
187 logdir=$spool_directory/log
192 logdir=`echo $log_file_path | sed 's?/[^/]*$??'`
193 logbase=`echo $log_file_path | sed 's?^.*/??'`
194 mainlog=`echo $logbase | sed 's/%s/main/'`
195 rejectlog=`echo $logbase | sed 's/%s/reject/'`
196 paniclog=`echo $logbase | sed 's/%s/panic/'`
199 # Get into the log directory to do the business.
203 # If there is no main log file, do nothing.
205 if [ ! -f $mainlog ]; then exit; fi
207 # Find out the owner and group of the main log file so that we can re-instate
208 # this on moved and compressed files, since some operating systems may change
209 # things. This is a tedious bit of code, but it should work both in operating
210 # systems where the -l option of ls gives the user and group, and those in which
211 # you need -lg. The condition is that, if the fifth field of the output from
212 # ls consists entirely of digits, then the third and fourth fields are the user
218 # These statements work fine in the Bourne or Korn shells, but not in Bash.
219 # So for the benefit of systems whose /bin/sh is really Bash, they have been
220 # changed to a messier form.
222 # user=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $3; }'`
223 # group=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $4; }'`
227 " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $3; exit; } }'`
231 " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $4; exit; } }'`
233 # Now do the job. First remove the files that have "fallen off the bottom".
234 # Look for both the compressed and uncompressed forms.
236 if [ $keep -lt 10 ]; then keept=0$keep; else keept=$keep; fi;
238 if [ -f $mainlog.$keept ]; then $rm $mainlog.$keept; fi;
239 if [ -f $mainlog.$keept.$suffix ]; then $rm $mainlog.$keept.$suffix; fi;
241 if [ -f $rejectlog.$keept ]; then $rm $rejectlog.$keept; fi;
242 if [ -f $rejectlog.$keept.$suffix ]; then $rm $rejectlog.$keept.$suffix; fi;
244 if [ -f $paniclog.$keept ]; then $rm $rejectlog.$keept; fi;
245 if [ -f $paniclog.$keept.$suffix ]; then $rm $rejectlog.$keept.$suffix; fi;
247 # Now rename all the previous old files by increasing their numbers by 1.
248 # When the number is less than 10, insert a leading zero.
251 if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
253 while [ $count -gt 1 ]; do
254 old=`expr $count - 1`
255 if [ $keep -gt 99 ]; then
256 if [ $old -lt 10 ]; then oldt=00$old
257 elif [ $old -lt 100 ]; then oldt=0$old
261 if [ $old -lt 10 ]; then oldt=0$old; else oldt=$old; fi;
263 if [ -f $mainlog.$oldt ]; then
264 $mv $mainlog.$oldt $mainlog.$countt
265 elif [ -f $mainlog.$oldt.$suffix ]; then
266 $mv $mainlog.$oldt.$suffix $mainlog.$countt.$suffix
268 if [ -f $rejectlog.$oldt ]; then
269 $mv $rejectlog.$oldt $rejectlog.$countt
270 elif [ -f $rejectlog.$oldt.$suffix ]; then
271 $mv $rejectlog.$oldt.$suffix $rejectlog.$countt.$suffix
273 if [ -f $paniclog.$oldt ]; then
274 $mv $paniclog.$oldt $paniclog.$countt
275 elif [ -f $paniclog.$oldt.$suffix ]; then
276 $mv $paniclog.$oldt.$suffix $paniclog.$countt.$suffix
282 # Now rename the current files as 01 or 001 if keeping more than 99
284 if [ $keep -gt 99 ]; then first=001; else first=01; fi
286 if [ -f $mainlog ]; then
287 $mv $mainlog $mainlog.$first
288 $chown $user:$group $mainlog.$first
290 $chown $user:$group $mainlog
294 if [ -f $rejectlog ]; then
295 $mv $rejectlog $rejectlog.$first
296 $chown $user:$group $rejectlog.$first
298 $chown $user:$group $rejectlog
299 $chmod 640 $rejectlog
302 if [ -f $paniclog ]; then
303 $mv $paniclog $paniclog.$first
304 $chown $user:$group $rejectlog.$first
306 $chown $user:$group $paniclog
310 # Now scan the (0)02 and later files, compressing where necessary, and
311 # ensuring that their owners and groups are correct.
315 while [ $count -le $keep ]; do
316 if [ $keep -gt 99 ]; then
317 if [ $count -lt 10 ]; then countt=00$count
318 elif [ $count -lt 100 ]; then countt=0$count
322 if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
324 if [ -f $mainlog.$countt ]; then $compress $mainlog.$countt; fi
325 if [ -f $mainlog.$countt.$suffix ]; then
326 $chown $user:$group $mainlog.$countt.$suffix
328 if [ -f $rejectlog.$countt ]; then $compress $rejectlog.$countt; fi
329 if [ -f $rejectlog.$countt.$suffix ]; then
330 $chown $user:$group $rejectlog.$countt.$suffix
332 if [ -f $paniclog.$countt ]; then $compress $paniclog.$countt; fi
333 if [ -f $paniclog.$countt.$suffix ]; then
334 $chown $user:$group $paniclog.$countt.$suffix
337 count=`expr $count + 1`