Start
[exim.git] / src / src / exicyclog.src
1 #! /bin/sh
2 # $Cambridge: exim/src/src/exicyclog.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $
3
4 # Copyright (c) 2004 University of Cambridge.
5 # See the file NOTICE for conditions of use and distribution.
6
7 # Except when they appear in comments, the following placeholders in this
8 # source are replaced when it is turned into a runnable script:
9 #
10 # CONFIGURE_FILE_USE_NODE
11 # CONFIGURE_FILE_USE_EUID
12 # CONFIGURE_FILE
13 # BIN_DIRECTORY
14 # EXICYCLOG_MAX
15 # COMPRESS_COMMAND
16 # COMPRESS_SUFFIX
17 # CHOWN_COMMAND
18 # CHGRP_COMMAND
19 # MV_COMMAND
20 # RM_COMMAND
21
22 # PROCESSED_FLAG
23
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.
29
30 # This script should be called regularly (e.g. daily) by a root crontab
31 # entry of the form
32
33 # 1 0 * * *   /opt/exim/bin/exicyclog
34
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.
42
43 keep=EXICYCLOG_MAX
44 compress=COMPRESS_COMMAND
45 suffix=COMPRESS_SUFFIX
46
47 chown=CHOWN_COMMAND
48 chgrp=CHGRP_COMMAND
49 mv=MV_COMMAND
50 rm=RM_COMMAND
51
52 # End of editable lines
53 #########################################################################
54
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
57 # requested.
58
59 for cmd in chown chgrp mv rm ; do
60   eval "oldcmd=\$$cmd"
61   if [ "$oldcmd" != "look_for_it" ] ; then continue ; fi
62   newcmd=$cmd
63   for dir in /bin /usr/bin /usr/sbin /usr/etc ; do
64     if [ -f $dir/$cmd ] ; then
65       newcmd=$dir/$cmd
66       break
67     fi
68   done
69   eval $cmd=$newcmd
70 done
71
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
75 # euid.
76
77 if [ "CONFIGURE_FILE_USE_EUID" = "yes" ]; then
78   euid=.`id -u`
79 fi
80
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.
83
84 if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then
85   hostsuffix=.`uname -n`
86 fi
87
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.
92
93 set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End
94 CONFIGURE_FILE
95 End
96 `
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
101     config="$1$euid"
102   elif [ -f "$1$hostsuffix" ] ; then
103     config="$1$hostsuffix"
104   elif [ -f "$1" ] ; then
105     config="$1"
106   fi
107   shift
108 done
109
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.
116
117 st='     '
118 exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"`
119 if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi
120
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/.*=[  ]*//'`
123
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.
126
127 if [ "$log_file_path" = "syslog" ] ; then
128   echo "*** Exim is logging to syslog - no log files to cycle ***"
129   exit 1
130 fi
131
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.
136
137 log_file_path=`echo "$log_file_path" | \
138   sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`
139
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.
144
145 if [ "$log_file_path" = "" ]; then
146   logdir=$spool_directory/log
147   mainlog=mainlog
148   rejectlog=rejectlog
149 else
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/'`
154 fi
155
156 # Get into the log directory to do the business.
157
158 cd $logdir
159
160 # If there is no main log file, do nothing.
161
162 if [ ! -f $mainlog ]; then exit; fi
163
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
170 # and group.
171
172 a=`ls -lg $mainlog`
173 b=`ls -l  $mainlog`
174
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.
178
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; }'`
181
182 user=`echo "$a
183 $b
184 " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $3; exit; } }'`
185
186 group=`echo "$a
187 $b
188 " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $4; exit; } }'`
189
190 # Now do the job. First remove the files that have "fallen off the bottom".
191 # Look for both the compressed and uncompressed forms.
192
193 if [ $keep -lt 10 ]; then keept=0$keep; else keept=$keep; fi;
194
195 if [ -f $mainlog.$keept ]; then $rm $mainlog.$keept; fi;
196 if [ -f $mainlog.$keept.$suffix ]; then $rm $mainlog.$keept.$suffix; fi;
197
198 if [ -f $rejectlog.$keept ]; then $rm $rejectlog.$keept; fi;
199 if [ -f $rejectlog.$keept.$suffix ]; then $rm $rejectlog.$keept.$suffix; fi;
200
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.
203
204 count=$keep
205 if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi;
206
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
214   fi
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
219   fi
220   count=$old
221   countt=$oldt
222 done
223
224 # Now rename the current files as 01
225
226 if [ -f $mainlog ]; then
227   $mv $mainlog $mainlog.01
228   $chown $user:$group $mainlog.01
229 fi
230
231 if [ -f $rejectlog ]; then
232   $mv $rejectlog $rejectlog.01
233   $chown $user:$group $rejectlog.01
234 fi
235
236 # Now scan the 02 and later files, compressing where necessary, and
237 # ensuring that their owners and groups are correct.
238
239 count=2;
240
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
246   fi
247   if [ -f $rejectlog.$countt ]; then $compress $rejectlog.$countt; fi
248   if [ -f $rejectlog.$countt.$suffix ]; then
249     $chown $user:$group $rejectlog.$countt.$suffix
250   fi
251   count=`expr $count + 1`
252 done
253
254 # End of exicyclog