exicyclog additions. Fixes: #602
[exim.git] / src / src / exicyclog.src
1 #! /bin/sh
2 # $Cambridge: exim/src/src/exicyclog.src,v 1.6 2008/01/30 20:09:24 nm4 Exp $
3
4 # Copyright (c) 2006 University of Cambridge.
5 # See the file NOTICE for conditions of use and distribution.
6
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
10
11 # Except when they appear in comments, the following placeholders in this
12 # source are replaced when it is turned into a runnable script:
13 #
14 # CONFIGURE_FILE_USE_NODE
15 # CONFIGURE_FILE_USE_EUID
16 # CONFIGURE_FILE
17 # BIN_DIRECTORY
18 # EXICYCLOG_MAX
19 # COMPRESS_COMMAND
20 # COMPRESS_SUFFIX
21 # CHOWN_COMMAND
22 # CHGRP_COMMAND
23 # CHMOD_COMMAND
24 # TOUCH_COMMAND
25 # MV_COMMAND
26 # RM_COMMAND
27
28 # PROCESSED_FLAG
29
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.
37
38 # This script should be called regularly (e.g. daily) by a root crontab
39 # entry of the form
40
41 # 1 0 * * *   /opt/exim/bin/exicyclog
42
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.
51
52 keep=EXICYCLOG_MAX
53 compress=COMPRESS_COMMAND
54 suffix=COMPRESS_SUFFIX
55
56 chown=CHOWN_COMMAND
57 chgrp=CHGRP_COMMAND
58 chmod=CHMOD_COMMAND
59 touch=TOUCH_COMMAND
60 mv=MV_COMMAND
61 rm=RM_COMMAND
62
63 # End of editable lines
64 #########################################################################
65
66 # Sort out command line options.
67
68 while [ $# -gt 0 ] ; do
69   case "$1" in
70   -l) log_file_path=$2
71       shift
72       ;;
73   -k) keep=$2
74       shift
75       ;;
76    *) echo "** exicyclog: unknown option $1"
77       exit 1
78       ;;
79    esac
80    shift
81 done
82
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
85 # requested.
86
87 for cmd in chown chgrp mv rm ; do
88   eval "oldcmd=\$$cmd"
89   if [ "$oldcmd" != "look_for_it" ] ; then continue ; fi
90   newcmd=$cmd
91   for dir in /bin /usr/bin /usr/sbin /usr/etc ; do
92     if [ -f $dir/$cmd ] ; then
93       newcmd=$dir/$cmd
94       break
95     fi
96   done
97   eval $cmd=$newcmd
98 done
99
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
103 # euid.
104
105 if [ "CONFIGURE_FILE_USE_EUID" = "yes" ]; then
106   euid=.`id -u`
107 fi
108
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.
111
112 if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then
113   hostsuffix=.`uname -n`
114 fi
115
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.
120
121 set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End
122 CONFIGURE_FILE
123 End
124 `
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
129     config="$1$euid"
130   elif [ -f "$1$hostsuffix" ] ; then
131     config="$1$hostsuffix"
132   elif [ -f "$1" ] ; then
133     config="$1"
134   fi
135   shift
136 done
137
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
144 # tab in one place.
145
146 st='     '
147 exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"`
148 if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi
149
150 spool_directory=`$exim_path -C $config -bP spool_directory | sed 's/.*=[  ]*//'`
151
152 if [ "$log_file_path" = "" ] ; then
153   log_file_path=`$exim_path -C $config -bP log_file_path | sed 's/.*=[  ]*//'`
154 fi
155
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.
158
159 if [ "$log_file_path" = "syslog" ] ; then
160   echo "*** Exim is logging to syslog - no log files to cycle ***"
161   exit 1
162 fi
163
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.
168
169 log_file_path=`echo "$log_file_path" | \
170   sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`
171
172 # If log_file_path is empty, try and get the compiled in default by using
173 # /dev/null as the configuration file.
174
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 *$//'`
179 fi
180
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.
185
186 if [ "$log_file_path" = "" ]; then
187   logdir=$spool_directory/log
188   mainlog=mainlog
189   rejectlog=rejectlog
190   paniclog=paniclog
191 else
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/'`
197 fi
198
199 # Get into the log directory to do the business.
200
201 cd $logdir || exit 1
202
203 # If there is no main log file, do nothing.
204
205 if [ ! -f $mainlog ]; then exit; fi
206
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
213 # and group.
214
215 a=`ls -lg $mainlog`
216 b=`ls -l  $mainlog`
217
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.
221
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; }'`
224
225 user=`echo "$a
226 $b
227 " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $3; exit; } }'`
228
229 group=`echo "$a
230 $b
231 " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $4; exit; } }'`
232
233 # Now do the job. First remove the files that have "fallen off the bottom".
234 # Look for both the compressed and uncompressed forms.
235
236 if [ $keep -lt 10 ]; then keept=0$keep; else keept=$keep; fi;
237
238 if [ -f $mainlog.$keept ]; then $rm $mainlog.$keept; fi;
239 if [ -f $mainlog.$keept.$suffix ]; then $rm $mainlog.$keept.$suffix; fi;
240
241 if [ -f $rejectlog.$keept ]; then $rm $rejectlog.$keept; fi;
242 if [ -f $rejectlog.$keept.$suffix ]; then $rm $rejectlog.$keept.$suffix; fi;
243
244 if [ -f $paniclog.$keept ]; then $rm $rejectlog.$keept; fi;
245 if [ -f $paniclog.$keept.$suffix ]; then $rm $rejectlog.$keept.$suffix; fi;
246
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.
249
250 count=$keep
251 if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
252
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
258     else oldt=$old
259     fi
260   else
261     if [ $old -lt 10 ]; then oldt=0$old; else oldt=$old; fi;
262   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
267   fi
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
272   fi
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
277   fi
278   count=$old
279   countt=$oldt
280 done
281
282 # Now rename the current files as 01 or 001 if keeping more than 99
283
284 if [ $keep -gt 99 ]; then first=001; else first=01; fi
285
286 if [ -f $mainlog ]; then
287   $mv $mainlog $mainlog.$first
288   $chown $user:$group $mainlog.$first
289   $touch $mainlog
290   $chown $user:$group $mainlog
291   $chmod 640 $mainlog
292 fi
293
294 if [ -f $rejectlog ]; then
295   $mv $rejectlog $rejectlog.$first
296   $chown $user:$group $rejectlog.$first
297   $touch $rejectlog
298   $chown $user:$group $rejectlog
299   $chmod 640 $rejectlog
300 fi
301
302 if [ -f $paniclog ]; then
303   $mv $paniclog $paniclog.$first
304   $chown $user:$group $rejectlog.$first
305   $touch $paniclog
306   $chown $user:$group $paniclog
307   $chmod 640 $paniclog
308 fi
309
310 # Now scan the (0)02 and later files, compressing where necessary, and
311 # ensuring that their owners and groups are correct.
312
313 count=2;
314
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
319     else countt=$count
320     fi
321   else
322     if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
323   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
327   fi
328   if [ -f $rejectlog.$countt ]; then $compress $rejectlog.$countt; fi
329   if [ -f $rejectlog.$countt.$suffix ]; then
330     $chown $user:$group $rejectlog.$countt.$suffix
331   fi
332   if [ -f $paniclog.$countt ]; then $compress $paniclog.$countt; fi
333   if [ -f $paniclog.$countt.$suffix ]; then
334     $chown $user:$group $paniclog.$countt.$suffix
335   fi
336
337   count=`expr $count + 1`
338 done
339
340 # End of exicyclog