Commit | Line | Data |
---|---|---|
420a0d19 CE |
1 | #! /bin/sh |
2 | ||
2ea97746 | 3 | # Copyright (c) University of Cambridge, 1995 - 2015 |
420a0d19 CE |
4 | # See the file NOTICE for conditions of use and distribution. |
5 | ||
6 | # This script takes the following command line arguments: | |
7 | # -l dir Log file directory | |
8 | # -k days Number of days to keep the log files | |
9 | ||
10 | # Except when they appear in comments, the following placeholders in this | |
11 | # source are replaced when it is turned into a runnable script: | |
12 | # | |
13 | # CONFIGURE_FILE_USE_NODE | |
14 | # CONFIGURE_FILE_USE_EUID | |
15 | # CONFIGURE_FILE | |
16 | # BIN_DIRECTORY | |
17 | # EXICYCLOG_MAX | |
18 | # COMPRESS_COMMAND | |
19 | # COMPRESS_SUFFIX | |
20 | # CHOWN_COMMAND | |
21 | # CHGRP_COMMAND | |
22 | # CHMOD_COMMAND | |
23 | # TOUCH_COMMAND | |
24 | # MV_COMMAND | |
25 | # RM_COMMAND | |
26 | ||
27 | # PROCESSED_FLAG | |
28 | ||
29 | # This is a shell script for cycling exim main and reject log files. Each time | |
30 | # it is run, the files get "shuffled down" by one, the current one (e.g. | |
31 | # mainlog) becoming mainlog.01, the previous mainlog.01 becoming mainlog.02, | |
32 | # and so on, up to the limit configured here. When the number to keep is | |
33 | # greater than 99 (not common, but some people do it), three digits are used | |
34 | # (e.g. mainlog.001). The same shuffling happens to the reject logs. All | |
35 | # renamed files with numbers greater than 1 are compressed. | |
36 | ||
37 | # This script should be called regularly (e.g. daily) by a root crontab | |
38 | # entry of the form | |
39 | ||
40 | # 1 0 * * * /opt/exim/bin/exicyclog | |
41 | ||
42 | # The following lines are generated from Exim's configuration file when | |
43 | # this source is built into a script, but you can subsequently edit them | |
44 | # without rebuilding things, as long are you are careful not to overwrite | |
45 | # the script in the next Exim rebuild/install. "Keep" is the number of old log | |
46 | # files that are required to be kept. Its value can be overridden by the -k | |
47 | # command line option. "Compress" and "suffix" define your chosen compression | |
48 | # method. The others are provided because the location of certain commands | |
49 | # varies from OS to OS. Sigh. | |
50 | ||
51 | keep=EXICYCLOG_MAX | |
52 | compress=COMPRESS_COMMAND | |
53 | suffix=COMPRESS_SUFFIX | |
54 | ||
55 | chgrp=CHGRP_COMMAND | |
56 | chmod=CHMOD_COMMAND | |
57 | chown=CHOWN_COMMAND | |
58 | mv=MV_COMMAND | |
59 | rm=RM_COMMAND | |
60 | touch=TOUCH_COMMAND | |
61 | ||
62 | # End of editable lines | |
63 | ######################################################################### | |
64 | ||
65 | # Sort out command line options. | |
66 | ||
67 | while [ $# -gt 0 ] ; do | |
68 | case "$1" in | |
69 | -l) log_file_path=$2 | |
70 | shift | |
71 | ;; | |
72 | -k) keep=$2 | |
73 | shift | |
74 | ;; | |
2ea97746 CE |
75 | --version) |
76 | echo "`basename $0`: $0" | |
77 | echo "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION" | |
78 | exit 0 | |
79 | ;; | |
420a0d19 CE |
80 | *) echo "** exicyclog: unknown option $1" |
81 | exit 1 | |
82 | ;; | |
83 | esac | |
84 | shift | |
85 | done | |
86 | ||
87 | # Some operating systems have different versions in which the commands live | |
88 | # in different places. We have a fudge that will search the usual suspects if | |
89 | # requested. | |
90 | ||
91 | for cmd in chgrp chmod chown mv rm touch; do | |
92 | eval "oldcmd=\$$cmd" | |
93 | if [ "$oldcmd" != "look_for_it" ] ; then continue ; fi | |
94 | newcmd=$cmd | |
95 | for dir in /bin /usr/bin /usr/sbin /usr/etc ; do | |
96 | if [ -f $dir/$cmd ] ; then | |
97 | newcmd=$dir/$cmd | |
98 | break | |
99 | fi | |
100 | done | |
101 | eval $cmd=$newcmd | |
102 | done | |
103 | ||
104 | # See if this installation is using the esoteric "USE_EUID" feature of Exim, | |
105 | # in which it uses the effective user id as a suffix for the configuration file | |
106 | # name. In order for this to work, exicyclog must be run under the appropriate | |
107 | # euid. | |
108 | ||
109 | if [ "CONFIGURE_FILE_USE_EUID" = "yes" ]; then | |
110 | euid=.`id -u` | |
111 | fi | |
112 | ||
113 | # See if this installation is using the esoteric "USE_NODE" feature of Exim, | |
114 | # in which it uses the host's name as a suffix for the configuration file name. | |
115 | ||
116 | if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then | |
117 | hostsuffix=.`uname -n` | |
118 | fi | |
119 | ||
120 | # Now find the configuration file name. This has got complicated because the | |
121 | # CONFIGURE_FILE value may now be a list of files. The one that is used is the | |
122 | # first one that exists. Mimic the code in readconf.c by testing first for the | |
123 | # suffixed file in each case. | |
124 | ||
125 | set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End | |
126 | CONFIGURE_FILE | |
127 | End | |
128 | ` | |
129 | while [ "$config" = "" -a $# -gt 0 ] ; do | |
130 | if [ -f "$1$euid$hostsuffix" ] ; then | |
131 | config="$1$euid$hostsuffix" | |
132 | elif [ -f "$1$euid" ] ; then | |
133 | config="$1$euid" | |
134 | elif [ -f "$1$hostsuffix" ] ; then | |
135 | config="$1$hostsuffix" | |
136 | elif [ -f "$1" ] ; then | |
137 | config="$1" | |
138 | fi | |
139 | shift | |
140 | done | |
141 | ||
142 | # Determine if the log file path is set, and where the spool directory is. | |
143 | # Search for an exim_path setting in the configure file; otherwise use the bin | |
144 | # directory. Call that version of Exim to find the spool directory and log file | |
145 | # path, unless log_file_path was set above by a command line option. BEWARE: a | |
146 | # tab character is needed in the command below. It has had a nasty tendency to | |
147 | # get lost in the past. Use a variable to hold a space and a tab to keep the | |
148 | # tab in one place. | |
149 | ||
150 | st=' ' | |
151 | exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"` | |
152 | if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi | |
153 | ||
154 | spool_directory=`$exim_path -C $config -bP spool_directory | sed 's/.*=[ ]*//'` | |
155 | ||
156 | if [ "$log_file_path" = "" ] ; then | |
157 | log_file_path=`$exim_path -C $config -bP log_file_path | sed 's/.*=[ ]*//'` | |
158 | fi | |
159 | ||
160 | # If log_file_path contains only "syslog" then no Exim log files are in use. | |
161 | # We can't cycle anything. Complain and give up. | |
162 | ||
163 | if [ "$log_file_path" = "syslog" ] ; then | |
164 | echo "*** Exim is logging to syslog - no log files to cycle ***" | |
165 | exit 1 | |
166 | fi | |
167 | ||
168 | # Otherwise, remove ":syslog" or "syslog:" (some spaces allowed) and inspect | |
169 | # what remains. The simplistic regex originally used failed when a filename | |
170 | # contained "syslog", so we have to use three less general ones, because sed | |
171 | # doesn't have much power in its regexs. | |
172 | ||
173 | log_file_path=`echo "$log_file_path" | \ | |
174 | sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'` | |
175 | ||
176 | # If log_file_path is empty, try and get the compiled in default by using | |
177 | # /dev/null as the configuration file. | |
178 | ||
179 | if [ "$log_file_path" = "" ]; then | |
180 | log_file_path=`$exim_path -C /dev/null -bP log_file_path | sed 's/.*=[ ]*//'` | |
181 | log_file_path=`echo "$log_file_path" | \ | |
182 | sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'` | |
183 | fi | |
184 | ||
185 | # If log_file_path is still empty, the logs we are interested in are probably | |
186 | # called "mainlog" and "rejectlog" in the directory called "log" in the spool | |
187 | # directory. Otherwise we fish out the directory from the given path, and also | |
188 | # the names of the logs. | |
189 | ||
190 | if [ "$log_file_path" = "" ]; then | |
191 | logdir=$spool_directory/log | |
192 | mainlog=mainlog | |
193 | rejectlog=rejectlog | |
194 | paniclog=paniclog | |
195 | else | |
196 | logdir=`echo $log_file_path | sed 's?/[^/]*$??'` | |
197 | logbase=`echo $log_file_path | sed 's?^.*/??'` | |
198 | mainlog=`echo $logbase | sed 's/%s/main/'` | |
199 | rejectlog=`echo $logbase | sed 's/%s/reject/'` | |
200 | paniclog=`echo $logbase | sed 's/%s/panic/'` | |
201 | fi | |
202 | ||
203 | # Get into the log directory to do the business. | |
204 | ||
205 | cd $logdir || exit 1 | |
206 | ||
207 | # If there is no main log file, do nothing. | |
208 | ||
209 | if [ ! -f $mainlog ]; then exit; fi | |
210 | ||
211 | # Find out the owner and group of the main log file so that we can re-instate | |
212 | # this on moved and compressed files, since some operating systems may change | |
213 | # things. This is a tedious bit of code, but it should work both in operating | |
214 | # systems where the -l option of ls gives the user and group, and those in which | |
215 | # you need -lg. The condition is that, if the fifth field of the output from | |
216 | # ls consists entirely of digits, then the third and fourth fields are the user | |
217 | # and group. | |
218 | ||
219 | a=`ls -lg $mainlog` | |
220 | b=`ls -l $mainlog` | |
221 | ||
222 | # These statements work fine in the Bourne or Korn shells, but not in Bash. | |
223 | # So for the benefit of systems whose /bin/sh is really Bash, they have been | |
224 | # changed to a messier form. | |
225 | ||
226 | # user=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $3; }'` | |
227 | # group=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $4; }'` | |
228 | ||
229 | user=`echo "$a | |
230 | $b | |
231 | " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $3; exit; } }'` | |
232 | ||
233 | group=`echo "$a | |
234 | $b | |
235 | " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $4; exit; } }'` | |
236 | ||
237 | # Now do the job. First remove the files that have "fallen off the bottom". | |
238 | # Look for both the compressed and uncompressed forms. | |
239 | ||
2ea97746 | 240 | if [ $keep -lt 10 ]; then rotation=0$keep; else rotation=$keep; fi; |
420a0d19 | 241 | |
2ea97746 CE |
242 | if [ -f $mainlog.$rotation ]; then $rm $mainlog.$rotation; fi; |
243 | if [ -f $mainlog.$rotation.$suffix ]; then $rm $mainlog.$rotation.$suffix; fi; | |
420a0d19 | 244 | |
2ea97746 CE |
245 | if [ -f $rejectlog.$rotation ]; then $rm $rejectlog.$rotation; fi; |
246 | if [ -f $rejectlog.$rotation.$suffix ]; then $rm $rejectlog.$rotation.$suffix; fi; | |
420a0d19 | 247 | |
2ea97746 CE |
248 | if [ -f $paniclog.$rotation ]; then $rm $paniclog.$rotation; fi; |
249 | if [ -f $paniclog.$rotation.$suffix ]; then $rm $paniclog.$rotation.$suffix; fi; | |
420a0d19 CE |
250 | |
251 | # Now rename all the previous old files by increasing their numbers by 1. | |
252 | # When the number is less than 10, insert a leading zero. | |
253 | ||
254 | count=$keep | |
255 | if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi | |
256 | ||
257 | while [ $count -gt 1 ]; do | |
258 | old=`expr -- $count - 1` | |
259 | if [ $keep -gt 99 ]; then | |
260 | if [ $old -lt 10 ]; then oldt=00$old | |
261 | elif [ $old -lt 100 ]; then oldt=0$old | |
262 | else oldt=$old | |
263 | fi | |
264 | else | |
265 | if [ $old -lt 10 ]; then oldt=0$old; else oldt=$old; fi; | |
266 | fi | |
267 | if [ -f $mainlog.$oldt ]; then | |
268 | $mv $mainlog.$oldt $mainlog.$countt | |
269 | elif [ -f $mainlog.$oldt.$suffix ]; then | |
270 | $mv $mainlog.$oldt.$suffix $mainlog.$countt.$suffix | |
271 | fi | |
272 | if [ -f $rejectlog.$oldt ]; then | |
273 | $mv $rejectlog.$oldt $rejectlog.$countt | |
274 | elif [ -f $rejectlog.$oldt.$suffix ]; then | |
275 | $mv $rejectlog.$oldt.$suffix $rejectlog.$countt.$suffix | |
276 | fi | |
277 | if [ -f $paniclog.$oldt ]; then | |
278 | $mv $paniclog.$oldt $paniclog.$countt | |
279 | elif [ -f $paniclog.$oldt.$suffix ]; then | |
280 | $mv $paniclog.$oldt.$suffix $paniclog.$countt.$suffix | |
281 | fi | |
282 | count=$old | |
283 | countt=$oldt | |
284 | done | |
285 | ||
286 | # Now rename the current files as 01 or 001 if keeping more than 99 | |
287 | ||
288 | if [ $keep -gt 99 ]; then first=001; else first=01; fi | |
289 | ||
2ea97746 CE |
290 | # Grab our pid ro avoid race in file creation |
291 | ourpid=$$ | |
292 | ||
420a0d19 CE |
293 | if [ -f $mainlog ]; then |
294 | $mv $mainlog $mainlog.$first | |
295 | $chown $user:$group $mainlog.$first | |
2ea97746 CE |
296 | $touch $mainlog.$ourpid |
297 | $chown $user:$group $mainlog.$ourpid | |
298 | $chmod 640 $mainlog.$ourpid | |
299 | $mv $mainlog.$ourpid $mainlog | |
420a0d19 CE |
300 | fi |
301 | ||
302 | if [ -f $rejectlog ]; then | |
303 | $mv $rejectlog $rejectlog.$first | |
304 | $chown $user:$group $rejectlog.$first | |
2ea97746 CE |
305 | $touch $rejectlog.$ourpid |
306 | $chown $user:$group $rejectlog.$ourpid | |
307 | $chmod 640 $rejectlog.$ourpid | |
308 | $mv $rejectlog.$ourpid $rejectlog | |
420a0d19 CE |
309 | fi |
310 | ||
311 | if [ -f $paniclog ]; then | |
312 | $mv $paniclog $paniclog.$first | |
313 | $chown $user:$group $paniclog.$first | |
2ea97746 CE |
314 | $touch $paniclog.$ourpid |
315 | $chown $user:$group $paniclog.$ourpid | |
316 | $chmod 640 $paniclog.$ourpid | |
317 | $mv $paniclog.$ourpid $paniclog | |
420a0d19 CE |
318 | fi |
319 | ||
320 | # Now scan the (0)02 and later files, compressing where necessary, and | |
321 | # ensuring that their owners and groups are correct. | |
322 | ||
323 | count=2; | |
324 | ||
325 | while [ $count -le $keep ]; do | |
326 | if [ $keep -gt 99 ]; then | |
327 | if [ $count -lt 10 ]; then countt=00$count | |
328 | elif [ $count -lt 100 ]; then countt=0$count | |
329 | else countt=$count | |
330 | fi | |
331 | else | |
332 | if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi | |
333 | fi | |
334 | if [ -f $mainlog.$countt ]; then $compress $mainlog.$countt; fi | |
335 | if [ -f $mainlog.$countt.$suffix ]; then | |
336 | $chown $user:$group $mainlog.$countt.$suffix | |
337 | fi | |
338 | if [ -f $rejectlog.$countt ]; then $compress $rejectlog.$countt; fi | |
339 | if [ -f $rejectlog.$countt.$suffix ]; then | |
340 | $chown $user:$group $rejectlog.$countt.$suffix | |
341 | fi | |
342 | if [ -f $paniclog.$countt ]; then $compress $paniclog.$countt; fi | |
343 | if [ -f $paniclog.$countt.$suffix ]; then | |
344 | $chown $user:$group $paniclog.$countt.$suffix | |
345 | fi | |
346 | ||
347 | count=`expr -- $count + 1` | |
348 | done | |
349 | ||
350 | # End of exicyclog |